-
Notifications
You must be signed in to change notification settings - Fork 1
/
github.go
96 lines (92 loc) · 2.41 KB
/
github.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package github
import (
"context"
"fmt"
"github.com/sambaiz/cdkbot/tasks/operation/command"
"github.com/sambaiz/cdkbot/tasks/operation/logger"
"github.com/sambaiz/cdkbot/tasks/operation/platform/github/client"
"golang.org/x/xerrors"
"os"
"strings"
"github.com/aws/aws-lambda-go/events"
goGitHub "github.com/google/go-github/v26/github"
)
// Handler handles GitHub webhook
func Handler(
ctx context.Context,
req events.APIGatewayProxyRequest,
logger logger.Loggerer,
) error {
if err := goGitHub.ValidateSignature(
req.Headers["X-Hub-Signature"],
[]byte(req.Body),
[]byte(os.Getenv("GITHUB_WEBHOOK_SECRET")),
); err != nil {
return xerrors.Errorf("Signature is invalid: %w", err)
}
hook, err := goGitHub.ParseWebHook(req.Headers["X-GitHub-Event"], []byte(req.Body))
if err != nil {
return xerrors.Errorf("parse hook error: %w", err)
}
switch ev := hook.(type) {
case *goGitHub.PullRequestEvent:
runner := command.NewRunner(
client.New(
ctx,
ev.GetRepo().GetOwner().GetLogin(),
ev.GetRepo().GetName(),
ev.GetPullRequest().GetNumber(),
),
fmt.Sprintf("https://%s:%s@%s",
os.Getenv("GITHUB_USER_NAME"),
os.Getenv("GITHUB_ACCESS_TOKEN"),
strings.Replace(ev.GetRepo().GetCloneURL(), "https://", "", 1)),
logger,
)
switch ev.GetAction() {
case "opened":
err = runner.Diff(ctx)
}
case *goGitHub.PushEvent:
client, err := client.NewWithHeadBranch(
ctx,
ev.GetRepo().GetOwner().GetLogin(),
ev.GetRepo().GetName(),
strings.TrimLeft(ev.GetRef(), "refs/heads/"),
)
if err != nil {
// When push to branch where PR is not created, nothing is to do
return nil
}
err = command.NewRunner(
client,
fmt.Sprintf("https://%s:%s@%s",
os.Getenv("GITHUB_USER_NAME"),
os.Getenv("GITHUB_ACCESS_TOKEN"),
strings.Replace(ev.GetRepo().GetCloneURL(), "https://", "", 1)),
logger,
).Diff(ctx)
case *goGitHub.IssueCommentEvent:
runner := command.NewRunner(
client.New(
ctx,
ev.GetRepo().GetOwner().GetLogin(),
ev.GetRepo().GetName(),
ev.GetIssue().GetNumber(),
),
fmt.Sprintf("https://%s:%s@%s",
os.Getenv("GITHUB_USER_NAME"),
os.Getenv("GITHUB_ACCESS_TOKEN"),
strings.Replace(ev.GetRepo().GetCloneURL(), "https://", "", 1)),
logger,
)
switch ev.GetAction() {
case "created":
err = runner.Run(ctx, ev.GetComment().GetBody(), ev.GetSender().GetLogin())
}
}
if err != nil {
return err
}
return nil
}