def step8(self):
     # creates the merge request
     sys.argv[1] = 'setrole'
     del sys.argv[2:]
     # project setrole
     sys.argv.append('-p')
     # ARG role
     sys.argv.append(self.__class__.user_role)
     # ARG user
     sys.argv.append(self.__class__.user_ldap_member)
     # ARG project
     sys.argv.append(self.__class__.project_2)
     gitutils.main()
     time.sleep(2)
     project_id = gitlab_utils.get_project_id('hax_l',
                                              self.__class__.project_2)
     members = gitlab_utils.get_project(project_id).members.all(all=True)
     for i in members:
         if i.attributes['name'] == self.__class__.user_ldap_member:
             found = True
             self.assertEqual(
                 i.attributes['access_level'],
                 gitlab_utils.check_role(self.__class__.user_role))
     self.assertTrue(found)
     time.sleep(2)
     #################################
     ##### CLEANUP OF TEST STUFF #####
     #################################
     # removal of personal project
     gitlab_utils.remove_project(project_id)
     # removal of test group
     gitlab_utils.remove_group(
         gitlab_utils.get_group_id(self.__class__.group_name))
    def step7(self):
        # creates the merge request
        sys.argv[1] = 'merge'
        del sys.argv[2:]

        sys.argv.append('-p')
        sys.argv.append(gitlab_utils.get_username() + '/' +
                        self.__class__.project_2)
        # ARG TITLE
        sys.argv.append('-t')
        sys.argv.append(self.__class__.merge_request_title)
        # ARG Description
        sys.argv.append('-d')
        sys.argv.append(self.__class__.merge_request_description)
        gitutils.main()
        time.sleep(2)

        # verifies if the merge request exists on the forked project
        project_id = gitlab_utils.get_project_id(self.__class__.group_name,
                                                 self.__class__.project_2)
        project = gitlab_utils.get_project(project_id)
        mrs = project.mergerequests.list(state='opened')
        self.assertEqual(mrs[0].attributes['title'],
                         self.__class__.merge_request_title)
        self.assertEqual(mrs[0].attributes['description'],
                         self.__class__.merge_request_description_ref)
        time.sleep(2)
 def step31(self):
     # CMD
     sys.argv[1] = 'fork'
     # deletes previous args
     del sys.argv[2:]
     # ARG PROJECT
     # not clone
     sys.argv.append('-n')
     # removes previous fork and creates a new one
     sys.argv.append('-c')
     sys.argv.append(self.__class__.group_name + '/' +
                     self.__class__.project_2)
     # calls main to fork
     gitutils.main()
     # verifies if forked project exists under personal account
     time.sleep(2)
     own_projects = gitlab_utils.get_owned_projects()
     found = any(proj['name'] == self.__class__.project_2
                 for proj in own_projects)
     self.assertTrue(found)
     time.sleep(2)
     # creates a change
     own_projects = gitlab_utils.get_owned_projects()
     project = None
     for proj in own_projects:
         if proj['name'] == self.__class__.project_2:
             project = gitlab_utils.get_project(proj['id'])
     self.assertIsNotNone(project)
     # creates the commit
     data = {
         'branch':
         'master',
         'commit_message':
         'unit test commit',
         'actions': [{
             'action': 'create',
             'file_path': 'README_new.rst',
             'content': "UNIT TEST GITUTILS",
         }]
     }
     project.commits.create(data)
     time.sleep(2)
Ejemplo n.º 4
0
def set_role(role, username, git_groups, project_flag):
    """
    Sets a role to a specified user in a specified group or project
    :param role: Role that will be given to the user.
    :type role: int
    :param username: Username
    :type username: str
    :param git_group: Group or project names
    :type git_group: list
    :param project_flag: Flag to indicate project-level role.
    :type project_flag: bool
    :return:
    """
    # gets user_id
    user_id = gitlab_utils.get_user_id(username)
    if user_id == -1:
        raise gitutils_exception.GitutilsError(
            const.ROLE_SETROLE_PROBLEM_USERID)
    #################
    # GROUP SETROLE #
    #################
    if not project_flag:
        for git_group in git_groups:
            group_id = gitlab_utils.get_group_id(git_group)
            print(const.SETROLE_INIT_MSG % (
                const.bcolors.BOLD,
                role,
                const.bcolors.ENDC,
                const.bcolors.BOLD,
                username,
                user_id,
                const.bcolors.ENDC,
                const.bcolors.BOLD,
                git_group,
                group_id,
                const.bcolors.ENDC,
            ),
                  end="")
            # gets group
            group = gitlab_utils.get_group(git_group)
            # adds member with the desired access level to the group
            group.members.create({'user_id': user_id, 'access_level': role})
            # verification
            members = group.members.all(all=True)
            found = any(member.attributes['username'] == username
                        for member in members)
            if not found:
                print(' ⨯')
                raise gitutils_exception.GitutilsError(
                    const.SETROLE_PROJECT_VALIDATION_FAILS)
            print(' ✓')
    else:
        ###################
        # PROJECT SETROLE #
        ###################
        # git_groups has projects
        for project_name in git_groups:
            print(const.SETROLE_P_INIT_MSG % (
                const.bcolors.BOLD,
                username,
                role,
                const.bcolors.ENDC,
                const.bcolors.BOLD,
                project_name,
                const.bcolors.ENDC,
            ),
                  end="")
            # gets group name
            group_name = gitlab_utils.get_project_group_simplified(
                project_name)
            # gets project id
            project_id = gitlab_utils.get_project_id(group_name, project_name)
            # gets the project
            project = gitlab_utils.get_project(project_id)
            # adds member with the desired role to the project
            project.members.create({'user_id': user_id, 'access_level': role})
            # verification
            members = project.members.all(all=True)
            found = any(member.attributes['username'] == username
                        for member in members)
            if not found:
                print(' ⨯')
                raise gitutils_exception.GitutilsError(
                    const.SETROLE_PROJECT_VALIDATION_FAILS)
            print(' ✓')