Skip to content

Commit

Permalink
Fix for issue #43
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronjwhiteside committed Feb 22, 2019
1 parent d00623e commit 7bf9568
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
Expand Up @@ -22,8 +22,9 @@
import java.io.StringReader;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* Listens for GitHub events.
Expand Down Expand Up @@ -95,36 +96,33 @@ private void handleIssueComment(final GHSubscriberEvent event) {
}

// create key for this comment's PR
String key = String.format("%s/%s/%d",
final String key = String.format("%s/%s/%d",
issueCommentEvent.getRepository().getOwnerName(),
issueCommentEvent.getRepository().getName(),
issueCommentEvent.getIssue().getNumber());

// lookup trigger
IssueCommentTrigger.DescriptorImpl triggerDescriptor =
(IssueCommentTrigger.DescriptorImpl) Jenkins.getInstance()
final IssueCommentTrigger.DescriptorImpl triggerDescriptor = (IssueCommentTrigger.DescriptorImpl) Jenkins.get()
.getDescriptor(IssueCommentTrigger.class);

if (triggerDescriptor == null) {
LOG.error("Unable to find the IssueComment Trigger, this shouldn't happen.");
return;
}

// lookup job
WorkflowJob job = triggerDescriptor.getJob(key);

if (job == null) {
LOG.debug("No job found matching key: {}", key);
} else {
Optional<IssueCommentTrigger> matchingTrigger = job.getTriggersJobProperty()
// lookup jobs
for (final WorkflowJob job : triggerDescriptor.getJobs(key)) {
// find triggers
final List<IssueCommentTrigger> matchingTriggers = job.getTriggersJobProperty()
.getTriggers()
.stream()
.filter(t -> t instanceof IssueCommentTrigger)
.filter(IssueCommentTrigger.class::isInstance)
.map(IssueCommentTrigger.class::cast)
.filter(t -> triggerMatches(t, issueCommentEvent.getComment(), job))
.findAny();
.collect(Collectors.toList());

if (matchingTrigger.isPresent()) {
// check if they have authorization
for (final IssueCommentTrigger matchingTrigger : matchingTriggers) {
String commentAuthor = issueCommentEvent.getComment().getUserName();
boolean authorized = isAuthorized(job, commentAuthor);

Expand All @@ -133,19 +131,16 @@ private void handleIssueComment(final GHSubscriberEvent event) {
new IssueCommentCause(
issueCommentEvent.getComment().getUserName(),
issueCommentEvent.getComment().getBody(),
matchingTrigger.get().getCommentPattern()));
matchingTrigger.getCommentPattern()));
LOG.info("Job: {} triggered by IssueComment: {}",
job.getFullName(), issueCommentEvent.getComment());
} else {
LOG.warn("Job: {}, IssueComment: {}, Comment Author: {} is not a collaborator, " +
"and is therefore not authorized to trigger a build.",
"and is therefore not authorized to trigger a build.",
job.getFullName(),
issueCommentEvent.getComment(),
commentAuthor);
}
} else {
LOG.debug("Job: {}, IssueComment: {}, No matching triggers could be found for this comment.",
job.getFullName(), issueCommentEvent.getComment());
}
}
}
Expand Down Expand Up @@ -175,7 +170,7 @@ private boolean triggerMatches(final IssueCommentTrigger trigger,

@Override
protected Set<GHEvent> events() {
Set<GHEvent> events = new HashSet<>();
final Set<GHEvent> events = new HashSet<>();
// events.add(GHEvent.PULL_REQUEST_REVIEW_COMMENT);
// events.add(GHEvent.COMMIT_COMMENT);
events.add(GHEvent.ISSUE_COMMENT);
Expand Down
Expand Up @@ -16,7 +16,10 @@
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;

Expand All @@ -43,21 +46,24 @@ public void start(final WorkflowJob project, final boolean newInstance) {
super.start(project, newInstance);
// we only care about pull requests
if (SCMHead.HeadByItem.findHead(project) instanceof PullRequestSCMHead) {
DescriptorImpl.jobs.put(getKey(project), project);
DescriptorImpl.jobs
.computeIfAbsent(getKey(project), key -> new HashSet<>())
.add(project);
}
}

@Override
public void stop() {
if (SCMHead.HeadByItem.findHead(job) instanceof PullRequestSCMHead) {
DescriptorImpl.jobs.put(getKey(job), job);
DescriptorImpl.jobs.getOrDefault(getKey(job), Collections.emptySet())
.remove(job);
}
}

@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
private String getKey(final WorkflowJob project) {
GitHubSCMSource scmSource = (GitHubSCMSource) SCMSource.SourceByItem.findSource(project);
PullRequestSCMHead scmHead = (PullRequestSCMHead) SCMHead.HeadByItem.findHead(project);
final GitHubSCMSource scmSource = (GitHubSCMSource) SCMSource.SourceByItem.findSource(project);
final PullRequestSCMHead scmHead = (PullRequestSCMHead) SCMHead.HeadByItem.findHead(project);

return String.format("%s/%s/%d",
scmSource.getRepoOwner(),
Expand All @@ -78,15 +84,15 @@ boolean matchesComment(final String comment) {
@Symbol("issueCommentTrigger")
@Extension
public static class DescriptorImpl extends TriggerDescriptor {
private transient static final Map<String, WorkflowJob> jobs = new ConcurrentHashMap<>();
private transient static final Map<String, Set<WorkflowJob>> jobs = new ConcurrentHashMap<>();

@Override
public boolean isApplicable(final Item item) {
return false; // this is not configurable from the ui.
}

public WorkflowJob getJob(final String key) {
return jobs.get(key);
public Set<WorkflowJob> getJobs(final String key) {
return jobs.getOrDefault(key, Collections.emptySet());
}
}

Expand Down

0 comments on commit 7bf9568

Please sign in to comment.