【问题标题】:Using GraphQL and the gql package - updating GitHub Repository Branch Protection Rule Setting使用 GraphQL 和 gql 包 - 更新 GitHub 存储库分支保护规则设置
【发布时间】:2020-10-06 15:10:36
【问题描述】:

我一直在尝试通过 Python 集成 GitHub 存储库设置,并且使用 GitHub 包一切顺利。不幸的是,该包不包含任何创建新的 repo 分支保护规则的功能,所以我不得不切换到 gql 包。我很难理解这个包裹周围的术语,经过数周的在线搜索,我没有找到任何关于这个主题的内容。

目标是选择到我的存储库的传输、创建客户端、设置查询并运行客户端。我相信前两个设置正确,这是我认为我可能搞砸的查询。我一直在使用这两个链接来指导我:https://github.community/t/rest-api-v3-wildcard-branch-protection/13593/8https://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)

这会导致以下错误:

data = asyncio.run(client.execute_async(query = query,variables = variables))

TypeError:execute_async() 缺少 1 个必需的位置参数:'document'

【问题讨论】:

    标签: python github gql


    【解决方案1】:

    execute_async 需要等待,因为它是一个异步函数。如果您查看python-graphql-client readme,这里有一个如何操作的示例。

    # Asynchronous request
    import asyncio
    
    data = asyncio.run(client.execute_async(query=query, variables=variables))
    print(data)  # => {'data': {'country': {'code': 'CA', 'name': 'Canada'}}}
    

    【讨论】:

    • 您好!感谢您的答复!因此,我尝试更新下面发布的代码,但现在出现了一个新错误:“RuntimeError:asyncio.run() 无法从正在运行的事件循环中调用”我更新了上面的问题以进行澄清。抱歉,我是 Stackoverflow 的新手。
    猜你喜欢
    • 2021-10-05
    • 2021-12-05
    • 2022-07-08
    • 2013-03-16
    • 2018-08-02
    • 1970-01-01
    • 2022-01-13
    • 2019-06-10
    • 1970-01-01
    相关资源
    最近更新 更多