Skip to content

Commit

Permalink
Fix casting to GitLabMergeRequestTrigger and add more debug logs in J…
Browse files Browse the repository at this point in the history
…enkins for plugin. Add option to alwaysIgnoreMRWorkInProgress (#266)
  • Loading branch information
mifitous authored Feb 21, 2023
1 parent aa66886 commit 62ab6fc
Show file tree
Hide file tree
Showing 17 changed files with 643 additions and 524 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public abstract class AbstractGitLabSCMHeadEvent<E> extends SCMHeadEvent<E> {

private static final Logger LOGGER = Logger.getLogger(AbstractGitLabSCMHeadEvent.class.getName());
public static final Logger LOGGER = Logger.getLogger(AbstractGitLabSCMHeadEvent.class.getName());

private static final Pattern NONE_HASH_PATTERN = Pattern.compile("^0+$");

Expand All @@ -30,7 +30,8 @@ static <E extends AbstractPushEvent> Type typeOf(E pushEvent) {
} else if (hasBefore) {
result = Type.REMOVED;
} else {
LOGGER.warning("Received push event with both \"before\" and \"after\" set to non-existing revision. Assuming removal.");
LOGGER.warning(
"Received push event with both \"before\" and \"after\" set to non-existing revision. Assuming removal.");
result = Type.REMOVED;
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@

public class GitLabMergeRequestCommentTrigger extends AbstractGitLabJobTrigger<NoteEvent> {

public static final Logger LOGGER = Logger
.getLogger(GitLabMergeRequestCommentTrigger.class.getName());
public static final Logger LOGGER = Logger.getLogger(GitLabMergeRequestCommentTrigger.class.getName());

public GitLabMergeRequestCommentTrigger(NoteEvent payload) {
super(payload);
Expand All @@ -28,11 +27,11 @@ public GitLabMergeRequestCommentTrigger(NoteEvent payload) {
@Override
public void isMatch() {
if (getPayload().getObjectAttributes().getNoteableType()
.equals(NoteEvent.NoteableType.MERGE_REQUEST)) {
.equals(NoteEvent.NoteableType.MERGE_REQUEST)) {
Long mergeRequestId = getPayload().getMergeRequest().getIid();
final Pattern mergeRequestJobNamePattern = Pattern
.compile("^MR-" + mergeRequestId + "\\b.*$",
Pattern.CASE_INSENSITIVE);
.compile("^MR-" + mergeRequestId + "\\b.*$",
Pattern.CASE_INSENSITIVE);
final String commentBody = getPayload().getObjectAttributes().getNote();
final String commentUrl = getPayload().getObjectAttributes().getUrl();
try (ACLContext ctx = ACL.as(ACL.SYSTEM)) {
Expand All @@ -49,35 +48,33 @@ public void isMatch() {
}
GitLabSCMSource gitLabSCMSource = (GitLabSCMSource) source;
final GitLabSCMSourceContext sourceContext = new GitLabSCMSourceContext(
null, SCMHeadObserver.none())
.withTraits(gitLabSCMSource.getTraits());
null, SCMHeadObserver.none())
.withTraits(gitLabSCMSource.getTraits());
if (!sourceContext.mrCommentTriggerEnabled()) {
continue;
}
if (gitLabSCMSource.getProjectId() == getPayload().getMergeRequest()
.getTargetProjectId() && isTrustedMember(gitLabSCMSource, sourceContext.getOnlyTrustedMembersCanTrigger())) {
if (gitLabSCMSource.getProjectId() == getPayload().getMergeRequest().getTargetProjectId()
&& isTrustedMember(gitLabSCMSource, sourceContext.getOnlyTrustedMembersCanTrigger())) {
for (Job<?, ?> job : owner.getAllJobs()) {
if (mergeRequestJobNamePattern.matcher(job.getName()).matches()) {
String expectedCommentBody = sourceContext.getCommentBody();
Pattern pattern = Pattern.compile(expectedCommentBody,
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
if (commentBody == null || pattern.matcher(commentBody)
.matches()) {
.matches()) {
ParameterizedJobMixIn.scheduleBuild2(job, 0,
new CauseAction(
new GitLabMergeRequestCommentCause(commentUrl, getPayload())));
new CauseAction(
new GitLabMergeRequestCommentCause(commentUrl, getPayload())));
LOGGER.log(Level.INFO,
"Triggered build for {0} due to MR comment on {1}",
new Object[]{
job.getFullName(),
getPayload().getProject().getPathWithNamespace()
}
);
"Triggered build for {0} due to MR comment on {1}",
new Object[] {
job.getFullName(),
getPayload().getProject().getPathWithNamespace()
});
} else {
LOGGER.log(Level.INFO,
"MR comment does not match the trigger build string ({0}) for {1}",
new Object[]{expectedCommentBody, job.getFullName()}
);
"MR comment does not match the trigger build string ({0}) for {1}",
new Object[] { expectedCommentBody, job.getFullName() });
}
break;
}
Expand All @@ -88,22 +85,21 @@ public void isMatch() {
}
if (!jobFound) {
LOGGER.log(Level.INFO, "MR comment on {0} did not match any job",
new Object[]{
getPayload().getProject().getPathWithNamespace()
}
);
new Object[] {
getPayload().getProject().getPathWithNamespace()
});
}
}
}
}

private boolean isTrustedMember(GitLabSCMSource gitLabSCMSource, boolean check) {
// Return true if only trusted members can trigger option is not checked
if(!check) {
if (!check) {
return true;
}
AccessLevel permission = gitLabSCMSource.getMembers()
.get(getPayload().getUser().getUsername());
.get(getPayload().getUser().getUsername());
if (permission != null) {
switch (permission) {
case MAINTAINER:
Expand Down
Loading

0 comments on commit 62ab6fc

Please sign in to comment.