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 main(repo, dry_run, comment_file=False):
    '''
    The main function for this program. The actual program execution happens
    here
    '''
    github = GitHubHandler(repo, dry_run, comment_file)
    commit = CommitHandler(repo)
    # get commit message
    commit_msg = get_commit_message()

    # check if updates/fixes: xxx appears in the commit
    issues = commit.parse_commit_message(commit_msg)
    issue_check_success = 0

    if issues:
        for issue in issues:
            if not github.check_issue(issue['id']):
                issue_check_success = 1

            github.write_error_string()
        # remove duplicates from previous commit message (if any)
        newissues = commit.remove_duplicates(issues)
        # comment on issue: xxx about the review
        github.comment_on_issues(newissues, commit_msg)
    sys.exit(issue_check_success)
Exemple #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']))
Exemple #4
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 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
Exemple #6
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_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)
    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'))
    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_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'))
    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)
 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')
 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)
Exemple #14
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, [])
Exemple #15
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, [])
 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')
Exemple #18
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_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, [])
 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, [])
 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)
 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_unicode(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.assertEqual(issues[0]['id'], '4567')
 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)
def main():
    '''
    Main function where function call and variables are initiated
    '''
    # check if the project is GlusterFS
    if os.getenv('GERRIT_PROJECT') != 'glusterfs':
        return False

    #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 bug found in the commit message. Mention the bug that needs \
              to be backported in the specific release branch")
        return False

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

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

    # get all the values from Jenkins environment variables
    change_id = os.environ.get('GERRIT_CHANGE_ID')
    branch = os.environ.get('GERRIT_BRANCH')
    project = os.environ.get('GERRIT_PROJECT')
    patchset_number = os.environ.get('GERRIT_PATCHSET_NUMBER')
    bz_username = os.environ.get('HTTP_USERNAME')
    bz_password = os.environ.get('HTTP_PASSWORD')
    # todo: Get proper commit message with new bug id
    message = os.environ.get('GERRIT_CHANGE_COMMIT_MESSAGE')
    commit_message = '{}\n\nChange-Id: {}'.format(message, change_id)
    comment_added = os.environ.get('GERRIT_CHANGE_COMMENT_TEXT')
    version = comment_added.split(' ')[1]
    component = comment_added.split(' ')[2]

    cloned_bug = clone_bug(bug, component=component, version=version)
    gerrit_update(cloned_bug.id, version, change_id, branch, project,
                  patchset_number, commit_message, bz_username, bz_password)
Exemple #27
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)
Exemple #28
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'])
    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']))
    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_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.assertEqual(issues[0]['id'], '1234')

        c.revision_number = 1
        deduped = c.remove_duplicates(issues)
        self.assertListEqual(deduped, issues)
Exemple #33
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]))
Exemple #34
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, [])
    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']))
    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']))
    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'))
Exemple #38
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_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.assertEqual(issues[0]['id'], '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'])
    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']))
    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_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_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.assertEqual(issues[0]['id'], '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, [])
 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.assertEqual(issues[0]['id'], '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]['id']))
    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 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