Skip to content

Commit 7bf9568

Browse files
Fix for issue jenkinsci#43
1 parent d00623e commit 7bf9568

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

src/main/java/org/jenkinsci/plugins/pipeline/github/trigger/GitHubEventSubscriber.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@
2222
import java.io.StringReader;
2323
import java.util.Collections;
2424
import java.util.HashSet;
25-
import java.util.Optional;
25+
import java.util.List;
2626
import java.util.Set;
27+
import java.util.stream.Collectors;
2728

2829
/**
2930
* Listens for GitHub events.
@@ -95,36 +96,33 @@ private void handleIssueComment(final GHSubscriberEvent event) {
9596
}
9697

9798
// create key for this comment's PR
98-
String key = String.format("%s/%s/%d",
99+
final String key = String.format("%s/%s/%d",
99100
issueCommentEvent.getRepository().getOwnerName(),
100101
issueCommentEvent.getRepository().getName(),
101102
issueCommentEvent.getIssue().getNumber());
102103

103104
// lookup trigger
104-
IssueCommentTrigger.DescriptorImpl triggerDescriptor =
105-
(IssueCommentTrigger.DescriptorImpl) Jenkins.getInstance()
105+
final IssueCommentTrigger.DescriptorImpl triggerDescriptor = (IssueCommentTrigger.DescriptorImpl) Jenkins.get()
106106
.getDescriptor(IssueCommentTrigger.class);
107107

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

113-
// lookup job
114-
WorkflowJob job = triggerDescriptor.getJob(key);
115-
116-
if (job == null) {
117-
LOG.debug("No job found matching key: {}", key);
118-
} else {
119-
Optional<IssueCommentTrigger> matchingTrigger = job.getTriggersJobProperty()
113+
// lookup jobs
114+
for (final WorkflowJob job : triggerDescriptor.getJobs(key)) {
115+
// find triggers
116+
final List<IssueCommentTrigger> matchingTriggers = job.getTriggersJobProperty()
120117
.getTriggers()
121118
.stream()
122-
.filter(t -> t instanceof IssueCommentTrigger)
119+
.filter(IssueCommentTrigger.class::isInstance)
123120
.map(IssueCommentTrigger.class::cast)
124121
.filter(t -> triggerMatches(t, issueCommentEvent.getComment(), job))
125-
.findAny();
122+
.collect(Collectors.toList());
126123

127-
if (matchingTrigger.isPresent()) {
124+
// check if they have authorization
125+
for (final IssueCommentTrigger matchingTrigger : matchingTriggers) {
128126
String commentAuthor = issueCommentEvent.getComment().getUserName();
129127
boolean authorized = isAuthorized(job, commentAuthor);
130128

@@ -133,19 +131,16 @@ private void handleIssueComment(final GHSubscriberEvent event) {
133131
new IssueCommentCause(
134132
issueCommentEvent.getComment().getUserName(),
135133
issueCommentEvent.getComment().getBody(),
136-
matchingTrigger.get().getCommentPattern()));
134+
matchingTrigger.getCommentPattern()));
137135
LOG.info("Job: {} triggered by IssueComment: {}",
138136
job.getFullName(), issueCommentEvent.getComment());
139137
} else {
140138
LOG.warn("Job: {}, IssueComment: {}, Comment Author: {} is not a collaborator, " +
141-
"and is therefore not authorized to trigger a build.",
139+
"and is therefore not authorized to trigger a build.",
142140
job.getFullName(),
143141
issueCommentEvent.getComment(),
144142
commentAuthor);
145143
}
146-
} else {
147-
LOG.debug("Job: {}, IssueComment: {}, No matching triggers could be found for this comment.",
148-
job.getFullName(), issueCommentEvent.getComment());
149144
}
150145
}
151146
}
@@ -175,7 +170,7 @@ private boolean triggerMatches(final IssueCommentTrigger trigger,
175170

176171
@Override
177172
protected Set<GHEvent> events() {
178-
Set<GHEvent> events = new HashSet<>();
173+
final Set<GHEvent> events = new HashSet<>();
179174
// events.add(GHEvent.PULL_REQUEST_REVIEW_COMMENT);
180175
// events.add(GHEvent.COMMIT_COMMENT);
181176
events.add(GHEvent.ISSUE_COMMENT);

src/main/java/org/jenkinsci/plugins/pipeline/github/trigger/IssueCommentTrigger.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
import org.slf4j.LoggerFactory;
1717

1818
import javax.annotation.Nonnull;
19+
import java.util.Collections;
20+
import java.util.HashSet;
1921
import java.util.Map;
22+
import java.util.Set;
2023
import java.util.concurrent.ConcurrentHashMap;
2124
import java.util.regex.Pattern;
2225

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

5055
@Override
5156
public void stop() {
5257
if (SCMHead.HeadByItem.findHead(job) instanceof PullRequestSCMHead) {
53-
DescriptorImpl.jobs.put(getKey(job), job);
58+
DescriptorImpl.jobs.getOrDefault(getKey(job), Collections.emptySet())
59+
.remove(job);
5460
}
5561
}
5662

5763
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE")
5864
private String getKey(final WorkflowJob project) {
59-
GitHubSCMSource scmSource = (GitHubSCMSource) SCMSource.SourceByItem.findSource(project);
60-
PullRequestSCMHead scmHead = (PullRequestSCMHead) SCMHead.HeadByItem.findHead(project);
65+
final GitHubSCMSource scmSource = (GitHubSCMSource) SCMSource.SourceByItem.findSource(project);
66+
final PullRequestSCMHead scmHead = (PullRequestSCMHead) SCMHead.HeadByItem.findHead(project);
6167

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

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

88-
public WorkflowJob getJob(final String key) {
89-
return jobs.get(key);
94+
public Set<WorkflowJob> getJobs(final String key) {
95+
return jobs.getOrDefault(key, Collections.emptySet());
9096
}
9197
}
9298

0 commit comments

Comments
 (0)