/
main.go
72 lines (58 loc) · 1.46 KB
/
main.go
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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
func main() {
eventName := os.Getenv("GITHUB_EVENT_NAME")
if eventName != "pull_request" {
fmt.Printf("Ignoring event %s (only listens for 'pull_request')\n", eventName)
return
}
ctx := context.Background()
client := newClient(ctx)
f, err := os.Open(os.Getenv("GITHUB_EVENT_PATH"))
if err != nil {
log.Fatal(err)
}
decorder := json.NewDecoder(f)
var triggerEvent github.PullRequestEvent
if err := decorder.Decode(&triggerEvent); err != nil {
log.Fatal(err)
}
fullRepo := os.Getenv("GITHUB_REPOSITORY")
repo := strings.SplitN(fullRepo, "/", 2)
if len(repo) != 2 {
log.Fatalf("Invalid repo name: %s\n", fullRepo)
}
number := *triggerEvent.Number
body := os.Getenv("COMMENT")
if len(body) > 0 {
issueComment := &github.IssueComment{Body: &body}
_, _, err = client.Issues.CreateComment(ctx, repo[0], repo[1], number, issueComment)
if err != nil {
log.Fatal(err)
}
}
state := "closed"
pr := &github.PullRequest{State: &state}
_, _, err = client.PullRequests.Edit(ctx, repo[0], repo[1], number, pr)
if err != nil {
log.Fatal(err)
}
log.Printf("Closed PR %d\n", number)
}
func newClient(ctx context.Context) *github.Client {
token := os.Getenv("GITHUB_TOKEN")
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
return github.NewClient(tc)
}