jenkinsci / gitlab-plugin

A Jenkins plugin for interfacing with GitLab
https://plugins.jenkins.io/gitlab-plugin/
GNU General Public License v2.0
1.44k stars 614 forks source link

"Retrieving gitlab project ids" failure due to wrong projectId (source instead of target) #1151

Open ijsf opened 3 years ago

ijsf commented 3 years ago

Version report

Jenkins and plugins versions report:

Jenkins: 2.289.1
OS: Linux - 5.4.0-70-generic
---
...
gitlab-plugin:1.5.20
Linux 5.4.0-70-generic #78-Ubuntu SMP Fri Mar 19 13:29:52 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Reproduction steps

Results

Expected result:

The updateGitlabCommitStatus is performed on the target/upstream repository, and the commit (pipeline) status is updated successfully.

Actual result:

The updateGitlabCommitStatus is not performed. The Gitlab plugin fails to retrieve the project ids because it tries to access the source and not the target's project id. In this case, the source is the fork project (from User B) and returns an error since User A does not have access to User B's fork. The relevant API call:

Retrieving gitlab project ids
Aug 16, 2021 11:29:07 AM FINEST com.dabsquared.gitlabjenkins.gitlab.api.impl.ResteasyGitLabClientBuilder
Call GitLab:
HTTP method: GET
URL: https://gitlab.com/api/v4/projects/.../repository/commits/...
Request headers: [
Accept = [application/json],
PRIVATE-TOKEN = [****FILTERED****]
]

The ... here seems to be the wrong project id. I assume the project id should be the project.id property (e.g. the target/upstream repository) from the MergeRequest object. Instead, it uses the object_attributes.source.id property.

In any case, the result is of course a 404, because the fork ("source project") is not accessible with the Personal Access Token that the plugin uses (User A):

Got response from GitLab:
URL: https://gitlab.com/api/v4/projects/.../repository/commits/...
Status: 404 Not Found

The offending code seems to be located here:

https://github.com/TestYourLuck/gitlab-plugin/blob/ee4c3d62ff31b3766a396cf6744e45148b183634/src/main/java/com/dabsquared/gitlabjenkins/util/CommitStatusUpdater.java#L119

I'm not quite sure why this code is there, I'm sure there's a reason, but I would expect the code to either do that and on failure (in this case) fall back to the "target" project id, or to not query the source project id at all (though there's probably a reason for this).

Unfortunately with a team of developers using a fork strategy keeping changes in their own fork branches, and then opening up MRs in the target/upstream repository, this completely breaks the pipeline statuses.

jsight commented 9 months ago

Duplicate of https://github.com/jenkinsci/gitlab-plugin/issues/1044