0

我正在使用 google cloud build 用于 CI/CD 目的,其中我需要授予对特定存储库的访问权限(不能使用所有存储库选项)。有没有办法通过 python 代码提供存储库的访问权限。如果无法通过 python 实现,是否有任何替代此要求的方法。

谢谢,

拉古纳特。

4

1 回答 1

0

当我检查 GitHub 支持时,他们在下面的链接中分享了我。

获取存储库 ID - https://developer.github.com/v3/repos/#get-a-repository

要获取安装详细信息 - https://developer.github.com/v3/orgs/#list-installations-for-an-organization

将存储库添加到安装 - https://developer.github.com/v3/apps/installations/#add-repository-to-installation

我使用这些链接创建了下面提到的代码,这些代码帮助我实现了所需的要求。

# Header to use in request
header = dict()
header['Authorization'] = 'token %s' % personal_token
header['Accept'] = 'application/vnd.github.machine-man-preview+json'

# Fetching repository id
url = f'https://api.github.com/repos/{organization_name}/{repo_name}'
output = requests.get(url, headers=header)
repo_id = output.json()['id']

# Adding repository to Google Cloud build installation
url = f'https://api.github.com/user/installations/{gcb_installation_id}/repositories/{repo_id}'
output = requests.put(url, headers=header)
于 2020-07-03T08:17:51.777 回答