Skip to content

Commit

Permalink
Merge pull request #65 from v1v/expose/variable
Browse files Browse the repository at this point in the history
expose comment and author as environment variables
  • Loading branch information
aaronjwhiteside authored Mar 6, 2020
2 parents 2b96664 + e10021d commit aeb1116
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ if (triggerCause) {
}
```

#### Environment variables

The GitHub comment and author that triggered the build are exposed as environment variables (from version > 2.5).

* `GITHUB_COMMENT`
* `GITHUB_COMMENT_AUTHOR`

# Global Variables

## `repository`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.jenkinsci.plugins.pipeline.github.trigger;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.EnvironmentContributor;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;


@Restricted(NoExternalUse.class)
public class GitHubEnvironmentVariablesAction extends ParametersAction {

private List<ParameterValue> parameters;

public GitHubEnvironmentVariablesAction(List<ParameterValue> parameters) {
super(parameters);
this.parameters = parameters;
}

public GitHubEnvironmentVariablesAction(ParameterValue... parameters) {
this(Arrays.asList(parameters));
}

@Override
public List<ParameterValue> getParameters() {
return Collections.unmodifiableList(parameters);
}

@Override
public ParameterValue getParameter(String name) {
for (ParameterValue parameter : parameters) {
if (parameter != null && parameter.getName().equals(name)) {
return parameter;
}
}

return null;
}

@Extension
public static final class GitHubAdditionalParameterEnvironmentContributor extends EnvironmentContributor {

@Override
@SuppressWarnings("rawtypes")
public void buildEnvironmentFor(@Nonnull Run run,
@Nonnull EnvVars envs,
@Nonnull TaskListener listener) throws IOException, InterruptedException {

GitHubEnvironmentVariablesAction action = run.getAction(GitHubEnvironmentVariablesAction.class);
if (action != null) {
for (ParameterValue p : action.getParameters()) {
envs.put(p.getName(), String.valueOf(p.getValue()));
}
}
super.buildEnvironmentFor(run, envs, listener);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.jenkinsci.plugins.pipeline.github.trigger;

import hudson.Extension;
import hudson.model.CauseAction;
import hudson.model.Item;
import hudson.model.ParameterValue;
import hudson.model.StringParameterValue;
import jenkins.model.Jenkins;
import jenkins.scm.api.SCMSource;
import jenkins.scm.api.SCMSourceOwner;
Expand All @@ -20,6 +23,7 @@
import javax.annotation.Nullable;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -119,6 +123,11 @@ private void handleIssueComment(final GHSubscriberEvent event) {
return;
}

// create values for the action if a new job is triggered afterward
ArrayList<ParameterValue> values = new ArrayList<ParameterValue>();
values.add(new StringParameterValue("GITHUB_COMMENT", String.valueOf(issueCommentEvent.getComment().getBody())));
values.add(new StringParameterValue("GITHUB_COMMENT_AUTHOR", String.valueOf(issueCommentEvent.getComment().getUserName())));

// lookup jobs
for (final WorkflowJob job : triggerDescriptor.getJobs(key)) {
// find triggers
Expand All @@ -136,11 +145,14 @@ private void handleIssueComment(final GHSubscriberEvent event) {
boolean authorized = isAuthorized(job, commentAuthor);

if (authorized) {
job.scheduleBuild(
new IssueCommentCause(
issueCommentEvent.getComment().getUserName(),
issueCommentEvent.getComment().getBody(),
matchingTrigger.getCommentPattern()));
job.scheduleBuild2(
Jenkins.getInstance().getQuietPeriod(),
new CauseAction(new IssueCommentCause(
issueCommentEvent.getComment().getUserName(),
issueCommentEvent.getComment().getBody(),
matchingTrigger.getCommentPattern())),
new GitHubEnvironmentVariablesAction(values));

LOG.info("Job: {} triggered by IssueComment: {}",
job.getFullName(), issueCommentEvent.getComment());
} else {
Expand Down

0 comments on commit aeb1116

Please sign in to comment.