Skip to content

Commit

Permalink
Expose additional trigger information from build cause
Browse files Browse the repository at this point in the history
With this, it should be possible to use multiple different triggers and to
execute different steps based on the particular trigger. It also provides a way
to notify the user who triggered the build via GitHub comments.

The full comment is exposed so that trigger commands could be parameterized.
E.g. extracting the module name from `test module-x`, which matches a generic
`test.*` trigger.
  • Loading branch information
deiwin committed Apr 4, 2018
1 parent 97b8568 commit 76ab485
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 8 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ pipeline {
}
```

### Detecting whether a build was started by the trigger in a script:
```groovy
def triggerCause = currentBuild.rawBuild.getCause(
org.jenkinsci.plugins.pipeline.github.trigger.IssueCommentCause
)
if (triggerCause) {
echo("Build was started by ${triggerCause.userLogin}, who wrote: " +
"\"${triggerCause.comment}\", which matches the " +
"\"${triggerCause.triggerPattern}\" trigger pattern.")
} else {
echo('Build was not started by a trigger')
}
```

# Global Variables

## `repository`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.StringReader;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

/**
Expand Down Expand Up @@ -115,22 +116,24 @@ private void handleIssueComment(final GHSubscriberEvent event) {
if (job == null) {
LOG.debug("No job found matching key: {}", key);
} else {
boolean matchingTriggers = job.getTriggersJobProperty()
Optional<IssueCommentTrigger> matchingTrigger = job.getTriggersJobProperty()
.getTriggers()
.stream()
.filter(t -> t instanceof IssueCommentTrigger)
.map(IssueCommentTrigger.class::cast)
.filter(t -> triggerMatches(t, issueCommentEvent.getComment(), job))
.findAny()
.map(t -> Boolean.TRUE)
.orElse(Boolean.FALSE);
.findAny();

if (matchingTriggers) {
if (matchingTrigger.isPresent()) {
String commentAuthor = issueCommentEvent.getComment().getUserName();
boolean authorized = isAuthorized(job, commentAuthor);

if (authorized) {
job.scheduleBuild(new IssueCommentCause(issueCommentEvent.getComment().getUserName()));
job.scheduleBuild(
new IssueCommentCause(
issueCommentEvent.getComment().getUserName(),
issueCommentEvent.getComment().getBody(),
matchingTrigger.get().getCommentPattern()));
LOG.info("Job: {} triggered by IssueComment: {}",
job.getFullName(), issueCommentEvent.getComment());
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jenkinsci.plugins.pipeline.github.trigger;

import hudson.model.Cause;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;

/**
* Represents the user who authored the comment that triggered the build.
Expand All @@ -9,13 +10,32 @@
*/
public class IssueCommentCause extends Cause {
private final String userLogin;
private final String comment;
private final String triggerPattern;

public IssueCommentCause(final String userLogin) {
public IssueCommentCause(final String userLogin, final String comment, final String triggerPattern) {
this.userLogin = userLogin;
this.comment = comment;
this.triggerPattern = triggerPattern;
}

@Whitelisted
public String getUserLogin() {
return userLogin;
}

@Whitelisted
public String getComment() {
return comment;
}

@Whitelisted
public String getTriggerPattern() {
return triggerPattern;
}

@Override
public String getShortDescription() {
return String.format("Started by an IssueComment from user: %s", userLogin);
return String.format("%s commented: %s", userLogin, comment);
}
}

0 comments on commit 76ab485

Please sign in to comment.