Skip to content

Commit c8bb86f

Browse files
authored
add web hook cause (#36)
1 parent 50dd30f commit c8bb86f

File tree

8 files changed

+111
-0
lines changed

8 files changed

+111
-0
lines changed

src/main/java/io/jenkins/plugins/gitlabbranchsource/AbstractGitLabSCMHeadEvent.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ public boolean isMatch(@NonNull SCM scm) {
4949
@NonNull
5050
protected abstract Map<SCMHead, SCMRevision> headsFor(GitLabSCMSource source);
5151

52+
public abstract GitLabWebHookCause getCause();
53+
5254
}

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestSCMEvent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,9 @@ public Map<SCMHead, SCMRevision> headsFor(GitLabSCMSource source) {
164164
return result;
165165
}
166166

167+
@Override
168+
public GitLabWebHookCause getCause() {
169+
return new GitLabWebHookCause().fromMergeRequest(getPayload());
170+
}
171+
167172
}

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabPushSCMEvent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,10 @@ public Map<SCMHead, SCMRevision> headsFor(GitLabSCMSource source) {
8686
StringUtils.isNotBlank(getPayload().getAfter())
8787
? new BranchSCMRevision(h, getPayload().getAfter()) : null);
8888
}
89+
90+
@Override
91+
public GitLabWebHookCause getCause() {
92+
return new GitLabWebHookCause().fromPush(getPayload());
93+
}
94+
8995
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package io.jenkins.plugins.gitlabbranchsource;
2+
3+
import hudson.model.Cause;
4+
import hudson.model.CauseAction;
5+
6+
public class GitLabSCMCauseAction extends CauseAction {
7+
8+
public GitLabSCMCauseAction(Cause... causes) {
9+
super(causes);
10+
}
11+
12+
public String getDescription() {
13+
GitLabWebHookCause cause = findCause(GitLabWebHookCause.class);
14+
return (cause != null) ? cause.getShortDescription() : null;
15+
}
16+
17+
}

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ protected List<Action> retrieveActions(@NonNull SCMRevision revision, SCMHeadEve
633633
actions.add(GitLabLink.toCommit(commitUrl));
634634
}
635635

636+
if (event instanceof AbstractGitLabSCMHeadEvent) {
637+
actions.add(new GitLabSCMCauseAction(((AbstractGitLabSCMHeadEvent) event).getCause()));
638+
}
639+
636640
return actions;
637641
}
638642

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabTagPushSCMEvent.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ public Map<SCMHead, SCMRevision> headsFor(GitLabSCMSource source) {
8484
? new GitTagSCMRevision(h, hash) : null);
8585
}
8686

87+
@Override
88+
public GitLabWebHookCause getCause() {
89+
return new GitLabWebHookCause().fromTag(getPayload());
90+
}
91+
8792
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package io.jenkins.plugins.gitlabbranchsource;
2+
3+
import hudson.triggers.SCMTrigger.SCMTriggerCause;
4+
import org.apache.commons.lang.StringUtils;
5+
import org.gitlab4j.api.webhook.MergeRequestEvent;
6+
import org.gitlab4j.api.webhook.MergeRequestEvent.ObjectAttributes;
7+
import org.gitlab4j.api.webhook.PushEvent;
8+
import org.gitlab4j.api.webhook.TagPushEvent;
9+
10+
public class GitLabWebHookCause extends SCMTriggerCause {
11+
12+
private String description;
13+
14+
public GitLabWebHookCause() {
15+
super("");
16+
}
17+
18+
public GitLabWebHookCause fromPush(PushEvent pushEvent) {
19+
String userName = pushEvent.getUserName();
20+
if (StringUtils.isBlank(userName)) {
21+
description = Messages.GitLabWebHookCause_ShortDescription_Push_noUser();
22+
} else {
23+
description = Messages.GitLabWebHookCause_ShortDescription_Push(userName);
24+
}
25+
return this;
26+
}
27+
28+
public GitLabWebHookCause fromMergeRequest(MergeRequestEvent mergeRequestEvent) {
29+
ObjectAttributes objectAttributes = mergeRequestEvent.getObjectAttributes();
30+
String id = String.valueOf(objectAttributes.getIid());
31+
String sourceNameSpace = objectAttributes.getSource().getNamespace();
32+
String targetNameSpace = objectAttributes.getTarget().getNamespace();
33+
String nameSpace =
34+
StringUtils.equals(sourceNameSpace, targetNameSpace) ? "" : sourceNameSpace + "/";
35+
String source = String.format("%s%s", nameSpace, objectAttributes.getSourceBranch());
36+
description = Messages.GitLabWebHookCause_ShortDescription_MergeRequestHook(
37+
id, source, objectAttributes.getTargetBranch());
38+
return this;
39+
}
40+
41+
public GitLabWebHookCause fromTag(TagPushEvent tagPushEvent) {
42+
String userName = tagPushEvent.getUserName();
43+
if (StringUtils.isBlank(userName)) {
44+
description = Messages.GitLabWebHookCause_ShortDescription_Push_noUser();
45+
} else {
46+
description = Messages.GitLabWebHookCause_ShortDescription_Push(userName);
47+
}
48+
return this;
49+
}
50+
51+
@Override
52+
public String getShortDescription() {
53+
return description;
54+
}
55+
56+
@Override
57+
public boolean equals(Object o) {
58+
if (!super.equals(o)) {
59+
return false;
60+
}
61+
return o instanceof GitLabWebHookCause;
62+
}
63+
64+
@Override
65+
public int hashCode() {
66+
return super.hashCode();
67+
}
68+
69+
}

src/main/resources/io/jenkins/plugins/gitlabbranchsource/Messages.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,6 @@ HookRegistrationTrait.useSystem=Use System credentials for {0} management mode (
4848
SkipNotificationsTrait.displayName=Skip pipeline status notifications
4949
LogCommentTrait.displayName=Log build status as comment on GitLab
5050
TriggerMRCommentTrait.displayName=Trigger build on merge request comment
51+
GitLabWebHookCause.ShortDescription.Push_noUser=Started by GitLab push
52+
GitLabWebHookCause.ShortDescription.Push=Started by GitLab push by {0}
53+
GitLabWebHookCause.ShortDescription.MergeRequestHook=Triggered by GitLab Merge Request #{0}: {1} => {2}

0 commit comments

Comments
 (0)