Skip to content

Fix Wrong PR head reference is being pulled on build #292

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
Oct 1, 2021
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>tuleap-api</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ public class TuleapPullRequestSCMHead extends SCMHead implements ChangeRequestSC
private final TuleapBranchSCMHead target;
private final int originRepositoryId;
private final Integer targetRepositoryId;
private final String headReference;

public TuleapPullRequestSCMHead(GitPullRequest pullRequest, SCMHeadOrigin origin, TuleapBranchSCMHead target, Integer originRepositoryId, Integer targetRepositoryId) {
public TuleapPullRequestSCMHead(GitPullRequest pullRequest, SCMHeadOrigin origin, TuleapBranchSCMHead target, Integer originRepositoryId, Integer targetRepositoryId, String headReference) {
super("TLP-PR-" + pullRequest.getId());
this.pullRequest = pullRequest;
this.origin = origin;
this.target = target;
this.originRepositoryId = originRepositoryId;
this.targetRepositoryId = targetRepositoryId;
this.headReference = headReference;
}

@NotNull
Expand Down Expand Up @@ -62,4 +64,8 @@ public int getOriginRepositoryId() {
public Integer getTargetRepositoryId() {
return this.targetRepositoryId;
}

public String getHeadReference() {
return this.headReference;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public TuleapSCMBuilder(@NotNull SCMHead head, SCMRevision revision, @NotNull St
withoutRefSpecs();
if (head instanceof TuleapPullRequestSCMHead) {
TuleapPullRequestSCMHead tuleapPullRequestSCMHead = (TuleapPullRequestSCMHead) head;
withRefSpec("+refs/tlpr/" + tuleapPullRequestSCMHead.getId() + "/head:refs/remotes/@{remote}/" + tuleapPullRequestSCMHead.getName());
withRefSpec("+" + tuleapPullRequestSCMHead.getHeadReference() + ":refs/remotes/@{remote}/" + tuleapPullRequestSCMHead.getName());
} else {
withRefSpec("+refs/heads/" + head.getName() + ":refs/remotes/@{remote}/" + head.getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ public SCMSourceCriteria.Probe create(@NotNull TuleapBranchSCMHead head, @Nullab
request.listener().getLogger().format("Fetching pull requests for repository at %s %n", this.repositoryPath);
GitApi gitApi = TuleapApiRetriever.getGitApi();

List<GitPullRequest> pullRequests = gitApi.getPullRequests(Integer.toString(this.repository.getId()), this.credentials);
List<GitPullRequest> partialPullRequests = gitApi.getPullRequests(Integer.toString(this.repository.getId()), this.credentials);
int prCount = 0;
for (GitPullRequest pullRequest : pullRequests) {
for (GitPullRequest partialPullRequest : partialPullRequests) {
PullRequest pullRequest = TuleapApiRetriever.getPullRequestApi().getPullRequest(partialPullRequest.getId(), this.credentials);
request.listener().getLogger().format("Check the PR id: '%s' %n", pullRequest.getId());
prCount++;
boolean isFork = !pullRequest.getSourceRepository().getId().equals(pullRequest.getDestinationRepository().getId());
Expand All @@ -193,7 +194,7 @@ else if (!isFork && !request.isRetrieveOriginPullRequests()) {
}
TuleapBranchSCMHead targetBranch = new TuleapBranchSCMHead(pullRequest.getDestinationBranch());

TuleapPullRequestSCMHead tlpPRSCMHead = new TuleapPullRequestSCMHead(pullRequest, origin, targetBranch, originRepositoryId, targetRepositoryId);
TuleapPullRequestSCMHead tlpPRSCMHead = new TuleapPullRequestSCMHead(partialPullRequest, origin, targetBranch, originRepositoryId, targetRepositoryId, pullRequest.getHeadReference());
GitCommit targetLastCommit = gitApi.getCommit(Integer.toString(this.repository.getId()), targetBranch.getName(), this.credentials);
if (request.process(tlpPRSCMHead,
(SCMSourceRequest.RevisionLambda<TuleapPullRequestSCMHead, TuleapPullRequestRevision>) head ->
Expand All @@ -205,7 +206,7 @@ else if (!isFork && !request.isRetrieveOriginPullRequests()) {
),
new TuleapBranchSCMRevision(
new TuleapBranchSCMHead(head.getOriginName()),
pullRequest.getHead().getId()
partialPullRequest.getHead().getId()
)
),
new SCMSourceRequest.ProbeLambda<TuleapPullRequestSCMHead, TuleapPullRequestRevision>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void itReturnsFalseIfTheRevisionIsNotATuleapPRRevision() {
TuleapBranchSCMHead originBranch = new TuleapBranchSCMHead("origino-branchu");
TuleapBranchSCMRevision originBranchRevision = new TuleapBranchSCMRevision(originBranch, "0r1g4n_h4sh");

TuleapPullRequestSCMHead pullRequestSCMHead = new TuleapPullRequestSCMHead(this.getGitPullRequest(), SCMHeadOrigin.DEFAULT, targetBranch, 101, 102);
TuleapPullRequestSCMHead pullRequestSCMHead = new TuleapPullRequestSCMHead(this.getGitPullRequest(), SCMHeadOrigin.DEFAULT, targetBranch, 101, 102, "refs/tlpr/4");

TuleapPullRequestRevision tuleapPullRequestRevision = new TuleapPullRequestRevision(pullRequestSCMHead, targetBranchRevision, originBranchRevision);

Expand All @@ -39,7 +39,7 @@ public void itReturnsTrueIfTheGivenRevisionIsTheWantedRevision() {
TuleapBranchSCMHead originBranch = new TuleapBranchSCMHead("origino-branchu");
TuleapBranchSCMRevision originBranchRevision = new TuleapBranchSCMRevision(originBranch, "0r1g4n_h4sh");

TuleapPullRequestSCMHead pullRequestSCMHead = new TuleapPullRequestSCMHead(this.getGitPullRequest(), SCMHeadOrigin.DEFAULT, targetBranch, 101, 101);
TuleapPullRequestSCMHead pullRequestSCMHead = new TuleapPullRequestSCMHead(this.getGitPullRequest(), SCMHeadOrigin.DEFAULT, targetBranch, 101, 101, "refs/tlpr/4");

TuleapPullRequestRevision tuleapPullRequestRevision = new TuleapPullRequestRevision(pullRequestSCMHead, targetBranchRevision, originBranchRevision);

Expand All @@ -54,7 +54,7 @@ public void itReturnsFalseIfTheGivenRevisionIsNotTheWantedRevision() {
TuleapBranchSCMHead originBranch = new TuleapBranchSCMHead("origino-branchu");
TuleapBranchSCMRevision originBranchRevision = new TuleapBranchSCMRevision(originBranch, "0r1g4n_h4sh");

TuleapPullRequestSCMHead pullRequestSCMHead = new TuleapPullRequestSCMHead(this.getGitPullRequest(), SCMHeadOrigin.DEFAULT, targetBranch, 101, 102);
TuleapPullRequestSCMHead pullRequestSCMHead = new TuleapPullRequestSCMHead(this.getGitPullRequest(), SCMHeadOrigin.DEFAULT, targetBranch, 101, 102, "refs/tlpr/4");

TuleapPullRequestRevision tuleapPullRequestRevision = new TuleapPullRequestRevision(pullRequestSCMHead, targetBranchRevision, originBranchRevision);

Expand All @@ -64,7 +64,7 @@ public void itReturnsFalseIfTheGivenRevisionIsNotTheWantedRevision() {
TuleapBranchSCMHead otherOriginBranch = new TuleapBranchSCMHead("origino-0th3r-branchu");
TuleapBranchSCMRevision otherOriginBranchRevision = new TuleapBranchSCMRevision(otherOriginBranch, "0r1g4n_h4sh_-0th3r");

TuleapPullRequestSCMHead otherPullRequestSCMHead = new TuleapPullRequestSCMHead(this.getGitPullRequest(), SCMHeadOrigin.DEFAULT, otherTargetBranch, 101, 102);
TuleapPullRequestSCMHead otherPullRequestSCMHead = new TuleapPullRequestSCMHead(this.getGitPullRequest(), SCMHeadOrigin.DEFAULT, otherTargetBranch, 101, 102, "refs/tlpr/4");

TuleapPullRequestRevision otherPullRequestRevision = new TuleapPullRequestRevision(otherPullRequestSCMHead, otherTargetBranchRevision,otherOriginBranchRevision);
assertFalse(tuleapPullRequestRevision.equivalent(otherPullRequestRevision));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ public void testItCleansAndAddThePullRequestBranchRefSpecWhenThereIsAPullRequest
TuleapBranchSCMHead tlpScmHeadOrigin = new TuleapBranchSCMHead("tchiki-tchiki");
TuleapBranchSCMRevision tlpRevisionOrigin = new TuleapBranchSCMRevision(tlpScmHeadOrigin, "h4shu_or1g1n");

TuleapPullRequestSCMHead tlpPrScmHead = new TuleapPullRequestSCMHead(this.getPullRequest(), SCMHeadOrigin.DEFAULT, tlpScmHeadTarget,4,4);
TuleapPullRequestSCMHead tlpPrScmHead = new TuleapPullRequestSCMHead(this.getPullRequest(), SCMHeadOrigin.DEFAULT, tlpScmHeadTarget,4,4, "refs/tlpr/4");
TuleapPullRequestRevision tlpPrRevision = new TuleapPullRequestRevision(tlpPrScmHead, tlpRevisionTarget, tlpRevisionOrigin);

String remote = "https://tuleap.example.com/repo.git";
String credentialsId = "1581";

TuleapSCMBuilder tuleapSCMBuilder = new TuleapSCMBuilder(tlpPrScmHead, tlpPrRevision, remote, credentialsId, "https://tuleap.example.com/plugins/git/somerepo");
assertEquals(1, tuleapSCMBuilder.refSpecs().size());
assertEquals("+refs/tlpr/3/head:refs/remotes/@{remote}/TLP-PR-3", tuleapSCMBuilder.refSpecs().get(0));
assertEquals("+refs/tlpr/4:refs/remotes/@{remote}/TLP-PR-3", tuleapSCMBuilder.refSpecs().get(0));
assertEquals("https://tuleap.example.com/repo.git", tuleapSCMBuilder.remote());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public GitHead getHead() {

SCMHeadOrigin originHead = SCMHeadOrigin.DEFAULT;
TuleapBranchSCMHead targetHead = new TuleapBranchSCMHead("my-c63-tlp-branch");
TuleapPullRequestSCMHead head = new TuleapPullRequestSCMHead(pullRequest, originHead, targetHead,10,10);
TuleapPullRequestSCMHead head = new TuleapPullRequestSCMHead(pullRequest, originHead, targetHead,10,10, "refs/tlpr/4");

TuleapBranchSCMRevision target = new TuleapBranchSCMRevision(head.getTarget(), "h4sH_t4rG3t");
TuleapBranchSCMRevision origin = new TuleapBranchSCMRevision(new TuleapBranchSCMHead(head.getOriginName()), "h4sH_or1g1n");
Expand Down