コード例 #1
0
    def test_mergeable_pull_requests(self):
        pulls = load_data('github-open-pulls.json')
        orgs = load_data('github-user-orgs.json')
        comments = load_data('github-pull-request-comments.json')

        responses.add(
            responses.GET,
            'https://api.github.com/users/mitechie/orgs',
            body=orgs,
            status=200,
            content_type='application/json'
        )
        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls',
            body=pulls,
            status=200,
            content_type='application/json'
        )
        responses.add(
            responses.GET,
            (
                u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
                u'comments'
            ),
            body=comments,
            status=200,
            content_type='application/json'
        )

        info = GithubInfo('CanonicalJS', 'juju-gui', 'jujugui', None)
        mergeable = mergeable_pull_requests('$$merge$$', info)

        self.assertEqual(1, len(mergeable))
        self.assertEqual(5, mergeable[0]['number'])
コード例 #2
0
    def test_pull_request_build_failed(self):
        """Adds a comment to the pull request about the failure."""
        new_comment = load_data('github-new-issue-comment.json')
        pulls = load_data('github-open-pulls.json', load_json=True)
        pull_request = pulls[0]

        responses.add(
            responses.POST,
            (
                u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
                u'comments'
            ),
            body=new_comment,
            status=201,
            content_type='application/json'
        )

        info = GithubInfo('CanonicalJS', 'juju-gui', 'jujugui', '1234')

        result = pull_request_build_failed(
            pull_request,
            'http://jenkins.com/job/gui/12',
            'Failure message',
            info
        )

        self.assertTrue('body' in result)
コード例 #3
0
    def test_merge_pull_request_fails(self):
        merge_response = load_data('github-merge-failed.json')
        pulls = load_data('github-open-pulls.json', load_json=True)
        pull_request = pulls[0]

        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/4',
            body=json.dumps(pull_request),
            status=200,
            content_type='application/json'
        )

        responses.add(
            responses.PUT,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/4/merge',
            body=merge_response,
            status=405,
            content_type='application/json'
        )

        info = GithubInfo('CanonicalJS', 'juju-gui', 'jujugui', None)
        result = merge_pull_request(
            4,
            'http://jenkins.com/job/gui/12',
            info
        )

        self.assertEqual(False, result['merged'])
        self.assertEqual("Failure reason", result['message'])
コード例 #4
0
    def test_no_mergeable_pull_requests(self):
        pulls = load_data('github-open-pulls.json')

        responses.add(
            responses.GET,
            'https://api.github.com/repos/juju/project/pulls',
            body=pulls,
            status=200,
            content_type='application/json'
        )

        comments = load_data(
            'github-pull-request-comments.json',
            load_json=True)
        # Remove the first comment since it's the trigger one.
        comments.pop(0)

        responses.add(
            responses.GET,
            (
                u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
                u'comments'
            ),
            body=json.dumps(comments),
            status=200,
            content_type='application/json'
        )

        info = GithubInfo('juju', 'project', 'jujugui', None)
        mergeable = mergeable_pull_requests('$$merge$$', info)

        self.assertEqual(0, len(mergeable))
コード例 #5
0
    def test_merge_pull_request_kicker(self):
        # Fake out the data for the github requests.
        pulls = load_data('github-open-pulls.json')
        orgs = load_data('github-user-orgs.json')
        comments = load_data('github-pull-request-comments.json')
        new_comment = load_data('github-new-issue-comment.json')

        responses.add(
            responses.GET,
            'https://api.github.com/users/mitechie/orgs',
            body=orgs,
            status=200,
            content_type='application/json'
        )
        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls',
            body=pulls,
            status=200,
            content_type='application/json'
        )
        responses.add(
            responses.GET,
            (
                u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
                u'comments'
            ),
            body=comments,
            status=200,
            content_type='application/json'
        )
        responses.add(
            responses.POST,
            (
                u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
                u'comments'
            ),
            body=new_comment,
            status=200,
            content_type='application/json'
        )

        fake_config = {
            'github.owner': 'CanonicalJS',
            'github.project': 'juju-gui',
            'github.username': '******',
            'github.token': '1234',
            'jenkins.merge.url': 'http://jenkins/job/{0}/build',
            'jenkins.merge.job': 'juju-gui-merge',
            'jenkins.merge.token': 'buildme',
            'jenkins.merge.trigger': '$$merge$$',
        }

        with mock.patch('jenkinsgithublander.jobs.kick_jenkins_merge'):
            kicked = kick_mergeable_pull_requests(fake_config)

        self.assertEqual(1, len(kicked))
        self.assertTrue(
            kicked[0].startswith('Kicking pull request: 5 at sha '))
コード例 #6
0
    def test_merge_pull_request_kicker(self):
        # Fake out the data for the github requests.
        pulls = load_data('github-open-pulls.json')
        orgs = load_data('github-user-orgs.json')
        comments = load_data('github-pull-request-comments.json')
        new_comment = load_data('github-new-issue-comment.json')

        responses.add(responses.GET,
                      'https://api.github.com/users/mitechie/orgs',
                      body=orgs,
                      status=200,
                      content_type='application/json')
        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls',
            body=pulls,
            status=200,
            content_type='application/json')
        responses.add(
            responses.GET,
            (u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
             u'comments'),
            body=comments,
            status=200,
            content_type='application/json')
        responses.add(
            responses.POST,
            (u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
             u'comments'),
            body=new_comment,
            status=200,
            content_type='application/json')

        fake_config = {
            'github.owner': 'CanonicalJS',
            'github.project': 'juju-gui',
            'github.username': '******',
            'github.token': '1234',
            'jenkins.merge.url': 'http://jenkins/job/{0}/build',
            'jenkins.merge.job': 'juju-gui-merge',
            'jenkins.merge.token': 'buildme',
            'jenkins.merge.trigger': '$$merge$$',
        }

        with mock.patch('jenkinsgithublander.jobs.kick_jenkins_merge'):
            kicked = kick_mergeable_pull_requests(fake_config)

        self.assertEqual(1, len(kicked))
        self.assertTrue(
            kicked[0].startswith('Kicking pull request: 5 at sha '))
コード例 #7
0
    def test_merge_pull_request_fail_unplanned(self):
        """Still throws exception on expected request failure."""
        pulls = load_data('github-open-pulls.json', load_json=True)
        pull_request = pulls[0]

        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/4',
            body=json.dumps(pull_request),
            status=200,
            content_type='application/json'
        )
        responses.add(
            responses.PUT,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/4/merge',
            body='Not Found',
            status=404,
            content_type='application/json'
        )

        info = GithubInfo('CanonicalJS', 'juju-gui', 'jujugui', None)

        self.assertRaises(
            GithubError,
            merge_pull_request,
            4,
            'http://jenkins.com/job/gui/12',
            info
        )
コード例 #8
0
    def test_mark_pull_request_build_failed(self):
        # Fake out the data for the github requests.
        pull_request = 5
        build_number = 10
        pulls = load_data('github-open-pulls.json', load_json=True)
        comment = load_data('github-new-issue-comment.json', load_json=True)
        pull_data = pulls[0]

        # Will need to mock out the pull request get, the comment response.
        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/5',
            body=json.dumps(pull_data),
            status=200,
            content_type='application/json'
        )
        responses.add(
            responses.POST,
            (
                u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
                u'comments'
            ),
            body=json.dumps(comment),
            status=200,
            content_type='application/json'
        )

        fake_config = {
            'github.owner': 'CanonicalJS',
            'github.project': 'juju-gui',
            'github.username': '******',
            'github.token': '1234',
            'jenkins.merge.url': 'http://jenkins/job/{0}/build',
            'jenkins.merge.job': 'juju-gui-merge',
            'jenkins.merge.token': 'buildme',
            'jenkins.merge.trigger': '$$merge$$',
        }

        resp = mark_pull_request_build_failed(
            pull_request,
            build_number,
            'build Failed',
            fake_config
        )

        self.assertTrue(resp.startswith('https://api.github.com'))
コード例 #9
0
    def test_merge_pull_request(self):
        # Fake out the data for the github requests.
        pull_request = 5
        build_number = 10
        merged = load_data('github-merge-success.json')
        pulls = load_data('github-open-pulls.json', load_json=True)
        pull_request_data = pulls[0]

        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/5',
            body=json.dumps(pull_request_data),
            status=200,
            content_type='application/json'
        )
        # Will need to mock out the pull request get, the comment response.
        responses.add(
            responses.PUT,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/5/merge',
            body=merged,
            status=200,
            content_type='application/json'
        )

        fake_config = {
            'github.owner': 'CanonicalJS',
            'github.project': 'juju-gui',
            'github.username': '******',
            'github.token': '1234',
            'jenkins.merge.url': 'http://jenkins/job/{0}/build',
            'jenkins.merge.job': 'juju-gui-merge',
            'jenkins.merge.token': 'buildme',
            'jenkins.merge.trigger': '$$merge$$',
        }

        resp = do_merge_pull_request(
            pull_request,
            build_number,
            fake_config
        )

        self.assertEqual('Pull Request successfully merged', resp)
コード例 #10
0
    def test_pull_request_kicked(self):
        new_comment = load_data('github-new-issue-comment.json')
        pulls = load_data('github-open-pulls.json', load_json=True)
        pull_request = pulls[0]

        responses.add(
            responses.POST,
            (
                u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
                u'comments'
            ),
            body=new_comment,
            status=201,
            content_type='application/json'
        )

        info = GithubInfo('juju', 'project', 'jujugui', None)
        resp = pull_request_kicked(pull_request, 'http://jenkins/job/1', info)
        comment = resp['body']
        self.assertIn(github.MERGE_SCHEDULED, comment)
コード例 #11
0
    def test_not_mergable_if_already_merging(self):
        pulls = load_data('github-open-pulls.json')
        orgs = load_data('github-user-orgs.json')
        comments = load_data(
            'github-pull-request-comments.json', load_json=True)

        # Add the currently merging comment to the list of the pull request to
        # verify it does not mark this as a mergable pull request then.
        merging_comment = load_data(
            'github-new-issue-comment.json', load_json=True)
        comments.append(merging_comment)

        responses.add(
            responses.GET,
            'https://api.github.com/users/mitechie/orgs',
            body=orgs,
            status=200,
            content_type='application/json'
        )
        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls',
            body=pulls,
            status=200,
            content_type='application/json'
        )
        responses.add(
            responses.GET,
            (
                u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
                u'comments'
            ),
            body=json.dumps(comments),
            status=200,
            content_type='application/json'
        )

        info = GithubInfo('CanonicalJS', 'juju-gui', 'jujugui', None)
        mergeable = mergeable_pull_requests('$$merge$$', info)

        self.assertEqual(0, len(mergeable))
コード例 #12
0
    def test_not_mergeable_if_not_in_org(self):
        pulls = load_data('github-open-pulls.json')
        orgs = load_data('github-user-orgs.json', load_json=True)
        comments = load_data('github-pull-request-comments.json')

        # Remove the CanonicalJS group so that the user fails to be in the
        #org.
        orgs.pop(0)

        responses.add(
            responses.GET,
            'https://api.github.com/users/mitechie/orgs',
            body=json.dumps(orgs),
            status=200,
            content_type='application/json'
        )

        responses.add(
            responses.GET,
            'https://api.github.com/repos/juju/project/pulls',
            body=pulls,
            status=200,
            content_type='application/json'
        )

        responses.add(
            responses.GET,
            (
                u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
                u'comments'
            ),
            body=comments,
            status=200,
            content_type='application/json'
        )

        info = GithubInfo('juju', 'project', 'jujugui', None)
        mergeable = mergeable_pull_requests('$$merge$$', info)

        self.assertEqual(0, len(mergeable))
コード例 #13
0
    def test_mark_pull_request_build_failed(self):
        # Fake out the data for the github requests.
        pull_request = 5
        build_number = 10
        pulls = load_data('github-open-pulls.json', load_json=True)
        comment = load_data('github-new-issue-comment.json', load_json=True)
        pull_data = pulls[0]

        # Will need to mock out the pull request get, the comment response.
        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/5',
            body=json.dumps(pull_data),
            status=200,
            content_type='application/json')
        responses.add(
            responses.POST,
            (u'https://api.github.com/repos/CanonicalJS/juju-gui/issues/5/'
             u'comments'),
            body=json.dumps(comment),
            status=200,
            content_type='application/json')

        fake_config = {
            'github.owner': 'CanonicalJS',
            'github.project': 'juju-gui',
            'github.username': '******',
            'github.token': '1234',
            'jenkins.merge.url': 'http://jenkins/job/{0}/build',
            'jenkins.merge.job': 'juju-gui-merge',
            'jenkins.merge.token': 'buildme',
            'jenkins.merge.trigger': '$$merge$$',
        }

        resp = mark_pull_request_build_failed(pull_request, build_number,
                                              'build Failed', fake_config)

        self.assertTrue(resp.startswith('https://api.github.com'))
コード例 #14
0
    def test_user_is_in_org(self):
        user_orgs = load_data('github-user-orgs.json')
        responses.add(
            responses.GET,
            'https://api.github.com/users/jujugui/orgs',
            body=user_orgs,
            status=200,
            content_type='application/json'
        )

        info = GithubInfo('juju', 'gui', 'jujugui', '1234')
        in_org = github.user_is_in_org('jujugui', 'CanonicalJS', info)

        self.assertTrue(in_org)
コード例 #15
0
    def test_merge_pull_request(self):
        # Fake out the data for the github requests.
        pull_request = 5
        build_number = 10
        merged = load_data('github-merge-success.json')
        pulls = load_data('github-open-pulls.json', load_json=True)
        pull_request_data = pulls[0]

        responses.add(
            responses.GET,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/5',
            body=json.dumps(pull_request_data),
            status=200,
            content_type='application/json')
        # Will need to mock out the pull request get, the comment response.
        responses.add(
            responses.PUT,
            'https://api.github.com/repos/CanonicalJS/juju-gui/pulls/5/merge',
            body=merged,
            status=200,
            content_type='application/json')

        fake_config = {
            'github.owner': 'CanonicalJS',
            'github.project': 'juju-gui',
            'github.username': '******',
            'github.token': '1234',
            'jenkins.merge.url': 'http://jenkins/job/{0}/build',
            'jenkins.merge.job': 'juju-gui-merge',
            'jenkins.merge.token': 'buildme',
            'jenkins.merge.trigger': '$$merge$$',
        }

        resp = do_merge_pull_request(pull_request, build_number, fake_config)

        self.assertEqual('Pull Request successfully merged', resp)
コード例 #16
0
    def test_open_pull_requests(self):
        """Verify we can parse the list."""
        resp_json = load_data('github-open-pulls.json')

        responses.add(
            responses.GET,
            'https://api.github.com/repos/juju/project/pulls',
            body=resp_json,
            status=200,
            content_type='application/json'
        )

        info = GithubInfo('juju', 'project', 'jujugui', None)
        open_requests = get_open_pull_requests(info)

        self.assertEqual(1, len(open_requests))
        self.assertTrue(
            open_requests[0]['_links']['comments']['href'].endswith(
                '/repos/CanonicalJS/juju-gui/issues/5/comments',
            )
        )