def get_slack_usernames_from_owners(owners_from_repo, users, usergroup):
    if owners_from_repo is None:
        return []

    all_slack_usernames = []

    for url_ref in owners_from_repo:
        # allow passing repo_url:ref to select different branch
        if url_ref.count(":") == 2:
            url, ref = url_ref.rsplit(":", 1)
        else:
            url = url_ref
            ref = "master"

        repo_cli = GitApi(url)

        if isinstance(repo_cli, GitLabApi):
            user_key = "org_username"
            missing_user_log_method = logging.warning
        elif isinstance(repo_cli, GithubApi):
            user_key = "github_username"
            missing_user_log_method = logging.debug
        else:
            raise TypeError(f"{type(repo_cli)} not supported")

        repo_owners = RepoOwners(git_cli=repo_cli, ref=ref)

        owners = repo_owners.get_root_owners()
        all_owners = owners["approvers"] + owners["reviewers"]

        if not all_owners:
            continue

        all_username_keys = [u[user_key] for u in users]

        slack_usernames = [
            get_slack_username(u) for u in users
            if u[user_key].lower() in [o.lower() for o in all_owners]
        ]
        not_found_users = [
            owner for owner in all_owners
            if owner.lower() not in [u.lower() for u in all_username_keys]
        ]
        if not_found_users:
            msg = (f"[{usergroup}] {user_key} not found in app-interface: " +
                   f"{not_found_users}")
            missing_user_log_method(msg)

        all_slack_usernames.extend(slack_usernames)

    return all_slack_usernames
Пример #2
0
def get_slack_usernames_from_owners(owners_from_repo, users, usergroup):
    if owners_from_repo is None:
        return []

    all_slack_usernames = []

    for url_ref in owners_from_repo:
        # allow passing repo_url:ref to select different branch
        if url_ref.count(':') == 2:
            url, ref = url_ref.rsplit(':', 1)
        else:
            url = url_ref
            ref = 'master'

        repo_cli = GitApi(url)

        if isinstance(repo_cli, GitLabApi):
            user_key = 'org_username'
            missing_user_log_method = logging.warning
        elif isinstance(repo_cli, GithubApi):
            user_key = 'github_username'
            missing_user_log_method = logging.debug
        else:
            raise TypeError(f'{type(repo_cli)} not supported')

        repo_owners = RepoOwners(git_cli=repo_cli, ref=ref)

        owners = repo_owners.get_root_owners()
        all_owners = owners['approvers'] + owners['reviewers']

        if not all_owners:
            continue

        all_username_keys = [u[user_key] for u in users]

        slack_usernames = [
            get_slack_username(u) for u in users if u[user_key] in all_owners
        ]
        not_found_users = [
            owner for owner in all_owners if owner not in all_username_keys
        ]
        if not_found_users:
            msg = f'[{usergroup}] {user_key} not found in app-interface: ' + \
                f'{not_found_users}'
            missing_user_log_method(msg)

        all_slack_usernames.extend(slack_usernames)

    return all_slack_usernames
Пример #3
0
def act(repo, dry_run, instance, settings):
    gitlab_cli = GitLabApi(instance, project_url=repo, settings=settings)
    project_owners = RepoOwners(git_cli=gitlab_cli,
                                ref=gitlab_cli.project.default_branch)

    for mr in gitlab_cli.get_merge_requests(state=MRState.OPENED):
        mr_approval = MRApproval(gitlab_client=gitlab_cli,
                                 merge_request=mr,
                                 owners=project_owners,
                                 dry_run=dry_run)

        if mr_approval.top_commit_created_at is None:
            _LOG.info([f'Project:{gitlab_cli.project.id} '
                       f'Merge Request:{mr.iid} '
                       f'- skipping'])
            continue

        approval_status = mr_approval.get_approval_status()
        if approval_status['approved']:
            if mr_approval.has_approval_label():
                _LOG.info([f'Project:{gitlab_cli.project.id} '
                           f'Merge Request:{mr.iid} '
                           f'- already approved'])
                continue
            _LOG.info([f'Project:{gitlab_cli.project.id} '
                       f'Merge Request:{mr.iid} '
                       f'- approving now'])
            if not dry_run:
                gitlab_cli.add_label_to_merge_request(mr.iid,
                                                      APPROVAL_LABEL)
            continue

        if not dry_run:
            if mr_approval.has_approval_label():
                _LOG.info([f'Project:{gitlab_cli.project.id} '
                           f'Merge Request:{mr.iid} '
                           f'- removing approval'])
                gitlab_cli.remove_label_from_merge_request(mr.iid,
                                                           APPROVAL_LABEL)

        if approval_status['report'] is not None:
            _LOG.info([f'Project:{gitlab_cli.project.id} '
                       f'Merge Request:{mr.iid} '
                       f'- publishing approval report'])

            if not dry_run:
                gitlab_cli.remove_label_from_merge_request(mr.iid,
                                                           APPROVAL_LABEL)
                mr.notes.create({'body': approval_status['report']})
            continue

        _LOG.info([f'Project:{gitlab_cli.project.id} '
                   f'Merge Request:{mr.iid} '
                   f'- not fully approved'])