python-3.x - 如何通过 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
参考:
- 如何检索一个人的所有 github 存储库的列表?
- https://github.com/settings/tokens具有完整存储库访问权限的 GitHub 令牌
解决方案
我在如何通过他们的 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()
新参考:
推荐阅读
- laravel-8 - Laravel 8 本地化:缺少 [Route: password.reset] [URI: {language}/password/reset/{token}] [缺少参数:language] 的必需参数
- nginx - Nginx 返回 403 禁止 http 不允许
- laravel - laravel 连接到谷歌驱动器文件夹的错误
- c++ - 使用临时初始化成员时,如何确保对临时构造函数的单次调用?
- python - 使用特定参数在 python 中运行 shell 脚本
- linker - RISC-V 链接器松弛不适用于“尾”伪指令
- java - 如何在指定文件夹中安装 Homebrew?例如,在主目录中
- javascript - 如何仅在提交时进行浏览器默认表单验证?
- python-3.x - 计算networkx中单个节点的中心性的最佳方法是什么?
- javascript - Flickity 轮播不工作时是用js生成的