Skip to content

Commit

Permalink
JENKINS-73871 Fix tag and branch names not building with slash in it (#…
Browse files Browse the repository at this point in the history
…447)

* JENKINS-73871 Fix tag and branch names not building with slash in it
  • Loading branch information
atl-dyon authored Oct 15, 2024
1 parent 909d723 commit 65b98c1
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 23 deletions.
3 changes: 3 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ Integration tests are run under the `it` profile with the Failsafe plugin using

## Changelog

### 4.1.1
- [JEKINS-73871](https://issues.jenkins.io/browse/JENKINS-73871): Fix branch and tag names with slashes, not being built.

### 4.1.0
- [JENKINS-72120](https://issues.jenkins.io/browse/JENKINS-72120) Implemented discovery of tags. This introduces a tag discovery trait enabling Multibranch pipelines to
detect tags. The trait will not initialise builds.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import com.atlassian.bitbucket.jenkins.internal.model.BitbucketCommit;
import okhttp3.HttpUrl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class BitbucketCommitClientImpl implements BitbucketCommitClient {

private final BitbucketRequestExecutor bitbucketRequestExecutor;
Expand All @@ -19,14 +22,19 @@ public BitbucketCommitClientImpl(BitbucketRequestExecutor bitbucketRequestExecut

@Override
public BitbucketCommit getCommit(String identifier) {
HttpUrl url = bitbucketRequestExecutor.getCoreRestPath().newBuilder()
.addPathSegment("projects")
.addPathSegment(projectKey)
.addPathSegment("repos")
.addPathSegment(repositorySlug)
.addPathSegment("commits")
.addPathSegment(identifier)
.build();
HttpUrl url = null;
try {
url = bitbucketRequestExecutor.getCoreRestPath().newBuilder()
.addPathSegment("projects")
.addPathSegment(projectKey)
.addPathSegment("repos")
.addPathSegment(repositorySlug)
.addPathSegment("commits")
.addPathSegment(URLEncoder.encode(identifier, "UTF-8"))
.build();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}

return bitbucketRequestExecutor.makeGetRequest(url, BitbucketCommit.class).getBody();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import hudson.model.TaskListener;
import okhttp3.HttpUrl;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -60,13 +62,18 @@ public Stream<BitbucketTag> getRemoteTags() {
}

public BitbucketTag getRemoteTag(String tagName) {
HttpUrl.Builder urlBuilder = bitbucketRequestExecutor.getCoreRestPath().newBuilder()
.addPathSegment("projects")
.addPathSegment(projectKey)
.addPathSegment("repos")
.addPathSegment(repositorySlug)
.addPathSegment("tags")
.addPathSegment(tagName);
HttpUrl.Builder urlBuilder = null;
try {
urlBuilder = bitbucketRequestExecutor.getCoreRestPath().newBuilder()
.addPathSegment("projects")
.addPathSegment(projectKey)
.addPathSegment("repos")
.addPathSegment(repositorySlug)
.addPathSegment("tags")
.addPathSegment(URLEncoder.encode(tagName, "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}

HttpUrl url = urlBuilder.build();
BitbucketTag tag = bitbucketRequestExecutor.makeGetRequest(url, BitbucketTag.class).getBody();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.UnsupportedEncodingException;

import static com.atlassian.bitbucket.jenkins.internal.credentials.BitbucketCredentials.ANONYMOUS_CREDENTIALS;
import static com.atlassian.bitbucket.jenkins.internal.util.TestUtils.*;
import static java.lang.String.format;
Expand All @@ -35,17 +37,18 @@ public void setup() {
}

@Test
public void testGetCommit() {
String commitId = "559aa7ba386";
public void testGetCommit() throws UnsupportedEncodingException {
String commitId = "feature/myfeature";
String encodedCommitId = "feature%252Fmyfeature"; // for some reason apache double encodes the url
String response = readFileToString("/commit.json");
String url = format(COMMITS_URL, BITBUCKET_BASE_URL, PROJECT_KEY, REPO_SLUG, commitId);
String url = format(COMMITS_URL, BITBUCKET_BASE_URL, PROJECT_KEY, REPO_SLUG, encodedCommitId);
fakeRemoteHttpServer.mapUrlToResult(url, response);

BitbucketCommitClient commitClient = client.getCommitClient();
BitbucketCommit commit = commitClient.getCommit(commitId);

assertEquals("559aa7ba386219254f9448ed24cdaa6e914e5fde", commit.getId());
assertEquals("559aa7ba386", commit.getDisplayId());
assertEquals("feature/myfeature", commit.getDisplayId());
assertEquals("Commit message", commit.getMessage());
assertEquals(1421908805000L, commit.getCommitterTimestamp());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public void testGetRemoteTags() {
BitbucketTagClient tagClient = client.getBitbucketTagClient(taskListener);
List<BitbucketTag> tagList = tagClient.getRemoteTags().collect(Collectors.toList());

assertEquals(tagList.size(), 1);
assertEquals(tagList.get(0).getDisplayId(), "tag_1");
assertEquals(1, tagList.size());
assertEquals("release/tag_1", tagList.get(0).getDisplayId());
}

@Test(expected = IllegalArgumentException.class)
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/commit.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "559aa7ba386219254f9448ed24cdaa6e914e5fde",
"displayId": "559aa7ba386",
"displayId": "feature/myfeature",
"committerTimestamp": "1421908805000",
"message": "Commit message"
}
2 changes: 1 addition & 1 deletion src/test/resources/tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"values": [
{
"id":"refs/tags/tag_1",
"displayId":"tag_1",
"displayId":"release/tag_1",
"latestCommit":"a69daea0ed930057bac6d5fc2f7ca21acad66491"
}
]
Expand Down

0 comments on commit 65b98c1

Please sign in to comment.