ClickHouse/tests/ci/pr_info.py

42 lines
1.5 KiB
Python
Raw Normal View History

2021-09-15 12:59:39 +00:00
#!/usr/bin/env python3
import requests
2021-09-15 13:39:11 +00:00
import json
2021-09-15 13:48:31 +00:00
import os
2021-09-15 16:32:17 +00:00
import subprocess
2021-09-15 17:48:39 +00:00
import urllib
2021-09-15 16:32:17 +00:00
from unidiff import PatchSet
2021-09-15 12:59:39 +00:00
class PRInfo:
2021-09-15 16:32:17 +00:00
def __init__(self, github_event, need_orgs=False, need_changed_files=False):
2021-09-15 12:59:39 +00:00
self.number = github_event['number']
2021-09-15 13:45:44 +00:00
if 'after' in github_event:
self.sha = github_event['after']
else:
2021-09-15 14:12:23 +00:00
self.sha = github_event['pull_request']['head']['sha']
2021-09-15 13:45:44 +00:00
2021-09-15 12:59:39 +00:00
self.labels = set([l['name'] for l in github_event['pull_request']['labels']])
self.user_login = github_event['pull_request']['user']['login']
2021-09-15 14:15:02 +00:00
self.user_orgs = set([])
if need_orgs:
user_orgs_response = requests.get(github_event['pull_request']['user']['organizations_url'])
if user_orgs_response.ok:
response_json = user_orgs_response.json()
self.user_orgs = set(org['id'] for org in response_json)
2021-09-15 13:56:03 +00:00
2021-09-15 16:32:17 +00:00
self.changed_files = set([])
if need_changed_files:
diff_url = github_event['pull_request']['diff_url']
diff = urllib.request.urlopen(github_event['pull_request']['diff_url'])
diff_object = PatchSet(diff, diff.headers.get_charsets()[0])
self.changed_files = set([f.path for f in diff_object])
2021-09-15 13:56:03 +00:00
def get_dict(self):
return {
'sha': self.sha,
'number': self.number,
'labels': self.labels,
'user_login': self.user_login,
'user_orgs': self.user_orgs,
}