Skip to content

Commit

Permalink
Decoration issue fails due invalid project id when branch is in fork
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Canavidez committed Apr 15, 2022
1 parent d10d791 commit 89cca05
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ public class MergeRequest {
private final long iid;
private final DiffRefs diffRefs;
private final long sourceProjectId;
private final long targetProjectId;
private final String webUrl;

public MergeRequest(@JsonProperty("iid") long iid, @JsonProperty("diff_refs") DiffRefs diffRefs,
@JsonProperty("source_project_id") long sourceProjectId,
@JsonProperty("target_project_id") long targetProjectId,
@JsonProperty("web_url") String webUrl) {
this.iid = iid;
this.diffRefs = diffRefs;
this.sourceProjectId = sourceProjectId;
this.webUrl = webUrl;
this.targetProjectId = targetProjectId;
}

public long getIid() {
Expand All @@ -47,6 +50,10 @@ public long getSourceProjectId() {
return sourceProjectId;
}

public long getTargetProjectId() {
return targetProjectId;
}

public String getWebUrl() {
return webUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected User getCurrentUser(GitlabClient gitlabClient) {
@Override
protected List<String> getCommitIdsForPullRequest(GitlabClient gitlabClient, MergeRequest mergeRequest) {
try {
return gitlabClient.getMergeRequestCommits(mergeRequest.getSourceProjectId(), mergeRequest.getIid()).stream()
return gitlabClient.getMergeRequestCommits(mergeRequest.getTargetProjectId(), mergeRequest.getIid()).stream()
.map(Commit::getId)
.collect(Collectors.toList());
} catch (IOException ex) {
Expand All @@ -130,7 +130,7 @@ protected void submitPipelineStatus(GitlabClient gitlabClient, MergeRequest merg
analysisSummary.getNewCoverage(),
pipelineId);

gitlabClient.setMergeRequestPipelineStatus(mergeRequest.getSourceProjectId(), analysis.getCommitSha(), pipelineStatus);
gitlabClient.setMergeRequestPipelineStatus(mergeRequest.getTargetProjectId(), analysis.getCommitSha(), pipelineStatus);
} catch (IOException ex) {
throw new IllegalStateException("Could not update pipeline status in Gitlab", ex);
}
Expand All @@ -141,7 +141,7 @@ protected void submitCommitNoteForIssue(GitlabClient client, MergeRequest mergeR
Integer line = Optional.ofNullable(issue.getIssue().getLine()).orElseThrow(() -> new IllegalStateException("No line is associated with this issue"));

try {
client.addMergeRequestDiscussion(mergeRequest.getSourceProjectId(), mergeRequest.getIid(),
client.addMergeRequestDiscussion(mergeRequest.getTargetProjectId(), mergeRequest.getIid(),
new CommitNote(analysisIssueSummary.format(formatterFactory),
mergeRequest.getDiffRefs().getBaseSha(),
mergeRequest.getDiffRefs().getStartSha(),
Expand All @@ -157,11 +157,11 @@ protected void submitCommitNoteForIssue(GitlabClient client, MergeRequest mergeR
@Override
protected void submitSummaryNote(GitlabClient client, MergeRequest mergeRequest, AnalysisDetails analysis, AnalysisSummary analysisSummary) {
try {
Discussion summaryComment = client.addMergeRequestDiscussion(mergeRequest.getSourceProjectId(),
Discussion summaryComment = client.addMergeRequestDiscussion(mergeRequest.getTargetProjectId(),
mergeRequest.getIid(),
new MergeRequestNote(analysisSummary.format(formatterFactory)));
if (analysis.getQualityGateStatus() == QualityGate.Status.OK) {
client.resolveMergeRequestDiscussion(mergeRequest.getSourceProjectId(), mergeRequest.getIid(), summaryComment.getId());
client.resolveMergeRequestDiscussion(mergeRequest.getTargetProjectId(), mergeRequest.getIid(), summaryComment.getId());
}
} catch (IOException ex) {
throw new IllegalStateException("Could not submit summary comment to Gitlab", ex);
Expand All @@ -172,7 +172,7 @@ protected void submitSummaryNote(GitlabClient client, MergeRequest mergeRequest,
@Override
protected List<Discussion> getDiscussions(GitlabClient client, MergeRequest pullRequest) {
try {
return client.getMergeRequestDiscussions(pullRequest.getSourceProjectId(), pullRequest.getIid());
return client.getMergeRequestDiscussions(pullRequest.getTargetProjectId(), pullRequest.getIid());
} catch (IOException ex) {
throw new IllegalStateException("Could not retrieve Merge Request discussions", ex);
}
Expand Down Expand Up @@ -209,7 +209,7 @@ protected boolean isUserNote(Note note) {
@Override
protected void addNoteToDiscussion(GitlabClient client, Discussion discussion, MergeRequest pullRequest, String note) {
try {
client.addMergeRequestDiscussionNote(pullRequest.getSourceProjectId(), pullRequest.getIid(), discussion.getId(), note);
client.addMergeRequestDiscussionNote(pullRequest.getTargetProjectId(), pullRequest.getIid(), discussion.getId(), note);
} catch (IOException ex) {
throw new IllegalStateException("Could not add note to Merge Request discussion", ex);
}
Expand All @@ -218,7 +218,7 @@ protected void addNoteToDiscussion(GitlabClient client, Discussion discussion, M
@Override
protected void resolveDiscussion(GitlabClient client, Discussion discussion, MergeRequest pullRequest) {
try {
client.resolveMergeRequestDiscussion(pullRequest.getSourceProjectId(), pullRequest.getIid(), discussion.getId());
client.resolveMergeRequestDiscussion(pullRequest.getTargetProjectId(), pullRequest.getIid(), discussion.getId());
} catch (IOException ex) {
throw new IllegalStateException("Could not resolve Merge Request discussion", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ private void decorateQualityGateStatus(QualityGate.Status status) {
wireMockRule.stubFor(get(urlPathEqualTo("/api/v4/projects/" + urlEncode(repositorySlug) + "/merge_requests/" + mergeRequestIid)).willReturn(okJson("{\n" +
" \"id\": 15235,\n" +
" \"iid\": " + mergeRequestIid + ",\n" +
" \"target_project_id\": " + sourceProjectId + ",\n" +
" \"web_url\": \"http://gitlab.example.com/my-group/my-project/merge_requests/1\",\n" +
" \"diff_refs\": {\n" +
" \"base_sha\":\"d6a420d043dfe85e7c240fd136fc6e197998b10a\",\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public void setUp() throws IOException {
when(analysisDetails.getPullRequestId()).thenReturn(Long.toString(MERGE_REQUEST_IID));
when(mergeRequest.getIid()).thenReturn(MERGE_REQUEST_IID);
when(mergeRequest.getSourceProjectId()).thenReturn(PROJECT_ID);
when(mergeRequest.getTargetProjectId()).thenReturn(PROJECT_ID);
when(mergeRequest.getDiffRefs()).thenReturn(diffRefs);
when(mergeRequest.getWebUrl()).thenReturn(MERGE_REQUEST_WEB_URL);
when(diffRefs.getBaseSha()).thenReturn(BASE_SHA);
Expand Down

0 comments on commit 89cca05

Please sign in to comment.