Skip to content

Commit 0e63677

Browse files
committed
Merge master into prod, release: v1.1.4
2 parents 41bbe94 + 99d35c6 commit 0e63677

File tree

6 files changed

+385
-4
lines changed

6 files changed

+385
-4
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ For more information check the *How to add support for a new Provider* section.
2222
* handled on the path: `/h/visualstudio/BITRISE-APP-SLUG/BITRISE-APP-API-TOKEN`
2323
* [GitLab](https://gitlab.com)
2424
* handled on the path: `/h/gitlab/BITRISE-APP-SLUG/BITRISE-APP-API-TOKEN`
25+
* [Gogs](https://gogs.io)
26+
* handled on the path: `/h/gogs/BITRISE-APP-SLUG/BITRISE-APP-API-TOKEN`
2527

2628

2729
### GitHub - setup & usage:
@@ -78,6 +80,22 @@ a [GitLab](https://gitlab.com) *project*.
7880
That's all, the next time you push code (into your repository) a build will be triggered.
7981

8082

83+
### Gogs - setup & usage:
84+
85+
All you have to do is register your `bitrise-webhooks` URL as a Webhook in your [Gogs](https://gogs.io) repository.
86+
87+
1. Open your *project* on your repository's hosting URL.
88+
1. Go to `Settings` of the *project*
89+
1. Select `Webhooks`, `Add Webhook`, then `Gogs`.
90+
1. Specify the `bitrise-webhooks` URL (`.../h/gogs/BITRISE-APP-SLUG/BITRISE-APP-API-TOKEN`) in the `Payload URL` field.
91+
1. Set the `Content Type` to `application/json`.
92+
1. A Secret is not required at this time.
93+
1. Set the trigger to be fired on `Just the push event`
94+
1. Save the Webhook.
95+
96+
That's all, the next time you push code (into your repository) a build will be triggered.
97+
98+
8199
### Visual Studio Online / Visual Studio Team Services - setup & usage:
82100

83101
All you have to do is register your `bitrise-webhooks` URL for
@@ -331,5 +349,11 @@ response provider will be used.
331349
## TODO
332350

333351
* Re-try handling
352+
* Docker image: auto-create & publish a Docker Image for the webhooks server, to make it easy to run it on your own server
334353
* Bitbucket V1 (aka "Services" on the Bitbucket web UI) - not sure whether we should support this,
335354
it's already kind of deprecated, and we already support the newer, V2 webhooks.
355+
356+
## Contributors
357+
358+
* [The Bitrise Team](https://github.com/bitrise-io)
359+
* [Chad Robinson](https://github.com/crrobinson14)

bitrise.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ workflows:
5959
6060
# Go lint
6161
go get -u github.com/golang/lint/golint
62-
63-
# Go Vet
64-
go get -u golang.org/x/tools/cmd/vet
6562
test:
6663
before_run:
6764
- _install_test_tools

service/hook/endpoint.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
hookCommon "github.com/bitrise-io/bitrise-webhooks/service/hook/common"
1515
"github.com/bitrise-io/bitrise-webhooks/service/hook/github"
1616
"github.com/bitrise-io/bitrise-webhooks/service/hook/gitlab"
17+
"github.com/bitrise-io/bitrise-webhooks/service/hook/gogs"
1718
"github.com/bitrise-io/bitrise-webhooks/service/hook/slack"
1819
"github.com/bitrise-io/bitrise-webhooks/service/hook/visualstudioteamservices"
1920
"github.com/gorilla/mux"
@@ -26,6 +27,7 @@ func supportedProviders() map[string]hookCommon.Provider {
2627
"slack": slack.HookProvider{},
2728
"visualstudio": visualstudioteamservices.HookProvider{},
2829
"gitlab": gitlab.HookProvider{},
30+
"gogs": gogs.HookProvider{},
2931
}
3032
}
3133

service/hook/gogs/gogs.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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

Comments
 (0)