Skip to content

Commit 9fee0ae

Browse files
authored
feat: add Azure DevOps git push event (#181)
Added Azure DevOps code pushed event based on following definition: https://learn.microsoft.com/en-us/azure/devops/service-hooks/events?view=azure-devops#code-pushed
1 parent 61716b8 commit 9fee0ae

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

azuredevops/azuredevops.go

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
GitPullRequestCreatedEventType Event = "git.pullrequest.created"
2828
GitPullRequestUpdatedEventType Event = "git.pullrequest.updated"
2929
GitPullRequestMergedEventType Event = "git.pullrequest.merged"
30+
GitPushEventType Event = "git.push"
3031
)
3132

3233
// Webhook instance contains all methods needed to process events
@@ -62,6 +63,10 @@ func (hook Webhook) Parse(r *http.Request, events ...Event) (interface{}, error)
6263
}
6364

6465
switch pl.EventType {
66+
case GitPushEventType:
67+
var fpl GitPushEvent
68+
err = json.Unmarshal([]byte(payload), &fpl)
69+
return fpl, err
6570
case GitPullRequestCreatedEventType, GitPullRequestMergedEventType, GitPullRequestUpdatedEventType:
6671
var fpl GitPullRequestEvent
6772
err = json.Unmarshal([]byte(payload), &fpl)

azuredevops/azuredevops_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ func TestWebhooks(t *testing.T) {
8080
typ: GitPullRequestEvent{},
8181
filename: "../testdata/azuredevops/git.pullrequest.updated.json",
8282
},
83+
{
84+
name: "git.push",
85+
event: GitPushEventType,
86+
typ: GitPushEvent{},
87+
filename: "../testdata/azuredevops/git.push.json",
88+
},
8389
}
8490

8591
for _, tt := range tests {

azuredevops/payload.go

+47
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,21 @@ type GitPullRequestEvent struct {
3636
CreatedDate Date `json:"createdDate"`
3737
}
3838

39+
// git.push
40+
41+
type GitPushEvent struct {
42+
CreatedDate string `json:"createdDate"`
43+
DetailedMessage Message `json:"detailedMessage"`
44+
EventType string `json:"eventType"`
45+
ID string `json:"id"`
46+
Message Message `json:"message"`
47+
PublisherID string `json:"publisherId"`
48+
Resource Resource `json:"resource"`
49+
ResourceContainers ResourceContainers `json:"resourceContainers"`
50+
ResourceVersion string `json:"resourceVersion"`
51+
Scope string `json:"scope"`
52+
}
53+
3954
// build.complete
4055

4156
type BuildCompleteEvent struct {
@@ -176,6 +191,38 @@ type Request struct {
176191
RequestedFor User `json:"requestedFor"`
177192
}
178193

194+
type Resource struct {
195+
Commits []Commit `json:"commits"`
196+
Date string `json:"date"`
197+
PushedBy PushedBy `json:"pushedBy"`
198+
PushID int `json:"pushId"`
199+
RefUpdates []RefUpdate `json:"refUpdates"`
200+
Repository Repository `json:"repository"`
201+
URL string `json:"url"`
202+
}
203+
204+
type RefUpdate struct {
205+
Name string `json:"name"`
206+
NewObjectID string `json:"newObjectId"`
207+
OldObjectID string `json:"oldObjectId"`
208+
}
209+
210+
type PushedBy struct {
211+
DisplayName string `json:"displayName"`
212+
ID string `json:"id"`
213+
UniqueName string `json:"uniqueName"`
214+
}
215+
216+
type ResourceContainers struct {
217+
Account Account `json:"account"`
218+
Collection Account `json:"collection"`
219+
Project Account `json:"project"`
220+
}
221+
222+
type Account struct {
223+
ID string `json:"id"`
224+
}
225+
179226
type Date time.Time
180227

181228
func (b *Date) UnmarshalJSON(p []byte) error {

testdata/azuredevops/git.push.json

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"id": "03c164c2-8912-4d5e-8009-3707d5f83734",
3+
"eventType": "git.push",
4+
"publisherId": "tfs",
5+
"scope": "all",
6+
"message": {
7+
"text": "Jamal Hartnett pushed updates to branch master of repository Fabrikam-Fiber-Git.",
8+
"html": "Jamal Hartnett pushed updates to branch master of repository Fabrikam-Fiber-Git.",
9+
"markdown": "Jamal Hartnett pushed updates to branch `master` of repository `Fabrikam-Fiber-Git`."
10+
},
11+
"detailedMessage": {
12+
"text": "Jamal Hartnett pushed 1 commit to branch master of repository Fabrikam-Fiber-Git.\n - Fixed bug in web.config file 33b55f7c",
13+
"html": "Jamal Hartnett pushed 1 commit to branch <a href=\"https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_git/Fabrikam-Fiber-Git/#version=GBmaster\">master</a> of repository <a href=\"https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_git/Fabrikam-Fiber-Git/\">Fabrikam-Fiber-Git</a>.\n<ul>\n<li>Fixed bug in web.config file <a href=\"https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_git/Fabrikam-Fiber-Git/commit/33b55f7cb7e7e245323987634f960cf4a6e6bc74\">33b55f7c</a>\n</ul>",
14+
"markdown": "Jamal Hartnett pushed 1 commit to branch [master](https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_git/Fabrikam-Fiber-Git/#version=GBmaster) of repository [Fabrikam-Fiber-Git](https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_git/Fabrikam-Fiber-Git/).\n* Fixed bug in web.config file [33b55f7c](https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_git/Fabrikam-Fiber-Git/commit/33b55f7cb7e7e245323987634f960cf4a6e6bc74)"
15+
},
16+
"resource": {
17+
"commits": [
18+
{
19+
"commitId": "33b55f7cb7e7e245323987634f960cf4a6e6bc74",
20+
"author": {
21+
"name": "Jamal Hartnett",
22+
"email": "[email protected]",
23+
"date": "2015-02-25T19:01:00Z"
24+
},
25+
"committer": {
26+
"name": "Jamal Hartnett",
27+
"email": "[email protected]",
28+
"date": "2015-02-25T19:01:00Z"
29+
},
30+
"comment": "Fixed bug in web.config file",
31+
"url": "https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_git/Fabrikam-Fiber-Git/commit/33b55f7cb7e7e245323987634f960cf4a6e6bc74"
32+
}
33+
],
34+
"refUpdates": [
35+
{
36+
"name": "refs/heads/main",
37+
"oldObjectId": "aad331d8d3b131fa9ae03cf5e53965b51942618a",
38+
"newObjectId": "33b55f7cb7e7e245323987634f960cf4a6e6bc74"
39+
}
40+
],
41+
"repository": {
42+
"id": "278d5cd2-584d-4b63-824a-2ba458937249",
43+
"name": "Fabrikam-Fiber-Git",
44+
"url": "https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_apis/repos/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249",
45+
"project": {
46+
"id": "6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
47+
"name": "Fabrikam-Fiber-Git",
48+
"url": "https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_apis/projects/6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c",
49+
"state": "wellFormed"
50+
},
51+
"defaultBranch": "refs/heads/main",
52+
"remoteUrl": "https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_git/Fabrikam-Fiber-Git"
53+
},
54+
"pushedBy": {
55+
56+
"displayName": "Jamal Hartnett",
57+
"uniqueName": "Windows Live ID\\[email protected]"
58+
},
59+
"pushId": 14,
60+
"date": "2014-05-02T19:17:13.3309587Z",
61+
"url": "https://dev.azure.com/fabrikam-fiber-inc/DefaultCollection/_apis/repos/git/repositories/278d5cd2-584d-4b63-824a-2ba458937249/pushes/14"
62+
},
63+
"resourceVersion": "1.0",
64+
"resourceContainers": {
65+
"collection": {
66+
"id": "c12d0eb8-e382-443b-9f9c-c52cba5014c2"
67+
},
68+
"account": {
69+
"id": "f844ec47-a9db-4511-8281-8b63f4eaf94e"
70+
},
71+
"project": {
72+
"id": "be9b3917-87e6-42a4-a549-2bc06a7a878f"
73+
}
74+
},
75+
"createdDate": "2016-09-19T13:03:27.0379153Z"
76+
}

0 commit comments

Comments
 (0)