aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 75c508970f362208615b455ae84f606b370a0ffd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
This program is public domain, or under the terms of Creative Commons Zero
1.0 Universal, at your choice.  In addition, a Waiver of Patent Rights
apply.  See the LICENSE file for details.
*/

/*
NOTE: Do not use this in production yet as GitHub secrets validation,
      replay attack prevention, quite a lot of error checking, etc., are not
      implemented yet.
*/

/*
NOTE: This program requires a working git-send-email setup.  This should
      use /sbin/sendmail by default on unconfigured systems with sendmail.
      You could uncomment the lines that connect to a local SMTP server.
*/

package main

import (
	// "log"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
	"os/exec"
)

const ENVELOPE_FROM string = "hybrid@runxiyu.org"
const HEADER_FROM string = "Hybrid <hybrid@runxiyu.org>"
const REPLY_TO string = "me@runxiyu.org"

func getToAddress(repo string) string {
	switch repo {
	case "runxiyu/sjdb-src":
		return "~runxiyu/sjdb@lists.sr.ht"
	case "runxiyu/ykps-wsgi":
		return "~runxiyu/ykps@lists.sr.ht"
	default:
		return "~runxiyu/public-inbox@lists.sr.ht"
	}
}

type PullRequestMessage struct {
	Action     string
	Repository struct {
		FullName string `json:"full_name"`
	} `json:"repository"`
	PullRequest struct {
		PatchUrl string `json:"patch_url"`
	} `json:"pull_request"`
}

func pullRequest(w http.ResponseWriter, req *http.Request) {
	if req.Method != "POST" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		return
	}

	// TODO: Authentication

	var jq PullRequestMessage
	err := json.NewDecoder(req.Body).Decode(&jq)
	if err != nil {
		panic(err)
	}

	repo := jq.Repository.FullName
	if len(repo) == 0 {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "No repo name found.")
		return
	}

	patchUrl := jq.PullRequest.PatchUrl
	if len(patchUrl) == 0 {
		w.WriteHeader(http.StatusBadRequest)
		fmt.Fprintln(w, "No patch URL found.")
		return
	}

	f, err := os.CreateTemp("", "github-patch-*.mbox")
	defer os.Remove(f.Name())

	// TODO: make http request to get patch
	resp, err := http.Get(patchUrl)
	defer resp.Body.Close()
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Error fetching patch.")
		return
	}
	if resp.StatusCode != http.StatusOK {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Error fetching patch.")
		return
	}
	_, err = io.Copy(f, resp.Body)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Error copying patch to temporary file.")
		return
	}

	err = exec.Command(
		"git", "send-email",
		"--from", HEADER_FROM,
		"--8bit-encoding", "UTF-8",
		"--to", getToAddress(repo),
		"--confirm", "never",
		// "--suppress-cc", "all",
		"--reply-to", REPLY_TO,
		"--envelope-sender", ENVELOPE_FROM,
		// "--no-smtp-auth",
		// "--smtp-server", "localhost",
		// "--smtp-server-port", "25",
		f.Name(),
	).Run()
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		fmt.Fprintln(w, "Error sending email.")
		return
	}
	if err := f.Close; f.Close() != nil {
		panic(err)
	}
}

func main() {
	http.HandleFunc("/github/pull-request", pullRequest)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		panic(err)
	}
}