Skip to content

Fix: link to PR or git repository are not encoded #462

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
Aug 1, 2023
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
@@ -0,0 +1,40 @@
package org.jenkinsci.plugins.tuleap_git_branch_source;

import hudson.model.Action;
import hudson.model.Actionable;
import io.jenkins.plugins.tuleap_api.deprecated_client.api.TuleapGitRepository;
import jenkins.scm.api.SCMHead;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public final class TuleapRepositoryActionBuilder {
public static List<Action> buildTuleapRepositoryActions(
Actionable ownerAction,
SCMHead head,
TuleapGitRepository repository,
String projectId,
String gitBaseUri
) {
List<Action> result = new ArrayList<>();
TuleapLink repoLink = ownerAction.getAction(TuleapLink.class);
if (repoLink != null) {
if (head instanceof TuleapBranchSCMHead) {
String url = repoLink.getUrl() + "?a=shortlog&h=" + URLEncoder.encode(head.getName(), StandardCharsets.UTF_8);
result.add(new TuleapLink("icon-git-branch", url));
} else if (head instanceof TuleapPullRequestSCMHead) {
TuleapPullRequestSCMHead tuleapPullRequestSCMHead = (TuleapPullRequestSCMHead) head;
String encodedRepositoryId = URLEncoder.encode(Integer.toString(repository.getId()), StandardCharsets.UTF_8);
String encodedPullRequestId = URLEncoder.encode(tuleapPullRequestSCMHead.getId(), StandardCharsets.UTF_8);
String encodedProjectId = URLEncoder.encode(projectId, StandardCharsets.UTF_8);
String prUrl = gitBaseUri+
"?action=pull-requests&repo_id=" + encodedRepositoryId +
"&group_id=" + encodedProjectId + "#/pull-requests/" + encodedPullRequestId + "/overview";
result.add(new TuleapLink("icon-git-branch", prUrl));
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import io.jenkins.plugins.tuleap_credentials.TuleapAccessToken;
import io.jenkins.plugins.tuleap_server_configuration.TuleapConfiguration;
import jenkins.plugins.git.AbstractGitSCMSource;
import jenkins.plugins.git.GitSCMBuilder;
import jenkins.plugins.git.traits.RefSpecsSCMSourceTrait;
import jenkins.scm.api.*;
import jenkins.scm.api.trait.SCMSourceRequest;
Expand Down Expand Up @@ -98,24 +97,12 @@ public TuleapSCMSource(TuleapProject project, TuleapGitRepository repository) {
@NonNull
@Override
protected List<Action> retrieveActions(@NonNull SCMHead head, @CheckForNull SCMHeadEvent event,
@NonNull TaskListener listener) throws IOException, InterruptedException {
List<Action> result = new ArrayList<>();
@NonNull TaskListener listener) throws IOException, InterruptedException {
SCMSourceOwner owner = getOwner();
if (owner instanceof Actionable) {
TuleapLink repoLink = ((Actionable) owner).getAction(TuleapLink.class);
if (repoLink != null) {
if(head instanceof TuleapBranchSCMHead) {
String canonicalRepoName = repositoryPath.replace(project.getShortname() + "/", "");
String url = repoLink.getUrl() + "?p=" + canonicalRepoName + "&a=shortlog&h=" + head.getName();
result.add(new TuleapLink("icon-git-branch", url));
} else if (head instanceof TuleapPullRequestSCMHead){
TuleapPullRequestSCMHead tuleapPullRequestSCMHead = (TuleapPullRequestSCMHead) head;
String prUrl = this.getGitBaseUri()+"?action=pull-requests&repo_id="+this.repository.getId()+"&group_id="+this.projectId+"#/pull-requests/"+tuleapPullRequestSCMHead.getId()+"/overview";
result.add(new TuleapLink("icon-git-branch", prUrl));
}
}
return TuleapRepositoryActionBuilder.buildTuleapRepositoryActions((Actionable) owner, head,this.repository, this.projectId, this.getGitBaseUri());
}
return result;
return Collections.emptyList();
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.jenkinsci.plugins.tuleap_git_branch_source;

import hudson.model.Action;
import hudson.model.Actionable;
import io.jenkins.plugins.tuleap_api.deprecated_client.api.TuleapGitRepository;
import jenkins.scm.api.SCMHeadOrigin;
import org.jenkinsci.plugins.tuleap_git_branch_source.stubs.GitPullRequestStub;
import org.jenkinsci.plugins.tuleap_git_branch_source.stubs.TuleapActionableStub;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public final class TuleapRepositoryActionBuilderTest {

@Test
public void testItReturnsTheGitRepositoryLink() {
Actionable owner = TuleapActionableStub.withDefaultTuleapLink();
TuleapBranchSCMHead branch = new TuleapBranchSCMHead("dev");
TuleapGitRepository gitRepository = new TuleapGitRepository();
gitRepository.setName("dev");
String projectId = "101";
String gitBaseUri = "https://tuleap-git-base.example.com";

List<Action> result = TuleapRepositoryActionBuilder.buildTuleapRepositoryActions(
owner,
branch,
gitRepository,
projectId,
gitBaseUri
);

TuleapLink expectedLink = new TuleapLink(
"icon-git-branch",
"https://tuleap-git-link.example.com?a=shortlog&h=dev"
);
assertEquals(expectedLink.getIconClassName(), ((TuleapLink) result.get(0)).getIconClassName());
assertEquals(expectedLink.getUrlName(), result.get(0).getUrlName());
}

@Test
public void testItReturnsTheGitPullRequestLink() {
Actionable owner = TuleapActionableStub.withDefaultTuleapLink();
TuleapBranchSCMHead targetBranch = new TuleapBranchSCMHead("targeto-branchu");
TuleapPullRequestSCMHead pullRequestSCMHead = new TuleapPullRequestSCMHead(GitPullRequestStub.withId("15"), SCMHeadOrigin.DEFAULT, targetBranch, 101, 101, "refs/tlpr/4");

TuleapGitRepository gitRepository = new TuleapGitRepository();
gitRepository.setName("dev");
String projectId = "101";
String gitBaseUri = "https://tuleap-git-base.example.com";

List<Action> result = TuleapRepositoryActionBuilder.buildTuleapRepositoryActions(
owner,
pullRequestSCMHead,
gitRepository,
projectId,
gitBaseUri
);

TuleapLink expectedLink = new TuleapLink(
"icon-git-branch",
"https://tuleap-git-base.example.com?action=pull-requests&repo_id=0&group_id=101#/pull-requests/15/overview"
);
assertEquals(expectedLink.getIconClassName(), ((TuleapLink) result.get(0)).getIconClassName());
assertEquals(expectedLink.getUrlName(), result.get(0).getUrlName());
}

@Test
public void testItReturnsEmptyListIfTheCurrentSCMIsNotFromTuleap() {
Actionable owner = TuleapActionableStub.withNull();

TuleapBranchSCMHead branch = new TuleapBranchSCMHead("dev");
TuleapGitRepository gitRepository = new TuleapGitRepository();
gitRepository.setName("dev");
String projectId = "101";
String gitBaseUri = "https://tuleap-git-base.example.com";

List<Action> result = TuleapRepositoryActionBuilder.buildTuleapRepositoryActions(
owner,
branch,
gitRepository,
projectId,
gitBaseUri
);
assertTrue(result.isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.jenkinsci.plugins.tuleap_git_branch_source.stubs;

import io.jenkins.plugins.tuleap_api.client.GitHead;
import io.jenkins.plugins.tuleap_api.client.GitPullRequest;
import io.jenkins.plugins.tuleap_api.client.GitRepositoryReference;

public class GitPullRequestStub implements GitPullRequest {

private final String id;

private GitPullRequestStub(String id) {
this.id = id;
}

public static GitPullRequestStub withId(String id) {
return new GitPullRequestStub(id);
}


@Override
public String getId() {
return this.id;
}

@Override
public String getTitle() {
return null;
}

@Override
public GitRepositoryReference getSourceRepository() {
return null;
}

@Override
public GitRepositoryReference getDestinationRepository() {
return null;
}

@Override
public String getSourceBranch() {
return null;
}

@Override
public String getDestinationBranch() {
return null;
}

@Override
public GitHead getHead() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jenkinsci.plugins.tuleap_git_branch_source.stubs;

import hudson.model.Actionable;
import org.jenkinsci.plugins.tuleap_git_branch_source.TuleapLink;

public final class TuleapActionableStub extends Actionable {

private final TuleapLink tuleapLink;

private TuleapActionableStub(TuleapLink tuleapLink){
this.tuleapLink = tuleapLink;
}

public static TuleapActionableStub withDefaultTuleapLink(){
return new TuleapActionableStub(new TuleapLink("tuleap-class", "https://tuleap-git-link.example.com"));
}
public static TuleapActionableStub withNull(){
return new TuleapActionableStub(null);
}

@Override
public String getDisplayName() {
return null;
}

@Override
public String getSearchUrl() {
return null;
}

@Override
public TuleapLink getAction(Class type) {
return this.tuleapLink;
}

}