Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitChanges -> newContent undefined / Impossible to Get Raw git diff text #571

Open
marc-wilson opened this issue Oct 11, 2023 · 11 comments
Open

Comments

@marc-wilson
Copy link

  1. Please check our current Issues to see if someone already reported this https://github.com/Microsoft/vsts-node-api/issues
  2. Take a look at our Instructions for logging issues https://github.com/Microsoft/vsts-node-api/blob/master/CONTRIBUTING.md#instructions-for-logging-issues

Environment

Node version: 18.13
Npm version: 8.19.3
OS and version:
azure-devops-node-api version: ^12.1.0

Issue Description

I am trying to get the raw diff text of a given commit. If it were GitHub, you would do this by sending a GET request to commits/sha with the header application/vnd.github.diff. Azure DevOps doesn't seem to have a similar concept.

The closest thing I can find are the below methods:

  • getFileDiffs
  • getChanges

getFileDiffs gives some relative information with line counts, but not enough info to come up with an actual diff.
getChanges returns GitCommitDiffs which is ultimately a wrapper around GitChange[]. In the documentation, it states that GitChange has some interesting properties that appear to do what I need. But, they are never defined. Is there some header or different method I need to call in order to get a git diff given a commit and/or file path?

GitChange
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/diffs/get?view=azure-devops-rest-7.2&tabs=HTTP#gitchange

newContent | ItemContent | Content of the item after the change.

newContent
ItemContent

Content of the item after the change.

Expected behaviour

There should be a way to obtain raw diff text from the api. I am trying to create automated release notes but I need the raw diff text in order to do so.

Actual behaviour

There are methods that lead you to believe you can come up with enough information to figure out exact differences and compile the diff text on your own, but they all ultimately lead to a dead end.

There are few hacks/workaround posted out there but none seem to work:

Copy link

github-actions bot commented Jan 9, 2024

This issue has had no activity in 90 days. Please comment if it is not actually stale

@github-actions github-actions bot added the stale label Jan 9, 2024
@marc-wilson
Copy link
Author

not stale

@github-actions github-actions bot removed the stale label Jan 10, 2024
Copy link

github-actions bot commented Apr 9, 2024

This issue has had no activity in 90 days. Please comment if it is not actually stale

@solmon
Copy link

solmon commented Apr 17, 2024

FileDiff function gives absolutely non relatable output, I have tried all possible ways to find a solution. Nothing works. Any possible solutions please need some pointers. Thank you.

@marc-wilson marc-wilson reopened this Apr 17, 2024
@github-actions github-actions bot removed the stale label Apr 17, 2024
@joatkn
Copy link

joatkn commented May 8, 2024

Indeed FileDiff is not useful and I cannot find any guidance on getChanges. While I was able to replicate the results from the "hacks" posted in the initial issue, it requires using an undocumented API so there is no way to clearly define/modify the API call e.g. to retrieve batches / diffs for multiple files etc. The work-around is unfortunately extremely inefficient for large sample sizes and not transparent to me.
The ItemContent documented in the Azure DevOps API documentation reads like it would indeed provide a diff. It would be very useful if this could be retrieved directly over the API. Kindly let me know if anyone finds a solution. Cheers!

@patrykJaniewskiJobgit
Copy link

https://learn.microsoft.com/en-us/rest/api/azure/devops/git/diffs/get?view=azure-devops-rest-7.0&tabs=HTTP#gitchange
this restpi api endpoint still doesn't return newContent in GitChange object

@GreenzoneDev
Copy link

The rest API is still not populating the newContent, do we have a fix date for this yet?

@fouadbakkour
Copy link

fouadbakkour commented Aug 26, 2024

Hi, I was able to find a solution for this, a workaround.
There is no way Azure will provide a raw diff text, however, my case was I need to run some checks during a pull request, a CI/CD on a cloud machine (agent).

So I wrote a shell script to run on an agent to get the diff text by:

  • Call get pull request details API
  • Parse the details to get the target and source branches.
  • Clone them in temporary folder
  • run git command line to get the diff between them; something like this: git diff ${targetBranch}..${sourceBranch} > diff.txt

The full command is like this:
rm -rf diff_logic && mkdir diff_logic && cd diff_logic && git clone ${process.env.CURRENT_REPO_ACCESS_GIT_URL} && cd $(basename ${process.env.CURRENT_REPO_ACCESS_GIT_URL} .git) && git checkout ${targetBranch} && git checkout ${sourceBranch} && git diff ${targetBranch}..${sourceBranch} > diff.txt && mv diff.txt ../..

CURRENT_REPO_ACCESS_GIT_URL = The current project repository access git URL, usually it contains read/write access token, something like this: https://${PAT}@dev.azure.com/organization/project/_git/repo
targetBranch = PRDetailsJSON.targetRefName.replace("refs/heads/", "");
sourceBranch = PRDetailsJSON.sourceRefName.replace("refs/heads/", "");

@marc-wilson
Copy link
Author

Hi, I was able to find a solution for this, a workaround. There is no way Azure will provide a raw diff text

Why? GitHub Api does it. It looks like your script ultimately downloads a repo and compares two branches manually. There's no reason why the Azure Api can't do this. A few endpoint suggest that it's possible but just doesn't seem to work.

@fouadbakkour
Copy link

Hi, I was able to find a solution for this, a workaround. There is no way Azure will provide a raw diff text

Why? GitHub Api does it. It looks like your script ultimately downloads a repo and compares two branches manually. There's no reason why the Azure Api can't do this. A few endpoint suggest that it's possible but just doesn't seem to work.

@marc-wilson Yeah I know Github does that, also BitBucket API as well, my case is different, I have a SaaS that should work with git services, everything in place but Azure DevOps, I have this workaround for it only. The others are just fine

@melon-husk
Copy link

Any update on this issue ? Still not able to get raw git diff from Azure DevOps REST API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants