|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import random |
| 5 | +import sys |
| 6 | + |
| 7 | +import requests |
| 8 | + |
| 9 | +active_label = 'pull-request-has-preview' |
| 10 | + |
| 11 | +logging.basicConfig(level=logging.INFO) |
| 12 | +logger = logging.getLogger(__name__) |
| 13 | + |
| 14 | + |
| 15 | +class Status(object): |
| 16 | + SUCCESS = 0 |
| 17 | + FAIL = 1 |
| 18 | + NEUTRAL = 0 |
| 19 | + |
| 20 | + |
| 21 | +def request(url, method_name, data=None, json_data=None, ignore_body=False): |
| 22 | + github_token = os.environ.get('GITHUB_TOKEN') |
| 23 | + |
| 24 | + kwargs = { |
| 25 | + 'headers': { |
| 26 | + 'Authorization': 'token {}'.format(github_token), |
| 27 | + 'Accept': 'application/vnd.github.machine-man-preview+json' |
| 28 | + } |
| 29 | + } |
| 30 | + method = getattr(requests, method_name) |
| 31 | + |
| 32 | + logger.info('Issuing request: {} {}'.format(method_name.upper(), url)) |
| 33 | + if json_data is not None or data is not None: |
| 34 | + kwargs['json'] = json_data |
| 35 | + kwargs['data'] = data |
| 36 | + |
| 37 | + resp = method(url, **kwargs) |
| 38 | + |
| 39 | + resp.raise_for_status() |
| 40 | + |
| 41 | + if not ignore_body: |
| 42 | + return resp.json() |
| 43 | + |
| 44 | + |
| 45 | +def resource_exists(url): |
| 46 | + try: |
| 47 | + request(url, 'get', ignore_body=True) |
| 48 | + except requests.HTTPError as exception: |
| 49 | + if exception.response.status_code == 404: |
| 50 | + return False |
| 51 | + raise |
| 52 | + |
| 53 | + return True |
| 54 | + |
| 55 | + |
| 56 | +class GitHub(object): |
| 57 | + def __init__(self, api_root, owner, repo): |
| 58 | + self.api_root = api_root |
| 59 | + self.owner = owner |
| 60 | + self.repo = repo |
| 61 | + |
| 62 | + def is_collaborator(self, login): |
| 63 | + return resource_exists( |
| 64 | + '{}/repos/{}/{}/collaborators/{}'.format( |
| 65 | + self.api_root, self.owner, self.repo, login |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | + def ref_exists(self, ref): |
| 70 | + return resource_exists( |
| 71 | + '{}/repos/{}/{}/git/refs/{}'.format( |
| 72 | + self.api_root, self.owner, self.repo, ref |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + def create_ref(self, ref, sha): |
| 77 | + data = { |
| 78 | + 'ref': 'refs/{}'.format(ref), |
| 79 | + 'sha': sha |
| 80 | + } |
| 81 | + url = '{}/repos/{}/{}/git/refs'.format( |
| 82 | + self.api_root, self.owner, self.repo |
| 83 | + ) |
| 84 | + |
| 85 | + logger.info('Creating ref "{}" as {}'.format(ref, sha)) |
| 86 | + |
| 87 | + request(url, 'post', json_data=data) |
| 88 | + |
| 89 | + def update_ref(self, ref, sha): |
| 90 | + data = { |
| 91 | + 'force': True, |
| 92 | + 'sha': sha |
| 93 | + } |
| 94 | + url = '{}/repos/{}/{}/git/refs/{}'.format( |
| 95 | + self.api_root, self.owner, self.repo, ref |
| 96 | + ) |
| 97 | + |
| 98 | + logger.info('Updating ref "{}" as {}'.format(ref, sha)) |
| 99 | + |
| 100 | + request(url, 'patch', json_data=data) |
| 101 | + |
| 102 | + def delete_ref(self, ref): |
| 103 | + url = '{}/repos/{}/{}/git/refs/{}'.format( |
| 104 | + self.api_root, self.owner, self.repo, ref |
| 105 | + ) |
| 106 | + |
| 107 | + logger.info('Deleting ref "{}"'.format(ref)) |
| 108 | + |
| 109 | + try: |
| 110 | + request(url, 'delete', ignore_body=True) |
| 111 | + except requests.HTTPError as exception: |
| 112 | + if exception.response.status_code != 404: |
| 113 | + raise |
| 114 | + |
| 115 | + logger.info( |
| 116 | + 'Attempted to delete non-existent ref: {}'.format(ref) |
| 117 | + ) |
| 118 | + |
| 119 | + def set_ref(self, ref, sha): |
| 120 | + if self.ref_exists(ref): |
| 121 | + self.update_ref(ref, sha) |
| 122 | + else: |
| 123 | + self.create_ref(ref, sha) |
| 124 | + |
| 125 | + def add_label(self, pr_number, label_name): |
| 126 | + data = { |
| 127 | + 'labels': [label_name] |
| 128 | + } |
| 129 | + url = '{}/repos/{}/{}/issues/{}/labels'.format( |
| 130 | + self.api_root, self.owner, self.repo, pr_number |
| 131 | + ) |
| 132 | + |
| 133 | + logger.info('Adding label') |
| 134 | + |
| 135 | + request(url, 'post', json_data=data) |
| 136 | + |
| 137 | + def remove_label(self, pr_number, label_name): |
| 138 | + url = '{}/repos/{}/{}/issues/{}/labels/{}'.format( |
| 139 | + self.api_root, self.owner, self.repo, pr_number, label_name |
| 140 | + ) |
| 141 | + |
| 142 | + logger.info('Removing label') |
| 143 | + |
| 144 | + try: |
| 145 | + request(url, 'delete') |
| 146 | + except requests.HTTPError as exception: |
| 147 | + if exception.response.status_code != 404: |
| 148 | + raise |
| 149 | + |
| 150 | + logger.info( |
| 151 | + 'Attempted to remove non-existent label: {}'.format(label_name) |
| 152 | + ) |
| 153 | + |
| 154 | + |
| 155 | +def main(api_root): |
| 156 | + with open(os.environ['GITHUB_EVENT_PATH']) as handle: |
| 157 | + event = json.load(handle) |
| 158 | + logger.info(json.dumps(event, indent=2)) |
| 159 | + |
| 160 | + owner, repo = os.environ['GITHUB_REPOSITORY'].split('/', 1) |
| 161 | + github = GitHub(api_root, owner, repo) |
| 162 | + sha = '8be01b6e0d191a641f45ad59e83718155775921d' |
| 163 | + name = 'random-%d' % (random.random() * 1000) |
| 164 | + github.set_ref(name, sha) |
| 165 | + |
| 166 | + return Status.SUCCESS |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == '__main__': |
| 170 | + code = main(sys.argv[1]) |
| 171 | + assert isinstance(code, int) |
| 172 | + sys.exit(code) |
0 commit comments