-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
228 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
image: golang:1.10 | ||
stages: | ||
- test | ||
- build | ||
|
||
before_script: | ||
- go get -u github.com/golang/dep/cmd/dep | ||
- mkdir -p /go/src/gitlab.com/yakshaving.art | ||
- cp -r /builds/yakshaving.art/propaganda /go/src/gitlab.com/yakshaving.art/ | ||
- cd /go/src/gitlab.com/yakshaving.art/propaganda | ||
- make ensure | ||
|
||
test: | ||
stage: test | ||
coverage: '/^total:\s+\(statements\)\s+(\d+.\d+)%$/' | ||
script: | ||
- make test | ||
|
||
build: | ||
stage: build | ||
script: | ||
- make build | ||
|
||
# test: | ||
# stage: test | ||
# coverage: '/^total:\s+\(statements\)\s+(\d+.\d+)%$/' | ||
# script: | ||
# - go test -v -coverprofile=coverage.out $(go list ./... | grep -v '/vendor/') && go tool cover -func=coverage.out | ||
- make build | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
|
||
all: ensure build | ||
all: ensure test build | ||
|
||
ensure: | ||
dep ensure | ||
|
||
build: | ||
test: ensure | ||
go test -v -coverprofile=coverage.out $$(go list ./... | grep -v '/vendor/') && go tool cover -func=coverage.out | ||
|
||
build: ensure | ||
go build |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package gitlab | ||
|
||
// Headers: | ||
// "Connection":[]string{"close"}, | ||
// "X-Forwarded-For":[]string{"51.15.62.67"}, | ||
// "X-Gitlab-Event":[]string{"Merge Request Hook"}, | ||
// "X-Gitlab-Token":[]string{"my-secret-token"}, | ||
// "Accept-Encoding":[]string{"gzip"}, | ||
// "User-Agent":[]string{"Go-http-client/1.1"}, | ||
// "Content-Length":[]string{"3963"}, | ||
// "Content-Type":[]string{"application/json"}, | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
) | ||
|
||
// MergeRequest is the MR object | ||
type MergeRequest struct { | ||
Kind string `json:"object_kind"` | ||
Project Project `json:"project"` | ||
Attributes Attributes `json:"object_attributes"` | ||
} | ||
|
||
// Project is used to identify which project it is including the namespace | ||
type Project struct { | ||
PathWithNamespace string `json:"path_with_namespace"` | ||
} | ||
|
||
// Attributes represent things like state, title, url or action | ||
type Attributes struct { | ||
State string `json:"state"` | ||
Title string `json:"title"` | ||
URL string `json:"url"` | ||
Action string `json:"action"` | ||
} | ||
|
||
// ParseMergeRequest creates a new merge request object from the passed payload | ||
func ParseMergeRequest(payload []byte) (MergeRequest, error) { | ||
var mr MergeRequest | ||
if err := json.Unmarshal(payload, &mr); err != nil { | ||
return mr, fmt.Errorf("could not parse json payload: %s", err) | ||
} | ||
if mr.Kind != "merge_request" { | ||
return MergeRequest{}, fmt.Errorf("json payload is not a merge request but a %s", mr.Kind) | ||
} | ||
return mr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package gitlab_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"gitlab.com/yakshaving.art/propaganda/gitlab" | ||
) | ||
|
||
func TestParsingPayloads(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
jsonFilename string | ||
expected gitlab.MergeRequest | ||
}{ | ||
{ | ||
"MR Create", | ||
"fixtures/gitlab-mr-create.json", | ||
gitlab.MergeRequest{ | ||
Kind: "merge_request", | ||
Project: gitlab.Project{ | ||
PathWithNamespace: "pablo/testing-webhooks", | ||
}, | ||
Attributes: gitlab.Attributes{ | ||
State: "opened", | ||
Title: "[announce] Update README.md", | ||
URL: "https://git.yakshaving.art/pablo/testing-webhooks/merge_requests/1", | ||
Action: "open", | ||
}, | ||
}, | ||
}, | ||
{ | ||
"MR Merged", | ||
"fixtures/gitlab-mr-merged.json", | ||
gitlab.MergeRequest{ | ||
Kind: "merge_request", | ||
Project: gitlab.Project{ | ||
PathWithNamespace: "pablo/testing-webhooks", | ||
}, | ||
Attributes: gitlab.Attributes{ | ||
State: "merged", | ||
Title: "[announce] Update README.md", | ||
URL: "https://git.yakshaving.art/pablo/testing-webhooks/merge_requests/1", | ||
Action: "merge", | ||
}, | ||
}, | ||
}, | ||
{ | ||
"MR Closed without a merge", | ||
"fixtures/gitlab-mr-close-no-merge.json", | ||
gitlab.MergeRequest{ | ||
Kind: "merge_request", | ||
Project: gitlab.Project{ | ||
PathWithNamespace: "pablo/testing-webhooks", | ||
}, | ||
Attributes: gitlab.Attributes{ | ||
State: "closed", | ||
Title: "Update README.md", | ||
URL: "https://git.yakshaving.art/pablo/testing-webhooks/merge_requests/2", | ||
Action: "close", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
a := assert.New(t) | ||
b, err := ioutil.ReadFile(tc.jsonFilename) | ||
a.Nilf(err, "could not read fixture file %s", tc.jsonFilename) | ||
a.NotNilf(b, "content should not be nil") | ||
|
||
mr, err := gitlab.ParseMergeRequest(b) | ||
a.NoErrorf(err, "could not unmarshal MR json") | ||
|
||
a.EqualValuesf(tc.expected, mr, "parsed merge request is not as expected") | ||
}) | ||
} | ||
} | ||
|
||
func TestInvalidPayloadErrs(t *testing.T) { | ||
a := assert.New(t) | ||
|
||
b, err := ioutil.ReadFile("fixtures/gitlab-push-event.json") | ||
a.Nil(err, "could not read fixture file") | ||
a.NotNilf(b, "content should not be nil") | ||
|
||
mr, err := gitlab.ParseMergeRequest(b) | ||
a.Errorf(err, "json payload is not a merge request but a push") | ||
a.Equalf(gitlab.MergeRequest{}, mr, "merge request should be empty") | ||
} |