How can I programmatically detect inactive branches in GitHub repositories?

女生的网名这么多〃 提交于 2019-12-05 00:26:50

问题


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?


回答1:


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@gmail.com",
        "date": "2014-05-03T21:28:26Z"
      },
      "committer": {
        "name": "nulltoken",
        "email": "email@gmail.com",
        "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",

[...]



回答2:


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


来源:https://stackoverflow.com/questions/25505551/how-can-i-programmatically-detect-inactive-branches-in-github-repositories

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!