项目作者: floe

项目描述 :
Allows bulk editing of Github repositories
高级语言: Python
项目地址: git://github.com/floe/github-bulk-editor.git
创建时间: 2019-02-06T08:59:55Z
项目社区:https://github.com/floe/github-bulk-editor

开源协议:GNU General Public License v3.0

下载


github-bulk-editor

I needed to transfer a large bunch of repositories from one organization to another one. I had already found https://github.com/ahmadnassri/github-bulk-transfer, but that looked quite brittle, and I am honestly not a big fan of node.js projects anyway (cough left-pad cough).

So I put this tool together over the course of a couple of evenings, which allows you to:

  • fetch a list of items from the Github API - currently supported:
    • all repos the current user has permission for
    • all teams in an organization
    • all members of an organization
  • and execute an API command for a selected subset of the items - currently supported:
    • transfer repos to another organization
    • remove teams
    • remove members

Screenshot

To use it, you need to add your own account and access token from https://github.com/settings/tokens in line 10 (https://github.com/floe/github-bulk-editor/blob/master/github-bulk-editor.py#L10).

Currently, the only well-tested actions are a) fetching a list of repos you have access to, and b) transferring those repos to another user/org, because that is the functionality I needed right now.

However, I’ve tried to keep the internals as generic as possible (see https://github.com/floe/github-bulk-editor/blob/master/github-bulk-editor.py#L14-L27):

  1. # command title: [ GET url, JSON field to extract from result ]
  2. fetch_cmds = {
  3. "Get all repositories": [ "https://api.github.com/user/repos", "full_name"],
  4. "Get all teams": [ "https://api.github.com/orgs/mmbuw/teams", "slug" ], # TODO: make org name editable
  5. "Get all members": [ "https://api.github.com/orgs/mmbuw/members", "login" ], # TODO: make org name editable
  6. }
  7. # command title: [ request function, url, parameters ] (will be passed through format(name,id), hence the double braces)
  8. action_cmds = {
  9. "Transfer repository": [ "github_post", "https://api.github.com/repos/{0}/transfer", '{{ "new_owner": "{0}", "team_ids": [] }}' ],
  10. "Delete team": [ "github_delete", "https://api.github.com/teams/{1}", "" ],
  11. "Remove member": [ "github_delete", "https://api.github.com/orgs/mmbuw/members/{0}", "" ], # TODO: fixed org name
  12. }
  13. # ...
  14. # post a payload to url
  15. def github_post(url,payload):
  16. r = requests.post(url,auth=cred,headers=headers,data=payload,allow_redirects=True)
  17. return r

In theory, you should be able to just add new entries to fetch_cmds and action_cmds to support new API calls, and perhaps add something like another github_put (or whatever) function.

P.S. If you’re wondering why I used requests + json (as also suggested at https://stackoverflow.com/a/10626326/838719) instead of PyGithub or similar, none of the existing Python libraries actually support the “Transfer Repo” command. Duh.