python - 使用 GraphQL 和 gql 包 - 更新 GitHub 存储库分支保护规则设置
问题描述
我一直在尝试通过 Python 集成 GitHub 存储库设置,并且使用 GitHub 包一切顺利。不幸的是,该包不包含任何创建新的 repo 分支保护规则的功能,所以我不得不切换到 gql 包。我很难理解这个包裹周围的术语,经过数周的在线搜索,我没有发现任何关于这个主题的内容。
目标是选择到我的仓库的传输,创建客户端,设置查询并运行客户端。我相信前两个设置正确,这是我认为我可能搞砸的查询。我一直在使用这两个链接来指导我:https ://github.community/t/rest-api-v3-wildcard-branch-protection/13593/8和https://docs.github.com/en/free -pro-team@latest/graphql/reference/input-objects#createbranchprotectionruleinput
# Code:
# Select your transport with a defined url endpoint
transport = AIOHTTPTransport(url=myurl,
headers={'Authorization': 'mytoken'}) # I use my url and token
# Create a GraphQL client using the defined transport
client = Client(transport = transport, fetch_schema_from_transport = True)
方法:
# Provide a GraphQL query
def test(client):
query = gql("""
mutation($input: CreateBranchProtectionRuleInput!) {
createBranchProtectionRule(input: $variables) {
branchProtectionRule {
id
}
}
}
"""
)
variables = {"repositoryId": "123456", "pattern": "release/*"}
# Execute the query on the transport
data = asyncio.run(client.execute_async(query,variables))
print(data)
test(client)
这会导致以下错误:
数据 = asyncio.run(client.execute_async(查询 = 查询,变量 = 变量))
类型错误:execute_async() 缺少 1 个必需的位置参数:'document'
解决方案
execute_async
需要等待,因为它是一个异步函数。如果您查看python-graphql-client 自述文件,则会有一个示例说明如何执行此操作。
# Asynchronous request
import asyncio
data = asyncio.run(client.execute_async(query=query, variables=variables))
print(data) # => {'data': {'country': {'code': 'CA', 'name': 'Canada'}}}
推荐阅读
- reactjs - 根据道具值显示组件反应
- google-cloud-platform - 无法开始在 Web UI 上训练我的 GCP AutoML 实体提取模型
- asp.net-core - Blazor 参数是否会带来额外的开销?
- clickhouse - Clickhouse - 输出每个数组项及其索引/位置
- r - R代码将列转为行,将行转为列
- javascript - Firebase Node.js 本地测试凭证与功能
- html - 每当屏幕在引导程序中变为中等时,我的列就会消失
- java - Spring boot:将类转换为bean
- javascript - 在使用 javascript 呈现的选择字段上出错,同时在烧瓶中提交表单
- sqlite - SQLite 查找来自用户输入的数据位于哪个表