GraphQL API: Blob.sha does not return the last_commit_id but the blob_id
Summary
I'm using the GraphQL API for querying commit information of files / blobs in my project. I'm interested in retrieving the commit sha of the last commit in which a specific file has been modified. According to the GraphQL API documentation, I assumed that this information will be available in the Blob.sha field (see https://docs.gitlab.com/ee/api/graphql/reference/#blob). This field is described to contain the "Last commit SHA for the entry." However, this field does not return the SHA of the last commit. Instead, it seems to return the blob id of the file.
Steps to reproduce
- Use the following GraphQL query for retrieving the
Blob.shafield of a file in a GitLab project. (For simplicity, I hard-coded the query parameters for retrieving the information of the following file: https://gitlab.com/klaasdellschaft/graphql-last-commit-id/-/blob/main/README.md?ref_type=a0440b6aa1f2cee0a7c0c51f2cde137b7f1862ef from my example project. The project just contains that single file, and it only contains the commita0440b6aa1f2cee0a7c0c51f2cde137b7f1862ef.)
query getLastCommitSha() {
project(fullPath:"klaasdellschaft/graphql-last-commit-id") {
id
fullPath
repository {
tree(path:"", ref:"a0440b6aa1f2cee0a7c0c51f2cde137b7f1862ef") {
lastCommit {
sha
}
blobs {
nodes {
id
name
sha
}
}
}
}
}
}
- Compare the value in the
Blob.shafield with the commit information displayed in the UI and in the REST API, and with theBlob.idfield of the same GraphQL result.
Example Project
https://gitlab.com/klaasdellschaft/graphql-last-commit-id
What is the current bug behavior?
The query from above returns the following result:
{
"data": {
"project": {
"id": "gid://gitlab/Project/60780836",
"fullPath": "klaasdellschaft/graphql-last-commit-id",
"repository": {
"tree": {
"lastCommit": {
"sha": "a0440b6aa1f2cee0a7c0c51f2cde137b7f1862ef"
},
"blobs": {
"nodes": [
{
"id": "gid://gitlab/Gitlab::Graphql::Representation::TreeEntry/75e1c01b433b593e3c16cc8ceb4eb39cd52ac6aa",
"name": "README.md",
"sha": "75e1c01b433b593e3c16cc8ceb4eb39cd52ac6aa"
}
]
}
}
}
}
}
}
As you can see, Blob.sha=75e1c01b433b593e3c16cc8ceb4eb39cd52ac6aa which seems to be the blob ID, as it is also exposed in Blob.id. You can also confirm that this is the blob ID of the file by querying the same information via the REST API. The REST API query
GET https://gitlab.com/api/v4/projects/60780836/repository/files/README.md?ref=a0440b6aa1f2cee0a7c0c51f2cde137b7f1862ef
returns the following result:
{
"file_path": "README.md",
"ref": "a0440b6aa1f2cee0a7c0c51f2cde137b7f1862ef",
"blob_id": "75e1c01b433b593e3c16cc8ceb4eb39cd52ac6aa",
"commit_id": "a0440b6aa1f2cee0a7c0c51f2cde137b7f1862ef",
"last_commit_id": "a0440b6aa1f2cee0a7c0c51f2cde137b7f1862ef",
...
}
What is the expected correct behavior?
Based on the description of the field in the GraphQL API documentation (Last commit SHA for the entry.), I would have expected that the Blob.sha field contains the SHA of the last commit in which a specific blob / file has been modified. In the example above (based on the Git commit history, the UI and the REST API response), I would have expected that Blob.sha=a0440b6aa1f2cee0a7c0c51f2cde137b7f1862ef.
Output of checks
This bug happens on GitLab.com