|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Generate and upload changelog |
| 4 | + |
| 5 | +from git import Repo, exc |
| 6 | +from github import Github |
| 7 | +import os |
| 8 | +import sys |
| 9 | + |
| 10 | +upload_changelog = True |
| 11 | + |
| 12 | +try: |
| 13 | + current_tag = os.environ['TRAVIS_TAG'] |
| 14 | + if current_tag == '': |
| 15 | + current_tag = 'HEAD' |
| 16 | + upload_changelog = False |
| 17 | + print('TRAVIS_TAG is set to {}'.format(current_tag)) |
| 18 | +except KeyError: |
| 19 | + print('TRAVIS_TAG not set - not uploading changelog') |
| 20 | + current_tag = 'HEAD' |
| 21 | + upload_changelog = False |
| 22 | + |
| 23 | +try: |
| 24 | + api_key = os.environ['GITHUB_OAUTH_TOKEN'] |
| 25 | +except KeyError: |
| 26 | + print('GITHUB_OAUTH_TOKEN not set - not uploading changelog') |
| 27 | + api_key = None |
| 28 | + upload_changelog = False |
| 29 | + |
| 30 | +try: |
| 31 | + repo_slug = os.environ['TRAVIS_REPO_SLUG'] |
| 32 | +except KeyError: |
| 33 | + print('TRAVIS_REPO_SLUG not set - cannot determine remote repository') |
| 34 | + repo_slug = '' |
| 35 | + exit(1) |
| 36 | + |
| 37 | +if len(sys.argv) > 1: |
| 38 | + repo_path = sys.argv[1] |
| 39 | +else: |
| 40 | + repo_path = '.' |
| 41 | + |
| 42 | +print('Opening repository at {}'.format(repo_path)) |
| 43 | +repo = Repo(repo_path) |
| 44 | +git = repo.git() |
| 45 | +try: |
| 46 | + print('Unshallowing repository') |
| 47 | + git.fetch('--unshallow', '--tags') |
| 48 | +except exc.GitCommandError: |
| 49 | + print('Repository already unshallowed') |
| 50 | +print('Attempting to get previous tag') |
| 51 | +base_tag = git.describe('--tags', '--abbrev=0', '{}^'.format(current_tag)) |
| 52 | +print('Base tag set to {}'.format(base_tag)) |
| 53 | + |
| 54 | +changelog = git.log('{}...{}'.format(base_tag, current_tag), '--pretty=format:* %H %s *(%an)*') |
| 55 | +print('Current changelog: \n{}'.format(changelog)) |
| 56 | + |
| 57 | +# Only interact with Github if uploading is enabled |
| 58 | +if upload_changelog: |
| 59 | + gh = Github(api_key) |
| 60 | + gh_repo = gh.get_repo(repo_slug) |
| 61 | + # Get all releases and find ours by its tag name |
| 62 | + gh_release = None |
| 63 | + for release in gh_repo.get_releases(): |
| 64 | + if release.tag_name == current_tag: |
| 65 | + gh_release = release |
| 66 | + if gh_release is None: |
| 67 | + # We could not find the correct release, so here's our last resort. It will most likely fail. |
| 68 | + gh_release = gh_repo.get_release(current_tag) |
| 69 | + gh_body = gh_release.body |
| 70 | + if gh_body is None: |
| 71 | + gh_body = '' |
| 72 | + gh_body = '{}\nChanges between `{}` and `{}`:\n\n{}'.format(gh_body, base_tag, current_tag, changelog) |
| 73 | + print('New release body: {}'.format(gh_body)) |
| 74 | + gh_release.update_release(gh_release.tag_name, gh_body, draft=True, prerelease=True, |
| 75 | + tag_name=gh_release.tag_name, target_commitish=gh_release.target_commitish) |
0 commit comments