Esempio n. 1
0
    def add_member_to_gitlab(self, user, identity):
        gitlab_services = TenantServiceInfo.objects.only(
            'git_project_id',
            'git_url').filter(tenant_id=self.tenant.tenant_id,
                              git_project_id__gt=0)

        if identity in ('admin', 'developer'):
            gitlab_identity = gitlab_identity_map.get(identity)
            try:
                added_pids = []
                for s in gitlab_services:
                    if s.git_url is None:
                        break

                    project_id = s.git_project_id
                    if project_id in added_pids:
                        break
                    parsed_git_url = git_url_parse(s.git_url)
                    if parsed_git_url.host == 'code.goodrain.com':
                        logger.info(
                            "perm.gitlab",
                            "add user {0} into project {1} with address {2}".
                            format(user.nick_name, project_id, s.git_url))
                        codeRepositoriesService.addProjectMember(
                            project_id, user.git_user_id, gitlab_identity)
                        added_pids.append(project_id)
            except Exception, e:
                logger.exception("perm.gitlab", e)
Esempio n. 2
0
    def do_gitlab_perm_works(self, user, identity):
        gitlab_services = TenantServiceInfo.objects.only(
            'git_project_id',
            'git_url').filter(tenant_id=self.tenant.tenant_id,
                              git_project_id__gt=0)

        added_pids = []
        for s in gitlab_services:
            project_id = s.git_project_id
            if s.git_url is None:
                break

            if project_id in added_pids:
                break

            parsed_git_url = git_url_parse(s.git_url)
            if parsed_git_url.host != 'code.goodrain.com':
                return

            try:
                current_members = codeRepositoriesService.listProjectMembers(
                    project_id)
                is_member = self.user_exists_in_gitlab(user, current_members)

                if identity in ('admin', 'developer'):
                    gitlab_identity = gitlab_identity_map.get(identity)
                    if is_member:
                        logger.info(
                            "perm.gitlab",
                            "modify user {0} identity for project {1} with address {2}"
                            .format(user.nick_name, project_id, s.git_url))
                        codeRepositoriesService.editMemberIdentity(
                            project_id, user.git_user_id, gitlab_identity)
                    else:
                        logger.info(
                            "perm.gitlab",
                            "add user {0} into project {1} with address {2}".
                            format(user.nick_name, project_id, s.git_url))
                        codeRepositoriesService.addProjectMember(
                            project_id, user.git_user_id, gitlab_identity)
                elif identity == 'remove':
                    if is_member:
                        logger.info(
                            "perm.gitlab",
                            "remove user {0} perms from project {1} with address {2}"
                            .format(user.nick_name, project_id, s.git_url))
                        codeRepositoriesService.deleteProjectMember(
                            project_id, user.git_user_id)
                else:
                    if is_member:
                        logger.info(
                            "perm.gitlab",
                            "remove user {0} perms from project {1} with address {2}"
                            .format(user.nick_name, project_id, s.git_url))
                        codeRepositoriesService.deleteProjectMember(
                            project_id, user.git_user_id)
                added_pids.append(project_id)
            except Exception, e:
                logger.exception("perm.gitlab", e)
                continue
Esempio n. 3
0
    def post(self, request, *args, **kwargs):
        email = request.POST.get('email')
        identity = request.POST.get('identity')

        result = {
            "ok": True,
            "email": email,
            "identity": identity,
            "desc": None
        }

        try:
            user = Users.objects.get(email=email)
            try:
                PermRelService.objects.get(user_id=user.pk,
                                           service_id=self.service.pk)
                result['desc'] = u"{0}已经有应用权限了".format(user.nick_name)
            except PermRelService.DoesNotExist:
                PermRelService.objects.create(user_id=user.pk,
                                              service_id=self.service.pk,
                                              identity=identity)
                try:
                    PermRelTenant.objects.get(user_id=user.pk,
                                              tenant_id=self.tenant.pk)
                except PermRelTenant.DoesNotExist:
                    PermRelTenant.objects.create(user_id=user.pk,
                                                 tenant_id=self.tenant.pk,
                                                 identity='access')
                result['desc'] = u"已向{0}授权".format(user.nick_name)
                result['show'] = True

                # add gitlab project member
                if self.service.git_url is not None:
                    parsed_git_url = git_url_parse(self.service.git_url)
                    if parsed_git_url.host == 'code.goodrain.com':

                        git_project_id = self.service.git_project_id
                        if git_project_id > 0 and user.git_user_id > 0:
                            if identity in ("developer", "admin"):
                                gitlab_identity = gitlab_identity_map.get(
                                    identity)
                                codeRepositoriesService.addProjectMember(
                                    git_project_id, user.git_user_id,
                                    gitlab_identity)
                                logger.info(
                                    "perm.gitlab",
                                    "add user {0} into project {1} with address {2}"
                                    .format(user.nick_name, git_project_id,
                                            self.service.git_url))

        except Users.DoesNotExist:
            send_invite_mail_withHtml(
                email,
                self.invite_content(email, self.tenant.tenant_name,
                                    self.service.service_alias, identity))
            result['desc'] = u'已向{0}发送邀请邮件'.format(email)

        return JsonResponse(result, status=200)
Esempio n. 4
0
    def do_gitlab_perm_works(self, user, identity):
        if self.service.git_url is None:
            return

        project_id = self.service.git_project_id
        if project_id > 0:
            parsed_git_url = git_url_parse(self.service.git_url)
            if parsed_git_url.host != 'code.goodrain.com':
                return
            try:
                current_members = codeRepositoriesService.listProjectMembers(
                    project_id)
                is_member = self.user_exists_in_gitlab(user, current_members)

                if identity in ('admin', 'developer'):
                    gitlab_identity = gitlab_identity_map.get(identity)
                    if is_member:
                        logger.info(
                            "perm.gitlab",
                            "modify user {0} identity for project {1} with address {2}"
                            .format(user.nick_name, project_id,
                                    self.service.git_url))
                        codeRepositoriesService.editMemberIdentity(
                            project_id, user.git_user_id, gitlab_identity)
                    else:
                        logger.info(
                            "perm.gitlab",
                            "add user {0} into project {1} with address {2}".
                            format(user.nick_name, project_id,
                                   self.service.git_url))
                        codeRepositoriesService.addProjectMember(
                            project_id, user.git_user_id, gitlab_identity)
                elif identity == 'remove':
                    if is_member:
                        logger.info(
                            "perm.gitlab",
                            "remove user {0} perms from project {1} with address {2}"
                            .format(user.nick_name, project_id,
                                    self.service.git_url))
                        codeRepositoriesService.deleteProjectMember(
                            project_id, user.git_user_id)
                else:
                    if is_member:
                        logger.info(
                            "perm.gitlab",
                            "remove user {0} perms from project {1} with address {2}"
                            .format(user.nick_name, project_id,
                                    self.service.git_url))
                        codeRepositoriesService.deleteProjectMember(
                            project_id, user.git_user_id)
            except Exception, e:
                logger.exception('perm.gitlab', e)
Esempio n. 5
0
 def get_service_code_branch(self, user, service):
     if service.service_source == AppConstants.SOURCE_CODE:
         code_type = ""
         parsed_git_url = git_url_parse(service.git_url)
         if service.code_from.startswith("gitlab"):
             code_type = "gitlab"
         elif parsed_git_url.host.endswith('github.com'):
             code_type = "github"
         code, msg, branchs = self.get_code_branch(user, code_type, service.git_url, service.git_project_id)
         if code != 200:
             return []
         return branchs
     return []
Esempio n. 6
0
 def get_code_branch(self, user, code_type, git_url, git_project_id):
     parsed_git_url = git_url_parse(git_url)
     host = parsed_git_url.host
     if host:
         if code_type == "gitlab":
             git_project_id = int(git_project_id)
             if git_project_id is None:
                 return 400, "gitlab检测需提供检测的代码ID", None
             branches = self.__get_gitlab_branchs(git_project_id)
         elif code_type == "github":
             branches = self.__get_github_branchs(user, parsed_git_url)
         else:
             branches = ["master"]
     else:
         branches = []
     return 200, "success", branches
Esempio n. 7
0
 def get_service_code_branch(self, user, service):
     if service.service_source == AppConstants.SOURCE_CODE:
         code_type = ""
         parsed_git_url = git_url_parse(service.git_url, False)
         if service.code_from.startswith(
                 "gitlab") and service.code_from != "gitlab_manual":
             code_type = "gitlab"
         else:
             if parsed_git_url.host:
                 if parsed_git_url.host.endswith('github.com'):
                     code_type = "github"
         code, msg, branchs = self.get_code_branch(
             user,
             code_type,
             service.git_url,
             service.git_project_id,
             current_branch=service.code_version)
         if code != 200:
             return []
         return branchs
     return []