Skip to content

Instantly share code, notes, and snippets.

@stevemar
Last active August 1, 2019 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevemar/f35477bcc9e8e77ac7cb3c901c34da5a to your computer and use it in GitHub Desktop.
Save stevemar/f35477bcc9e8e77ac7cb3c901c34da5a to your computer and use it in GitHub Desktop.
import json
import re
import requests
# The public org where new repos will be created.
PUBLIC_ORG = 'foo'
# The repo where folks will request new repos for the public org in the form of a comment
GHE_REQUEST_REPO = 'https://github.mycompany.com/api/v3/repos/myorg/myrepo'
def _get_info_from_body(body):
# "body": "## Essentials\r\n\r\n* name: foo\r\n* users: stevemar, stevemart\r\n* description: this is for fun\r\n* license: apache\r\n\r\n## Tips\r\n\r\n* Repo names CANNOT have spaces.\r\n* User IDs must be from public github, if you're not sure, go to https://github.com/ and login.\r\n"
m = re.search(r'\* name:(.*)(\r\n|$)', body)
repo_name = m.group(1).strip() if m else None
repo_name = repo_name.strip() if repo_name else None
m = re.search(r'\* users:(.*)(\r\n|$)', body)
users = m.group(1).strip() if m else []
users = [x.strip() for x in users.split(',')] if users else []
m = re.search(r'\* teams:(.*)(\r\n|$)', body)
teams = m.group(1).strip() if m else []
teams = [x.strip() for x in teams.split(',')] if teams else []
m = re.search(r'\* description:(.*)(\r\n|$)', body)
description = m.group(1).strip() if m else ''
m = re.search(r'\* license:(.*)(\r\n|$)', body)
license = m.group(1).strip() if m else 'apache-2.0'
license = license.strip() if license else 'apache-2.0'
return {'repo_name': repo_name, 'users': users,
'teams': teams, 'description': description, 'license': license}
def _post_a_comment(issue_number, message, ghe_token):
# post comment on issue -- https://developer.github.com/v3/issues/comments/#create-a-comment
headers = {'Authorization': 'token %s' % ghe_token}
url = GHE_REQUEST_REPO + '/issues/' + str(issue_number) + '/comments'
payload = {'body': message}
requests.post(url, headers=headers, data=json.dumps(payload))
def main(params):
if params['comment']['body'].strip() == '/approve':
sender = params['sender']['login']
ghe_token = params['GHE_TOKEN']
issue_number = params['issue']['number']
if sender == 'stevemar':
gh_token = params['GH_PUB_TOKEN']
else:
message = 'approve comment made by unauthorized user, please wait for an authorized user to approve'
_post_a_comment(issue_number, message, ghe_token)
return {'message': message}
# get info from the issue body
body = params['issue']['body']
info = _get_info_from_body(body)
# quit if we can't get a repo name
if info['repo_name'] is None:
message = 'could not find a repo name, please provide one'
_post_a_comment(issue_number, message, ghe_token)
return {'message': message}
# TODO: quit if we can't find at least one user or one team
# set up auth headers to call github APIs
headers = {'Authorization': 'token %s' % gh_token}
# create the repo -- https://developer.github.com/v3/repos/#create
url = 'https://api.github.com/orgs/' + PUBLIC_ORG + '/repos'
payload = {
'name': info['repo_name'],
'description': info['description'],
'license_template': info['license'],
'auto_init': 'true'
}
r = requests.post(url, headers=headers, data=json.dumps(payload))
if r.status_code != 201:
message = "could not create repo: " + r.text
_post_a_comment(issue_number, message, ghe_token)
return {'message': message}
else:
message = "created repo: " + info['repo_name'] + ". now adding users and teams"
_post_a_comment(issue_number, message, ghe_token)
# TODO: actually look up the user and handle errors
# add user to collaborate -- https://developer.github.com/v3/repos/collaborators/#add-user-as-a-collaborator
for user in info['users']:
url = 'https://api.github.com/repos/' + PUBLIC_ORG + '/' + info['repo_name'] + '/collaborators/' + user
payload = {
'permission': 'admin'
}
r = requests.put(url, headers=headers, data=json.dumps(payload))
# add teams if specified, first look up team name to get id, then add the repository
for team in info['teams']:
# https://developer.github.com/v3/teams/#get-team-by-name
url = 'https://api.github.com/orgs/' + PUBLIC_ORG + '/teams/' + team
r = requests.get(url, headers=headers)
team_id = str(r.json()['id'])
# https://developer.github.com/v3/teams/#add-or-update-team-repository
url = 'https://api.github.com/teams/' + team_id + '/repos/' + PUBLIC_ORG + '/' + info['repo_name']
payload = {
'permission': 'admin'
}
r = requests.put(url, headers=headers, data=json.dumps(payload))
# invite users to the org -- https://developer.github.com/v3/orgs/members/#add-or-update-organization-membership
for user in info['users']:
url = 'https://api.github.com/orgs/' + PUBLIC_ORG + '/memberships/' + user
payload = {
'role': 'member'
}
r = requests.put(url, headers=headers, data=json.dumps(payload))
# post comment on issue -- https://developer.github.com/v3/issues/comments/#create-a-comment
message = 'created repo https://github.com/%s/%s with admins (%s) and teams (%s)' % (PUBLIC_ORG, info['repo_name'], info['users'], info['teams'])
_post_a_comment(issue_number, message, ghe_token)
return {'message': message}
else:
return {'message': 'not approved yet -- ignoring'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment