如何通过GitHub API语法启用我拥有的给定存储库的问题跟踪器?

时间:2018-12-10 01:49:02

标签: python-3.x curl github 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  ) )

找到How do I rename a GitHub repository via their API?之后,我设法构建以下代码:

# 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. How to retrieve the list of all github repositories of a person?
  2. https://github.com/settings/tokens具有完整存储库访问权限的GitHub令牌

1 个答案:

答案 0 :(得分:0)

我在How do I rename a GitHub repository via their API?上使用的答案是错误的。它使用的是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"
}

然后,我在json中添加了"name": "repository_name",并且可以正常工作。这是新代码:

#!/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. Programmatically enable Github Pages for a repository
  2. Escape double quotes for JSON in Python
  3. Github API v3 doesn't show all user repositories