summaryrefslogtreecommitdiff
path: root/linaro_metrics/sync_github_changes.py
blob: 329822093fa70e38b61edc210a22dd7fe37d31a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python

import contextlib
import json
import logging
import os
import re
import sys
import textwrap
import urllib2

from datetime import datetime

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from bin import django_setup, add_logging_arguments
django_setup()  # must be called to get sys.path and django settings in place

from django.conf import settings

from patchwork.models import Patch, Project, State

from linaro_metrics.crowd import Crowd
from linaro_metrics.models import TeamCredit
from linaro_metrics.parsemail import get_linaro_person

log = logging.getLogger('sync_github_changes')

STATE_MAP = {'closed': 'Accepted', 'open': 'New'}
GITHUB_REPOS = [
    # tuple of: owner, repo, patchwork-project
    ('ARM-software', 'arm-trusted-firmware', 'arm-trusted-firmware'),
    ('jackmitch', 'libsoc', 'libsoc'),
    ('OP-TEE', 'optee_os', 'optee_os'),
    ('OP-TEE', 'optee_test', 'optee_test'),
    ('OP-TEE', 'optee_client', 'optee_client'),
    ('OP-TEE', 'build', 'optee_build'),
    ('OP-TEE', 'manifest', 'optee_manifest'),
    ('linaro-swg', 'optee_android_manifest', 'optee_android_manifest'),
    ('linaro-swg', 'optee_benchmark', 'optee_benchmark'),
    ('linaro-swg', 'linux', 'optee_linux'),
    ('linaro-swg', 'gen_rootfs', 'optee_gen_rootfs'),
    ('linaro-swg', 'bios_qemu_tz_arm', 'optee_bios_qemu_tz_arm'),
    ('linaro-swg', 'hello_world', 'optee_hello_world'),
    ('scheduler-tools', 'rt-app', 'rt-app'),
    ('WebPlatformForEmbedded', 'meta-wpe', 'meta-wpe'),
    ('WebPlatformForEmbedded', 'WPEWebKit', 'WPEWebKit'),
    ('ndechesne', 'meta-qcom', 'meta-qcom'),
    ('zephyrproject-rtos', 'zephyr', 'Zephyr'),
]


def _get(url):
    headers = {'Authorization': 'token %s' % settings.GITHUB_OAUTH_TOKEN}
    request = urllib2.Request(url, headers=headers)
    try:
        return urllib2.urlopen(request)
    except urllib2.HTTPError as e:
        log.error('HTTP_%d while GETing %s:\n %s',
                  e.getcode(), url, e.readlines())
        sys.exit(1)


def get_pull_requests(owner, repo, last_update=None):
    url = 'https://api.github.com/repos/%s/%s/pulls?state=all&sort=updated'
    url = url % (owner, repo)
    while url:
        resp = _get(url)
        data = json.loads(resp.read())
        for x in data:
            ts = datetime.strptime(x['updated_at'], '%Y-%m-%dT%H:%M:%SZ')
            if last_update and ts < last_update:
                log.debug('Hit old pull requests, exiting')
                return
            try:
                yield x
            except:
                log.error('Unable to process pr(%r)', x)
                raise
        url = resp.headers.get('link')
        if url:
            # find the <$URL>; rel="next" to get the next page of results
            m = re.match('<(\S+)>; rel="next"', url)
            url = None
            if m:
                url = m.group(1)


def get_author(crowd, pr):
    resp = _get(pr['commits_url'])
    data = json.loads(resp.read())
    if not len(data):
        # some PR's have no commits: https://github.com/docker/docker/pull/5894
        return
    email = data[0]['commit']['author']['email']
    return get_linaro_person(crowd, email)


def patchwork_state(github_status):
    return State.objects.get(name=STATE_MAP[github_status])


def get_patch_content(owner, repo, pr):
    fmt = textwrap.dedent('''\
        # %s
        This represents a change submitted via Github. It is mirrored
        here so that it is included in our statistics.''')
    return fmt % pr['html_url']


def create_or_update(proj, owner, repo, author, pr):
    msgid = '%s/%s@%d' % (owner, repo, pr['number'])
    created = datetime.strptime(pr['created_at'], '%Y-%m-%dT%H:%M:%SZ')
    updated = datetime.strptime(pr['updated_at'], '%Y-%m-%dT%H:%M:%SZ')
    fields = {
        'name': pr['title'],
        'project': Project.objects.get(name=proj),
        'state': patchwork_state(pr['state']),
    }
    try:
        p = Patch.objects.get(msgid=msgid)
        tcs = TeamCredit.objects.filter(patch=p)
        if updated > tcs[0].last_state_change:
            for k, v in fields.iteritems():
                setattr(p, k, v)
            p.save()
            TeamCredit.objects.filter(patch=p).update(
                last_state_change=updated)
    except Patch.DoesNotExist:
        fields['msgid'] = msgid
        fields['date'] = created
        fields['submitter'] = author
        fields['content'] = get_patch_content(owner, repo, pr)
        p = Patch.objects.create(**fields)
        # teamcredits are auto-set to "now", so we need to update it to what
        # came from gerrit
        TeamCredit.objects.filter(patch=p).update(last_state_change=updated)


@contextlib.contextmanager
def repo_cache():
    def dt_serialize(obj):
        if isinstance(obj, datetime):
            return obj.isoformat()
        return obj

    fname = os.path.join(settings.REPO_DIR, 'github.cache')
    data = {}
    try:
        with open(fname) as f:
            data = json.load(f)
            for repo, dt in data.items():
                data[repo] = datetime.strptime(dt, '%Y-%m-%dT%H:%M:%S.%f')
    except:
        log.exception('ignoring')
    yield data
    with open(fname, 'w') as f:
        json.dump(data, f, default=dt_serialize)


def main(args):
    crwd = Crowd(settings.CROWD_USER, settings.CROWD_PASS, settings.CROWD_URL)

    with crwd.cached(settings.CROWD_CACHE), repo_cache() as repos:
        for owner, repo, proj in GITHUB_REPOS:
            repo_path = '%s/%s' % (owner, repo)
            log.info('Looking at: %s', repo_path)
            now = datetime.now()
            last_update = repos.get(repo_path)
            x = 0
            try:
                for pr in get_pull_requests(owner, repo, last_update):
                    x += 1
                    auth = get_author(crwd, pr)
                    if auth:
                        log.debug('checking change: %d', pr['number'])
                        create_or_update(proj, owner, repo, auth, pr)
                repos[repo_path] = now
            finally:
                log.info('analayzed %d pull-requests', x)


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(
        description='Synchronize Linaro changes from github projects')
    add_logging_arguments(parser)
    args = parser.parse_args()
    main(args)