Skip to content

Commit 2ed1987

Browse files
committed
Add Bitbucket
1 parent efac49c commit 2ed1987

File tree

8 files changed

+1449
-0
lines changed

8 files changed

+1449
-0
lines changed

platform-api/src/main/java/com/flow/platform/api/git/GitEventEnvConverter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collections;
2929
import java.util.HashMap;
3030
import java.util.Map;
31+
import java.util.Objects;
3132

3233
/**
3334
* Extract required GitEnv from git event
@@ -105,6 +106,10 @@ public static Map<String, String> convert(GitEvent event) {
105106
info.put(GitEnvs.FLOW_GIT_CHANGELOG.name(), pt.getCommits().get(0).getMessage());
106107
}
107108

109+
if (Objects.equals(pt.getType(), GitEventType.TAG)) {
110+
info.put(GitEnvs.FLOW_GIT_CHANGELOG.name(), pt.getMessage());
111+
}
112+
108113
return info;
109114
}
110115

Lines changed: 344 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,344 @@
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.GitPullRequestEvent;
25+
import com.flow.platform.util.git.model.GitPullRequestInfo;
26+
import com.flow.platform.util.git.model.GitPushTagEvent;
27+
import com.flow.platform.util.git.model.GitSource;
28+
import com.google.gson.annotations.SerializedName;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
import java.util.Objects;
32+
33+
/**
34+
* @author yh@firim
35+
*/
36+
public class BitbucketEvents {
37+
38+
public static class Hooks {
39+
40+
public final static String HEADER = "x-event-key";
41+
42+
public final static String EVENT_TYPE_PUSH = "repo:push";
43+
44+
public final static String EVENT_TYPE_PR_CREATED = "pullrequest:created";
45+
46+
public final static String EVENT_TYPE_PR_UPDATED = "pullrequest:updated";
47+
48+
public final static String EVENT_TYPE_PR_MERGERED = "pullrequest:fulfilled";
49+
}
50+
51+
52+
private class UserInfo {
53+
54+
private String username;
55+
56+
private String type;
57+
58+
@SerializedName("display_name")
59+
private String displayName;
60+
61+
private String uuid;
62+
}
63+
64+
private class Repository {
65+
66+
private String scm;
67+
68+
private String name;
69+
70+
@SerializedName("full_name")
71+
private String fullName;
72+
73+
private UserInfo owner;
74+
75+
@SerializedName(value = "is_private")
76+
private Boolean isPrivate;
77+
78+
private String uuid;
79+
80+
}
81+
82+
private class AuthorHelper {
83+
84+
private String raw;
85+
private String type;
86+
private UserInfo user;
87+
}
88+
89+
private class HrefHelper {
90+
91+
@SerializedName(value = "href")
92+
private String value;
93+
}
94+
95+
96+
private class CompareHelper {
97+
98+
private HrefHelper commits;
99+
100+
private HrefHelper html;
101+
102+
private HrefHelper diff;
103+
}
104+
105+
private class CommitHelper {
106+
107+
private String hash;
108+
private AuthorHelper author;
109+
private String date;
110+
private String message;
111+
private String type;
112+
@SerializedName(value = "links")
113+
private CompareHelper diff;
114+
}
115+
116+
117+
public static class PushAndTagAdapter extends GitHookEventAdapter {
118+
119+
private enum PushType {
120+
BRANCH,
121+
TAG
122+
}
123+
124+
PushAndTagAdapter(GitSource gitSource, GitEventType eventType) {
125+
super(gitSource, eventType);
126+
}
127+
128+
private class RootHelper {
129+
130+
@SerializedName(value = "actor")
131+
private UserInfo pusher;
132+
133+
private Repository repository;
134+
135+
@SerializedName(value = "push")
136+
private AttrHelper attrs;
137+
}
138+
139+
private class AttrHelper {
140+
141+
private List<ChangeHelper> changes;
142+
}
143+
144+
private class ChangeHelper {
145+
146+
private Boolean forced;
147+
148+
private Boolean truncated;
149+
150+
private Boolean created;
151+
152+
private Boolean closed;
153+
154+
@SerializedName(value = "old")
155+
private PushHelper beforePush;
156+
157+
@SerializedName(value = "new")
158+
private PushHelper afterPush;
159+
160+
private List<CommitHelper> commits;
161+
162+
@SerializedName(value = "links")
163+
private CompareHelper diff;
164+
}
165+
166+
private class PushHelper {
167+
168+
private String type;
169+
170+
@SerializedName(value = "name")
171+
private String branchName;
172+
173+
private CommitHelper target;
174+
175+
private String message;
176+
}
177+
178+
179+
@Override
180+
public GitEvent convert(String json) throws GitException {
181+
RootHelper rootHelper = GSON.fromJson(json, RootHelper.class);
182+
GitPushTagEvent event;
183+
ChangeHelper changeHelper = rootHelper.attrs.changes.get(0);
184+
185+
// push
186+
if (changeHelper.afterPush.type.toUpperCase().equals(PushType.BRANCH.toString())) {
187+
event = new GitPushTagEvent(GitSource.BITBUCKET, GitEventType.PUSH);
188+
event.setAfter(changeHelper.afterPush.target.hash);
189+
convertBranch(changeHelper, event);
190+
// tag
191+
} else if (changeHelper.afterPush.type.toUpperCase().equals(PushType.TAG.toString())) {
192+
event = new GitPushTagEvent(GitSource.BITBUCKET, GitEventType.TAG);
193+
convertTag(changeHelper, event);
194+
} else {
195+
return null;
196+
}
197+
198+
event.setUsername(rootHelper.pusher.username);
199+
200+
// can't get user email
201+
event.setUserEmail(rootHelper.pusher.username);
202+
event.setUserId(rootHelper.pusher.uuid);
203+
event.setHeadCommitUrl(changeHelper.afterPush.target.diff.html.value);
204+
205+
return event;
206+
}
207+
208+
private GitEvent convertBranch(ChangeHelper changeHelper, GitPushTagEvent event) {
209+
event.setRef("refs/heads/" + changeHelper.afterPush.branchName);
210+
event.setBefore(changeHelper.beforePush.target.hash);
211+
event.setMessage(changeHelper.afterPush.target.message);
212+
213+
// set compare value
214+
event.setCompareUrl(changeHelper.diff.html.value);
215+
216+
// set compare id
217+
final String compareId = GitPushTagEvent.buildCompareId(event);
218+
event.setCompareId(compareId);
219+
220+
event.setCommits(new ArrayList<>(changeHelper.commits.size()));
221+
222+
for (CommitHelper commit : changeHelper.commits) {
223+
GitEventCommit eventCommit = new GitEventCommit();
224+
eventCommit.setId(commit.hash);
225+
eventCommit.setMessage(commit.message);
226+
eventCommit.setTimestamp(commit.date);
227+
228+
// set commit author
229+
GitEventAuthor author = new GitEventAuthor();
230+
author.setId(commit.author.user.uuid);
231+
author.setName(commit.author.user.username);
232+
eventCommit.setAuthor(author);
233+
eventCommit.setUrl(commit.diff.html.value);
234+
event.getCommits().add(eventCommit);
235+
}
236+
237+
return event;
238+
}
239+
240+
private GitEvent convertTag(ChangeHelper changeHelper, GitPushTagEvent event) {
241+
event.setRef("refs/tags/" + changeHelper.afterPush.branchName);
242+
event.setCommits(new ArrayList<>(0));
243+
event.setMessage(changeHelper.afterPush.message);
244+
event.setAfter(changeHelper.afterPush.target.hash);
245+
return event;
246+
}
247+
}
248+
249+
250+
public static class PullRequestAdapter extends GitHookEventAdapter {
251+
252+
private class RootHelper {
253+
254+
@SerializedName(value = "pullrequest")
255+
private PullRequestHelper pullRequest;
256+
257+
@SerializedName(value = "actor")
258+
private UserInfo creator;
259+
260+
private Repository repository;
261+
}
262+
263+
private class PullRequestHelper {
264+
265+
private String type;
266+
267+
private String description;
268+
269+
private String title;
270+
271+
@SerializedName(value = "close_source_branch")
272+
private Boolean closeSourceBranch;
273+
274+
private BranchHelper destination;
275+
276+
private BranchHelper source;
277+
278+
private UserInfo author;
279+
280+
private String state;
281+
282+
@SerializedName(value = "links")
283+
private CompareHelper diff;
284+
285+
}
286+
287+
private class BranchAttrHelper {
288+
289+
private String name;
290+
}
291+
292+
private class BranchHelper {
293+
294+
private CommitHelper commit;
295+
private BranchAttrHelper branch;
296+
private Repository repository;
297+
}
298+
299+
public final static String STATE_OPEN = "OPEN";
300+
301+
public final static String STATE_CLOSE = "MERGED";
302+
303+
public PullRequestAdapter(GitSource gitSource, GitEventType eventType) {
304+
super(gitSource, eventType);
305+
}
306+
307+
@Override
308+
public GitEvent convert(String json) throws GitException {
309+
310+
RootHelper rootHelper = GSON.fromJson(json, RootHelper.class);
311+
312+
GitPullRequestEvent event = new GitPullRequestEvent(gitSource, eventType);
313+
314+
if (Objects.equals(rootHelper.pullRequest.state, STATE_OPEN)) {
315+
event.setAction(STATE_OPEN);
316+
event.setSubmitter(rootHelper.creator.username);
317+
}
318+
319+
if (Objects.equals(rootHelper.pullRequest.state, STATE_CLOSE)) {
320+
event.setAction(STATE_CLOSE);
321+
event.setSubmitter(rootHelper.creator.username);
322+
}
323+
324+
event.setDescription(rootHelper.pullRequest.description);
325+
event.setTitle(rootHelper.pullRequest.title);
326+
event.setUrl(rootHelper.pullRequest.diff.html.value);
327+
event.setSource(new GitPullRequestInfo());
328+
event.setTarget(new GitPullRequestInfo());
329+
330+
//can't get user email
331+
event.setUserEmail(rootHelper.creator.username);
332+
333+
event.getSource().setBranch(rootHelper.pullRequest.source.branch.name);
334+
event.getSource().setSha(rootHelper.pullRequest.source.commit.hash);
335+
event.getSource().setProjectName(rootHelper.pullRequest.source.repository.fullName);
336+
337+
event.getTarget().setBranch(rootHelper.pullRequest.destination.branch.name);
338+
event.getTarget().setSha(rootHelper.pullRequest.destination.commit.hash);
339+
event.getTarget().setProjectName(rootHelper.pullRequest.destination.repository.fullName);
340+
341+
return event;
342+
}
343+
}
344+
}

0 commit comments

Comments
 (0)