Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically detect inactive branches in GitHub repositories?

I have a dozen repositories in the GitHub repository. The repository structure looks like below:

   + project1 
       +------- trunk
       +------- tags
       +------- branches
       + ------- releases
   + project2
       ....

Our policy requires any active branch to be deleted after 30 days of inactivity. However, there is no automatic way of detecting such an inactive branch. Occasionally, I have some inactive branch that survive past the 30-day mark.

Is there a script to list branches, as well as their last commit date in all GitHub repositories?

Edit1 -- Also is there a way of getting how many organizations and what projects they house through the API?

like image 744
Scooby Avatar asked Aug 26 '14 12:08

Scooby


2 Answers

The GitHub Repository API should be able to help you with that.

Listing branches

  • Syntax: GET /repos/:owner/:repo/branches
  • Sample: https://api.github.com/repos/libgit2/libgit2sharp/branches

Getting details about a branch

  • Syntax: GET /repos/:owner/:repo/branches/:branch
  • Sample: https://api.github.com/repos/libgit2/libgit2sharp/branches/coverity

This call method exposes the tip of the branch (ie. the latest commit), from which you can retrieve the commit date. Based on that, you may be able to evaluate the "activity" of each branch.

Below a sample output of a branch detail

{
  "name": "coverity",
  "commit": {
    "sha": "f341f3a1276cbec3f6ee9d02264bd4453ca20835",
    "commit": {
      "author": {
        "name": "nulltoken",
        "email": "[email protected]",
        "date": "2014-05-03T21:28:26Z"
      },
      "committer": {
        "name": "nulltoken",
        "email": "[email protected]",
        "date": "2014-05-09T11:10:01Z"
      },
      "message": "Configure Coverity Scan hook for Travis",
      "tree": {
        "sha": "a5092e975145b96356df6b57cbf50e2d8c6140f8",
        "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/trees/a5092e975145b96356df6b57cbf50e2d8c6140f8"
      },
      "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/commits/f341f3a1276cbec3f6ee9d02264bd4453ca20835",
      "comment_count": 0
    },
    "url": "https://api.github.com/repos/libgit2/libgit2sharp/commits/f341f3a1276cbec3f6ee9d02264bd4453ca20835",

[...]
like image 97
nulltoken Avatar answered Oct 03 '22 06:10

nulltoken


If you don't mind python below is a code snippet that lists inactive branches of a bare repository:

#!/bin/env python3
import pygit2, os, datetime

repo = pygit2.Repository(pygit2.discover_repository(os.getcwd()))
time_now = datetime.datetime.now()
for branch in (repo.lookup_branch(b) for b in repo.listall_branches()):
    last_commit = branch.get_object()
    commit_time = datetime.datetime.fromtimestamp(last_commit.commit_time)
    age = time_now - commit_time
    if age > datetime.timedelta(days=30):
        print("{} {} {}".format(last_commit.author.email, branch.branch_name, commit_time))

Or a shell script version which deletes branches which are older than 100 days:

git for-each-ref --sort=committerdate refs/ --format='%(committerdate:raw) %(refname:short)' | awk "\$1 < $(date -d "-100 day" "+%s") {print(\$3)}" | xargs git branch -D
like image 38
Uwe Brandt Avatar answered Oct 03 '22 06:10

Uwe Brandt