Skip to content

Commit

Permalink
Add first test for gitlab
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarranza committed Sep 20, 2018
1 parent 166cca0 commit d21efad
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 11 deletions.
17 changes: 9 additions & 8 deletions .gitlab-ci.yml
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

65 changes: 64 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Makefile
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.
10 changes: 10 additions & 0 deletions webhook/github.go → github/github.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
package webhook

// Headers
// "X-Hub-Signature":[]string{"sha1=be490c94029284a1074f6ed7d6f551affcfa6e8b"},
// "User-Agent":[]string{"GitHub-Hookshot/32d792e"},
// "Content-Length":[]string{"20774"},
// "X-Github-Delivery":[]string{"97757e90-bb8e-11e8-8017-464837e8ed07"},
// "X-Github-Event":[]string{"pull_request"},
// "Accept-Encoding":[]string{"gzip"},
// "Accept":[]string{"*/*"},
// "Content-Type":[]string{"application/json"}

// // Repository holds the repository information
// type Repository struct {
// URL string `json:"url"`
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
48 changes: 48 additions & 0 deletions gitlab/gitlab.go
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
}
92 changes: 92 additions & 0 deletions gitlab/gitlab_test.go
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")
}

0 comments on commit d21efad

Please sign in to comment.