Is it possible to have a link to raw content of file in Azure DevOps

Gregory Suvalian picture Gregory Suvalian · Jan 10, 2019 · Viewed 7.3k times · Source

It's possible to generate a link to raw content of the file in GitHub, is it possible to do with VSTS/DevOps?

Answer

Zach Posten picture Zach Posten · Dec 31, 2019

Even after reading the existing answers, I still struggled with this a bit, so I wanted to leave a bit more of a thorough response.

As others have said, the pattern is (query split onto separate lines for ease of reading):

https://dev.azure.com/{{organization}}/{{project}}/_apis/sourceProviders/{{providerName}}/filecontents
  ?repository={{repository}}
  &path={{path}}
  &commitOrBranch={{commitOrBranch}}
  &api-version=5.0-preview.1

But how do you find the values for these variables? If you go into your Azure DevOps, choose Repos > Files from the left navigation, and select a particular file, your current url should look something like this:

https://dev.azure.com/{{organization}}/{{project}}/_git/{{repository}}?path=%2Fpackage.json

You should use those values for organization, project, and repository. For path, you'll see an HTTP encoded version of the unix file path. %2F is the HTTP encoding for /, so that path is actually just /package.json (a tool like Postman will do that encoding for you).

Commit or branch is pretty self explanatory; you either know what you want for this value or you should use master. I have "hard-coded" the api version in the above url because that's what the documentation currently points to.

For the last variable, you need providerName. In short, you should probably use TfsGit. I got this value from looking through the list of source providers and looking for one with a value of true for supportedCapabilities.queryFileContents.

However, if you just request this URL you'll get a "203 Non-Authoritative Information" response back because you still need to authenticate yourself. Referring again to the same documentation, it says to use Basic auth with any value for the username and a personal access token for the password. You can create a personal access token at https://dev.azure.com/{{organization}}/_usersSettings/tokens. If you're unfamiliar with this sort of thing, again Postman is super helpful with getting these requests working before you get into the code.


So if you have a repository with a src directory at the root, and you're trying to get the file contents of src/package.json, your URL should look something like:

https://dev.azure.com/{{organization}}/{{project}}/_apis/sourceProviders/TfsGit/filecontents?repository={{repository}}&commitOrBranch=master&api-version={{api-version}}&path=src%2Fpackage.json

And don't forget the basic auth!