Luke Erickson
Luke Erickson

Reputation: 78

How to get Azure DevOps Repository Creation Date via API

How would you go about getting the repository creation date of an azure devops git repository? We have plenty of repositories that were moved from TFS but want to know the date the GIT repo was added. Is this information available via the API?

My first attempt is making a call to the repos api and then feeding those ID's into the commits api. For each item in the response, the count minus 1 should be the index to the first commit, right? For some of my repositories, this seems to be about halfway through their commit history. If I use an index of zero, I am just presented with the most recent commit?

Upvotes: 3

Views: 3175

Answers (3)

Kartik Gupta
Kartik Gupta

Reputation: 1

You can use this API to fetch the top 50,000 commits where
organization: Organisation Name

project: Project Name can be fetched using

p_url = f"https://dev.azure.com/{organization}/_apis/projects?api-version=7.1-preview.4"

repository_id: Repository ID can be fetched using

r_url = f"https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=6.1-preview.1"

Get the commit data using

url=f"https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repository_id}/commitsbatch?$top={50000}&api-version=7.1"

Make a POST request to the URL and get the response

response = requests.post(url, headers=headers)
    if response.status_code == 200:
        data = response.json()
        if "value" in data and data["value"]:
            # Extract the date of the first commit
            return data["value"][-1]["committer"]["date"] 
        else:
            return "No Commits"
    else:
        print(f"Failed to fetch commit date: {response.status_code} - {response.text}")
        return None

Upvotes: 0

Matt
Matt

Reputation: 4065

Personally, I have not leveraged the audit API extensively, so I can't really speak to the challenges of data volume and the practicality of searching through it until Microsoft offers filters for the API.

That being said, I was able to get the information utilizing the Query Audit Log API. There is some assumption that your Azure DevOps version when creating repository logged audit entries at the time. Some of the audit logging is pretty recent.

Example in PowerShell:

$AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($AzureDevOpsPAT)")) }

$uri = "https://auditservice.dev.azure.com/{organization}/_apis/audit/auditlog?api-version=6.0-preview.1"

Invoke-RestMethod -Method Get -Uri $uri -Headers $AzureDevOpsAuthenicationHeader | 
    Select-Object -ExpandProperty decoratedAuditLogEntries | 
    Where-Object { $_.actionId -eq 'Git.RepositoryCreated' } |
    Select-Object actionId, details, timestamp

Provides:

actionId              details                                           timestamp                   
--------              -------                                           ---------                   
Git.RepositoryCreated Created Git repository "Temp" in project Basic    2020-09-11T23:19:19.4007876Z
Git.RepositoryCreated Created Git repository "Testing" in project Basic 2020-09-09T21:10:56.1738383Z
Git.RepositoryCreated Created Git repository "CMMI" in project CMMI     2020-08-24T14:33:43.6040892Z
Git.RepositoryCreated Created Git repository "Basic" in project Basic   2020-08-24T14:33:05.3245162Z

Upvotes: 2

Luke Erickson
Luke Erickson

Reputation: 78

It seems the issue was with the branch naming and search criteria being viewed as a wildcard. Using searchCriteria.itemVersion.version=master in the Commits call was returning commits from any branch containing master, which gave the appearance that these were out of order when attempting to use the more obvious index values.

To those that ended up here just trying to get your first commit date on a repo, this is what worked for me:

  1. get list of all repository IDs (List - Repositories works).
  2. for each repository ID, get all the commits in the repository.
  3. Get the last commit's index (current repository's commits count - 1).
  4. the first commit will be your current commit[index].

Upvotes: 0

Related Questions