Skip to content

Commit 0a2a146

Browse files
author
Yang Guo
committed
support coding push webhook parsing
1 parent 77ee3a4 commit 0a2a146

File tree

6 files changed

+279
-0
lines changed

6 files changed

+279
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2017 flow.ci
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.flow.platform.util.git.hooks;
18+
19+
import com.flow.platform.util.git.GitException;
20+
import com.flow.platform.util.git.model.GitEvent;
21+
import com.flow.platform.util.git.model.GitEventAuthor;
22+
import com.flow.platform.util.git.model.GitEventCommit;
23+
import com.flow.platform.util.git.model.GitEventType;
24+
import com.flow.platform.util.git.model.GitPushTagEvent;
25+
import com.flow.platform.util.git.model.GitSource;
26+
import com.google.gson.annotations.SerializedName;
27+
import java.util.List;
28+
29+
/**
30+
* @author yang
31+
*/
32+
public class CodingEvents {
33+
34+
public static class Hooks {
35+
36+
public final static String HEADER = "x-coding-event";
37+
38+
public final static String EVENT_TYPE_PUSH = "push";
39+
40+
public final static String EVENT_TYPE_TAG = "push";
41+
42+
public final static String EVENT_TYPE_PR = "pull_request";
43+
}
44+
45+
public static class PushAndTagAdapter extends GitHookEventAdapter {
46+
47+
private class PushHelper {
48+
49+
private PushUserHelper user;
50+
51+
private RepoHelper repository;
52+
}
53+
54+
private class PushUserHelper {
55+
56+
private String path;
57+
58+
@SerializedName(value = "web_url")
59+
private String url;
60+
61+
private String name;
62+
63+
}
64+
65+
private class RepoHelper {
66+
67+
@SerializedName(value = "web_url")
68+
private String url;
69+
70+
@SerializedName(value = "project_id")
71+
private String id;
72+
}
73+
74+
PushAndTagAdapter(GitSource gitSource, GitEventType eventType) {
75+
super(gitSource, eventType);
76+
}
77+
78+
@Override
79+
public GitEvent convert(String json) throws GitException {
80+
GitPushTagEvent event = GSON.fromJson(json, GitPushTagEvent.class);
81+
PushHelper helper = GSON.fromJson(json, PushHelper.class);
82+
83+
84+
// for create tag event
85+
if (event.getRef().startsWith("refs/tags")) {
86+
event.setType(GitEventType.TAG);
87+
}
88+
89+
// for branch push event
90+
else {
91+
event.setType(GitEventType.PUSH);
92+
}
93+
94+
List<GitEventCommit> commits = event.getCommits();
95+
if (!commits.isEmpty()) {
96+
GitEventCommit latest = commits.get(0);
97+
event.setHeadCommitUrl(latest.getUrl());
98+
event.setMessage(latest.getMessage());
99+
100+
GitEventAuthor author = latest.getAuthor();
101+
if (author != null) {
102+
event.setUserId(author.getName());
103+
event.setUsername(author.getName());
104+
event.setUserEmail(author.getEmail());
105+
}
106+
}
107+
108+
// set compare id
109+
final String compareId = GitPushTagEvent.buildCompareId(event);
110+
event.setCompareId(compareId);
111+
112+
// set compare url
113+
final String compareUrl = helper.repository.url + "/git/compare/" + compareId;
114+
event.setCompareUrl(compareUrl);
115+
event.setGitSource(gitSource);
116+
return event;
117+
}
118+
}
119+
120+
}

platform-util-git/src/main/java/com/flow/platform/util/git/hooks/GitHookEventFactory.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.flow.platform.util.git.model.GitEventType.PR;
2020
import static com.flow.platform.util.git.model.GitEventType.PUSH;
2121
import static com.flow.platform.util.git.model.GitEventType.TAG;
22+
import static com.flow.platform.util.git.model.GitSource.CODING;
2223
import static com.flow.platform.util.git.model.GitSource.GITHUB;
2324
import static com.flow.platform.util.git.model.GitSource.GITLAB;
2425

@@ -55,6 +56,10 @@ public class GitHookEventFactory {
5556
gitHubAdaptors.put(GitHubEvents.Hooks.EVENT_TYPE_PUSH, new GitHubEvents.PushAndTagAdapter(GITHUB, PUSH));
5657
gitHubAdaptors.put(GitHubEvents.Hooks.EVENT_TYPE_TAG, new GitHubEvents.PushAndTagAdapter(GITHUB, TAG));
5758
adaptors.put(GitHubEvents.Hooks.HEADER, gitHubAdaptors);
59+
60+
Map<String, GitHookEventAdapter> codingAdaptors = new HashMap<>(3);
61+
codingAdaptors.put(CodingEvents.Hooks.EVENT_TYPE_PUSH, new CodingEvents.PushAndTagAdapter(CODING, PUSH));
62+
adaptors.put(CodingEvents.Hooks.HEADER, codingAdaptors);
5863
}
5964

6065
/**
@@ -77,6 +82,12 @@ public static GitEvent build(Map<String, String> header, String json) throws Git
7782
matchedAdaptor = adaptors.get(GitHubEvents.Hooks.HEADER).get(gitHubEventType);
7883
}
7984

85+
// looking for Coding event adaptors
86+
String codingEventType = header.get(CodingEvents.Hooks.HEADER);
87+
if (codingEventType != null) {
88+
matchedAdaptor = adaptors.get(CodingEvents.Hooks.HEADER).get(codingEventType);
89+
}
90+
8091
if (matchedAdaptor != null) {
8192
return matchedAdaptor.convert(json);
8293
}

platform-util-git/src/main/java/com/flow/platform/util/git/model/GitEventCommit.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.flow.platform.util.git.model;
1818

19+
import com.google.gson.annotations.SerializedName;
1920
import java.io.Serializable;
2021
import java.util.List;
2122

@@ -27,14 +28,18 @@ public class GitEventCommit implements Serializable {
2728
/**
2829
* Commit sha
2930
*/
31+
@SerializedName(value = "id", alternate = "sha")
3032
private String id;
3133

34+
@SerializedName(value = "message", alternate = "short_message")
3235
private String message;
3336

3437
private String timestamp;
3538

39+
@SerializedName(value = "url", alternate = "web_url")
3640
private String url;
3741

42+
@SerializedName(value = "author", alternate = "committer")
3843
private GitEventAuthor author;
3944

4045
private List<String> added;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2017 flow.ci
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.flow.platform.util.git.test;
18+
19+
import com.flow.platform.util.git.hooks.CodingEvents.Hooks;
20+
import com.flow.platform.util.git.hooks.GitHookEventFactory;
21+
import com.flow.platform.util.git.model.GitEventType;
22+
import com.flow.platform.util.git.model.GitPushTagEvent;
23+
import com.flow.platform.util.git.model.GitSource;
24+
import com.google.common.io.Files;
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.net.URL;
28+
import java.nio.charset.Charset;
29+
import java.util.HashMap;
30+
import java.util.Map;
31+
import org.junit.Assert;
32+
import org.junit.Test;
33+
34+
/**
35+
* @author yang
36+
*/
37+
public class CodingWebHooksEventTest {
38+
39+
@Test
40+
public void should_convert_to_push_event_obj() throws Throwable {
41+
// given:
42+
String pushEventContent = loadWebhookSampleJson("coding/webhook_push.json");
43+
Map<String, String> dummyHeader = new HashMap<>();
44+
dummyHeader.put(Hooks.HEADER, Hooks.EVENT_TYPE_PUSH);
45+
46+
// when: build event from header and json content
47+
GitPushTagEvent pushEvent = (GitPushTagEvent) GitHookEventFactory.build(dummyHeader, pushEventContent);
48+
Assert.assertNotNull(pushEvent);
49+
50+
// then:
51+
Assert.assertEquals(GitEventType.PUSH, pushEvent.getType());
52+
Assert.assertEquals(GitSource.CODING, pushEvent.getGitSource());
53+
54+
Assert.assertEquals("4841b089ecf8dd3dd0010f61bd649bfcc21c1fe8", pushEvent.getBefore());
55+
Assert.assertEquals("b972e2edd91e85ec25ec28c29d7dc3e823f28e8a", pushEvent.getAfter());
56+
Assert.assertEquals("update .flow.yml test test\n", pushEvent.getMessage());
57+
Assert.assertEquals(
58+
"https://coding.net/u/benqyang2006/p/flowclibasic/git/commit/b972e2edd91e85ec25ec28c29d7dc3e823f28e8a",
59+
pushEvent.getHeadCommitUrl());
60+
61+
Assert.assertEquals("4841b089ecf8...b972e2edd91e", pushEvent.getCompareId());
62+
Assert.assertEquals("https://coding.net/u/benqyang2006/p/flowclibasic/git/compare/4841b089ecf8...b972e2edd91e",
63+
pushEvent.getCompareUrl());
64+
65+
Assert.assertEquals("benqyang2006", pushEvent.getUserId());
66+
Assert.assertEquals("benqyang2006", pushEvent.getUsername());
67+
Assert.assertEquals("[email protected]", pushEvent.getUserEmail());
68+
}
69+
70+
private static String loadWebhookSampleJson(String classPath) throws IOException {
71+
URL resource = GitLabHooksEventTest.class.getClassLoader().getResource(classPath);
72+
return Files.toString(new File(resource.getFile()), Charset.forName("UTF-8"));
73+
}
74+
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"ref": "refs/heads/master",
3+
"before": "4841b089ecf8dd3dd0010f61bd649bfcc21c1fe8",
4+
"commits": [
5+
{
6+
"committer": {
7+
"name": "benqyang2006",
8+
"email": "[email protected]"
9+
},
10+
"web_url": "https://coding.net/u/benqyang2006/p/flowclibasic/git/commit/b972e2edd91e85ec25ec28c29d7dc3e823f28e8a",
11+
"short_message": "update .flow.yml test test\n",
12+
"sha": "b972e2edd91e85ec25ec28c29d7dc3e823f28e8a"
13+
}
14+
],
15+
"after": "b972e2edd91e85ec25ec28c29d7dc3e823f28e8a",
16+
"event": "push",
17+
"repository": {
18+
"owner": {
19+
"path": "/u/benqyang2006",
20+
"web_url": "https://coding.net/u/benqyang2006",
21+
"global_key": "benqyang2006",
22+
"name": "benqyang2006",
23+
"avatar": "https://dn-coding-net-production-static.qbox.me/16e639e2-45dd-4959-b38e-576e2ac0041c.jpg?imageMogr2/auto-orient/format/jpeg/crop/!498x498a48a302"
24+
},
25+
"https_url": "https://git.coding.net/benqyang2006/flowclibasic.git",
26+
"web_url": "https://coding.net/u/benqyang2006/p/flowclibasic",
27+
"project_id": "1243975",
28+
"ssh_url": "[email protected]:benqyang2006/flowclibasic.git",
29+
"name": "flowclibasic",
30+
"description": ""
31+
},
32+
"user": {
33+
"path": "/u/benqyang2006",
34+
"web_url": "https://coding.net/u/benqyang2006",
35+
"global_key": "benqyang2006",
36+
"name": "benqyang2006",
37+
"avatar": "https://dn-coding-net-production-static.qbox.me/16e639e2-45dd-4959-b38e-576e2ac0041c.jpg?imageMogr2/auto-orient/format/jpeg/crop/!498x498a48a302"
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"ref": "refs/tags/v1.0",
3+
"before": "0000000000000000000000000000000000000000",
4+
"commits": [],
5+
"after": "b972e2edd91e85ec25ec28c29d7dc3e823f28e8a",
6+
"event": "push",
7+
"repository": {
8+
"owner": {
9+
"path": "/u/benqyang2006",
10+
"web_url": "https://coding.net/u/benqyang2006",
11+
"global_key": "benqyang2006",
12+
"name": "benqyang2006",
13+
"avatar": "https://dn-coding-net-production-static.qbox.me/16e639e2-45dd-4959-b38e-576e2ac0041c.jpg?imageMogr2/auto-orient/format/jpeg/crop/!498x498a48a302"
14+
},
15+
"https_url": "https://git.coding.net/benqyang2006/flowclibasic.git",
16+
"web_url": "https://coding.net/u/benqyang2006/p/flowclibasic",
17+
"project_id": "1243975",
18+
"ssh_url": "[email protected]:benqyang2006/flowclibasic.git",
19+
"name": "flowclibasic",
20+
"description": ""
21+
},
22+
"user": {
23+
"path": "/u/benqyang2006",
24+
"web_url": "https://coding.net/u/benqyang2006",
25+
"global_key": "benqyang2006",
26+
"name": "benqyang2006",
27+
"avatar": "https://dn-coding-net-production-static.qbox.me/16e639e2-45dd-4959-b38e-576e2ac0041c.jpg?imageMogr2/auto-orient/format/jpeg/crop/!498x498a48a302"
28+
}
29+
}

0 commit comments

Comments
 (0)