Skip to content

Commit d8b0bba

Browse files
pyatizbyantseviaПятизбянцев Илья Андреевич
andauthored
Mark unstable build as success on Gitlab (#328)
Co-authored-by: Пятизбянцев Илья Андреевич <ipyatizbyantsev@nicetu.spb.ru>
1 parent 67b7169 commit d8b0bba

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package io.jenkins.plugins.gitlabbranchsource;
2+
3+
import hudson.Extension;
4+
import jenkins.scm.api.SCMSource;
5+
import jenkins.scm.api.trait.SCMSourceContext;
6+
import jenkins.scm.api.trait.SCMSourceTrait;
7+
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
8+
import org.jenkinsci.Symbol;
9+
import org.kohsuke.stapler.DataBoundConstructor;
10+
import org.kohsuke.stapler.DataBoundSetter;
11+
12+
public class GitLabMarkUnstableAsSuccessTrait extends SCMSourceTrait {
13+
14+
private boolean markUnstableAsSuccess = false;
15+
16+
@DataBoundConstructor
17+
public GitLabMarkUnstableAsSuccessTrait() {
18+
// empty
19+
}
20+
21+
@DataBoundSetter
22+
public void setMarkUnstableAsSuccess(boolean markUnstableAsSuccess) {
23+
this.markUnstableAsSuccess = markUnstableAsSuccess;
24+
}
25+
26+
public boolean getMarkUnstableAsSuccess() {
27+
return markUnstableAsSuccess;
28+
}
29+
30+
@Override
31+
protected void decorateContext(SCMSourceContext<?, ?> context) {
32+
if (context instanceof GitLabSCMSourceContext) {
33+
GitLabSCMSourceContext ctx = (GitLabSCMSourceContext) context;
34+
ctx.withMarkUnstableAsSuccess(doMarkUnstableAsSuccess());
35+
}
36+
}
37+
38+
public boolean doMarkUnstableAsSuccess() {
39+
return markUnstableAsSuccess;
40+
}
41+
42+
/**
43+
* Our descriptor.
44+
*/
45+
@Extension
46+
@Symbol("gitlabMarkUnstableAsSuccess")
47+
public static class DescriptorImpl extends SCMSourceTraitDescriptor {
48+
49+
@Override
50+
public String getDisplayName() {
51+
return Messages.GitLabMarkUnstableAsSuccessTrait_displayName();
52+
}
53+
54+
@Override
55+
public Class<? extends SCMSourceContext> getContextClass() {
56+
return GitLabSCMSourceContext.class;
57+
}
58+
59+
@Override
60+
public Class<? extends SCMSource> getSourceClass() {
61+
return GitLabSCMSource.class;
62+
}
63+
}
64+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public class GitLabSCMSourceContext extends SCMSourceContext<GitLabSCMSourceCont
7171

7272
private boolean alwaysIgnoreMRWorkInProgress = false;
7373

74+
private boolean markUnstableAsSuccess = false;
75+
7476
public GitLabSCMSourceContext(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer) {
7577
super(criteria, observer);
7678
}
@@ -147,6 +149,10 @@ public final boolean getOnlyTrustedMembersCanTrigger() {
147149
return onlyTrustedMembersCanTrigger;
148150
}
149151

152+
public final boolean getMarkUnstableAsSuccess() {
153+
return markUnstableAsSuccess;
154+
}
155+
150156
public boolean alwaysBuildMROpen() {
151157
return alwaysBuildMROpen;
152158
}
@@ -261,6 +267,12 @@ public final GitLabSCMSourceContext withProjectAvatarDisabled(boolean disabled)
261267
return this;
262268
}
263269

270+
@NonNull
271+
public final GitLabSCMSourceContext withMarkUnstableAsSuccess(boolean disabled) {
272+
this.markUnstableAsSuccess = disabled;
273+
return this;
274+
}
275+
264276
@NonNull
265277
public final GitLabSCMSourceContext withLogCommentEnabled(boolean enabled) {
266278
this.logCommentEnabled = enabled;

src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabPipelineStatusNotifier.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,13 @@ private static void sendNotifications(Run<?, ?> build, TaskListener listener) {
311311
state = Constants.CommitBuildState.SUCCESS;
312312
} else if (Result.UNSTABLE.equals(result)) {
313313
status.setDescription(build.toString() + ": This commit is unstable with partial failure.");
314-
status.setStatus("FAILED");
315-
state = Constants.CommitBuildState.FAILED;
314+
if (sourceContext.getMarkUnstableAsSuccess()) {
315+
status.setStatus("SUCCESS");
316+
state = Constants.CommitBuildState.SUCCESS;
317+
} else {
318+
status.setStatus("FAILED");
319+
state = Constants.CommitBuildState.FAILED;
320+
}
316321
} else if (Result.FAILURE.equals(result)) {
317322
status.setDescription(build.toString() + ": There was a failure building this commit.");
318323
status.setStatus("FAILED");
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?jelly escape-by-default='true'?>
2+
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
3+
<f:entry title="${%Mark unstable as success}" field="markUnstableAsSuccess">
4+
<f:checkbox default="true"/>
5+
</f:entry>
6+
</j:jelly>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ GitLabWebHookCause.ShortDescription.Push_noUser=Started by GitLab push
6060
GitLabWebHookCause.ShortDescription.Push=Started by GitLab push by {0}
6161
GitLabWebHookCause.ShortDescription.MergeRequestHook=Triggered by GitLab Merge Request #{0}: {1} => {2}
6262
WebhookListenerBuildConditionsTrait.displayName=Webhook Listener Conditions
63+
GitLabMarkUnstableAsSuccessTrait.displayName=Mark unstable build as successful on Gitlab

0 commit comments

Comments
 (0)