microsoft / azure-devops-node-api

Azure DevOps Client for Node.js
Other
462 stars 229 forks source link

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

Open marc-wilson opened 11 months ago

marc-wilson commented 11 months ago
  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 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:

github-actions[bot] commented 8 months ago

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

marc-wilson commented 8 months ago

not stale

github-actions[bot] commented 5 months ago

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

solmon commented 5 months ago

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.

joatkn commented 4 months ago

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 commented 2 months ago

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 commented 3 weeks ago

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

fouadbakkour commented 3 weeks ago

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:

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 commented 3 weeks ago

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 commented 3 weeks ago

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