How to find forks of a repository in Azure DevOps - azure

In GitLab I am able to see who has forked my repository by clicking the following link:
Does anyone know if there is a similar capability within Azure DevOps?
I was able to find the following API call however it doesn't seem to work:
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/forks/list?view=azure-devops-rest-6.0
-> I pass it a repo and various container ids where I know there are forks of that repo and it returns empty results.
Even when I find a fork in Azure devops by manually traversing the UI I cannot find a place to view where that repo was forked from.
Any help on the above would be greatly appreciated.

Even when I find a fork in Azure devops by manually traversing the UI I cannot find a place to view where that repo was forked from.
You could use the REST API Forks - Get Fork Sync Requests to retrieve all requested fork sync operations on this repository:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryNameOrId}/forkSyncRequests?api-version=6.0-preview.1
As the test result:
We could to know the repo ID where current repo was forked from.
And we could use the Repositories - List to know the name of the Repo:
Update:
In GitLab I am able to see who has forked my repository by
clicking the link. Does anyone know if there is a similar capability
within Azure DevOps?
If you want to know who forks your repo, you could just use your REST API
Forks - List:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryNameOrId}/forks/{collectionId}?api-version=6.0-preview.1
And we could use the oraganiztion id for the collectionId. We could use following RETS API to get the oraganiztion id:
Post https://dev.azure.com/{organization1}/_apis/Contribution/HierarchyQuery?api-version=5.0-preview.1
Body:
{
"contributionIds": ["ms.vss-features.my-organizations-data-provider"],
"dataProviderContext":
{
"properties":{}
}
}
The test result:

If you create a pull request for a repository that has been forked, its "into" options will include a select box that includes the list of forks from that repository.

Related

Gitlab webhook for sending notifications when image pulled from container registry?

Requirement - In my self hosted gitlab instance there are multiple projects maintained by different users which are all using one particular tag of an image from the container registry of my project. That tag is now outdated and I have created a new tag for the image and I would like to notify all the users to use the new tag
Is there any webhook available in gitlab which can be enabled for all PULL request of image:tag to send notifications (email,slack) to the authors of ci/cd pipelines?
Or maybe configure the pipeline to detect the image and tag being used and if it is the one in question then send notifications?
P.S. - Gitlab instance is using docker container registry
An approach that involves custom scripting. Less elegant than VonC's suggestion ;-)
… detect the image and tag being used and if it is the one in question then send notifications?
You could try tailing the logs while pulling the old tag manually.
Searching for the image & tag name in your log slice should help determine how the usernames of associated events can be parsed out. Probably with jq.
A custom script could then be added to regularly repeat that parsing and for example send an email to users who trigger those events.
"Webhook" ("custom HTTP callbacks") means a local listener to GitLab events.
Considering you are managing your GitLab instance, a better option would be to create a pipeline for external pull requests (since GitLab 12.3, Aug. 2019)
on-pull-requests:
script: echo 'this should run on pull requests'
only:
- external_pull_requests
This pipeline can check if a Dockerfile is being merged, and if that Dockerfile uses the wrong tag.
If it does, it can deny said pull request.

How to list files of the default branch for a repository using the Github API via one query?

Related problem: How do I find the default branch for a repository using the Github v3 API
Different from the related problem, I want to list the files of the default branch.
The API I know to list files of the master branch is:
https://api.github.com/repos/owner/repo_name/git/trees/master
However, the default branch name could be main.
Is there a way to list files of the default branch through only one query?
You could use HEAD simply (which refers to the default remote branch).
I tried it using gh api after a (gh auth login)
gh api repos/owner/repo_name/git/trees/HEAD
Example:
gh api repos/VonC/gitcred/git/trees/HEAD --jq ".tree[].path"

GitLab API: Get branch associated with issue

In a GitLab issue, you can associate a branch with an issue, and in the issue there will be the line
#whoever created the branch branchname to address this issue.
Is there a way of getting that branch using the issues API? I'm trying to set up an automation script that will merge all branches associated with issues that have a certain label into the prod branch, then push the result as a development brnach so I can deploy that to a dev environment. I don't want to use merge requests as they will be used when the dev work is complete and ready to be merged for deployment to production.
Unfortunately, there currently is no official API to fetch an issue's related branches.
Some possible ways you can work around this:
Use the notes API
When a user uses the issue interface to create the branch, you will see a system message, like you mention. This message will be present in the notes API for the issue.
Example using the python-gitlab library:
import re
...
branch_note_pattern = '^created branch \[\`(.*)\`\].*to address this issue'
issue = project.issues.get(ISSUE_NUMBER)
all_notes = list(issue.notes.list(as_list=False))
system_notes = [note for note in all_notes if note.system]
related_branches = []
for note in system_notes:
match = re.match(branch_note_pattern, note.body):
if match:
branch = match.groups()[0]
related_branches.append(branch)
print('BRANCHES RELATED TO ISSUE', ISSUE_NUMBER)
for branch_name in related_branches:
print(branch_name)
However, it is possible to have a related branch without that note appearing because the related branches is just based on naming convention. So, if someone just creates a branch with named like <issue_number>-some-branch-name then it will show up as a related branch, but there will not be a system message in the API.
So, if you rely on the notes API, you may miss related branches created manually.
Use the unofficial frontend API
The issues controller only returns related branches for the purposes of the frontend to render as HTML.
If you request /<:project_url>/-/issues/<:issue_number>/related_branches?format=json you will get a JSON response containing the HTML for the frontend to insert in the issue view. You can parse this HTML to get the related branches.
This will reliably fetch the same related branches you'll see in the UI, but is more work to implement, and is fragile because the API is not guaranteed to be stable between versions of GitLab.

Azure REST API Get Branches returning 0 branches

I am trying to get all branches for a project with
https://dev.azure.com/$organization/$project/_apis/tfvc/branches?&api-
version=5.0
I use the credentials, organization, and project variables in other queries that are working. The request format is based off the example here
https://learn.microsoft.com/en-us/rest/api/azure/devops/tfvc/branches/get?view=azure-devops-rest-5.0
The response I am getting is
count value
----- -----
0 {}
when there are many branches in the repository.
Should I be specifying the repository somewhere? Or what else? Thanks!
First you need to confirm which type of repository you need to use rest API to get branch from, git or tfvc.
If you want to get branches from a tfvc repo, then use the rest api as you refer to in your case.
But, you need to be aware that if the count you want to return is not 0, you should ensure that there are branches in your tfvc repo, not just folders and files.
To convert the folder in tfvc repo to branch, you can convert it in vs source control explorer. For details, please refer to this docs.
If you want to get branches from a git repo, you need to use the rest API as follows.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=5.0

How to update a fork from it's original via the Github API

I've created a fork of a github repository via the github API. Now, later on, I want to pull any updates from the origin repository into the fork. This should always be a fast-forward in my use case. I have read access to the origin repository and read-write to the fork.
I thought of maybe creating a Pull Request then accepting (both of which you can do via the API) but this creates noise (Pull Requests being created and destroyed) and just doesn't seem right.
Is there any way to do this via the API?
I don't have the inside scoop on this, so this might be a miss-feature that will be removed at some point. Until then:
Github makes available all commits across (I assume) the entire fork network; So APIs that accept commit hashes will be happy to work on hashes from the upstream, or across other forks (This is explicitly documented for repos/commits/compare and creating a pull requst).
So there are a couple of ways to update via APIs only:
Using Git data api: This will usually be the best option, if you don't change your fork's master.
Get upstream ref /repos/upstream/repo/git/refs/heads/master, and get the hash from it
Update your fork PATCH /repos/my/repo/git/refs/heads/master with the same hash.
Using a higher-level merge api: This will create a merge commit, which some people like.
Get the upstream ref like before
Create a merge to branch master in your repo.
Pull-request to yourself and merge it via api: This will end up creating not only a merge commit, but a PR as well.
Create PR: POST to /repos/your/repo/pulls with head = "upstream:master"
Get the PR url from the response,
Merge it: PUT to /repos/your/repo/pulls/number/merge
It's possible that the "upstream:master" notation would also work for options 1 & 2, saving an API call.
Not possible currently, but I've gone ahead and added that to our API wishlist. :)
This that work for me, because I needed update from upstream but without a merge request commit. My ref is master.
Create a pull request POST /repos/:myUsername/:myRepo/pulls
INPUT: {title, head: 'ownerFromUpStream:master', base: 'master', ...}
Get sha from pull request (ex. response.data.head.sha)
PATCH /repos/:myUsername/:myRepo/git/refs/master
PARAMS: {sha: shaFromPullRequest}
DOC.
Update ref
Create pull request
This is now possible in the GitHub API; documentation here, and announcement here.
In summary, make a POST request to /repos/{owner}/{repo}/merge-upstream with the proper authentication and the payload of { "branch": "branch-name" }.

Resources