我正在将Google Cloud build用于CI / CD,我需要在其中提供对特定存储库的访问权限(不能使用“所有存储库”选项)。有什么方法可以通过python代码按仓库明智地提供访问。如果无法通过python进行,则除了此要求之外,还有其他选择。
谢谢
Raghunath。
答案 0 :(得分: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)