|
| 1 | +package gogs |
| 2 | + |
| 3 | +// # Infos / notes: |
| 4 | +// |
| 5 | +// ## Webhook calls |
| 6 | +// |
| 7 | +// Official API docs: https://gogs.io/docs/features/webhook |
| 8 | +// |
| 9 | +// This module works very similarly to the Gitlab processor. |
| 10 | +// Please look there for more discussion of its operation. |
| 11 | + |
| 12 | +import ( |
| 13 | + "encoding/json" |
| 14 | + "errors" |
| 15 | + "fmt" |
| 16 | + "net/http" |
| 17 | + "strings" |
| 18 | + |
| 19 | + "github.com/bitrise-io/bitrise-webhooks/bitriseapi" |
| 20 | + hookCommon "github.com/bitrise-io/bitrise-webhooks/service/hook/common" |
| 21 | + "github.com/bitrise-io/go-utils/httputil" |
| 22 | +) |
| 23 | + |
| 24 | +// -------------------------- |
| 25 | +// --- Webhook Data Model --- |
| 26 | + |
| 27 | +const ( |
| 28 | + pushEventID = "push" |
| 29 | +) |
| 30 | + |
| 31 | +// CommitModel ... |
| 32 | +type CommitModel struct { |
| 33 | + CommitHash string `json:"id"` |
| 34 | + CommitMessage string `json:"message"` |
| 35 | +} |
| 36 | + |
| 37 | +// CodePushEventModel ... |
| 38 | +type CodePushEventModel struct { |
| 39 | + Secret string `json:"secret"` |
| 40 | + Ref string `json:"ref"` |
| 41 | + CheckoutSHA string `json:"after"` |
| 42 | + Commits []CommitModel `json:"commits"` |
| 43 | +} |
| 44 | + |
| 45 | +// --------------------------------------- |
| 46 | +// --- Webhook Provider Implementation --- |
| 47 | + |
| 48 | +// HookProvider ... |
| 49 | +type HookProvider struct{} |
| 50 | + |
| 51 | +func detectContentTypeAndEventID(header http.Header) (string, string, error) { |
| 52 | + contentType, err := httputil.GetSingleValueFromHeader("Content-Type", header) |
| 53 | + if err != nil { |
| 54 | + return "", "", fmt.Errorf("Issue with Content-Type Header: %s", err) |
| 55 | + } |
| 56 | + |
| 57 | + eventID, err := httputil.GetSingleValueFromHeader("X-Gogs-Event", header) |
| 58 | + if err != nil { |
| 59 | + return "", "", fmt.Errorf("Issue with X-Gogs-Event Header: %s", err) |
| 60 | + } |
| 61 | + |
| 62 | + return contentType, eventID, nil |
| 63 | +} |
| 64 | + |
| 65 | +func transformCodePushEvent(codePushEvent CodePushEventModel) hookCommon.TransformResultModel { |
| 66 | + if !strings.HasPrefix(codePushEvent.Ref, "refs/heads/") { |
| 67 | + return hookCommon.TransformResultModel{ |
| 68 | + Error: fmt.Errorf("Ref (%s) is not a head ref", codePushEvent.Ref), |
| 69 | + ShouldSkip: true, |
| 70 | + } |
| 71 | + } |
| 72 | + branch := strings.TrimPrefix(codePushEvent.Ref, "refs/heads/") |
| 73 | + |
| 74 | + lastCommit := CommitModel{} |
| 75 | + isLastCommitFound := false |
| 76 | + for _, aCommit := range codePushEvent.Commits { |
| 77 | + if aCommit.CommitHash == codePushEvent.CheckoutSHA { |
| 78 | + isLastCommitFound = true |
| 79 | + lastCommit = aCommit |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if !isLastCommitFound { |
| 85 | + return hookCommon.TransformResultModel{ |
| 86 | + Error: errors.New("The commit specified by 'after' was not included in the 'commits' array - no match found"), |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return hookCommon.TransformResultModel{ |
| 91 | + TriggerAPIParams: []bitriseapi.TriggerAPIParamsModel{ |
| 92 | + { |
| 93 | + BuildParams: bitriseapi.BuildParamsModel{ |
| 94 | + CommitHash: lastCommit.CommitHash, |
| 95 | + CommitMessage: lastCommit.CommitMessage, |
| 96 | + Branch: branch, |
| 97 | + }, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// TransformRequest ... |
| 104 | +func (hp HookProvider) TransformRequest(r *http.Request) hookCommon.TransformResultModel { |
| 105 | + contentType, eventID, err := detectContentTypeAndEventID(r.Header) |
| 106 | + if err != nil { |
| 107 | + return hookCommon.TransformResultModel{ |
| 108 | + Error: fmt.Errorf("Issue with Headers: %s", err), |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if contentType != "application/json" { |
| 113 | + return hookCommon.TransformResultModel{ |
| 114 | + Error: fmt.Errorf("Content-Type is not supported: %s", contentType), |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if eventID != "push" { |
| 119 | + // Unsupported Event |
| 120 | + return hookCommon.TransformResultModel{ |
| 121 | + Error: fmt.Errorf("Unsupported Webhook event: %s", eventID), |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if r.Body == nil { |
| 126 | + return hookCommon.TransformResultModel{ |
| 127 | + Error: fmt.Errorf("Failed to read content of request body: no or empty request body"), |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + // code push |
| 132 | + var codePushEvent CodePushEventModel |
| 133 | + if contentType == "application/json" { |
| 134 | + if err := json.NewDecoder(r.Body).Decode(&codePushEvent); err != nil { |
| 135 | + return hookCommon.TransformResultModel{Error: fmt.Errorf("Failed to parse request body: %s", err)} |
| 136 | + } |
| 137 | + } |
| 138 | + return transformCodePushEvent(codePushEvent) |
| 139 | +} |
0 commit comments