Created
January 4, 2019 06:03
-
-
Save toabctl/c9b160ca54bdb4db1c09928fc6141e64 to your computer and use it in GitHub Desktop.
Get changes for a github project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# | |
# Copyright 2016 SUSE Linux GmbH | |
# Author: Thomas Bechtold <[email protected]> | |
from __future__ import print_function | |
import argparse | |
import requests | |
import json | |
import sys | |
def parse_args(): | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--path', type=str, nargs=1, | |
help='optional path for comparison') | |
parser.add_argument('org', metavar='organization', type=str, nargs=1, | |
help='github organization') | |
parser.add_argument('repo', metavar='repository', type=str, nargs=1, | |
help='github repository') | |
parser.add_argument('rev1', metavar='rev1', type=str, nargs=1, | |
help='revision 1. Can be a sha or a tag') | |
parser.add_argument('rev2', metavar='rev2', type=str, nargs=1, | |
help='revision 2. Can be a sha or a tag') | |
return vars(parser.parse_args()) | |
def _commit_list_get_messages(commit_list, ignore_merge_commits=True): | |
messages = list() | |
for commit in reversed(commit_list): | |
# skip merge commits | |
if ignore_merge_commits and len(commit['parents']) > 1: | |
continue | |
# get the commit message | |
msg = _commit_get_message(commit) | |
messages.append(msg) | |
# remove duplicates | |
messages = list(set(messages)) | |
return messages | |
def _commit_get_message(commit, only_first_line=True): | |
msg = commit['commit']['message'] | |
if only_first_line: | |
return msg.splitlines()[0] | |
return msg | |
if __name__ == "__main__": | |
args = parse_args() | |
org = args['org'][0] | |
repo = args['repo'][0] | |
rev1 = args['rev1'][0] | |
rev2 = args['rev2'][0] | |
# get the compare url | |
url = 'https://api.github.com/repos/%s/%s/compare/%s...%s' % (org, repo, rev1, rev2) | |
r = requests.get(url) | |
if r.ok: | |
data = json.loads(r.text or r.content) | |
messages = _commit_list_get_messages(data['commits']) | |
print('update to version %s' % (rev2)) | |
for msg in messages: | |
# the RPM changelog format | |
print(' * %s' % (msg.encode('utf-8'))) | |
#pprint.pprint(data) | |
else: | |
print('Code %s for url: %s' % (r.status_code, url)) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment