Ejemplo n.º 1
0
    def get(self, author_id):
        authors = Author.find(author_id, get_current_user())
        if not authors and author_id == 'me':
            return '''Either there is no current user or you are not in the
              author table''', 401
        elif not authors:
            return 'author not found', 404

        try:
            author_email = authors[0].email
            request = PhabricatorRequest()
            request.connect()
            user_info = request.call('user.query', {'emails': [author_email]})

            if not user_info:
                return 'phabricator: %s not found' % author_email, 404

            author_phid = user_info[0]["phid"]

            diff_info = request.call('differential.query', {
                'authors': [author_phid],
                'status': "status-open"
            })

            diff_info.sort(key=lambda k: -1 * int(k['dateModified']))

            return diff_info
        except requests.exceptions.ConnectionError:
            return 'Unable to connect to Phabricator', 503
Ejemplo n.º 2
0
def author(**kwargs):
    if 'name' not in kwargs:
        kwargs['name'] = ' '.join(get_sentences(1)[0].split(' ')[0:2])
    if 'email' not in kwargs:
        kwargs['email'] = '{0}@example.com'.format(slugify(kwargs['name']))
    try:
        result = Author.query.filter_by(email=kwargs['email'])[0]
    except IndexError:
        result = Author(**kwargs)
        db.session.add(result)
    return result
Ejemplo n.º 3
0
    def get(self, author_id):
        authors = Author.find(author_id, get_current_user())
        if not authors and author_id == 'me':
            return '''Either there is no current user or you are not in the
              author table''', 401
        elif not authors:
            return 'author not found', 404

        try:
            author_email = authors[0].email
            request = PhabricatorRequest()
            request.connect()
            user_info = request.call('user.query', {'emails': [author_email]})

            if not user_info:
                return 'phabricator: %s not found' % author_email, 404

            author_phid = user_info[0]["phid"]

            diff_info = request.call('differential.query', {
                'authors': [author_phid],
                'status': "status-open"
            })

            diff_info.sort(key=lambda k: -1 * int(k['dateModified']))

        except requests.exceptions.ConnectionError:
            return 'Unable to connect to Phabricator', 503

        if not diff_info:
            # No diffs, no point in trying to find builds.
            return self.respond([])

        rows = list(db.session.query(
            PhabricatorDiff, Build
        ).join(
            Build, Build.source_id == PhabricatorDiff.source_id
        ).filter(
            PhabricatorDiff.revision_id.in_([d['id'] for d in diff_info])
        ))

        serialized_builds = zip(
            self.serialize([row.Build for row in rows]),
            [row.PhabricatorDiff for row in rows]
        )

        builds_map = defaultdict(list)
        for build, phabricator_diff in serialized_builds:
            builds_map[str(phabricator_diff.revision_id)].append(build)

        for d in diff_info:
            d['builds'] = builds_map[str(d['id'])]

        return self.respond(diff_info)
Ejemplo n.º 4
0
    def get(self, author_id):
        authors = Author.find(author_id, get_current_user())
        if not authors and author_id == 'me':
            return '''Either there is no current user or you are not in the
              author table''', 401
        elif not authors:
            return 'author not found', 404

        try:
            author_email = authors[0].email
            request = PhabricatorRequest()
            request.connect()
            user_info = request.call('user.query', {'emails': [author_email]})

            if not user_info:
                return 'phabricator: %s not found' % author_email, 404

            author_phid = user_info[0]["phid"]

            diff_info = request.call('differential.query', {
                'authors': [author_phid],
                'status': "status-open"
            })

            diff_info.sort(key=lambda k: -1 * int(k['dateModified']))

        except requests.exceptions.ConnectionError:
            return 'Unable to connect to Phabricator', 503

        if not diff_info:
            # No diffs, no point in trying to find builds.
            return self.respond([])

        rows = list(
            db.session.query(PhabricatorDiff, Build).join(
                Build, Build.source_id == PhabricatorDiff.source_id).filter(
                    PhabricatorDiff.revision_id.in_(
                        [d['id'] for d in diff_info])))

        serialized_builds = zip(self.serialize([row.Build for row in rows]),
                                [row.PhabricatorDiff for row in rows])

        builds_map = defaultdict(list)
        for build, phabricator_diff in serialized_builds:
            builds_map[str(phabricator_diff.revision_id)].append(build)

        for d in diff_info:
            d['builds'] = builds_map[str(d['id'])]

        return self.respond(diff_info)
Ejemplo n.º 5
0
    def create_author(self, email=None, **kwargs):
        if not kwargs.get('name'):
            kwargs['name'] = ' '.join(get_sentences(1)[0].split(' ')[0:2])

        if not email:
            email = '{0}-{1}@example.com'.format(
                slugify(kwargs['name']), uuid4().hex)

        kwargs.setdefault('name', 'Test Case')

        author = Author(email=email, **kwargs)
        db.session.add(author)
        db.session.commit()

        return author
Ejemplo n.º 6
0
    def get(self, author_id):
        authors = Author.find(author_id, get_current_user())
        if not authors and author_id == 'me':
            return '', 401
        elif not authors:
            return '', 404

        queryset = Build.query.options(
            joinedload('project'),
            joinedload('author'),
            joinedload('source').joinedload('revision'),
        ).filter(
            Build.author_id.in_([a.id for a in authors])
        ).order_by(Build.date_created.desc(), Build.date_started.desc())

        return self.paginate(queryset)
Ejemplo n.º 7
0
    def get(self, author_id):
        authors = Author.find(author_id, get_current_user())
        if not authors and author_id == 'me':
            return '', 401
        elif not authors:
            return '', 404

        args = self.get_parser.parse_args()

        # serialize everything when fetching so that we batch any needed data
        # fetching. we'll still rearrange things later

        # grab recent revisions by author (for any repository/project, which
        # means we can't use vcs commands)
        sources = self.serialize(list(Source.query.join(
            Revision, Source.revision_sha == Revision.sha
        ).filter(
            Revision.author_id.in_([a.id for a in authors]),
            Source.patch_id.is_(None),
        ).order_by(
            Revision.date_committed.desc(),
        ).limit(args.num_revs)))

        # grab builds for those revisions
        commit_builds_list = self.serialize(list(Build.query.options(
            joinedload('project'),
            joinedload('author'),
        ).filter(
            Build.source_id.in_([s['id'] for s in sources]),
        ).order_by(
            Build.date_created.desc(),
            Build.date_started.desc()
        )))

        # move builds into sources
        builds_map = defaultdict(list)
        revision_list = OrderedDict()

        for build in commit_builds_list:
            builds_map[build['source']['id']].append(build)

        for source in sources:
            source['builds'] = builds_map[source['id']]

        return self.respond(sources, serialize=False)
Ejemplo n.º 8
0
def test_simple():
    revision = Revision(
        sha='33846695b2774b29a71795a009e8168a',
        repository=Repository(),
        author=Author(
            name='Foo Bar',
            email='*****@*****.**',
        ),
        parents=['a' * 40],
        branches=['master'],
        message='hello world',
        date_created=datetime(2013, 9, 19, 22, 15, 22),
    )
    result = serialize(revision)
    assert result['id'] == '33846695b2774b29a71795a009e8168a'
    assert result['author']['name'] == 'Foo Bar'
    assert result['author']['email'] == '*****@*****.**'
    assert result['message'] == 'hello world'
    assert result['dateCreated'] == '2013-09-19T22:15:22'
    assert result['parents'] == ['a' * 40]
    assert result['branches'] == ['master']
Ejemplo n.º 9
0
    def test_simple(self):
        fake_author_id = uuid4()

        self.create_build(self.project)

        path = '/api/0/authors/{0}/builds/'.format(fake_author_id.hex)

        resp = self.client.get(path)
        assert resp.status_code == 200
        data = self.unserialize(resp)
        assert len(data) == 0

        author = Author(email=self.default_user.email, name='Foo Bar')
        db.session.add(author)
        build = self.create_build(self.project, author=author)

        path = '/api/0/authors/{0}/builds/'.format(author.id.hex)

        resp = self.client.get(path)
        assert resp.status_code == 200
        data = self.unserialize(resp)
        assert len(data) == 1
        assert data[0]['id'] == build.id.hex

        path = '/api/0/authors/me/builds/'

        resp = self.client.get(path)
        assert resp.status_code == 401

        self.login(self.default_user)

        path = '/api/0/authors/me/builds/'

        resp = self.client.get(path)
        assert resp.status_code == 200
        data = self.unserialize(resp)
        assert len(data) == 1
        assert data[0]['id'] == build.id.hex
Ejemplo n.º 10
0
def validate_author(author_id):
    current_user = get_current_user()
    if author_id == 'me' and not current_user:
        raise ValueError('You are not signed in.')

    return Author.find(author_id, current_user)