How to get branch name from commit id using azure Devops REST API? - azure

Scenario: I need to get when was the latest commit done in the repo and by whom, and to which branch did that user do the commit on?
Solution:
I'm using python azure.devops module. and here is my code:
cm_search_criteria = models.GitQueryCommitsCriteria(history_mode='firstParent', top=10)
commits = git_client.get_commits(repo.id, search_criteria=cm_search_criteria, project=project.name)
for i in commits:
datetimeobj = datetime.strptime(i.committer.date.strftime("%x"), '%m/%d/%y')
last_commit_on = datetimeobj.date()
last_commit_by = i.committer.email
break
Now how do I get the branch name to which the user had committed the code? In the UI we can see the branch name... how can i get the same data using Azure Devops REST API ?
enter image description here

you may need to use Stats - List to retrieve statistics about all branches within a repository, then evaluate the latest commit, you also need to consider argument of baseVersionDescriptor.versionOptions of firstParent
I'm not sure if Python wrapper module support this seems like the github project is achieved now.

Related

Azure Yaml Pipeline - Update Workitems from multiple Repositories

In a nutshell, I am looking to get a list of work items linked to a git branch.
In more detail
I am working with 4 repositories that I add as resources to my pipeline
- repository: Cms # code repository
name: <ProjectName>/Cms
type: git
ref: develop2022
- repository: QA-Automation # Automated Testing Repo
name: <ProjectName>/QA-Automation
type: git
ref: main
- repository: TdsWDPExplorer # Generate Reports Repo
name: <ProjectName>/TdsWDPExplorer
type: git
ref: master
The Pipeline yaml files them self's are in the 4th Repo and checked out as self
- checkout: Self
path: s/DE-DevOps
I am trying to update the work items associated with the Cms Repository.
I tried using the Workitem Updater task https://marketplace.visualstudio.com/items?itemName=BlueBasher.bluebasher-workitemupdater
But it only sees the workitems associated with the Repository holding the yaml Files (Self).
I also looked at the API to get a list of the work items.
_apis/git/repositories//refs?filter=heads%2fBRANCHNAME&includeLinks=true
Gives me details to a branch but I didn't find the linked work items
Also looking at the workitem I dint see that info
_apis/wit/workitems?ids=ITEM-ID's&$expand=all&api-version=6.0
I am thinking it might be somewhere in _apis/wit/reporting/workitemlinks but haven been able to get the info.
I found a answer that works for me, in a response to Obtain all work items from Azure DevOps that have been merged into a branch via JavaScript
we link the work items to pull requests, I can use the API to query the pull requests in to the given branch and get the linked work items like:
https://dev.azure.com/Organisation/Project/_apis/git/repositories/Cms/pullrequests?searchCriteria.status=completed&searchCriteria.targetRefName=refs/heads/BRANCHNAME&api-version=6.0
I should now be able to extract the ID's and pass them in a PS loop to the Item Updater Task https://marketplace.visualstudio.com/items?itemName=BlueBasher.bluebasher-workitemupdater or using the API to update the workitem
There is also the option to call the API for the pipeline run info https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/runs/get?view=azure-devops-rest-7.1#runresources
But I didn't see a way to extract the work items but the information must be somewhere there as well because I can see the work items listed in the pipeline view by resource

Probot Octokit - Get file from specific branch

I'm using Probot's Octokit to read a file from a repo:
const content = await context.octokit.repos.getContent({owner: "OWNER", repo: "REPO", path:"PATH"})
However this only works for the master branch, is there a way to get the file from another branch (e.g. the branch that the pull request action is coming from)? This isn't directly stated in the docs itself.
Thanks
Yes, it is possible.
When using Octokit, there is an optional parameter: ref where you can specify the branch or commit hash. You can find the reference here.

Bitbucket API is there a way to return last/current deployed branch name for specific environment?

I am building a small automation tool that will redeploy my last/current deployed branch again via api ,
looking through the document i could not find the right way to fetch my current branch name or anything related to it .
i tried the endpoint /deployments
i got a lot of data , but for somereason i can not see my last/current deployed pipeline in the list .
Thanks
i have found the way ,
https://api.bitbucket.org/2.0/repositories/{workspace}/{repo_slug}/environments/
then fetch pipeline uuid from the env u need and request the pipeline data via
/2.0/repositories/{workspace}/{repo_slug}/pipelines/{pipeline_uuid}

Deleting branches on Azure DevOps with Powershell

I am trying to delete old branches which are x days old from Azure DevOps by using Powershell, but it is unsuccessful. Could you please help me out on this matter or if you have any better idea to execute this task, I am more than welcome to hear them out! I have been stuck with it for a few weeks now.
Thanks.
Deleting branches on Azure DevOps with Powershell
You could use the REST API to delete those Branch.
However, we do not recommend you to do this. It is not safe to use scripts to delete some old branches, because the script cannot determine whether the branch is important, but it is risky to delete based on the date. So you need to be clear about this before deleting.
First, we could use the REST API Refs - List to list all the branches for the Repo:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?api-version=6.0
Then, we loop each branch with REST API Commits - Get Commits:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/commits?&searchCriteria.compareVersion.version=<YouBranchName>&api-version=6.0
And compare creation date or activation date, use the REST API to delete those branches:
POST https://dev.azure.com/{organization name}/{project name}/_apis/git/repositories/{repositoryId}/refs?api-version=5.1
Request Body:
[
{
"name": "{branchName}",
"oldObjectId": "{branchObjectId}",
"newObjectId": "0000000000000000000000000000000000000000"
}
]
Alternatively, if you want use git command line to delete, you could refer below document for some more details:
Deleting Old Local Branches With PowerShell

How do I find the default branch for a repository using the Github v3 API

My goal is to get the tree for the latest SHA in the default branch
GET /repos/:owner/:repo/git/trees/:sha
How do I find the lastest SHA from the default branch?
I know that I can call
GET /repos/:owner/:repo/branches/:branch
But I can't just use "master" for the branch as not all repos use master as the default branch.
How do I find out what the default branch for a repo is?
Make a call to /repos/:owner/:repo and read the default_branch property value - this is the name of the default branch. See example response here: http://developer.github.com/v3/repos/#get
This is also now avaialable with the github cli as well
gh repo list <Your_Name> --json nameWithOwner,defaultBranchRef
If you want to slightly cleanup the output, you can remap with jq
gh repo list <Your_Name> --json nameWithOwner,defaultBranchRef \
--jq ".[] | { nameWithOwner , defaultBranch: .defaultBranchRef.name}"
The advantage to this approach being auth is integrated and much easier to manage
This is a cleaner and faster way using gh:
gh api /repos/{owner}/{repo} --jq '.default_branch'

Resources