python cleanup-forks-from-github.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python cleanup-forks-from-github.py相关的知识,希望对你有一定的参考价值。

#! /usr/bin/python3

import argparse
from operator import attrgetter
import os
import sys
from github import Github


def main():
    desc = 'List and delete fork repositories in a GitHub org (env: GH_TOKEN)'
    parser = argparse.ArgumentParser(description=desc)
    parser.add_argument('--github-token',
                        help='GitHub API Token',
                        dest='token',
                        default=os.environ.get('GH_TOKEN', None),)
    parser.add_argument('-d,--delete',
                        help='delete forks in organization',
                        dest='delete',
                        action='store_true')
    parser.add_argument('--org',
                        help='github organization',
                        dest='org',
                        required=True)
    args = parser.parse_args()
    if not args.token:
        sys.exit(parser.print_usage())

    g = Github(args.token)
    forks = retrieve_forks(g, args.org)
    if args.delete:
        answer = input('please confirm deletion! [y/n]\n')
        if answer == 'yes' or answer == 'y':
            delete_forks(forks)
    else:
        list_forks(forks)


def list_forks(forks):
    for fork in forks:
        print(fork.name)


def delete_forks(forks):
    for fork in forks:
        fork.delete()


def retrieve_forks(g, org):
    org = g.get_organization(org)
    repos = org.get_repos('forks')
    forks = [repo for repo in repos]
    return sorted(forks, key=attrgetter('name'))

    
if __name__ == '__main__':
    main()

以上是关于python cleanup-forks-from-github.py的主要内容,如果未能解决你的问题,请参考以下文章

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python

python初识