|
| 1 | +package gitlab |
| 2 | + |
| 3 | +// # Infos / notes: |
| 4 | +// |
| 5 | +// ## Webhook calls |
| 6 | +// |
| 7 | +// Official API docs: https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/web_hooks/web_hooks.md |
| 8 | +// |
| 9 | +// ### Code Push |
| 10 | +// |
| 11 | +// A code push webhook is sent with the header: `X-Gitlab-Event: Push Hook`. |
| 12 | +// Official docs: https://gitlab.com/gitlab-org/gitlab-ce/blob/master/doc/web_hooks/web_hooks.md#push-events |
| 13 | +// |
| 14 | +// GitLab sends push webhooks for every branch separately. Even if you |
| 15 | +// push to two different branches at the same time (git push --all) it'll |
| 16 | +// trigger two webhook calls, one for each branch. |
| 17 | +// |
| 18 | +// Commits are grouped in the webhook - if you push more than one commit |
| 19 | +// to a single branch it'll be included in a single webhook call, including |
| 20 | +// all of the commits. |
| 21 | +// |
| 22 | +// The latest commit's hash is included as the "checkout_sha" parameter |
| 23 | +// in the webhook. As we don't want to trigger build for every commit |
| 24 | +// which is related to a single branch we will only handle the commit |
| 25 | +// with the hash / id specified as the "checkout_sha". |
| 26 | +// |
| 27 | + |
| 28 | +import ( |
| 29 | + "encoding/json" |
| 30 | + "errors" |
| 31 | + "fmt" |
| 32 | + "net/http" |
| 33 | + "strings" |
| 34 | + |
| 35 | + "github.com/bitrise-io/bitrise-webhooks/bitriseapi" |
| 36 | + hookCommon "github.com/bitrise-io/bitrise-webhooks/service/hook/common" |
| 37 | + "github.com/bitrise-io/go-utils/httputil" |
| 38 | +) |
| 39 | + |
| 40 | +// -------------------------- |
| 41 | +// --- Webhook Data Model --- |
| 42 | + |
| 43 | +const ( |
| 44 | + pushEventID = "Push Hook" |
| 45 | +) |
| 46 | + |
| 47 | +// CommitModel ... |
| 48 | +type CommitModel struct { |
| 49 | + CommitHash string `json:"id"` |
| 50 | + CommitMessage string `json:"message"` |
| 51 | +} |
| 52 | + |
| 53 | +// CodePushEventModel ... |
| 54 | +type CodePushEventModel struct { |
| 55 | + ObjectKind string `json:"object_kind"` |
| 56 | + Ref string `json:"ref"` |
| 57 | + CheckoutSHA string `json:"checkout_sha"` |
| 58 | + Commits []CommitModel `json:"commits"` |
| 59 | +} |
| 60 | + |
| 61 | +// --------------------------------------- |
| 62 | +// --- Webhook Provider Implementation --- |
| 63 | + |
| 64 | +// HookProvider ... |
| 65 | +type HookProvider struct{} |
| 66 | + |
| 67 | +func detectContentTypeAndEventID(header http.Header) (string, string, error) { |
| 68 | + contentType, err := httputil.GetSingleValueFromHeader("Content-Type", header) |
| 69 | + if err != nil { |
| 70 | + return "", "", fmt.Errorf("Issue with Content-Type Header: %s", err) |
| 71 | + } |
| 72 | + |
| 73 | + eventID, err := httputil.GetSingleValueFromHeader("X-Gitlab-Event", header) |
| 74 | + if err != nil { |
| 75 | + return "", "", fmt.Errorf("Issue with X-Gitlab-Event Header: %s", err) |
| 76 | + } |
| 77 | + |
| 78 | + return contentType, eventID, nil |
| 79 | +} |
| 80 | + |
| 81 | +func transformCodePushEvent(codePushEvent CodePushEventModel) hookCommon.TransformResultModel { |
| 82 | + if !strings.HasPrefix(codePushEvent.Ref, "refs/heads/") { |
| 83 | + return hookCommon.TransformResultModel{ |
| 84 | + Error: fmt.Errorf("Ref (%s) is not a head ref", codePushEvent.Ref), |
| 85 | + ShouldSkip: true, |
| 86 | + } |
| 87 | + } |
| 88 | + branch := strings.TrimPrefix(codePushEvent.Ref, "refs/heads/") |
| 89 | + |
| 90 | + lastCommit := CommitModel{} |
| 91 | + isLastCommitFound := false |
| 92 | + for _, aCommit := range codePushEvent.Commits { |
| 93 | + if aCommit.CommitHash == codePushEvent.CheckoutSHA { |
| 94 | + isLastCommitFound = true |
| 95 | + lastCommit = aCommit |
| 96 | + break |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if !isLastCommitFound { |
| 101 | + return hookCommon.TransformResultModel{ |
| 102 | + Error: errors.New("The commit specified by 'checkout_sha' was not included in the 'commits' array - no match found"), |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return hookCommon.TransformResultModel{ |
| 107 | + TriggerAPIParams: []bitriseapi.TriggerAPIParamsModel{ |
| 108 | + { |
| 109 | + BuildParams: bitriseapi.BuildParamsModel{ |
| 110 | + CommitHash: lastCommit.CommitHash, |
| 111 | + CommitMessage: lastCommit.CommitMessage, |
| 112 | + Branch: branch, |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// TransformRequest ... |
| 120 | +func (hp HookProvider) TransformRequest(r *http.Request) hookCommon.TransformResultModel { |
| 121 | + contentType, eventID, err := detectContentTypeAndEventID(r.Header) |
| 122 | + if err != nil { |
| 123 | + return hookCommon.TransformResultModel{ |
| 124 | + Error: fmt.Errorf("Issue with Headers: %s", err), |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if contentType != "application/json" { |
| 129 | + return hookCommon.TransformResultModel{ |
| 130 | + Error: fmt.Errorf("Content-Type is not supported: %s", contentType), |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if eventID != "Push Hook" { |
| 135 | + // Unsupported Event |
| 136 | + return hookCommon.TransformResultModel{ |
| 137 | + Error: fmt.Errorf("Unsupported Webhook event: %s", eventID), |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if r.Body == nil { |
| 142 | + return hookCommon.TransformResultModel{ |
| 143 | + Error: fmt.Errorf("Failed to read content of request body: no or empty request body"), |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // code push |
| 148 | + var codePushEvent CodePushEventModel |
| 149 | + if contentType == "application/json" { |
| 150 | + if err := json.NewDecoder(r.Body).Decode(&codePushEvent); err != nil { |
| 151 | + return hookCommon.TransformResultModel{Error: fmt.Errorf("Failed to parse request body: %s", err)} |
| 152 | + } |
| 153 | + } |
| 154 | + return transformCodePushEvent(codePushEvent) |
| 155 | +} |
0 commit comments