Skip to content

BitbucketTagSCMHead does not receive an annotated tag date #991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public class BitbucketTagSCMHead extends GitTagSCMHead implements TagSCMHead {
private static final long serialVersionUID = 1L;

/**
* Constructor.
* Default constructor.
*
* @param tagName the tag name
* @param timestamp the timestamp of tag
* @param timestamp the timestamp of annotated tag or the commit is referring to
*/
public BitbucketTagSCMHead(@NonNull String tagName, long timestamp) {
super(tagName, timestamp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ public SCMFileSystem build(@NonNull SCMSource source, @NonNull SCMHead head, @Ch
String owner = src.getRepoOwner();
String repository = src.getRepository();
String serverUrl = src.getServerUrl();
StandardCredentials credentials;
credentials = lookupScanCredentials(src.getOwner(), credentialsId, serverUrl);
StandardCredentials credentials = lookupScanCredentials(src.getOwner(), credentialsId, serverUrl);

BitbucketAuthenticator authenticator = AuthenticationTokens.convert(BitbucketAuthenticator.authenticationContext(serverUrl), credentials);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@
import com.cloudbees.jenkins.plugins.bitbucket.BranchSCMHead;
import com.cloudbees.jenkins.plugins.bitbucket.PullRequestSCMHead;
import com.cloudbees.jenkins.plugins.bitbucket.PullRequestSCMRevision;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApiFactory;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketCommit;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketPullRequest;
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketCredentials;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.pullrequest.BitbucketServerPullRequest;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.repository.BitbucketServerRepository;
import com.cloudbees.jenkins.plugins.bitbucket.server.events.NativeServerChange;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.google.common.base.Ascii;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -48,6 +54,7 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.authentication.tokens.api.AuthenticationTokens;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.scm.api.SCMEvent;
import jenkins.scm.api.SCMHead;
Expand Down Expand Up @@ -138,7 +145,19 @@
: new AbstractGitSCMSource.SCMRevisionImpl(head, change.getToHash());
result.put(head, revision);
} else if ("TAG".equals(refType)) {
SCMHead head = new BitbucketTagSCMHead(change.getRef().getDisplayId(), 0);
String tagName = change.getRef().getDisplayId();
// FIXME slow workaround until a real example of server tag payload has been provided

Check warning on line 149 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/ServerPushEvent.java

View check run for this annotation

ci.jenkins.io / Open Tasks Scanner

FIXME

HIGH: slow workaround until a real example of server tag payload has been provided
// I expect in the payload there is also the referred commit.
long tagTimestamp;
try (BitbucketApi client = getClient(src)) {
// BitbucketBranch tag = client.getTag(tagName); // requires two API call and does not return the tag timestamp
BitbucketCommit tag = client.resolveCommit(change.getFromHash());
tagTimestamp = tag != null ? tag.getDateMillis() : 0;

Check warning on line 155 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/ServerPushEvent.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 155 is only partially covered, one branch is missing
} catch (InterruptedException | IOException e) {
LOGGER.log(Level.SEVERE, "Fail to retrive the timestamp for tag event {0}", tagName);
tagTimestamp = 0;

Check warning on line 158 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/ServerPushEvent.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 156-158 are not covered by tests
}
SCMHead head = new BitbucketTagSCMHead(tagName, tagTimestamp);
final SCMRevision revision = getType() == SCMEvent.Type.REMOVED ? null
: new AbstractGitSCMSource.SCMRevisionImpl(head, change.getToHash());
result.put(head, revision);
Expand All @@ -149,6 +168,19 @@
}
}

protected BitbucketApi getClient(BitbucketSCMSource src) {
String serverURL = src.getServerUrl();

StandardCredentials credentials = BitbucketCredentials.lookupCredentials(
serverURL,
src.getOwner(),
src.getCredentialsId(),
StandardCredentials.class
);
BitbucketAuthenticator authenticator = AuthenticationTokens.convert(BitbucketAuthenticator.authenticationContext(serverURL), credentials);
return BitbucketApiFactory.newInstance(serverURL, authenticator, src.getRepoOwner(), null, src.getRepository());
}

private void addPullRequests(BitbucketSCMSource src, Map<SCMHead, SCMRevision> result) throws InterruptedException {
if (getType() != SCMEvent.Type.UPDATED) {
return; // adds/deletes won't be handled here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketApi;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.impl.util.BitbucketApiUtils;
import com.cloudbees.jenkins.plugins.bitbucket.server.BitbucketServerWebhookImplementation;
import com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand Down Expand Up @@ -91,7 +92,7 @@ public static class BitbucketServerIntegrationClient extends BitbucketServerAPIC
private final IRequestAudit audit;

private BitbucketServerIntegrationClient(String payloadRootPath, String baseURL, String owner, String repositoryName) {
super(baseURL, owner, repositoryName, mock(BitbucketAuthenticator.class), false);
super(baseURL, owner, repositoryName, mock(BitbucketAuthenticator.class), false, BitbucketServerWebhookImplementation.NATIVE);

if (payloadRootPath == null) {
this.payloadRootPath = PAYLOAD_RESOURCE_ROOTPATH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,19 @@
import java.io.FileNotFoundException;
import jenkins.scm.api.SCMFile;
import jenkins.scm.api.SCMFile.Type;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

@WithJenkins
class BitbucketSCMFileTest {

@SuppressWarnings("unused")
private static JenkinsRule j;

@BeforeAll
static void init(JenkinsRule rule) {
j = rule;
}

@Issue("JENKINS-75157")
@Test
void verify_content_throws_FileNotFoundException_when_file_does_not_exists() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
*/
package com.cloudbees.jenkins.plugins.bitbucket.hooks;

import com.cloudbees.jenkins.plugins.bitbucket.BitbucketMockApiFactory;
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketSCMSource;
import com.cloudbees.jenkins.plugins.bitbucket.BitbucketTagSCMHead;
import com.cloudbees.jenkins.plugins.bitbucket.BranchSCMHead;
import com.cloudbees.jenkins.plugins.bitbucket.client.BitbucketIntegrationClientFactory;
import hudson.scm.SCM;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -39,13 +42,15 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class NativeServerPushHookProcessorTest {

private static final String SERVER_URL = "http://localhost:7990/";
private static final String SERVER_URL = "http://localhost:7990";
private static final String MIRROR_ID = "ABCD-1234-EFGH-5678";
private NativeServerPushHookProcessor sut;
private SCMHeadEvent<?> scmEvent;
Expand Down Expand Up @@ -80,6 +85,27 @@
.isEqualTo(new BranchSCMHead("main"));
}

@WithJenkins
@Test
void test_tag_timestamp(JenkinsRule rule) throws Exception {

Check notice

Code scanning / CodeQL

Useless parameter Note test

The parameter 'rule' is never used.

Copilot Autofix

AI 2 months ago

To fix the problem, we should remove the unused parameter JenkinsRule rule from the test_tag_timestamp method. This will simplify the method signature and eliminate the unnecessary parameter, adhering to best practices.

  • Remove the JenkinsRule rule parameter from the test_tag_timestamp method.
  • Ensure that the method still functions correctly without this parameter.
Suggested changeset 1
src/test/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/NativeServerPushHookProcessorTest.java

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/NativeServerPushHookProcessorTest.java b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/NativeServerPushHookProcessorTest.java
--- a/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/NativeServerPushHookProcessorTest.java
+++ b/src/test/java/com/cloudbees/jenkins/plugins/bitbucket/hooks/NativeServerPushHookProcessorTest.java
@@ -89,3 +89,3 @@
     @Test
-    void test_tag_timestamp(JenkinsRule rule) throws Exception {
+    void test_tag_timestamp() throws Exception {
         sut.process(HookEventType.SERVER_REFS_CHANGED, loadResource("native/tagPayload.json"), BitbucketType.SERVER, "origin", SERVER_URL);
EOF
@@ -89,3 +89,3 @@
@Test
void test_tag_timestamp(JenkinsRule rule) throws Exception {
void test_tag_timestamp() throws Exception {
sut.process(HookEventType.SERVER_REFS_CHANGED, loadResource("native/tagPayload.json"), BitbucketType.SERVER, "origin", SERVER_URL);
Copilot is powered by AI and may make mistakes. Always verify output.
sut.process(HookEventType.SERVER_REFS_CHANGED, loadResource("native/tagPayload.json"), BitbucketType.SERVER, "origin", SERVER_URL);
assertThat(scmEvent)
.isInstanceOf(ServerPushEvent.class)
.isNotNull();

BitbucketSCMSource scmSource = new BitbucketSCMSource("amuniz", "test-repos");
scmSource.setServerUrl(SERVER_URL);

BitbucketMockApiFactory.add(SERVER_URL, BitbucketIntegrationClientFactory.getApiMockClient(SERVER_URL));

Map<SCMHead, SCMRevision> result = scmEvent.heads(scmSource);
assertThat(result.keySet())
.hasSize(1)
.first()
.usingRecursiveComparison()
.isEqualTo(new BitbucketTagSCMHead("v0.0.0", 1537538991000L));
}

@Test
@Issue("JENKINS-55927")
void test_mirror_sync_reflimitexceeed() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"actor": {
"active": true,
"displayName": "Antonio Muniz",
"emailAddress": "[email protected]",
"id": 2,
"links": {
"self": [
{
"href": "http://localhost:7990/users/amuniz"
}
]
},
"name": "amuniz",
"slug": "amuniz",
"type": "NORMAL"
},
"changes": [
{
"fromHash": "fb522a6f08c7c7df337312e4e65ec1b57710672e",
"ref": {
"displayId": "v0.0.0",
"id": "refs/tags/v0.0.0",
"type": "TAG"
},
"refId": "refs/heads/main",
"toHash": "ad64eb8f05364f825e8557a7a672cc576baffb9a",
"type": "ADD"
}
],
"commits": [
{
"author": {
"active": true,
"displayName": "Antonio Muniz",
"emailAddress": "[email protected]",
"id": 2,
"links": {
"self": [
{
"href": "http://localhost:7990/users/amuniz"
}
]
},
"name": "amuniz",
"slug": "amuniz",
"type": "NORMAL"
},
"authorTimestamp": 1537538991000,
"committer": {
"active": true,
"displayName": "Antonio Muniz",
"emailAddress": "[email protected]",
"id": 2,
"links": {
"self": [
{
"href": "http://localhost:7990/users/amuniz"
}
]
},
"name": "amuniz",
"slug": "amuniz",
"type": "NORMAL"
},
"committerTimestamp": 1537538991000,
"displayId": "9fdd7b96d3f",
"id": "9fdd7b96d3f5c276d0b9e0bf38c879eb112d889a",
"message": "source-2: Wed 13 Nov 2024 21:54:36 AEST",
"parents": [
{
"displayId": "ae995d7a370",
"id": "ae995d7a37069d0988462a9c92828971e8a42b5d"
}
]
}
],
"date": "2024-11-27T01:34:45+0000",
"eventKey": "repo:refs_changed",
"repository": {
"archived": false,
"forkable": true,
"hierarchyId": "050ab73089457a2ae375",
"id": 1,
"links": {
"clone": [
{
"href": "ssh://git@localhost:7999/amuniz/test-repos.git",
"name": "ssh"
},
{
"href": "http://localhost:7990/scm/amuniz/test-repos.git",
"name": "http"
}
],
"self": [
{
"href": "http://localhost:7990/projects/AMUNIZ/repos/test-repos/browse"
}
]
},
"name": "test-repos",
"project": {
"id": 1,
"key": "AMUNIZ",
"links": {
"self": [
{
"href": "http://localhost:7990/projects/AMUNIZ"
}
]
},
"name": "prj",
"public": false,
"type": "NORMAL"
},
"public": false,
"scmId": "git",
"slug": "test-repos",
"state": "AVAILABLE",
"statusMessage": "Available"
},
"toCommit": {
"_comment": "NOT real payload!"
}
}