如何通过 GitHub API 以语法方式启用我拥有的给定存储库的问题跟踪器?
How to grammatically enable the issue tracker of a given repository I own through the 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
参考文献:
- How to retrieve the list of all github repositories of a person?
- https://github.com/settings/tokens GitHub 具有完整存储库访问权限的令牌
我在 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"
}
然后,我将 "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()
新引用:
- Escape double quotes for JSON in Python
- Github API v3 doesn't show all user repositories
我有一堆存储库分支,我想启用它们的所有问题跟踪器。我不确定为什么,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
参考文献:
- How to retrieve the list of all github repositories of a person?
- https://github.com/settings/tokens GitHub 具有完整存储库访问权限的令牌
我在 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"
}
然后,我将 "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()
新引用:
- Escape double quotes for JSON in Python
- Github API v3 doesn't show all user repositories