Skip to content

Commit

Permalink
Handle merge request labels in Note Webhooks (comments) (#1717)
Browse files Browse the repository at this point in the history
* add labels to merge request attributes, pass them to the note web hook CauseData

* add labels to note hook test
  • Loading branch information
yiftahw authored Oct 26, 2024
1 parent f559331 commit 0d841dc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dabsquared.gitlabjenkins.gitlab.hook.model;

import java.util.Date;
import java.util.List;
import net.karneim.pojobuilder.GeneratePojoBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class MergeRequestObjectAttributes {
private String url;
private Action action;
private Boolean workInProgress;
private List<MergeRequestLabel> labels;

public Integer getId() {
return id;
Expand Down Expand Up @@ -211,6 +213,14 @@ public void setWorkInProgress(Boolean workInProgress) {
this.workInProgress = workInProgress;
}

public List<MergeRequestLabel> getLabels() {
return labels;
}

public void setLabels(List<MergeRequestLabel> labels) {
this.labels = labels;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down Expand Up @@ -243,6 +253,7 @@ public boolean equals(Object o) {
.append(action, that.action)
.append(oldrev, that.oldrev)
.append(workInProgress, that.workInProgress)
.append(labels, that.labels)
.isEquals();
}

Expand Down Expand Up @@ -270,6 +281,7 @@ public int hashCode() {
.append(url)
.append(action)
.append(workInProgress)
.append(labels)
.toHashCode();
}

Expand Down Expand Up @@ -298,6 +310,7 @@ public String toString() {
.append("action", action)
.append("oldrev", oldrev)
.append("workInProgress", workInProgress)
.append("labels", labels)

Check warning on line 313 in src/main/java/com/dabsquared/gitlabjenkins/gitlab/hook/model/MergeRequestObjectAttributes.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 256-313 are not covered by tests
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import static com.dabsquared.gitlabjenkins.cause.CauseDataBuilder.causeData;
import static com.dabsquared.gitlabjenkins.trigger.handler.builder.generated.BuildStatusUpdateBuilder.buildStatusUpdate;
import static java.util.stream.Collectors.toList;

import com.dabsquared.gitlabjenkins.cause.CauseData;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.MergeRequestLabel;
import com.dabsquared.gitlabjenkins.gitlab.hook.model.NoteHook;
import com.dabsquared.gitlabjenkins.trigger.exception.NoRevisionToBuildException;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilter;
Expand Down Expand Up @@ -97,6 +99,12 @@ protected CauseData retrieveCauseData(NoteHook hook) {
.withTriggerPhrase(hook.getObjectAttributes().getNote())
.withCommentAuthor(
hook.getUser() == null ? null : hook.getUser().getUsername())
.withMergeRequestLabels(
hook.getMergeRequest().getLabels() == null

Check warning on line 103 in src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 103 is only partially covered, one branch is missing
? null

Check warning on line 104 in src/main/java/com/dabsquared/gitlabjenkins/trigger/handler/note/NoteHookTriggerHandlerImpl.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 104 is not covered by tests
: hook.getMergeRequest().getLabels().stream()
.map(MergeRequestLabel::getTitle)
.collect(toList()))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dabsquared.gitlabjenkins.trigger.handler.note;

import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.CommitBuilder.commit;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.MergeRequestLabelBuilder.mergeRequestLabel;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.MergeRequestObjectAttributesBuilder.mergeRequestObjectAttributes;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.NoteHookBuilder.noteHook;
import static com.dabsquared.gitlabjenkins.gitlab.hook.model.builder.generated.NoteObjectAttributesBuilder.noteObjectAttributes;
Expand All @@ -10,17 +11,20 @@
import static com.dabsquared.gitlabjenkins.trigger.filter.MergeRequestLabelFilterFactory.newMergeRequestLabelFilter;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;

import com.dabsquared.gitlabjenkins.gitlab.hook.model.State;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterFactory;
import com.dabsquared.gitlabjenkins.trigger.filter.BranchFilterType;
import hudson.EnvVars;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.FreeStyleProject;
import hudson.plugins.git.GitSCM;
import hudson.util.OneShotEvent;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.concurrent.ExecutionException;
import org.eclipse.jgit.api.Git;
Expand Down Expand Up @@ -62,6 +66,8 @@ public void note_ciSkip() throws IOException, InterruptedException {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
EnvVars env = build.getEnvironment(listener);
assertEquals(null, env.get("gitlabMergeRequestLabels"));
buildTriggered.signal();
return true;
}
Expand Down Expand Up @@ -109,6 +115,8 @@ public void note_build() throws IOException, InterruptedException, GitAPIExcepti
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
EnvVars env = build.getEnvironment(listener);
assertEquals("bugfix,help needed", env.get("gitlabMergeRequestLabels"));
buildTriggered.signal();
return true;
}
Expand Down Expand Up @@ -137,6 +145,31 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
.withSourceProjectId(1)
.withSourceBranch("feature")
.withTargetBranch("master")
.withLabels(Arrays.asList(
mergeRequestLabel()
.withId(3)
.withTitle("bugfix")
.withColor("#009966")
.withProjectId(1)
.withCreatedAt(currentDate)
.withUpdatedAt(currentDate)
.withTemplate(false)
.withDescription(null)
.withType("ProjectLabel")
.withGroupId(null)
.build(),
mergeRequestLabel()
.withId(4)
.withTitle("help needed")
.withColor("#FF0000")
.withProjectId(1)
.withCreatedAt(currentDate)
.withUpdatedAt(currentDate)
.withTemplate(false)
.withDescription(null)
.withType("ProjectLabel")
.withGroupId(null)
.build()))
.withLastCommit(commit().withAuthor(
user().withName("test").build())
.withId(commit.getName())
Expand Down

0 comments on commit 0d841dc

Please sign in to comment.