From baf50c9709aeaa27771250b1dc554f80612edaf9 Mon Sep 17 00:00:00 2001 From: Nicolas Carlier Date: Fri, 20 Mar 2015 10:57:21 +0000 Subject: [PATCH] feat: Add Gitlab hook. --- assets/gitlab.json | 42 ++++++++++++++++++++++++++++++++++++++++ scripts/gitlab/echo.sh | 4 ++++ src/hook/gitlab_hook.go | 30 ++++++++++++++++++++++++++++ src/hook/hook_factory.go | 2 ++ test.sh | 5 +++++ 5 files changed, 83 insertions(+) create mode 100644 assets/gitlab.json create mode 100755 scripts/gitlab/echo.sh create mode 100644 src/hook/gitlab_hook.go diff --git a/assets/gitlab.json b/assets/gitlab.json new file mode 100644 index 0000000..fb37661 --- /dev/null +++ b/assets/gitlab.json @@ -0,0 +1,42 @@ +{ + "object_kind": "push", + "before": "95790bf891e76fee5e1747ab589903a6a1f80f22", + "after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "ref": "refs/heads/master", + "user_id": 4, + "user_name": "John Smith", + "user_email": "john@example.com", + "project_id": 15, + "repository": { + "name": "Diaspora", + "url": "git@example.com:mike/diasporadiaspora.git", + "description": "", + "homepage": "http://example.com/mike/diaspora", + "git_http_url":"http://example.com/mike/diaspora.git", + "git_ssh_url":"git@example.com:mike/diaspora.git", + "visibility_level":0 + }, + "commits": [ + { + "id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", + "message": "Update Catalan translation to e38cb41.", + "timestamp": "2011-12-12T14:27:31+02:00", + "url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327", + "author": { + "name": "Jordi Mallach", + "email": "jordi@softcatala.org" + } + }, + { + "id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "message": "fixed readme", + "timestamp": "2012-01-03T23:36:29+02:00", + "url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7", + "author": { + "name": "GitLab dev user", + "email": "gitlabdev@dv6700.(none)" + } + } + ], + "total_commits_count": 4 +} diff --git a/scripts/gitlab/echo.sh b/scripts/gitlab/echo.sh new file mode 100755 index 0000000..da19837 --- /dev/null +++ b/scripts/gitlab/echo.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +echo "gitlab echo: $@" + diff --git a/src/hook/gitlab_hook.go b/src/hook/gitlab_hook.go new file mode 100644 index 0000000..c0d641a --- /dev/null +++ b/src/hook/gitlab_hook.go @@ -0,0 +1,30 @@ +package hook + +import ( + "encoding/json" + "net/http" +) + +type GitlabRecord struct { + Repository struct { + Name string `json:"name"` + URL string `json:"git_ssh_url"` + } `json:"repository"` +} + +func (r *GitlabRecord) GetURL() string { + return r.Repository.URL +} + +func (r *GitlabRecord) GetName() string { + return r.Repository.Name +} + +func (r *GitlabRecord) Decode(req *http.Request) error { + decoder := json.NewDecoder(req.Body) + err := decoder.Decode(&r) + if err != nil { + return err + } + return nil +} diff --git a/src/hook/hook_factory.go b/src/hook/hook_factory.go index c6c7286..2f79071 100644 --- a/src/hook/hook_factory.go +++ b/src/hook/hook_factory.go @@ -17,6 +17,8 @@ func RecordFactory(hookname string) (Record, error) { return new(BitbucketRecord), nil case "github": return new(GithubRecord), nil + case "gitlab": + return new(GitlabRecord), nil case "docker": return new(DockerRecord), nil default: diff --git a/test.sh b/test.sh index e784cd4..702a3f0 100755 --- a/test.sh +++ b/test.sh @@ -22,6 +22,11 @@ curl -H "Content-Type: application/json" \ --data @assets/github.json \ http://$IP:8080/github/echo +echo "Test Gitlab hook" +curl -H "Content-Type: application/json" \ + --data @assets/gitlab.json \ + http://$IP:8080/gitlab/echo + echo "Test Docker hook" curl -H "Content-Type: application/json" \ --data @assets/docker.json \