Ejemplo n.º 1
0
    def test_with_two_invalid_issues(self, mock1, mock2):
        '''
        A commit with two issues with incorrect labels
        '''
        # Mock the commit message
        mock1.return_value = ('This is a test commit\n\n'
                              'Fixes: #1234\n'
                              'Updates: #4567')
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertListEqual(issues, ['1234', '4567'])

        # Handle a valid issue
        mock2.side_effect = None
        ghub = handle_github.GitHubHandler('glusterfs', True)
        with MagicMock(name='mockedgithub') as m:
            ghub.ghub = m
            label = Mock(name='mockedlabel')
            label.name = 'SpecApproved'
            ghub.ghub.issue.return_value.labels = [label]
            self.assertFalse(ghub.check_issue(issues[0]))

        with MagicMock(name='mockedgithub') as m:
            ghub.ghub = m
            label2 = Mock(name='mockedlabel')
            label2.name = 'DocApproved'
            ghub.ghub.issue.return_value.labels = [label2]
            self.assertFalse(ghub.check_issue(issues[1]))
    def test_patchset_two_commit_message_not_updated(self, mock1, mock2,
                                                     mock3):
        '''
        A second patchset event with the same bug
        '''
        # Mock the commit message
        mock1.return_value = ('This is a test commit\n\n' 'Fixes: bz#1234\n')

        # Disable bz network calls
        mock2.return_value = None

        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs', issue=False)
        bugs = c.parse_commit_message(commit_msg)
        self.assertEqual(bugs[0]['id'], '1234')

        c.revision_number = 2
        bug = handle_bugzilla.Bug(bug_id=bugs[0]['id'],
                                  bug_status=bugs[0]['status'],
                                  product='glusterfs')

        # Mock the commit message from Gerrit
        mock3.return_value = ('This is the previous commit\n\n'
                              'Updates: bz#1234')
        self.assertFalse(bug.needs_update(c, 'patchset-created'))
Ejemplo n.º 3
0
    def test_with_two_closed_issues(self, mock1, mock2):
        '''
        A commit with two closed issues
        '''
        # Mock the commit message
        mock1.return_value = (
            'This is a test commit\n\n'
            'Fixes: #1234\n'
            'Updates: #4567')
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertEqual(issues,
                [{
                    'id': '1234',
                    'status': 'Fixes'
                }, {
                    'id': '4567',
                    'status': 'Updates'
        }])

        # Handle a valid issue
        mock2.side_effect = None
        ghub = handle_github.GitHubHandler('glusterfs', True)
        ghub.ghub = Mock(name='mockedgithub')
        ghub.ghub.issue.return_value.is_closed.return_value = True
        for issue in issues:
            self.assertFalse(ghub.check_issue(issue['id']))
    def test_patchset_one_two_bugs_one_new_bug(self, mock1, mock2, mock3):
        '''
        A second patchset with one bug and first patchset with two different
        bugs
        '''
        # Mock the commit message
        mock1.return_value = ('This is a test commit\n\n' 'Fixes: bz#1234\n')

        # Disable bz network calls
        mock2.return_value = None

        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs', issue=False)
        bugs = c.parse_commit_message(commit_msg)
        self.assertEqual(bugs[0]['id'], '1234')

        c.revision_number = 2
        bug = handle_bugzilla.Bug(bug_id=bugs[0]['id'],
                                  bug_status=bugs[0]['status'],
                                  product='glusterfs')

        # Mock the commit message from Gerrit
        mock3.return_value = ('This is the previous commit\n'
                              'Fixes: bz#4566\n'
                              'Updates: bz#7890\n')
        self.assertTrue(bug.needs_update(c, 'patchset-created'))
        if hasattr(self, 'assertCountEqual'):
            # py3
            self.assertCountEqual(['4566', '7890'], bug.old_bugs)
        else:
            # py2
            self.assertItemsEqual(['4566', '7890'], bug.old_bugs)
Ejemplo n.º 5
0
    def test_with_two_valid_issues(self, mock1, mock2):
        '''
        A commit with two issues with correct labels
        '''
        # Mock the commit message
        mock1.return_value = (
            'This is a test commit\n\n'
            'Fixes: #1234\n'
            'Updates: #4567')
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertEqual(issues,
                         [{
                             'id': '1234',
                             'status': 'Fixes'
                         }, {
                             'id': '4567',
                             'status': 'Updates'
                         }]
                        )

        # Handle a valid issue
        mock2.side_effect = None
        ghub = handle_github.GitHubHandler('glusterfs', True)
        ghub.ghub = Mock(name='mockedgithub')
        label1 = Mock(name='mockedlabel')
        label1.name = 'SpecApproved'
        label2 = Mock(name='mockedlabel')
        label2.name = 'DocApproved'
        ghub.ghub.issue.return_value.labels = [label1, label2]
        for issue in issues:
            self.assertTrue(ghub.check_issue(issue['id']))
 def test_with_issue(self, mock):
     '''
     A commit with an issue
     '''
     mock.return_value = ('This is a test commit\n\n' 'Fixes: #1234\n')
     commit_msg = commit.get_commit_message()
     c = commit.CommitHandler('glusterfs', issue=False)
     bugs = c.parse_commit_message(commit_msg)
     self.assertFalse(bugs)
 def test_with_bug(self, mock):
     '''
     A commit with a bug
     '''
     mock.return_value = ('This is a test commit\n\n' 'Fixes: bz#1234\n')
     commit_msg = commit.get_commit_message()
     c = commit.CommitHandler('glusterfs', issue=False)
     bugs = c.parse_commit_message(commit_msg)
     self.assertEqual(bugs[0]['id'], '1234')
Ejemplo n.º 8
0
 def test_with_no_issue(self, mock):
     '''
     A commit with no issue mentioned
     '''
     # Mock the commit message
     mock.return_value = ('This is a test commit\n\n' 'Fixes: bz#1234\n')
     commit_msg = commit.get_commit_message()
     c = commit.CommitHandler('glusterfs')
     issues = c.parse_commit_message(commit_msg)
     self.assertListEqual(issues, [])
Ejemplo n.º 9
0
 def test_issue_with_different_repo_name(self, mock):
     '''
     A commit with issue mentioning current repo
     '''
     # Mock the commit message
     mock.return_value = ('This is a test commit\n\n'
                          'Fixes: gluster/glusterdocs#1234\n')
     commit_msg = commit.get_commit_message()
     c = commit.CommitHandler('glusterfs')
     issues = c.parse_commit_message(commit_msg)
     self.assertListEqual(issues, [])
Ejemplo n.º 10
0
 def test_issue_and_bug(self, mock):
     '''
     A commit with an issue and bug
     '''
     # Mock the commit message
     mock.return_value = ('This is a test commit\n\n'
                          'Fixes: bz#1234\n'
                          'Fixes: #4567')
     commit_msg = commit.get_commit_message()
     c = commit.CommitHandler('glusterfs')
     issues = c.parse_commit_message(commit_msg)
     self.assertListEqual(issues, ['4567'])
 def test_with_unicode(self, mock):
     '''
     A commit with an issue and bug with unicode text
     '''
     # Mock the commit message
     mock.return_value = ('This is a test commit\n\n'
                          '♥\n'
                          'Fixes: bz#1234\n'
                          'Fixes: #4567')
     commit_msg = commit.get_commit_message()
     c = commit.CommitHandler('glusterfs', issue=False)
     bugs = c.parse_commit_message(commit_msg)
     self.assertEqual(bugs[0]['id'], '1234')
 def test_issue_and_bug(self, mock):
     '''
     A commit with a bug and issue
     '''
     # Mock the commit message
     mock.return_value = ('This is a test commit\n\n'
                          'Fixes: bz#1234\n'
                          'Fixes: #4567')
     commit_msg = commit.get_commit_message()
     c = commit.CommitHandler('glusterfs', issue=False)
     bugs = c.parse_commit_message(commit_msg)
     self.assertEqual(bugs[0]['id'], '1234')
     self.assertEqual(len(bugs), 1)
Ejemplo n.º 13
0
    def test_first_revision(self, mock):
        '''
        A test to check deduplication when the revision number is 1
        '''
        # Mock the commit message
        mock.return_value = ('This is a test commit\n\n' 'Updates: #1234')

        # Parse the commit message
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertListEqual(issues, ['1234'])

        c.revision_number = 1
        deduped = c.remove_duplicates(issues)
        self.assertListEqual(deduped, issues)
    def test_with_closed_issue(self, mock1, mock2):
        '''
        A commit with closed issue
        '''
        # Mock the commit message
        mock1.return_value = ('This is a test commit\n\n' 'Fixes: #1234\n')
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertEqual(issues[0]['id'], '1234')

        # Handle a valid issue
        mock2.side_effect = None
        ghub = handle_github.GitHubHandler('glusterfs', True)
        ghub.ghub = Mock(name='mockedgithub')
        ghub.ghub.issue.return_value.is_closed.return_value = True
        self.assertFalse(ghub.check_issue(issues[0]['id']))
Ejemplo n.º 15
0
    def test_second_revision_different_issue(self, mock1, mock2):
        '''
        A test to check deduplication when second revision has a different
        issue from the first revision
        '''
        mock1.return_value = ('This is a test commit\n\n' 'Updates: #4567')
        # Parse the commit message
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertListEqual(issues, ['4567'])

        # Mock the commit message from Gerrit
        mock2.return_value = ('This is the previous commit\n\n' 'Fixes: #1234')
        c.revision_number = 2
        deduped = c.remove_duplicates(issues)
        self.assertListEqual(deduped, ['4567'])
Ejemplo n.º 16
0
    def test_with_nonexistent(self, mock1, mock2):
        '''
        A commit with a non-existing issue
        '''
        # Mock the commit message
        mock1.return_value = ('This is a test commit with\n\n'
                              'Fixes: #123456\n')
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertListEqual(issues, ['123456'])

        # Handle a valid issue
        mock2.side_effect = None
        ghub = handle_github.GitHubHandler('glusterfs', True)
        ghub.ghub = Mock(name='mockedgithub')
        ghub.ghub.issue.return_value = None
        self.assertFalse(ghub.check_issue(issues[0]))
    def test_patchset_one(self, mock1, mock2):
        '''
        The first patchset event with a bug
        '''
        # Mock the commit message
        mock1.return_value = ('This is a test commit\n\n' 'Fixes: bz#1234\n')

        # Disable bz network calls
        mock2.return_value = None

        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs', issue=False)
        bugs = c.parse_commit_message(commit_msg)
        self.assertListEqual(bugs, ['1234'])

        c.revision_number = 1
        bug = handle_bugzilla.Bug(bug_id=bugs[0], product='glusterfs')
        self.assertTrue(bug.needs_update(c, 'patchset-created'))
 def test_two_bugs(self, mock):
     '''
     A commit with two bugs
     '''
     # Mock the commit message
     mock.return_value = ('This is a test commit\n\n'
                          'Updates: bz#1234\n'
                          'Fixes: bz#4567')
     commit_msg = commit.get_commit_message()
     c = commit.CommitHandler('glusterfs', issue=False)
     bugs = c.parse_commit_message(commit_msg)
     self.assertEqual(bugs, [{
         'status': 'Updates',
         'id': '1234'
     }, {
         'status': 'Fixes',
         'id': '4567'
     }])
    def test_with_valid_bug_fix_issue(self, mock1, mock2):
        '''
        A commit with an existing issue with correct labels
        '''
        # Mock the commit message
        mock1.return_value = ('This is a test commit\n\n' 'Fixes: #1234\n')
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertEqual(issues[0]['id'], '1234')

        # Handle a valid issue
        mock2.side_effect = None
        ghub = handle_github.GitHubHandler('glusterfs', True)
        ghub.ghub = Mock(name='mockedgithub')
        label1 = Mock(name='mockedlabel')
        label1.name = 'Type:Bug'
        ghub.ghub.issue.return_value.labels = [label1]
        self.assertTrue(ghub.check_issue(issues[0]['id']))
Ejemplo n.º 20
0
    def test_second_revision_same_issue(self, mock1, mock2):
        '''
        A test to check deduplication when revision number is 2 with same issue
        in commit message
        '''
        # Mock the commit message
        mock1.return_value = ('This is a test commit\n\n' 'Updates: #1234')
        # Parse the commit message
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertListEqual(issues, ['1234'])

        # Mock the commit message from Gerrit
        mock2.return_value = ('This is the previous commit\n\n'
                              'Updates: #1234')
        c.revision_number = 2
        deduped = c.remove_duplicates(issues)
        self.assertListEqual(deduped, [])
Ejemplo n.º 21
0
 def test_commit_with_exp_branch_issue(self, mock, mock2):
     '''
     A commit in the experimental branch
     '''
     # Mock the commit message
     mock.return_value = ('This is a test commit\n\n' 'Fixes: #1234\n')
     commit_msg = commit.get_commit_message()
     c = commit.CommitHandler('glusterfs')
     issues = c.parse_commit_message(commit_msg)
     self.assertListEqual(issues, ['1234'])
     # Handle a valid issue
     mock2.side_effect = None
     ghub = handle_github.GitHubHandler('glusterfs', True)
     with MagicMock(name='mockedgithub') as m:
         ghub.ghub = m
         label = Mock(name='mockedlabel')
         label.name = 'SpecApproved'
         ghub.ghub.issue.return_value.labels = [label]
         ghub.branch = 'experimental'
         self.assertTrue(ghub.check_issue(issues[0]))
    def test_patchset_patch_merged(self, mock1, mock2, mock3):
        '''
        Change merged event
        '''
        # Mock the commit message
        mock1.return_value = ('This is a test commit\n\n' 'Fixes: bz#1234\n')

        # Disable bz network calls
        mock2.return_value = None

        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs', issue=False)
        bugs = c.parse_commit_message(commit_msg)
        self.assertEqual(bugs[0]['id'], '1234')
        self.assertEqual(len(bugs), 1)

        c.revision_number = 2
        mock3.return_value = mock1.return_value
        bug = handle_bugzilla.Bug(bug_id=bugs[0]['id'],
                                  bug_status=bugs[0]['status'],
                                  product='glusterfs')
        self.assertTrue(bug.needs_update(c, 'change-merged'))
    def test_second_revision_new_issue(self, mock1, mock2):
        '''
        A test to check deduplication when the secon revision has an added
        issue
        '''
        mock1.return_value = ('This is a test commit\n\n'
                              'Updates: #1234\n'
                              'Fixes: #4567')
        # Parse the commit message
        commit_msg = commit.get_commit_message()
        c = commit.CommitHandler('glusterfs')
        issues = c.parse_commit_message(commit_msg)
        self.assertEqual(len(issues), 2)
        self.assertEqual(issues[0], {'id': '1234', 'status': 'Updates'})
        self.assertEqual(issues[1], {'id': '4567', 'status': 'Fixes'})

        # Mock the commit message from Gerrit
        mock2.return_value = ('This is the previous commit\n\n'
                              'Updates: #1234')
        c.revision_number = 2
        deduped = c.remove_duplicates(issues)
        self.assertListEqual(deduped, ['4567'])
def main(dry_run=True, abandon=False, restore=False):
    """
    Main function where everything comes together
    """
    if os.getenv("GERRIT_PROJECT") != "glusterfs":
        return False

    # Dict of change-related info
    change = {
        "url": os.getenv("GERRIT_CHANGE_URL"),
        "sub": os.getenv("GERRIT_CHANGE_SUBJECT"),
        "revision_number": os.getenv("GERRIT_PATCHSET_NUMBER"),
        "branch": os.getenv("GERRIT_BRANCH"),
        "uploader_name": os.getenv("GERRIT_PATCHSET_UPLOADER_NAME"),
        "event": os.getenv("GERRIT_EVENT_TYPE"),
        "number": os.getenv("GERRIT_CHANGE_NUMBER"),
    }

    # get commit message
    commit_obj = commit.CommitHandler(repo=None, issue=False)
    commit_msg = commit.get_commit_message()

    # get bugs from commit message
    bugs = commit_obj.parse_commit_message(commit_msg)

    # There should only be one bug. In the event, there's more than one, it's
    # a parse error. Raise the error rather than silently ignoring it
    if len(bugs) > 1:
        raise Exception(
            "More than one bug found in the commit message {}".format(bugs))
    elif not bugs:
        print("No bugs found in the commit message")
        return True

    # Create a bug object from ID
    print("Creating bug object")
    bug = Bug(
        bug_id=bugs[0]["id"],
        bug_status=bugs[0]["status"],
        product="GlusterFS",
        dry_run=dry_run,
    )

    print("Product check")
    # Check that the product is correct
    if not bug.product_check():
        raise Exception(
            "This bug is not filed in the {} product".format("GlusterFS"))

    if abandon:
        bug.abandon(change)
        return True
    if restore:
        bug.restore(change)
        return True
    # Check that the bug needs an update based on the event and the revision
    # number
    if not bug.needs_update(commit_obj, change["event"]):
        return True
    print("Posting update")
    bug.post_update(change)
    return True