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

Viewed 346

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

1 Answers

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