0

I am using google cloud build for CI/CD purpose, in which I need to give access for specific repositories (can't use All repositories option). Is there any way to provide access by repository wise through python code. If not possible through python is there any alternative to this requirement.

Thanks,

Raghunath.

Raghunath
  • 594
  • 4
  • 12
  • 27
  • You want to check out only one project with Cloud Build, and not have access to all? What is the relation with python? Can you describe what do you want to achieve? and why? – guillaume blaquiere Apr 08 '20 at 20:36
  • I create repository using Python based on a template. Once repository is created I can give access using Google Cloud UI (manual) but, I want to give repository access to Cloud build by automated process rather than manual. Due to this reason I asked for python process if possible. – Raghunath Apr 09 '20 at 04:55
  • AFAIK, the current process is an OAuth flow for authorizing Cloud Build to browse the github repo on your behalf. I don't know how to perform this in machine to machine world. maybe use cloud repository instead? – guillaume blaquiere Apr 09 '20 at 08:16

1 Answers1

0

When I checked with GitHub support, they have shared me below links.

To get repository id -- https://developer.github.com/v3/repos/#get-a-repository

To get installations details -- https://developer.github.com/v3/orgs/#list-installations-for-an-organization

To add repository to an installation -- https://developer.github.com/v3/apps/installations/#add-repository-to-installation

I used these links to create below mentioned code which has helped me to implement the desired requirement.

# 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)
Raghunath
  • 594
  • 4
  • 12
  • 27