ci: authenticate with GitHub API token from the environment

These script now check if GITHUB_API_TOKEN is set in the environment,
and use that for authenticating. There is no username needed when an API
token is used, but it may not be empty, so it is set to "unused".

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos 2020-10-13 11:52:31 +02:00 committed by mergify[bot]
parent aa062513c0
commit b27b2ee074
2 changed files with 29 additions and 3 deletions

View File

@ -12,11 +12,16 @@ Exit codes:
0: success 0: success
1: any unexpected failure 1: any unexpected failure
2: --has-label=<label> was passed, <label> is not set on the PR 2: --has-label=<label> was passed, <label> is not set on the PR
Environment:
GITHUB_API_TOKEN: the GitHub "personal access token" to use
''' '''
import argparse import argparse
import sys import os
import requests import requests
from requests.auth import HTTPBasicAuth
import sys
LABEL_URL_FMT = 'https://api.github.com/repos/ceph/ceph-csi/issues/%s/labels' LABEL_URL_FMT = 'https://api.github.com/repos/ceph/ceph-csi/issues/%s/labels'
''' '''
@ -32,7 +37,15 @@ def get_json_labels(gh_id):
''' '''
url = LABEL_URL_FMT % gh_id url = LABEL_URL_FMT % gh_id
headers = {'Accept': 'application/vnd.github.v3+json'} headers = {'Accept': 'application/vnd.github.v3+json'}
res = requests.get(url, headers=headers)
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)
# if "res.status_code != requests.codes.ok", raise an exception # if "res.status_code != requests.codes.ok", raise an exception
res.raise_for_status() res.raise_for_status()

View File

@ -5,11 +5,16 @@ release for a major version.
Parameters: Parameters:
--version=<version>: the major version to find the latest patch release for, i.e. v1.19 --version=<version>: the major version to find the latest patch release for, i.e. v1.19
Environment:
GITHUB_API_TOKEN: the GitHub "personal access token" to use
''' '''
import argparse import argparse
import sys import os
import requests import requests
from requests.auth import HTTPBasicAuth
import sys
RELEASE_URL = 'https://api.github.com/repos/kubernetes/kubernetes/releases' RELEASE_URL = 'https://api.github.com/repos/kubernetes/kubernetes/releases'
''' '''
@ -24,6 +29,14 @@ def get_json_releases():
obtained. obtained.
''' '''
headers = {'Accept': 'application/vnd.github.v3+json'} headers = {'Accept': 'application/vnd.github.v3+json'}
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(RELEASE_URL, headers=headers) res = requests.get(RELEASE_URL, headers=headers)
# if "res.status_code != requests.codes.ok", raise an exception # if "res.status_code != requests.codes.ok", raise an exception