如何使用pythonapi在azuredevops中添加分支策略

2024-03-29 11:08:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个脚本(在python中),通过以下示例在Azure DevOps中设置分支策略: https://docs.microsoft.com/en-us/rest/api/azure/devops/policy/configurations/create?view=azure-devops-rest-5.1#approval-count-policy

代码如下:

policyClient = connection.clients.get_policy_client()

jsonSettings = {
    'requiredReviewerIds': [
        '2ad77975-c0fc-471a-a161-3452b1ec842d', 
        'cf0931e8-2aa6-42b3-9597-3522689c5190'
    ], 
    'scope': [
        {
            'refName': 'refs/heads/master',
            'matchKind': 'Exact',
            'repositoryId': '70c7c55c-8fd0-44c4-a175-db7093e38ff2'
        }
    ]
}

policyConfiguration = PolicyConfiguration(
    is_enabled=True,
    is_blocking=True,
    settings=json.dumps(jsonSettings)
)

policyClient.create_policy_configuration(configuration=policyConfiguration,project="demo")

然而,答案是:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/azure/devops/released/policy/policy_client.py", line 46, in create_policy_configuration
    content=content)
  File "/usr/local/lib/python3.7/site-packages/azure/devops/client.py", line 104, in _send
    response = self._send_request(request=request, headers=headers, content=content, media_type=media_type)
  File "/usr/local/lib/python3.7/site-packages/azure/devops/client.py", line 68, in _send_request
    self._handle_error(request, response)
  File "/usr/local/lib/python3.7/site-packages/azure/devops/client.py", line 256, in _handle_error
    raise AzureDevOpsClientRequestError(wrapped_exception.message)
azure.devops.exceptions.AzureDevOpsClientRequestError: Error setting value to 'Settings' on 'Microsoft.TeamFoundation.Policy.WebApi.PolicyConfiguration'.

我做错什么了吗?你知道吗

谢谢 河流


Tags: inpyclientrequestlibpackagesusrlocal
1条回答
网友
1楼 · 发布于 2024-03-29 11:08:11
# Create policy Client
policyClient = connection.clients.get_policy_client()


### Master branch minimum reviewer
policyMasterBranchMinimumReviewersConfiguration = PolicyConfiguration(
    is_enabled=True,
    is_blocking=True,
    settings={
        "allowDownvotes": False,
        "creatorVoteCounts": False,
        "minimumApproverCount": 2,
        "resetOnSourcePush": False,
        "scope": [
            {
                "matchKind": "Exact",
                "refName": "refs/heads/master",
                "repositoryId": None
            }
        ]
    },
    type= {
      "displayName": "Minimum number of reviewers",
      "id": "fa4e907d-c16b-4a4c-9dfa-4906e5d171dd",
      "url": "https://devops.momenta.works/Momenta/beedef84-314a-4255-833b-405409b7526c/_apis/policy/types/fa4e907d-c16b-4a4c-9dfa-4906e5d171dd"
    },
)

policyClient.create_policy_configuration(configuration=policyMasterBranchMinimumReviewersConfiguration,project="demo")

如上所示,我们不需要对设置执行json.dumps。你知道吗

相关问题 更多 >