-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathgithub.py
More file actions
50 lines (40 loc) · 1.82 KB
/
Copy pathgithub.py
File metadata and controls
50 lines (40 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
github.py
Provide a couple of useful methods for github actions to interact with pull requests
SPDX-FileCopyrightText: 2020 Birger Schacht
SPDX-License-Identifier: AGPL-3.0-or-later
"""
import os
import requests
class Github:
""" Github class:
contains a request session object that holds the authorization token
"""
session = None
api = 'https://api.github.com/'
def __init__(self):
self.github_token = os.getenv('GITHUB_TOKEN', None)
self.github_repository = os.getenv('GITHUB_REPOSITORY', None)
self.github_ref = os.getenv('GITHUB_REF', None)
self.pr_id = int(self.github_ref.split('/')[2])
if not self.github_token:
raise AttributeError('GITHUB_TOKEN is not set.')
self.session = requests.Session()
self.session.headers['Authorization'] = 'token %s' % self.github_token
def get_reviews(self):
""" Get a list of reviews on a Github pull request as json object """
reviews = self.session.get(self.api + f'repos/{self.github_repository}/pulls/{self.pr_id}/reviews')
reviews.raise_for_status()
return reviews.json()
def update_review(self, review_id, body):
""" Update a review given by `review_id` and set its body to `body` """
payload = {'body': body}
resp = self.session.put(self.api + f'repos/{self.github_repository}/pulls/{self.pr_id}/reviews/{review_id}', json=payload)
resp.raise_for_status()
return resp.json()
def post_review(self, body):
""" Post a pull request review containing `body` and requesting changes """
payload = {'body': body, 'event': "REQUEST_CHANGES"}
resp = self.session.post(self.api + f'repos/{self.github_repository}/pulls/{self.pr_id}/reviews', json=payload)
resp.raise_for_status()
return resp.json()