Skip to content

Add `raw` option to list merge request diffs API

Problem to Solve

Currently the GitLab API returns diffs in via the list merge request diffs API as a JSON object. It's possible to piece these together to produce a raw diff that could be immediately applied, but it's cumbersome for applications to introduce this logic.

Proposal

We should add a new REST API endpoint that replicates the behavior of the internal .diff endpoint. This would make it easier for applications to programmatically access diffs and use them.

We can re-use the send_git_diff workhorse helper and make it usable in our rest API.

An example of how we do this in our API (not raw diff but a raw blob) can be found in lib/api/repositories.rb for get ':id/repository/blobs/:sha/raw' endpoint. This REST API endpoint will send back a raw blob which workhorse will fetch and inject internally.

Old Description ### Summary

The response of "Get single MR changes" API is different from documentation.

Steps to reproduce

$ curl -H "Authorization: Bearer XXXXXXXXXXXXXXXXXXX" https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/merge_requests/45705/changes   | jq
{
  "id": 75095749,
  ...
  "changes": [
    {
      ...
      "renamed_file": false,
      "deleted_file": false,
      "diff": "@@ -43,7 +43,7 @@\n     \"@babel/preset-env\": \"^7.10.1\",\n     \"@gitlab/at.js\": \"1.5.5\",\n     \"@gitlab/svgs\": \"1.171.0\",\n-    \"@gitlab/ui\": \"21.34.1\",\n+    \"@gitlab/ui\": \"21.34.2\",\n     \"@gitlab/visual-review-tools\": \"1.6.1\",\n     \"@rails/actioncable\": \"^6.0.3-3\",\n     \"@rails/ujs\": \"^6.0.3-2\",\n"
    },
    ...
  ]
}

Example Project

N/A

What is the current bug behavior?

"get single MR changes" API returns diff text that doesn't have two-line header about from-file/to-file.

   ...
   "diff": "@@ -43,7 +43,7 @@\n     \"@babel/preset-env\": \"^7.10.1\",\n     \"@gitlab/at.js\": \"1.5.5\",\n     \"@gitlab/svgs\": \"1.171.0\",\n-    \"@gitlab/ui\": \"21.34.1\",\n+    \"@gitlab/ui\": \"21.34.2\",\n     \"@gitlab/visual-review-tools\": \"1.6.1\",\n     \"@rails/actioncable\": \"^6.0.3-3\",\n     \"@rails/ujs\": \"^6.0.3-2\",\n"
   ...

What is the expected correct behavior?

I expect that diff text has two-line header about from-file/to-file as documented.

https://docs.gitlab.com/ee/api/merge_requests.html#get-single-mr-changes

    ...
    "diff": "--- a/VERSION\ +++ b/VERSION\ @@ -1 +1 @@\ -1.9.7\ +1.9.8",
    ...

Relevant logs and/or screenshots

Described in Steps to reproduce

Output of checks

This bug happens on GitLab.com

Results of GitLab environment info

N/A

Results of GitLab application Check

N/A

Possible fixes

It's easy way to fix API document. But, I prefer to fix the response of API, because some library (I'm using java-diff-utils) expect that diff text has two-line header.

Edited by François Rosé