def test_that_empty_request_is_not_valid(self, signature_mock): from swaglyrics_backend.issue_maker import app from swaglyrics_backend.utils import validate_request with app.test_client() as c: req = c.post() with self.assertRaises(ImATeapot): validate_request(req)
def test_that_not_valid_signature_aborts_code(self): from swaglyrics_backend.issue_maker import app from swaglyrics_backend.utils import validate_request with app.test_client() as c: req = c.post() req.headers = self.github_headers with self.assertRaises(ImATeapot): validate_request(req)
def test_that_request_is_valid(self, signature_mock): from swaglyrics_backend.issue_maker import app with app.test_client() as c: req = c.post(json=self.github_json) req.status_code = 200 req.headers = self.github_headers from swaglyrics_backend.utils import validate_request self.assertIsNotNone(validate_request(req))
def github_webhook(): """ `github_webhook` function handles all notification from GitHub relating to the org. Documentation for the webhooks can be found at https://developer.github.com/webhooks/ """ if request.method != 'POST': return 'OK' else: not_relevant = "Event type not unsupported song issue closed." event = request.headers.get('X-GitHub-Event') # type of event payload = validate_request(request) # Respond to ping as 200 OK if event == "ping": return json.dumps({'msg': 'pong'}) elif event == "issues": try: label = payload['issue']['labels'][0]['name'] # should be unsupported song for our purposes repo = payload['repository']['name'] # should be from the SwagLyrics for Spotify repo except IndexError: return not_relevant """ If the issue is concerning the `SwagLyrics-For-Spotify repo, the issue is being closed and the issue had the tag `unsupported song` then remove line from unsupported.txt """ if payload[ 'action'] == 'closed' and label == 'unsupported song' and repo == 'SwagLyrics-For-Spotify': title = payload['issue']['title'] title = wdt.match(title) song, artist = title.groups() logging.info(f'{song} by {artist} is to be deleted.') cnt = del_line(song, artist) return f'Deleted {cnt} instances from unsupported.txt' elif event == "issue_comment": # process comment commands if payload['action'] != "created" and payload['repository'][ 'name'] != 'SwagLyrics-For-Spotify': return json.dumps( {'msg': 'Not relevant issue comment create; ignoring'}) comment = payload['comment'] if comment['user']['id'] == 27063113 and comment[ 'author_association'] == 'MEMBER': # id of @aadibajpai if body := stp.match(comment['body']): cmd, stripper = body.groups() if cmd == "add": title = wdt.match(payload['issue']['title']) song, artist = title.groups() add_stripper_to_db(song, artist, stripper) return f"Added {stripper=} for {song} by {artist} to db successfully" else:
def github_webhook(): """ `github_webhook` function handles all notification from GitHub relating to the org. Documentation for the webhooks can be found at https://developer.github.com/webhooks/ """ if request.method != 'POST': return 'OK' else: not_relevant = "Event type not unsupported song issue closed." event = request.headers.get('X-GitHub-Event') # type of event payload = validate_request(request) # Respond to ping as 200 OK if event == "ping": return json.dumps({'msg': 'pong'}) # elif event == "issues": try: label = payload['issue']['labels'][0]['name'] # should be unsupported song for our purposes repo = payload['repository']['name'] # should be from the SwagLyrics for Spotify repo except IndexError: return not_relevant """ If the issue is concerning the `SwagLyrics-For-Spotify repo, the issue is being closed and the issue had the tag `unsupported song` then remove line from unsupported.txt """ if payload[ 'action'] == 'closed' and label == 'unsupported song' and repo == 'SwagLyrics-For-Spotify': title = payload['issue']['title'] title = wdt.match(title) song = title.group(1) artist = title.group(2) logging.info(f'{song} by {artist} is to be deleted.') cnt = del_line(song, artist) return f'Deleted {cnt} instances from unsupported.txt' else: return json.dumps({'msg': 'Wrong event type'}) return not_relevant
def update_webhook(): # Make sure request is of type post if request.method != 'POST': return 'OK' event = request.headers.get('X-GitHub-Event') if event == "ping": return json.dumps({'msg': 'Hi!'}) elif event == "push": payload = validate_request(request) if payload['ref'] != 'refs/heads/master': return json.dumps({'msg': 'Not master; ignoring'}) repo = git.Repo('/var/www/sites/mysite') origin = repo.remotes.origin pull_info = origin.pull() if len(pull_info) == 0: return json.dumps( {'msg': "Didn't pull any information from remote!"}) if pull_info[0].flags > 128: return json.dumps( {'msg': "Didn't pull any information from remote!"}) commit_hash = pull_info[0].commit.hexsha build_commit = f'build_commit = "{commit_hash}"' logging.info(f'{build_commit}') if commit_hash == payload["after"]: # since payload is from github and pull info is what we pulled from git discord_deploy(payload) else: logging.error( f'weird mismatch: {commit_hash=} {payload["after"]=}') return f'Updated PythonAnywhere server to commit {commit_hash}' else: return json.dumps({'msg': "Wrong event type"})