2020-08-24 07:29:17 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
'''
|
|
|
|
Fetches the labels of an Issue or Pull-Request from GitHub.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
--id=<id>: the number of the the GitHub Issue or Pull-Request
|
2020-10-13 08:34:29 +00:00
|
|
|
--has-label=<label>: label to check (exit 0 if set, 2 unset), without
|
2020-08-24 07:29:17 +00:00
|
|
|
--has-label, all labels of the Issue or Pull-Request
|
|
|
|
get printed.
|
2020-10-13 08:34:29 +00:00
|
|
|
|
|
|
|
Exit codes:
|
|
|
|
0: success
|
|
|
|
1: any unexpected failure
|
|
|
|
2: --has-label=<label> was passed, <label> is not set on the PR
|
2020-10-13 09:52:31 +00:00
|
|
|
|
|
|
|
Environment:
|
|
|
|
GITHUB_API_TOKEN: the GitHub "personal access token" to use
|
2020-08-24 07:29:17 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
import argparse
|
2020-10-13 09:52:31 +00:00
|
|
|
import os
|
2020-08-24 07:29:17 +00:00
|
|
|
import requests
|
2020-10-13 09:52:31 +00:00
|
|
|
from requests.auth import HTTPBasicAuth
|
|
|
|
import sys
|
2020-08-24 07:29:17 +00:00
|
|
|
|
|
|
|
LABEL_URL_FMT = 'https://api.github.com/repos/ceph/ceph-csi/issues/%s/labels'
|
|
|
|
'''
|
|
|
|
Formatted URL for fetching the labels, replace '%s' with the number of the
|
|
|
|
Issue or Pull-Request.
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
def get_json_labels(gh_id):
|
|
|
|
'''
|
|
|
|
Fetch the labels from GitHub, return the full JSON structures that were
|
|
|
|
obtained. These include several ID's, color, decription, name and more.
|
|
|
|
'''
|
|
|
|
url = LABEL_URL_FMT % gh_id
|
|
|
|
headers = {'Accept': 'application/vnd.github.v3+json'}
|
2020-10-13 09:52:31 +00:00
|
|
|
|
|
|
|
auth = None
|
|
|
|
if 'GITHUB_API_TOKEN' in os.environ:
|
|
|
|
github_api_token = os.environ['GITHUB_API_TOKEN']
|
|
|
|
if github_api_token != '':
|
|
|
|
# the username "unused" is not relevant, needs to be non-empty
|
|
|
|
auth = HTTPBasicAuth('unused', github_api_token)
|
|
|
|
|
|
|
|
res = requests.get(url, headers=headers, auth=auth)
|
2020-10-13 08:34:29 +00:00
|
|
|
|
|
|
|
# if "res.status_code != requests.codes.ok", raise an exception
|
|
|
|
res.raise_for_status()
|
|
|
|
|
2020-08-24 07:29:17 +00:00
|
|
|
return res.json()
|
|
|
|
|
|
|
|
|
|
|
|
def get_names(gh_labels):
|
|
|
|
'''
|
|
|
|
Take the JSON formatted labels, and return a list of the name for each label.
|
|
|
|
'''
|
|
|
|
names = list()
|
|
|
|
for label in gh_labels:
|
|
|
|
names.append(label['name'])
|
|
|
|
return names
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
'''
|
|
|
|
main() function to parse arguments and run the actions.
|
|
|
|
'''
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--id', type=int, required=True, help='ID of the Issue or Pull-Request')
|
|
|
|
parser.add_argument('--has-label', help='check if the Issue or Pull-Request has the label set')
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# get the labels for the issue
|
2020-10-13 08:34:29 +00:00
|
|
|
try:
|
|
|
|
json = get_json_labels(args.id)
|
|
|
|
except Exception as err:
|
|
|
|
print('Error: %s' % err)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-08-24 07:29:17 +00:00
|
|
|
names = get_names(json)
|
|
|
|
|
2020-10-13 08:34:29 +00:00
|
|
|
# in case --has-label is passed, exit with 0 or 2
|
2020-08-24 07:29:17 +00:00
|
|
|
if args.has_label:
|
|
|
|
if args.has_label in names:
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
2020-10-13 08:34:29 +00:00
|
|
|
sys.exit(2)
|
2020-08-24 07:29:17 +00:00
|
|
|
# --has-label was not passed, list all labels
|
|
|
|
else:
|
|
|
|
for name in names:
|
|
|
|
print(name)
|
|
|
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|