-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathgitwebhook.go
110 lines (91 loc) · 3.09 KB
/
gitwebhook.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"strings"
ghclient "github.com/google/go-github/github"
"golang.org/x/oauth2"
webhooks "gopkg.in/go-playground/webhooks.v3"
"gopkg.in/go-playground/webhooks.v3/github"
)
const (
// Secret given to github. Used for verifying the incoming objects.
personalAccessTokenKey = "GITHUB_PERSONAL_TOKEN"
// Personal Access Token created in github that allows us to make
// calls into github.
webhookSecretKey = "WEBHOOK_SECRET"
// this is what we tack onto each PR title if not there already
titleSuffix = "looks pretty legit"
)
// GithubHandler holds necessary objects for communicating with the Github.
type GithubHandler struct {
client *ghclient.Client
ctx context.Context
}
// HandlePullRequest is invoked whenever a PullRequest is modified (created, updated, etc.)
func (handler *GithubHandler) HandlePullRequest(payload interface{}, header webhooks.Header) {
log.Print("Handling Pull Request")
pl := payload.(github.PullRequestPayload)
// Do whatever you want from here...
title := pl.PullRequest.Title
log.Printf("GOT PR with Title: %q", title)
// Check the title and if it contains 'looks pretty legit' leave it alone
if strings.Contains(title, titleSuffix) {
// already modified, leave it alone.
return
}
newTitle := fmt.Sprintf("%s (%s)", title, titleSuffix)
updatedPR := ghclient.PullRequest{
Title: &newTitle,
}
newPR, response, err := handler.client.PullRequests.Edit(handler.ctx, pl.Repository.Owner.Login, pl.Repository.Name, int(pl.Number), &updatedPR)
if err != nil {
log.Printf("Failed to update PR: %s\n%s", err, response)
return
}
if newPR.Title != nil {
log.Printf("New PR Title: %q", *newPR.Title)
} else {
log.Print("New PR title is nil")
}
}
func main() {
flag.Parse()
log.Print("gitwebhook sample started.")
personalAccessToken := os.Getenv(personalAccessTokenKey)
secretToken := os.Getenv(webhookSecretKey)
// Set up the auth for being able to talk to Github. It's
// odd that you have to also pass context around for the
// calls even after giving it to client. But, whatever.
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: personalAccessToken},
)
tc := oauth2.NewClient(ctx, ts)
client := ghclient.NewClient(tc)
h := &GithubHandler{
client: client,
ctx: ctx,
}
hook := github.New(&github.Config{Secret: secretToken})
hook.RegisterEvents(h.HandlePullRequest, github.PullRequestEvent)
err := webhooks.Run(hook, ":8080", "/")
if err != nil {
fmt.Println("Failed to run the webhook")
}
}