36

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

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
Gregory Suvalian
  • 3,566
  • 7
  • 37
  • 66
  • 1
    Check out [How to get a link to a file from a VSO repo](https://stackoverflow.com/questions/56281152/how-to-get-a-link-to-a-file-from-a-vso-repo/56283730#56283730), which, I believe, addresses the exact issue. – Zlatko Michailov - MSFT May 28 '19 at 16:59

9 Answers9

44

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=7.0

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; ensure that it has the Build: Read permission.

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!

Zach Olivare
  • 3,805
  • 3
  • 32
  • 45
  • 4
    Great answer, small addition. I generated a personal access token, but it wouldn't work unless I enabled the "Token Administration - Read & Manage" permission. I would have expected the right privilege to be "Code - Read" but that seems to have no effect. – mtlynch Dec 14 '20 at 23:56
  • 1
    @mtlynch this comment just saved me from myself :) – Kisaragi Apr 13 '21 at 21:56
  • 1
    do you have an idea how i could change the mime-type of the response from text/plain to text/html? – jan-seins Jun 14 '22 at 13:47
  • 1
    As of now you have to set the "Build: Read" token scope to do the trick. "Token Administration" is no longer needed. You can also use the modern `api-version=7.0` if you like. The rest of the URL stays the same. – Anton Rusak Jul 25 '23 at 12:51
7

Sure, here's the rests call needed:

GET https://feeds.dev.azure.com/{organization}/_apis/packaging/Feeds/{feedId}/packages/{packageId}?includeAllVersions={includeAllVersions}&includeUrls={includeUrls}&isListed={isListed}&isRelease={isRelease}&includeDeleted={includeDeleted}&includeDescription={includeDescription}&api-version=5.0-preview.1

https://learn.microsoft.com/en-us/rest/api/azure/devops/artifacts/artifact%20%20details/get%20package?view=azure-devops-rest-5.0#package

4c74356b41
  • 69,186
  • 6
  • 100
  • 141
5

You can obtain the raw URL using chrome.

Turn on Developer tools and view the Network tab.

Navigate to view the required file in the DevOps portal (Content panel). Once the content view is visible check the network tab again and find the URL which starts with "Items?Path", this is json response which contains the required "url:" element.

Ron
  • 61
  • 1
  • 1
  • Disagree, several posts above reference obtaining the required parameters however the parameters can easily be obtained from chrome as per the post – Ron Apr 23 '21 at 01:19
  • My bad. Went through my first set of reviews recently and I'm pretty sure this was one of the first "answers" I reviewed. I must've either misread the question or misread the answer, because yea, this seems fine. Sorry about that and thanks for pointing out my mistake :) – Michael Hoobler Apr 23 '21 at 04:35
3

I was able to get the raw contents of a file using this URL.

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

I got this from here.

https://learn.microsoft.com/en-us/rest/api/azure/devops/build/source%20providers/get%20file%20contents?view=azure-devops-rest-5.0

  • 1
    Hi. Please see the help center on how to properly answer questions. https://stackoverflow.com/help/how-to-answer – DjSh Jun 27 '19 at 15:29
  • I’m not sure why this is considered improper, in that it gives a direct answer and the source for that answer. I had the same question and this was easily the better answer for it. – Alex White Aug 30 '19 at 10:48
  • It could be improved by mentioning the need to get a Personal Access Token to use in the authorization header (and where to get that) – Alex White Aug 30 '19 at 10:50
  • Hi @AlexWhite so I was trying to do this in order to add a deploy button in a readme file that points at a ARM template (json file). I found yet another API that could get me the raw content but learned that my repo had to be public and not private for this to work. Here's the other API in case you are interested. – Judy Ann Dixon Sep 03 '19 at 13:40
2

Most answers address this well, but in context of a public repo with anonymous access the api is different. Here is the one that works in such a scenario:

https://dev.azure.com/{{organization_name}}/{{project_name}}/_apis/git/repositories/{{repo_name_encoded}}/items?scopePath={{path_to_your_file}}&api-version=6.0

This is the exact equivalent of the "raw" url provided by Github.

bryanbcook
  • 16,210
  • 2
  • 40
  • 69
Joerg Krause
  • 1,759
  • 1
  • 16
  • 27
  • Thanks for the edit. Just to clarify: I'm self employeed and run my company under my name and I really have the same (personal) name here for "organization_name". So for some people it might not be that obvious. If one is in an organization, than it's the organization's name, indeed. – Joerg Krause May 22 '23 at 14:25
1

Drag the filename from the attachments windows and drop it in to any other MS application to get the raw URL or linked filename.

sao
  • 1,835
  • 6
  • 21
  • 40
0

I am fairly new to this and had an issue accessing a raw file in an Azure DevOps Repo. It's straightforward in Github.

I wanted to download a file in CMD and BASH using Curl. First I browsed to the file contents in the browser make a note of the bold sections: https://dev.azure.com/**myOrg**/_git/**myProjectName**?path=%2F**MyFileName.ps1**

I then constructed the URL similar to what @Zach posted above.

https://dev.azure.com/**myOrg**/**myProjectName**/_apis/sourceProviders/TfsGit/filecontents?repository=**myProjectName**&commitOrBranch=**master**&api-version=5.0-preview.1&path=%2F**MyFileName.ps1**

Now when I paste the above URL in the browser it displays the content in RAW form similar to GitHub. The difference was I had to setup a PAT (Personal Access Token) in My Azure DevOps account then authenticate the URL in DOS/BASH example below:

curl -u "<username>:<password>" "https://dev.azure.com/myOrg/myProjectName/_apis/sourceProviders/TfsGit/filecontents?repository=myProjectName&commitOrBranch=master&api-version=5.0-preview.1&path=%2FMyFileName.ps1" -# -L -o MyFileName.ps1
mhvelplund
  • 2,099
  • 3
  • 22
  • 38
imran3501
  • 1
  • 1
0

Another way that may be helpful if you want to quickly get the raw URL for a specific file that you are browsing:

  • install the browser extension named "Undisposition"
  • from the dot menu (top right) choose "Download": the file will open in a new browser tab from which you can copy the URL

(edit: unfortunately this will only work for file types that the browser knows how to open, otherwise it will still offer to download it...)

Olivier Gérardin
  • 1,113
  • 14
  • 27
0

So the idea is not to clone the repo but to pick one particular file from the repo, so here is an example of how to do so.

Let us assume this is your Azure DevOps Repo, accessible from the browser.

https://dev.azure.com/<Org>/<Project>/_git/<Repo>?path=<Path>

To access or get the file in your terminal, use this URI.

https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path> 

Windows

Invoke-WebRequest https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path> -Credential <PSCredential>

or

$pair = "<username>:<password>"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
   Authorization = $basicAuthValue
}
$repo = https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path>
$downloadPath = "C:\Temp\$fileName"
Invoke-WebRequest -Uri $uri -Headers $Headers 

Linux

curl -u "<username:password>" https://dev.azure.com/<Org>/<Project>/_apis/sourceProviders/TfsGit/filecontents?repository=<Repo>&commitOrBranch=master&api-version=6.0&path=<Path>

Note:

The username will be your Azure DevOps username and the password will be your PAT (personal access token)