0

One can publicly access the URL of a file in the repository of a publicly visible project or fetch its contents using the rest API.

Example: https://dev.azure.com/ayn/PowerShell/_git/AzIaaS?path=/README.md&_a=preview

fetch("https://dev.azure.com/ayn/PowerShell/_apis/git/repositories/4d348186-f42e-4ea5-9ed0-ec47530caee1/items?path=/README.md&api-version=7.1-preview.1")
      .then(response => response.text())
      .then(data => document.getElementById('code').textContent = data)
<pre id="code"></pre>

However, the approach shared in this answer doesn't work without a PAT. The URL below works on the browser when the user is logged in but not otherwise, even though it's a public repository.

Raw content URL: https://dev.azure.com/ayn/PowerShell/_apis/sourceProviders/tfsgit/filecontents?&repository=AzIaaS&commitOrBranch=main&api-version=7.0&path=/README.md

Is there a way to get the raw content URL of a public repository file that can be anonymously accessed from the browser?

Ayan Mullick
  • 67
  • 2
  • 11
  • 38
  • 1
    This seems to be a known issue with no good resolution at this time. https://developercommunity.visualstudio.com/t/sourceproviders-api-full-access-required-and-does/780085 – Dan Csharpster Mar 15 '23 at 14:40
  • Thanks for pointing this out, @Dan. However, a public project shouldn't have a dependency on a PAT. – Ayan Mullick Mar 15 '23 at 17:39
  • 1
    Agreed. I don't know why this is the case. I tried debugging that page a bit to see if I could figure out how its showing the content without a PAT. I'm sure its possible to use the same mechanism to get that content but it wasn't immediately clear how to do it. Another, more hacky option, would be to use something like Selenium to screen scrape that page and extract the content that way. – Dan Csharpster Mar 15 '23 at 17:45
  • The content is extractable with the API, as demoed in the snippet. I'm trying to [render the markdown from the repo. client-side](https://docsify-this.net/#/). The RAW content should be [visible on the browser](https://cdn.discordapp.com/attachments/1082838836071124992/1082867133551222934/image.png) **anonymously**, like a GitHub Raw page. – Ayan Mullick Mar 15 '23 at 19:17
  • I think your issue may be that you're trying this on a markdown file and most browsers like Chrome are set to download markdown files instead of render them in the browser. Can you try this with a different file type? – Dan Csharpster Mar 16 '23 at 17:54
  • @DanCsharpster, Even [the .ps1 file url](https://dev.azure.com/ayn/e7df3df2-6fd4-4560-8cb9-c111076fc587/_apis/git/repositories/4d348186-f42e-4ea5-9ed0-ec47530caee1/items?path=/Get-AzVMDeletionActivity.ps1) is facing the same issue. – Ayan Mullick Mar 16 '23 at 19:38
  • 1
    I don't think PowerShell files are meant to be viewed in a browser, either. I would suggest trying with something simple like .txt file, to be sure. – Dan Csharpster Mar 16 '23 at 20:28
  • @DanCsharpster, you are right. [The RAW URL](https://dev.azure.com/ayn/e7df3df2-6fd4-4560-8cb9-c111076fc587/_apis/git/repositories/4d348186-f42e-4ea5-9ed0-ec47530caee1/items?path=/dummy.txt) for a text file is working fine. Just not working with other formats. The problem is that in spite of explicitly specifying in the parameters, the `Content-Type` in ADO's response to `.md` and other filetypes is `application/octet-stream` while that in [GitHub's response](https://raw.githubusercontent.com/Ayanmullick/PowerShell/master/profile.ps1) is `text/plain` – Ayan Mullick Mar 17 '23 at 03:11
  • 1
    You could launder it through an intermediate api, perhaps an Azure function. Pass the pass to the azure function, it gets the content for you and passes the content along but updating the filetype and whatever else may be required to make it render in a browser for you. I realize that is annoying to have to do, but I don't see an easier way offhand. I'm also not sure why you need it. Perhaps a more elegant solution that meets your requirements exists. – Dan Csharpster Mar 17 '23 at 18:58

1 Answers1

1

For your demo repository, if you click download for the readme file, you will download from this URL:

https://dev.azure.com/ayn/e7df3df2-6fd4-4560-8cb9-c111076fc587/_apis/git/repositories/4d348186-f42e-4ea5-9ed0-ec47530caee1/items?path=/README.md&versionDescriptor%5BversionOptions%5D=0&versionDescriptor%5BversionType%5D=0&versionDescriptor%5Bversion%5D=main&resolveLfs=true&%24format=octetStream&api-version=5.0&download=true

You can replace the path parameter with the file you want. You can also use "simpler" URLs. I can download these files without any sort of previous session .

https://dev.azure.com/ayn/e7df3df2-6fd4-4560-8cb9-c111076fc587/_apis/git/repositories/4d348186-f42e-4ea5-9ed0-ec47530caee1/items?path=/README.md
https://dev.azure.com/ayn/e7df3df2-6fd4-4560-8cb9-c111076fc587/_apis/git/repositories/4d348186-f42e-4ea5-9ed0-ec47530caee1/items?path=/Get-AzVMDeletionActivity.ps1

You will need to replace the guids and project name with respective values, of course.

Alex AIT
  • 17,361
  • 3
  • 36
  • 73
  • I'm not looking to download, @Alex. I'm looking for the URL to [view it in the browser](https://cdn.discordapp.com/attachments/1082838836071124992/1082867133551222934/image.png), like one can view a raw file on GitHub. – Ayan Mullick Mar 13 '23 at 23:15