|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/hmac" |
| 5 | + "crypto/sha1" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "io/ioutil" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "os" |
| 12 | +) |
| 13 | + |
| 14 | +// To verify events are from out github webhook set this to the "secert" |
| 15 | +// we set on the github webhook. Leave it blank to skip the check. |
| 16 | +var GitHubSecret = os.Getenv("GIT_SECRET") |
| 17 | + |
| 18 | +// Verify the incoming github event by checking its signature against |
| 19 | +// the SHA of the body, using our GIT_SECRET as the key |
| 20 | +func VerifyEvent(req *http.Request, body []byte, secret string) bool { |
| 21 | + sig := req.Header.Get("X-HUB-SIGNATURE") |
| 22 | + if len(sig) != 45 || sig[:5] != "sha1=" { |
| 23 | + return false |
| 24 | + } |
| 25 | + calc := make([]byte, 20) |
| 26 | + hex.Decode(calc, []byte(sig[5:])) |
| 27 | + mac := hmac.New(sha1.New, []byte(secret)) |
| 28 | + mac.Write(body) |
| 29 | + return hmac.Equal(calc, mac.Sum(nil)) |
| 30 | +} |
| 31 | + |
| 32 | +// Do whatever logic we need for the incoming event |
| 33 | +func ProcessEvent(eventType string, eventBody []byte) { |
| 34 | + log.Printf("Event Type: %s", eventType) |
| 35 | + |
| 36 | + // Only care about "push" events |
| 37 | + if eventType != "push" { |
| 38 | + log.Print("Not a 'push' event so exit") |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + pretty, _ := json.MarshalIndent(json.RawMessage(eventBody), "", " ") |
| 43 | + log.Printf("\nEvent:\n%s", string(pretty)) |
| 44 | + |
| 45 | + // The following sample JSON is just a subset of the data in the event, |
| 46 | + // but has the key bits we care about: |
| 47 | + // |
| 48 | + // { |
| 49 | + // "ref": "refs/heads/master", |
| 50 | + // "before": "--commit-ID--", |
| 51 | + // "after": "--commit-ID--", |
| 52 | + // "repository": { |
| 53 | + // "name": "CodeEngine", |
| 54 | + // "full_name": "IBM/CodeEngin, |
| 55 | + // } |
| 56 | + // "pusher": { |
| 57 | + // "name": "github-id", |
| 58 | + // "email": "user-email" |
| 59 | + // }, |
| 60 | + // "sender": {}, |
| 61 | + // "commits": [] |
| 62 | + // } |
| 63 | + |
| 64 | + // Define a var/struct to hold the parsed event data |
| 65 | + PushEvent := struct { |
| 66 | + Ref string |
| 67 | + After string |
| 68 | + Repo struct { |
| 69 | + Name string |
| 70 | + FullName string |
| 71 | + } |
| 72 | + Pusher struct { |
| 73 | + Name string |
| 74 | + Email string |
| 75 | + } |
| 76 | + }{} |
| 77 | + |
| 78 | + // Parse the event data into the PushEvent object |
| 79 | + err := json.Unmarshal(eventBody, &PushEvent) |
| 80 | + if err != nil { |
| 81 | + log.Printf("Error parsing:\n%s\n%s", err, string(eventBody)) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + // Only care about pushes to the "main" branch |
| 86 | + if PushEvent.Ref != "refs/heads/main" { |
| 87 | + log.Printf("Skipping branch: %s", PushEvent.Ref) |
| 88 | + return |
| 89 | + } |
| 90 | + |
| 91 | + log.Printf("%s commited %q to %q branch", |
| 92 | + PushEvent.Pusher.Name, PushEvent.After, PushEvent.Ref) |
| 93 | + |
| 94 | + // Now we'd normally do a build, but let's just fake it. |
| 95 | + // To see how to kick off CodeEngine CLI commands from inside of an app |
| 96 | + // see the "cecli" sample. |
| 97 | + log.Print("ibmcloud ce buildrun submit ...") |
| 98 | +} |
| 99 | + |
| 100 | +// Simple web server that waits for incoming events, verifies they're |
| 101 | +// from our webhook (by using the GIT_SECRET, if set) and if ok process it |
| 102 | +func main() { |
| 103 | + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| 104 | + body, _ := ioutil.ReadAll(r.Body) |
| 105 | + |
| 106 | + // Make sure it comes from our repo (if we want to check it at all) |
| 107 | + if GitHubSecret != "" && !VerifyEvent(r, body, GitHubSecret) { |
| 108 | + log.Printf("Error verifying github event") |
| 109 | + w.WriteHeader(http.StatusUnauthorized) |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + ProcessEvent(r.Header.Get("X-Github-Event"), body) |
| 114 | + }) |
| 115 | + |
| 116 | + log.Printf("Listening on port 8080") |
| 117 | + http.ListenAndServe(":8080", nil) |
| 118 | +} |
0 commit comments