Skip to content

Instantly share code, notes, and snippets.

@diox
Forked from brutasse/commits.py
Created May 30, 2013 12:20
Show Gist options
  • Select an option

  • Save diox/5677443 to your computer and use it in GitHub Desktop.

Select an option

Save diox/5677443 to your computer and use it in GitHub Desktop.

Revisions

  1. @brutasse brutasse created this gist May 30, 2013.
    122 changes: 122 additions & 0 deletions commits.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,122 @@
    import os
    import re
    import requests

    import feedgenerator

    from dateutil.parser import parse
    from raven import Client

    REPO = 'django/django'
    BRANCH = 'master'
    HERE = os.path.abspath(os.path.dirname(__file__))
    TICKET_RE = re.compile(r'#(?P<id>\d+)')
    CHANGESET_RE = re.compile(r'r(?P<id>\d+)')
    SENTRY_DSN = 'bla bla bla'
    OAUTH_TOKEN = 'bla bla bla'


    def escape(html):
    return html.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')


    def commits(num=5):
    url = 'https://api.github.com/repos/{repo}/commits'.format(repo=REPO)
    response = requests.get(
    url, params={'sha': BRANCH},
    headers={'Authorization': 'token {0}'.format(OAUTH_TOKEN)},
    )
    commits = response.json

    feed = feedgenerator.Atom1Feed(
    title='Django commits',
    link='https://github.com/django/django/commits/master',
    feed_url='http://media.bruno.im/django.atom',
    description='Django pretty commits. Updates every hour.',
    language='en',
    )

    for commit in commits[:num]:
    url = commit['url']
    details = requests.get(
    url, headers={'Authorization': 'token {0}'.format(OAUTH_TOKEN)}
    ).json
    commit = details['commit']
    description = []

    title = commit['message'].split('\n')[0]
    url = url.replace('api.github.com/repos',
    'github.com').replace('commits', 'commit')

    author = '{name} &lt;{email}&gt;'.format(
    name=commit['author']['name'].encode('utf-8'),
    email=commit['author']['email'],
    )
    description.append('Author: {author}'.format(author=author))
    description.append('Date: {date}'.format(date=commit['author']['date']))
    description.append('New revision: <a href="{url}">{sha}</a>'.format(
    url=url, sha=commit['tree']['sha'],
    ))
    description.append('')

    files = {
    'added': [],
    'modified': [],
    'removed': [],
    'renamed': [],
    }
    diffs = []

    for file_ in details['files']:
    files[file_['status']].append(
    ' * <a href="{url}">{file}</a>'.format(
    url=file_['blob_url'],
    file=file_['filename'],
    )
    )

    if not 'patch' in file_:
    continue

    diffs.append('<a href="{href}">{file}</a>'.format(
    href=file_['blob_url'],
    file=file_['filename'],
    ))

    diffs.append('<pre><code class="diff">{diff}</code></pre>'.format(
    diff=escape(file_['patch'].encode('utf-8')),
    ))


    message = TICKET_RE.sub('<a href="http://code.djangoproject.com/ticket/\g<1>">#\g<1></a>', commit['message'].encode('utf-8'))
    message = CHANGESET_RE.sub('<a href="http://code.djangoproject.com/changeset/\g<1>">r\g<1></a>', message)

    description.append(message.replace('\n', '<br>\n'))
    description.append('')

    for key in ['added', 'modified', 'removed', 'renamed']:
    if files[key]:
    description.append('{key}:'.format(key=key.capitalize()))
    description.extend(files[key])
    description.append('')

    description.append('<br>\n'.join(diffs))

    feed.add_item(
    title=commit['message'].split('\n')[0],
    link=url,
    description='<br>\n'.join(description),
    pubdate=parse(commit['author']['date']),
    )

    with open(os.path.join(HERE, 'django.atom'), 'w') as fp:
    feed.write(fp, 'utf-8')


    if __name__ == '__main__':
    try:
    commits(30)
    except Exception:
    client = Client(dsn=SENTRY_DSN)
    client.captureException()
    raise
    6 changes: 6 additions & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    feedgenerator==1.2.1
    python-dateutil==2.1
    raven==2.0.7.1
    requests==0.14.2
    simplejson==2.6.2
    six==1.2.0