Skip to content

Commit

Permalink
User should choose if they want to notify Tuleap. (#244)
Browse files Browse the repository at this point in the history
* User should choose if they want to notify Tuleap.

[request #21858](https://tuleap.net/plugins/tracker/?tracker=140&aid=21858) Notifying Tuleap should not be systematic

How to test:
 - In your job configuration you now can add or remove the new behaviors: `Notify build status to Tuleap`
If the trait is selected then Jenkins will notify Tuleap.
If the traits is not selected then nothing will be done. Nothing will be
print in the Jenkins console of the build

This traits is not enabled by default, you have to select it if you want
notify Tuleap.

Co-authored-by: Clarck Robinson <[email protected]>
  • Loading branch information
robinsoc and Clarck Robinson authored Jul 9, 2021
1 parent 5994283 commit 8ee0db7
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import io.jenkins.plugins.tuleap_api.client.GitApi;
import io.jenkins.plugins.tuleap_api.client.TuleapApiGuiceModule;
import io.jenkins.plugins.tuleap_api.client.internals.entities.TuleapBuildStatus;
import org.jenkinsci.plugins.tuleap_git_branch_source.notify.TuleapPipelineStatusHandler;
import org.jenkinsci.plugins.tuleap_git_branch_source.notify.TuleapPipelineStatusNotifier;

import java.io.File;
import java.util.logging.Level;
Expand All @@ -24,10 +26,12 @@ public class TuleapPipelineWatcher {
private static final Logger LOGGER = Logger
.getLogger(TuleapPipelineWatcher.class.getName());

private static TuleapPipelineStatusNotifier getNotifier() {
private static TuleapPipelineStatusHandler getHandler() {
final Injector injector = Guice.createInjector(new TuleapApiGuiceModule());
return new TuleapPipelineStatusNotifier(
injector.getInstance(GitApi.class)
return new TuleapPipelineStatusHandler(
new TuleapPipelineStatusNotifier(
injector.getInstance(GitApi.class)
)
);
}

Expand All @@ -43,7 +47,7 @@ public void onCheckout(
SCMRevisionState pollingBaseline
) {
LOGGER.log(Level.INFO, String.format("Tuleap build: Checkout > %s", build.getFullDisplayName()));
getNotifier().sendBuildStatusToTuleap(build, listener.getLogger(), TuleapBuildStatus.pending);
getHandler().handleCommitNotification(build, listener.getLogger(), TuleapBuildStatus.pending);
}
}

Expand All @@ -56,7 +60,7 @@ public void onCompleted(Run<?, ?> build, @NonNull TaskListener listener) {
final Result buildResult = build.getResult();
final TuleapBuildStatus status = (buildResult == Result.SUCCESS) ? TuleapBuildStatus.success : TuleapBuildStatus.failure;

getNotifier().sendBuildStatusToTuleap(build, listener.getLogger(), status);
getHandler().handleCommitNotification(build, listener.getLogger(), status);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class TuleapSCMSourceContext extends SCMSourceContext<TuleapSCMSourceCont
*/
private boolean wantBranches = false;

private boolean notifyPullRequest = false;

public TuleapSCMSourceContext(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer) {
super(criteria, observer);
}
Expand All @@ -32,6 +34,10 @@ public final boolean wantBranches() {
return wantBranches;
}

public final boolean isNotifyPullRequest() {
return this.notifyPullRequest;
}

/**
* Adds a requirement for branch details to any {@link TuleapSCMSourceContext} for this context.
*
Expand All @@ -46,4 +52,10 @@ public TuleapSCMSourceContext wantBranches(boolean include) {
return this;
}

@NonNull
public TuleapSCMSourceContext notifyPullRequest(boolean notify) {
this.notifyPullRequest = this.notifyPullRequest || notify;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ public class TuleapSCMSourceRequest extends SCMSourceRequest {
*/
private final boolean fetchBranches;

private final boolean notifyPullRequest;

protected TuleapSCMSourceRequest(@NonNull SCMSource source, @NonNull TuleapSCMSourceContext context,
@CheckForNull TaskListener listener) {
super(source, context, listener);

fetchBranches = context.wantBranches();
this.fetchBranches = context.wantBranches();
this.notifyPullRequest = context.isNotifyPullRequest();
}
public boolean isFetchBranches() {
return fetchBranches;
return this.fetchBranches;
}

public boolean isNotifyPullRequest() {
return this.notifyPullRequest;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.jenkinsci.plugins.tuleap_git_branch_source.notify;

import hudson.model.Run;
import io.jenkins.plugins.tuleap_api.client.internals.entities.TuleapBuildStatus;
import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.tuleap_git_branch_source.TuleapSCMSource;
import org.jenkinsci.plugins.tuleap_git_branch_source.trait.TuleapCommitNotificationTrait;

import java.io.PrintStream;

public class TuleapPipelineStatusHandler {

private final TuleapPipelineStatusNotifier notifier;

public TuleapPipelineStatusHandler(TuleapPipelineStatusNotifier notifier) {
this.notifier = notifier;
}

public void handleCommitNotification(Run<?, ?> build, PrintStream logger, TuleapBuildStatus status) {
TuleapSCMSource source = this.getTuleapSCMSource(build);

if (source == null) {
return;
}

if (!this.isCommitNotificationTraitsEnabled(source)) {
return;
}

logger.println("Sending the commit build status");
this.notifier.sendBuildStatusToTuleap(build, logger, status, source);
}

private TuleapSCMSource getTuleapSCMSource(Run<?, ?> build) {
final SCMSource source = SCMSource.SourceByItem.findSource(build.getParent());
if (!(source instanceof TuleapSCMSource)) {
return null;
}
return (TuleapSCMSource) source;
}

private boolean isCommitNotificationTraitsEnabled(TuleapSCMSource source) {
return source.getTraits().stream().anyMatch(scmSourceTrait ->
scmSourceTrait instanceof TuleapCommitNotificationTrait);
}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.jenkinsci.plugins.tuleap_git_branch_source;
package org.jenkinsci.plugins.tuleap_git_branch_source.notify;

import hudson.model.Run;
import hudson.plugins.git.util.BuildData;
import io.jenkins.plugins.tuleap_api.client.GitApi;
import io.jenkins.plugins.tuleap_api.client.internals.entities.TuleapBuildStatus;
import io.jenkins.plugins.tuleap_credentials.TuleapAccessToken;

import jenkins.scm.api.SCMSource;
import org.jenkinsci.plugins.tuleap_git_branch_source.TuleapSCMSource;
import org.jenkinsci.plugins.tuleap_git_branch_source.config.TuleapConnector;
import org.jetbrains.annotations.Nullable;

Expand All @@ -19,22 +19,7 @@ public TuleapPipelineStatusNotifier(GitApi gitApi) {
this.gitApi = gitApi;
}

@Nullable
private TuleapSCMSource getTuleapSCMSource(Run<?, ?> build) {
final SCMSource source = SCMSource.SourceByItem.findSource(build.getParent());
if (!(source instanceof TuleapSCMSource)) {
return null;
}
return (TuleapSCMSource) source;
}

public void sendBuildStatusToTuleap(Run<?, ?> build, PrintStream logger, TuleapBuildStatus status) {
final TuleapSCMSource source = getTuleapSCMSource(build);
if (source == null) {
// Not a TuleapSCMSource, abort
return;
}

public void sendBuildStatusToTuleap(Run<?, ?> build, PrintStream logger, TuleapBuildStatus status, TuleapSCMSource source) {
final TuleapAccessToken token = getAccessKey(source);
if (token == null) {
throw new RuntimeException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package org.jenkinsci.plugins.tuleap_git_branch_source.trait;

import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import jenkins.scm.api.SCMHeadCategory;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.trait.SCMSourceContext;
import jenkins.scm.api.trait.SCMSourceTrait;
import jenkins.scm.api.trait.SCMSourceTraitDescriptor;
import jenkins.scm.impl.trait.Discovery;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.tuleap_git_branch_source.Messages;
import org.jenkinsci.plugins.tuleap_git_branch_source.TuleapSCMSource;
import org.jenkinsci.plugins.tuleap_git_branch_source.TuleapSCMSourceContext;
import org.kohsuke.stapler.DataBoundConstructor;

public class TuleapCommitNotificationTrait extends SCMSourceTrait {
@DataBoundConstructor
public TuleapCommitNotificationTrait() {}

/**
* {@inheritDoc}
*/
@Override
protected void decorateContext(SCMSourceContext<?, ?> context) {
TuleapSCMSourceContext tuleapSourceContext = (TuleapSCMSourceContext) context;
tuleapSourceContext.notifyPullRequest(true);
}

/**
* {@inheritDoc}
*/
@Override
public boolean includeCategory(@NonNull SCMHeadCategory category) {
return category.isUncategorized();
}

@Symbol("tuleapNotifyPullRequest")
@Extension
public static class DescriptorImpl extends SCMSourceTraitDescriptor {

/**
* {@inheritDoc}
*/
@Override
public String getDisplayName() {
return Messages.TuleapCommitNotificationTrait_displayName();
}

/**
* {@inheritDoc}
*/
@Override
public Class<? extends SCMSourceContext> getContextClass() {
return TuleapSCMSourceContext.class;
}

/**
* {@inheritDoc}
*/
@Override
public Class<? extends SCMSource> getSourceClass() {
return TuleapSCMSource.class;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ BranchSCMHead.pronoun=Branch

TuleapBranchDiscoveryTrait.displayName=Tuleap branches autodiscovery

TuleapCommitNotificationTrait.displayName=Notify build status to Tuleap

WildcardSCMSourceFilterTrait.displayName=Filter repositories by name (wildcards)
Loading

0 comments on commit 8ee0db7

Please sign in to comment.