|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""TODO - Document this |
| 3 | +""" |
| 4 | + |
| 5 | +import argparse |
| 6 | +import json |
| 7 | +import os |
| 8 | +import re |
| 9 | +import shlex |
| 10 | +import shutil |
| 11 | +import subprocess |
| 12 | +import sys |
| 13 | +import urllib.request |
| 14 | + |
| 15 | + |
| 16 | +class MissingDependency(Exception): |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +def get_token(): |
| 21 | + name = 'CIRCLE_CI_API_TOKEN' |
| 22 | + token = os.environ.get(name) |
| 23 | + if token is None: |
| 24 | + raise MissingDependency( |
| 25 | + f'{name} must be set in the environment. Note that it must be a "personal" API token.' |
| 26 | + ) |
| 27 | + return token |
| 28 | + |
| 29 | + |
| 30 | +def get_gh(): |
| 31 | + exe_path = shutil.which('gh') |
| 32 | + if exe_path is None: |
| 33 | + raise MissingDependency( |
| 34 | + 'The "gh" command must be available to publish a release to GitHub. Installation instructions are available at <https://cli.github.com/>.' |
| 35 | + ) |
| 36 | + return exe_path |
| 37 | + |
| 38 | + |
| 39 | +def get_gpg(): |
| 40 | + exe_path = shutil.which('gpg') |
| 41 | + if exe_path is None: |
| 42 | + raise MissingDependency( |
| 43 | + 'The "gpg" command must be available and configured to be able to create detached signatures of release artifacts.' |
| 44 | + ) |
| 45 | + return exe_path |
| 46 | + |
| 47 | + |
| 48 | +def get_git(): |
| 49 | + exe_path = shutil.which('git') |
| 50 | + if exe_path is None: |
| 51 | + raise MissingDependency( |
| 52 | + 'The "git" command must be available to tag the release commit.') |
| 53 | + return exe_path |
| 54 | + |
| 55 | + |
| 56 | +def validate_version_tag(tag): |
| 57 | + pattern = r'v[0-9]+\.[0-9]+\.[0-9]+' |
| 58 | + if re.fullmatch(pattern, tag) is None: |
| 59 | + raise ValueError( |
| 60 | + f'Tag does not match the regular expression /{pattern}/.') |
| 61 | + return tag |
| 62 | + |
| 63 | + |
| 64 | +def parse_options(args): |
| 65 | + parser = argparse.ArgumentParser( |
| 66 | + description='Build and publish a release of nginx-datadog.', |
| 67 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
| 68 | + parser.add_argument( |
| 69 | + '--remote', |
| 70 | + type=str, |
| 71 | + default='origin', |
| 72 | + help='local name of the git remote that points to GitHub') |
| 73 | + parser.add_argument('--no-tag', |
| 74 | + action='store_true', |
| 75 | + help="don't tag the current HEAD") |
| 76 | + parser.add_argument('--pipeline-id', |
| 77 | + type=str, |
| 78 | + help='use an already-started CircleCI pipeline') |
| 79 | + parser.add_argument( |
| 80 | + 'version_tag', |
| 81 | + help='git tag to associate with the release (e.g. "v1.2.3")') |
| 82 | + return parser.parse_args() |
| 83 | + |
| 84 | + |
| 85 | +try: |
| 86 | + ci_api_token = get_token() |
| 87 | + gh_exe = get_gh() |
| 88 | + gpg_exe = get_gpg() |
| 89 | + git_exe = get_git() |
| 90 | + options = parse_options(sys.argv[1:]) |
| 91 | + options.version_tag = validate_version_tag(options.version_tag) |
| 92 | +except (MissingDependency, ValueError) as error: |
| 93 | + print(str(error), file=sys.stderr) |
| 94 | + sys.exit(1) |
| 95 | + |
| 96 | +print({ |
| 97 | + 'token': ci_api_token, |
| 98 | + 'gh': gh_exe, |
| 99 | + 'gpg': gpg_exe, |
| 100 | + 'git': git_exe, |
| 101 | + 'tag': options.version_tag, |
| 102 | + 'remote': options.remote, |
| 103 | + 'no-tag': options.no_tag |
| 104 | +}) |
| 105 | + |
| 106 | + |
| 107 | +def run(command, *args, **kwargs): |
| 108 | + print('+', shlex.join(command), file=sys.stderr) |
| 109 | + return subprocess.run(command, *args, **kwargs) |
| 110 | + |
| 111 | + |
| 112 | +if not options.no_tag: |
| 113 | + command = [ |
| 114 | + git_exe, 'ls-remote', '--tags', options.remote, options.version_tag |
| 115 | + ] |
| 116 | + result = run(command, stdout=subprocess.PIPE, check=True) |
| 117 | + if result.stdout: |
| 118 | + raise Exception( |
| 119 | + f'Tag {options.version_tag} already exists on {options.remote}.') |
| 120 | + |
| 121 | + command = [ |
| 122 | + git_exe, 'tag', '-a', options.version_tag, '-m', |
| 123 | + f'release ${options.version_tag}' |
| 124 | + ] |
| 125 | + run(command, check=True) |
| 126 | + |
| 127 | + command = [git_exe, 'push', options.remote, options.version_tag] |
| 128 | + run(command, check=True) |
| 129 | + |
| 130 | +PROJECT_URL = 'https://circleci.com/api/v2/project/gh/DataDog/nginx-datadog' |
| 131 | + |
| 132 | + |
| 133 | +def send_ci_request(path, payload=None, method=None): |
| 134 | + headers = {'Circle-Token': ci_api_token} |
| 135 | + if payload is not None: |
| 136 | + headers['Content-Type'] = 'application/json; charset=utf-8' |
| 137 | + payload = json.dumps(payload).encode('utf8') |
| 138 | + |
| 139 | + url = f'{PROJECT_URL}{path}' |
| 140 | + request = urllib.request.Request(url, |
| 141 | + data=payload, |
| 142 | + headers=headers, |
| 143 | + method=method) |
| 144 | + print('+', request.get_method(), request.full_url, request.data or '') |
| 145 | + |
| 146 | + response = urllib.request.urlopen(request) |
| 147 | + response_body = json.load(response) |
| 148 | + if response.status < 200 or response.status > 299: |
| 149 | + raise Exception( |
| 150 | + f'HTTP error response {response.status}: {response_body}') |
| 151 | + return response.status, response_body |
| 152 | + |
| 153 | + |
| 154 | +# Kick off a CircleCI pipeline. |
| 155 | +if options.pipeline_id is not None: |
| 156 | + pipeline_id = options.pipeline_id |
| 157 | +else: |
| 158 | + body = {"tag": options.version_tag} |
| 159 | + _, response = send_ci_request('/pipeline', payload=body, method='POST') |
| 160 | + pipeline_id = response.get('id') |
| 161 | + if pipeline_id is None: |
| 162 | + raise Exception( |
| 163 | + f'POST [...]/pipeline response did not contain pipeline "id": {response}' |
| 164 | + ) |
| 165 | + |
| 166 | +# Fetch the pipeline's information. This will contain its jobs. |
| 167 | +_, response = send_ci_request(f'/pipeline/{pipeline_id}') |
| 168 | +print(response) |
| 169 | + |
| 170 | +# TODO |
| 171 | +# |
| 172 | +# x tag the current head |
| 173 | +# - kick off pipeline |
| 174 | +# - get workflows |
| 175 | +# - extrace "build-and-test-all" workflow |
| 176 | +# - poll workflow's jobs until all are successful |
| 177 | +# - for each build job, download artifacts |
| 178 | +# - .tgz with GPG detached signature |
| 179 | +# - use "gh" |
0 commit comments