首页 > 解决方案 > 如何通过 GitHub API 以语法方式启用我拥有的给定存储库的问题跟踪器?

问题描述

我有一堆存储库分支,我想启用他们所有的问题跟踪器。我不知道为什么,GitHub 默认禁用它们,我在分叉时忘记启用它们。

现在将太多的工作使他们的问题跟踪器一一启用,然后,我虽然可以编写一个程序来执行此操作。现在,我使用以下代码来获取我拥有的所有存储库的列表:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import shlex
import json
import subprocess

current_directory = os.path.dirname( os.path.realpath(__file__) )
print( 'directory walk %s', current_directory )

token = "Authorization: token mynicetoken102312312541230240021470300250230"
user_name = "myusername"

def run_command(absolute_path, command_name):
    command = shlex.split( command_name )
    print( 'command: %s' % command )

    command_line_interface = subprocess.Popen( command, stdout=subprocess.PIPE, cwd=absolute_path )
    output = command_line_interface.communicate()[0]

    print( "\n%s" % output.decode('utf-8') )
    return output

def main():
    result = run_command( current_directory, "curl -H '%s' https://api.github.com/users/%s/repos" % ( token, user_name ) )
    result_json = json.loads( result.decode('utf-8') )

    for repository_data in result_json:
        repository_full_name = repository_data['full_name']
        print( "Processing{:s}".format( repository_full_name ) )

        # Now, what do?
        run_command( current_directory, "curl -H '%s' https://api.github.com/%s/misterX" % ( token, repository_full_name  ) )

if __name__ == "__main__": main()

我认为唯一缺少的是完整的最后一行:

# Now, what do?
run_command( current_directory, "curl -H '%s' https://api.github.com/%s/misterX" % ( token, repository_full_name  ) )

在找到如何通过他们的 API 重命名 GitHub 存储库之后?我设法构建以下代码:

# Now, what do?
full_command = \
r"""
    curl
        -H "Authorization: Token %s"
        -H "Content-Type: application/json"
        -H "Accept: application/json"
        -X PATCH
        --data '{ "has_issues": true }'
        https://api.github.com/repos/:%s
""" % ( token, repository_full_name )

print( 'full_command: %s' % full_command )
run_command( current_directory, full_command )

但 GitHub 说:

{
  "message": "Not Found",
  "documentation_url": "https://developer.github.com/v3/repos/#edit"
}

他们的 API 页面没有多大帮助:https ://developer.github.com/v3/repos/#edit


参考:

  1. 如何检索一个人的所有 github 存储库的列表?
  2. https://github.com/settings/tokens具有完整存储库访问权限的 GitHub 令牌

标签: python-3.xcurlgithubgithub-api

解决方案


我在如何通过他们的 API 重命名 GitHub 存储库?错了。它正在使用https://api.github.com/repos/:owner/repo,但它应该是 https://api.github.com/repos/owner/repo。修复之后,GitHub 一直在说:

{
  "message": "Validation Failed",
  "errors": [
    {
      "resource": "Repository",
      "code": "custom",
      "field": "name",
      "message": "name is too short (minimum is 1 character)"
    }
  ],
  "documentation_url": "https://developer.github.com/v3/repos/#edit"
}

然后,我添加"name": "repository_name"到 json 中,它起作用了。这是这个新代码:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import os
import shlex
import json
import subprocess
import shutil

"""
Iterates through all repositories from a user and enable the issue tracker.
"""

# GitHub token with full repository access
# https://github.com/settings/tokens
token = "8217398127859182039802175098213389019766"
user_name = "username"

current_directory = os.path.dirname( os.path.realpath(__file__) )
print( 'directory walk %s' % current_directory )

# The maximum count of repositories to to process when calling this batch script.
maximum_process_limit = 1000

def run_command(absolute_path, command_name):
    command = shlex.split( command_name )
    print( 'command: %s' % command )

    command_line_interface = subprocess.Popen( 
           command, stdout=subprocess.PIPE, cwd=absolute_path )

    output = command_line_interface.communicate()[0]

    # print( "%s" % output )
    # print( "\n%s" % output.decode('utf-8') )
    return output

def main():
    page_index = 1

    while process_repositories_page( page_index ):
        page_index += 1

def process_repositories_page(page_index):
    global maximum_process_limit
    items_per_page = 100

    repositories_text = run_command( current_directory,
            "curl -H '%s' https://api.github.com/users/%s/repos?per_page=%s&page=%s" % ( 
            token, user_name, items_per_page, page_index ) )

    repositories_json = json.loads( repositories_text.decode('utf-8') )

    for repository_data in repositories_json:
        print( "Processing repository: %s" % repository_data['full_name'] )

        if maximum_process_limit <= 0: return
        maximum_process_limit -= 1

        full_command = \
        r"""
            curl
                -H "Authorization: Token {token}"
                -H "Content-Type: application/json"
                -H "Accept: application/json"
                -X PATCH
                --data '{data}'
                https://api.github.com/repos/{full_name}
        """.format(
                token=token,
                data=json.dumps(
                    {
                        "name": repository_data['name'],
                        "has_issues": True
                    }
                ),
                full_name=repository_data['full_name']
            )
        print( 'full_command: %s' % full_command )

        result = run_command( current_directory, full_command )
        print( 'result: %s' % result.decode('utf-8') )

    return len( repositories_json ) == items_per_page

if __name__ == "__main__":
    main()

新参考:

  1. 以编程方式为存储库启用 Github Pages
  2. 在 Python 中为 JSON 转义双引号
  3. Github API v3 不显示所有用户存储库

推荐阅读