From 10d96058f4769d2d1095186cd2a66b4285e3bb8d Mon Sep 17 00:00:00 2001 From: rsandell Date: Wed, 20 Nov 2024 14:41:47 +0100 Subject: [PATCH 1/4] Fix Autoboxing NPE --- .../gitlabbranchsource/GitLabSCMSource.java | 4 ++-- .../GitLabSCMSourceDeserializationTest.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java index 295bb51e..f3019535 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java @@ -237,12 +237,12 @@ public HashMap getMembers() { return members; } - public long getProjectId() { + public Long getProjectId() { return projectId; } @DataBoundSetter - public void setProjectId(long projectId) { + public void setProjectId(Long projectId) { this.projectId = projectId; } diff --git a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java index 2c965e4f..d2c7df1b 100644 --- a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java @@ -1,5 +1,6 @@ package io.jenkins.plugins.gitlabbranchsource; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import java.lang.reflect.Field; @@ -43,4 +44,25 @@ public void afterRestartingJenkinsTransientFieldsAreNotNull() throws Exception { assertNotNull(mergeRequestContributorCache.get(source)); }); } + + @Test + public void projectIdSurvivesConfigRoundtrip() { + plan.then(j -> { + GitLabSCMSourceBuilder sb = + new GitLabSCMSourceBuilder(SOURCE_ID, "server", "creds", "po", "group/project", "project"); + WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME); + GitLabSCMSource source = sb.build(); + project.getSourcesList().add(new BranchSource(source)); + long p = 42; + source.setProjectId(p); + j.configRoundtrip(project); + + WorkflowMultiBranchProject item = j.jenkins.getItemByFullName(PROJECT_NAME, + WorkflowMultiBranchProject.class); + assertNotNull(item); + GitLabSCMSource scmSource = (GitLabSCMSource) item.getSCMSource(SOURCE_ID); + assertNotNull(scmSource); + assertEquals(Long.valueOf(p), scmSource.getProjectId()); + }); + } } From fb43841be2adda0851637d42d46a391dc3f56b53 Mon Sep 17 00:00:00 2001 From: rsandell Date: Thu, 21 Nov 2024 10:11:55 +0100 Subject: [PATCH 2/4] Spotless --- .../GitLabSCMSourceDeserializationTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java index d2c7df1b..4326b01e 100644 --- a/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java +++ b/src/test/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSourceDeserializationTest.java @@ -49,7 +49,7 @@ public void afterRestartingJenkinsTransientFieldsAreNotNull() throws Exception { public void projectIdSurvivesConfigRoundtrip() { plan.then(j -> { GitLabSCMSourceBuilder sb = - new GitLabSCMSourceBuilder(SOURCE_ID, "server", "creds", "po", "group/project", "project"); + new GitLabSCMSourceBuilder(SOURCE_ID, "server", "creds", "po", "group/project", "project"); WorkflowMultiBranchProject project = j.createProject(WorkflowMultiBranchProject.class, PROJECT_NAME); GitLabSCMSource source = sb.build(); project.getSourcesList().add(new BranchSource(source)); @@ -57,8 +57,8 @@ public void projectIdSurvivesConfigRoundtrip() { source.setProjectId(p); j.configRoundtrip(project); - WorkflowMultiBranchProject item = j.jenkins.getItemByFullName(PROJECT_NAME, - WorkflowMultiBranchProject.class); + WorkflowMultiBranchProject item = + j.jenkins.getItemByFullName(PROJECT_NAME, WorkflowMultiBranchProject.class); assertNotNull(item); GitLabSCMSource scmSource = (GitLabSCMSource) item.getSCMSource(SOURCE_ID); assertNotNull(scmSource); From 338a33c250bb23636116c88f74729ed5f38b58dc Mon Sep 17 00:00:00 2001 From: rsandell Date: Thu, 21 Nov 2024 10:27:24 +0100 Subject: [PATCH 3/4] Spotbugs --- .../gitlabbranchsource/GitLabMergeRequestCommentTrigger.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestCommentTrigger.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestCommentTrigger.java index eedd8783..e3036cd7 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestCommentTrigger.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestCommentTrigger.java @@ -14,6 +14,7 @@ import jenkins.scm.api.SCMSource; import jenkins.scm.api.SCMSourceOwner; import jenkins.scm.api.SCMSourceOwners; +import org.apache.commons.lang.math.NumberUtils; import org.gitlab4j.api.models.AccessLevel; import org.gitlab4j.api.webhook.NoteEvent; @@ -52,8 +53,8 @@ public void isMatch() { if (!sourceContext.mrCommentTriggerEnabled()) { continue; } - if (gitLabSCMSource.getProjectId() - == getPayload().getMergeRequest().getTargetProjectId() + Long projectId = gitLabSCMSource.getProjectId(); + if (projectId != null && projectId.equals(getPayload().getMergeRequest().getTargetProjectId()) && isTrustedMember(gitLabSCMSource, sourceContext.getOnlyTrustedMembersCanTrigger())) { for (Job job : owner.getAllJobs()) { if (MultiBranchProject.class.isAssignableFrom( From 2566e28efacf70c6075b3ea191c88551f4ea1338 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Thu, 21 Nov 2024 07:28:01 -0800 Subject: [PATCH 4/4] `mvn spotless:apply` --- .../gitlabbranchsource/GitLabMergeRequestCommentTrigger.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestCommentTrigger.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestCommentTrigger.java index e3036cd7..46a02855 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestCommentTrigger.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabMergeRequestCommentTrigger.java @@ -14,7 +14,6 @@ import jenkins.scm.api.SCMSource; import jenkins.scm.api.SCMSourceOwner; import jenkins.scm.api.SCMSourceOwners; -import org.apache.commons.lang.math.NumberUtils; import org.gitlab4j.api.models.AccessLevel; import org.gitlab4j.api.webhook.NoteEvent; @@ -54,7 +53,9 @@ public void isMatch() { continue; } Long projectId = gitLabSCMSource.getProjectId(); - if (projectId != null && projectId.equals(getPayload().getMergeRequest().getTargetProjectId()) + if (projectId != null + && projectId.equals( + getPayload().getMergeRequest().getTargetProjectId()) && isTrustedMember(gitLabSCMSource, sourceContext.getOnlyTrustedMembersCanTrigger())) { for (Job job : owner.getAllJobs()) { if (MultiBranchProject.class.isAssignableFrom(