|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +"""Manage GitHub software releases.""" |
| 4 | + |
| 5 | + |
| 6 | +import argparse |
| 7 | +import logging |
| 8 | +import os |
| 9 | + |
| 10 | +import github |
| 11 | + |
| 12 | +NIGHTLY_TAG = 'nightly' |
| 13 | +NIGHTLY_BRANCH = 'master' |
| 14 | + |
| 15 | +TAGGED_RELEASE_MESSAGE = "tagged_release_message.md" |
| 16 | + |
| 17 | +################################################################ |
| 18 | + |
| 19 | +def argument_parser(): |
| 20 | + """Parser for command-line arguments.""" |
| 21 | + |
| 22 | + parser = argparse.ArgumentParser( |
| 23 | + description='Create GitHub software release with build artifacts.' |
| 24 | + ) |
| 25 | + parser.add_argument( |
| 26 | + '--verbose', |
| 27 | + action='store_true', |
| 28 | + help='Verbose output.' |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + '--debug', |
| 32 | + action='store_true', |
| 33 | + help='Debugging output.' |
| 34 | + ) |
| 35 | + |
| 36 | + return parser |
| 37 | + |
| 38 | +def argument_defaults(args): |
| 39 | + """Default values for command-line arguments.""" |
| 40 | + |
| 41 | + # Configure logging to print INFO messages by default |
| 42 | + args.verbose = True |
| 43 | + if args.debug: |
| 44 | + logging.basicConfig(level=logging.DEBUG, |
| 45 | + format='%(levelname)s: %(message)s') |
| 46 | + elif args.verbose: |
| 47 | + logging.basicConfig(level=logging.INFO, |
| 48 | + format='%(levelname)s: %(message)s') |
| 49 | + else: |
| 50 | + logging.basicConfig(format='%(levelname)s: %(message)s') |
| 51 | + |
| 52 | + return args |
| 53 | + |
| 54 | +################################################################ |
| 55 | + |
| 56 | +def get_repository(full_name, token): |
| 57 | + """GitHub repository with given name authenticated with given token.""" |
| 58 | + |
| 59 | + return github.Github(token).get_repo(full_name) |
| 60 | + |
| 61 | +def lookup_tag(repo, tag_name): |
| 62 | + """The reference corresponding to a tag name.""" |
| 63 | + |
| 64 | + try: |
| 65 | + ref = repo.get_git_ref('tags/{}'.format(tag_name)) |
| 66 | + except github.UnknownObjectException as error: |
| 67 | + logging.info('Failed to find reference for tag %s: %s', tag_name, error) |
| 68 | + return None |
| 69 | + |
| 70 | + logging.info('Found reference for tag %s: %s', tag_name, ref) |
| 71 | + return ref |
| 72 | + |
| 73 | +def lookup_release(repo, tag_name): |
| 74 | + """The release corresponding to a tag name.""" |
| 75 | + |
| 76 | + for release in repo.get_releases(): |
| 77 | + if release.tag_name == tag_name: |
| 78 | + logging.info('Found release for tag %s: %s', tag_name, release) |
| 79 | + return release |
| 80 | + logging.info('Failed to find release for tag %s', tag_name) |
| 81 | + return None |
| 82 | + |
| 83 | +################################################################ |
| 84 | +# Create a GitHub release for a versioned software release (a tagged |
| 85 | +# commit) with a set of installation packages for this version. |
| 86 | + |
| 87 | +def tagged_release_message(version, path=None): |
| 88 | + """The message to use with a tagged release.""" |
| 89 | + |
| 90 | + path = path or TAGGED_RELEASE_MESSAGE |
| 91 | + path = os.path.join(os.path.dirname(__file__), path) |
| 92 | + |
| 93 | + try: |
| 94 | + with open(path) as msg: |
| 95 | + return msg.read().format(version) |
| 96 | + except FileNotFoundError: |
| 97 | + logging.info("Couldn't open tagged release message file: %s", path) |
| 98 | + return "This is release {}".format(version) |
| 99 | + |
| 100 | +def create_tagged_release(repo, tag_name, version=None): |
| 101 | + """Create a release for a tagged commit.""" |
| 102 | + |
| 103 | + release = lookup_release(repo, tag_name) |
| 104 | + if release: |
| 105 | + release.delete_release() |
| 106 | + logging.info('Deleted release for tag %s: %s', tag_name, release) |
| 107 | + |
| 108 | + reference = lookup_tag(repo, tag_name) |
| 109 | + if not reference: |
| 110 | + raise UserWarning("Tag does not exist: {}".format(tag_name)) |
| 111 | + |
| 112 | + version = version or tag_name.split('-')[-1] |
| 113 | + release_name = tag_name |
| 114 | + release_msg = tagged_release_message(version) |
| 115 | + release = repo.create_git_release(tag_name, release_name, release_msg) |
| 116 | + logging.info('Created release for tag %s: %s', tag_name, release) |
| 117 | + if not release: |
| 118 | + raise UserWarning("Failed to create tagged release for tag {}" |
| 119 | + .format(tag_name)) |
| 120 | + |
| 121 | + return release |
| 122 | + |
| 123 | +def upload_release_asset(release, path, |
| 124 | + label=None, content_type=None, name=None): |
| 125 | + """Upload an asset (an installation package) to a release.""" |
| 126 | + |
| 127 | + filename = os.path.basename(path) |
| 128 | + label = label or filename |
| 129 | + name = name or filename |
| 130 | + content_type = content_type or 'text/plain' |
| 131 | + |
| 132 | + try: |
| 133 | + asset = release.upload_asset(path, label, content_type, name) |
| 134 | + except github.GithubException as error: |
| 135 | + logging.info("Failed to upload asset '%s': %s", path, error) |
| 136 | + |
| 137 | + logging.info("Uploaded asset '%s': %s", path, asset) |
| 138 | + return asset |
| 139 | + |
| 140 | +def tagged_software_release(repo, tag_name, version=None, assets=None): |
| 141 | + """Create a release for a tagged commit with a list of assets.""" |
| 142 | + |
| 143 | + # asset: { |
| 144 | + # path : required string: local path to the assest to upload to GitHub |
| 145 | + # name : string: filename to use for the asset on GitHub |
| 146 | + # label : string: text to display in the link to the asset in release |
| 147 | + # type : string content type of the asset |
| 148 | + # } |
| 149 | + assets = assets or [] |
| 150 | + |
| 151 | + release = create_tagged_release(repo, tag_name, version) |
| 152 | + for asset in assets: |
| 153 | + upload_release_asset(release, asset['path'], |
| 154 | + label=asset.get('label'), |
| 155 | + content_type=asset.get('type'), |
| 156 | + name=asset.get('name')) |
| 157 | + |
| 158 | +################################################################ |
| 159 | +# Create a GitHub release for a nightly build of a development branch |
| 160 | +# with a set of installation packages for the nightly build. |
| 161 | +# |
| 162 | +# This works by creating (updating) a 'nightly' tag for the tip of the |
| 163 | +# development branch, then creating an ordinary tagged release for |
| 164 | +# this newly tagged commit, but a) the release message tailored to a |
| 165 | +# nightly release, and b) the release type is set to "prerelease". |
| 166 | +# |
| 167 | +# One issue with this implementation is that the constant updating of |
| 168 | +# the 'nightly' tag will require constant forced pulls to the local |
| 169 | +# copy of the 'nightly' tag. This annoyance is the primary reason |
| 170 | +# this implementation is not currently used. |
| 171 | + |
| 172 | +def update_nightly_tag(repo): |
| 173 | + """Update the nightly tag to the tip of the development branch.""" |
| 174 | + |
| 175 | + reference = lookup_tag(repo, NIGHTLY_TAG) |
| 176 | + if reference: |
| 177 | + reference.delete() |
| 178 | + logging.info('Deleted tag %s: %s', NIGHTLY_TAG, reference) |
| 179 | + |
| 180 | + ref = 'refs/tags/{}'.format(NIGHTLY_TAG) |
| 181 | + sha = repo.get_branch(NIGHTLY_BRANCH).commit.sha |
| 182 | + reference = repo.create_git_ref(ref, sha) |
| 183 | + logging.info('Created tag %s for branch %s (ref %s, sha %s): %s', |
| 184 | + NIGHTLY_TAG, NIGHTLY_BRANCH, ref, sha, reference) |
| 185 | + |
| 186 | +def create_nightly_release(repo): |
| 187 | + """Create a tagged release for the nightly commit.""" |
| 188 | + |
| 189 | + release = lookup_release(repo, NIGHTLY_TAG) |
| 190 | + if release: |
| 191 | + release.delete_release() |
| 192 | + logging.info('Deleted release for tag %s: %s', NIGHTLY_TAG, release) |
| 193 | + |
| 194 | + update_nightly_tag(repo) |
| 195 | + reference = lookup_tag(repo, NIGHTLY_TAG) |
| 196 | + if not reference: |
| 197 | + raise UserWarning("Tag does not exist: {}".format(NIGHTLY_TAG)) |
| 198 | + |
| 199 | + release_name = "Nightly release" |
| 200 | + release_msg = "This is a nightly release" |
| 201 | + # GitHub doesn't display release with draft=False, prelease=True |
| 202 | + release = repo.create_git_release(NIGHTLY_TAG, |
| 203 | + release_name, release_msg, |
| 204 | + prerelease=True) |
| 205 | + logging.info('Created nightly release with tag %s for branch %s: %s', |
| 206 | + NIGHTLY_TAG, NIGHTLY_BRANCH, release) |
| 207 | + if not release: |
| 208 | + raise UserWarning( |
| 209 | + "Failed to create nightly release with tag {} for branch {}" |
| 210 | + .format(NIGHTLY_TAG, NIGHTLY_BRANCH)) |
| 211 | + return release |
| 212 | + |
| 213 | +def nightly_software_release(repo, assets=None): |
| 214 | + """Create a tagged release for the nightly commit and pacakges.""" |
| 215 | + |
| 216 | + # asset: { |
| 217 | + # path : required string: local path to the assest to upload to GitHub |
| 218 | + # name : string: filename to use for the asset on GitHub |
| 219 | + # label : string: text to display in the link to the asset in release |
| 220 | + # type : string: content type of the asset |
| 221 | + # } |
| 222 | + assets = assets or [] |
| 223 | + |
| 224 | + release = create_nightly_release(repo) |
| 225 | + for asset in assets: |
| 226 | + upload_release_asset(release, asset['path'], |
| 227 | + label=asset.get('label'), |
| 228 | + content_type=asset.get('type'), |
| 229 | + name=asset.get('name')) |
| 230 | + |
| 231 | +################################################################ |
0 commit comments