Commit parent IDs list is empty in some REST API's
Summary
When retrieving the commits via the Get Single MR commits API the parent_ids
field are always the empty list.
Steps to reproduce
Make an API request to retrieve any merge request's commits. Observe that the parent_ids
array is always empty.
Example Project
https://gitlab.com/gitlab-org/gitlab/
What is the current bug behavior?
The parent_ids
of the requested commit is always the empty list.
What is the expected correct behavior?
The parent_ids
list should be filled with the actual parent commit hash.
Relevant logs and/or screenshots
Requesting this merge request's commits in Python:
>>> [(commit['id'], commit['parent_ids']) for commit in requests.get('https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/merge_requests/19185/commits', headers={'private-token': token}).json()]
[('7c6e3d016c14b59de8ccf63ef08a0bcc20c468f2', []), ('ea66022b88322d596e746648331256920e0ae548', []), ('5bdba9cd622472c495d2cd909bda75d794be1926', [])]
Note that the Get a single commit API correctly fills out the parent_ids
array:
>>> requests.get('https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/repository/commits/7c6e3d016c14b59de8ccf63ef08a0bcc20c468f2', headers={'private-token': token}).json()['parent_ids']
['ea66022b88322d596e746648331256920e0ae548']
Following is an excerpt from the response of @blunceford (Gitlab support):
I was able to replicate this on my test instance running version 12.4. I was also able to see this in the rails console (
sudo gitlab-rails console
), using the two different ways to get a commit, either through themerge_request.commits
call or theproject.commit(:sha)
call:
mr = MergeRequest.find(<id here>)
commits = mr.commits
for commit in commits
puts commit.parent_ids
end
> no parent ids
project = Project.find(<id here>)
commit = project.commit('<sha here>')
commit.parent_ids
> parent ids
The merge request commits method returns a CommitCollection, while the project commit method retrieves the commit from the repository via the SHA. This mirrors what you see in the UI, as there are a list of commits in the Merge Request, and navigating to a commit will then show the parent id(s).
We recommend submitting a feature request in the GitLab issue tracker if you'd like the parent ID exposed in the Merge Request commits. Let us know if you submit an issue, and we can add the appropriate labels to it.
I am filing this under bug because the response parent_ids: []
is inherently wrong. I could argue that this is a feature request if the property is missing or set to null
. However, an empty list indicates to the client that there is no commit parent, suggesting that this is the very first commit in the repo. This is clearly the incorrect behavior.
Output of checks
(If you are reporting a bug on GitLab.com, write: This bug happens on GitLab.com)
Results of GitLab environment info
Expand for output related to GitLab environment info
(For installations with omnibus-gitlab package run and paste the output of:
sudo gitlab-rake gitlab:env:info
)(For installations from source run and paste the output of:
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production
)
Results of GitLab application Check
Expand for output related to the GitLab application check
(For installations with omnibus-gitlab package run and paste the output of:
sudo gitlab-rake gitlab:check SANITIZE=true
)(For installations from source run and paste the output of:
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true
)(we will only investigate if the tests are passing)
Possible fixes
(If you can, link to the line of code that might be responsible for the problem)