Exemple #1
0
def get_installation_id_from_github_repository(github_repository_id):
    # Get repository ID that references the github ID.
    try:
        repository = Repository().get_repository_by_external_id(github_repository_id, 'github')
    except DoesNotExist:
        return None

    # Get Organization from this repository
    organization = GitHubOrg()
    try:
        organization.load(repository.get_repository_organization_name())
    except DoesNotExist:
        return None

    # Get this organization's installation ID
    return organization.get_organization_installation_id()
    def update_change_request(self, installation_id, github_repository_id,
                              change_request_id):
        # Queries GH for the complete pull request details, see:
        # https://developer.github.com/v3/pulls/#response-1
        pull_request = self.get_pull_request(github_repository_id,
                                             change_request_id,
                                             installation_id)
        cla.log.debug(f'Retrieved pull request: {pull_request}')

        # Get all unique users/authors involved in this PR - returns a list of
        # (commit_sha_string, (author_id, author_username, author_email) tuples
        commit_authors = get_pull_request_commit_authors(pull_request)

        try:
            # Get existing repository info using the repository's external ID,
            # which is the repository ID assigned by github.
            cla.log.debug(
                f'PR: {pull_request.number}, Loading GitHub repository by id: {github_repository_id}'
            )
            repository = Repository().get_repository_by_external_id(
                github_repository_id, "github")
            if repository is None:
                cla.log.warning(
                    f'PR: {pull_request.number}, Failed to load GitHub repository by '
                    f'id: {github_repository_id} in our DB - repository reference is None - '
                    'Is this org/repo configured in the Project Console?'
                    ' Unable to update status.')
                return
        except DoesNotExist:
            cla.log.warning(
                f'PR: {pull_request.number}, could not find repository with the '
                f'repository ID: {github_repository_id}')
            cla.log.warning(
                f'PR: {pull_request.number}, failed to update change request of '
                f'repository {github_repository_id} - returning')
            return

        # Get Github Organization name that the repository is configured to.
        organization_name = repository.get_repository_organization_name()
        cla.log.debug('PR: {}, determined github organization is: {}'.format(
            pull_request.number, organization_name))

        # Check that the Github Organization exists.
        github_org = GitHubOrg()
        try:
            github_org.load(organization_name)
        except DoesNotExist:
            cla.log.warning(
                'PR: {}, Could not find Github Organization with the following organization name: {}'
                .format(pull_request.number, organization_name))
            cla.log.warning(
                'PR: {}, Failed to update change request of repository {} - returning'
                .format(pull_request.number, github_repository_id))
            return

            # Ensure that installation ID for this organization matches the given installation ID
        if github_org.get_organization_installation_id() != installation_id:
            cla.log.warning(
                'PR: {}, the installation ID: {} of this organization does not match '
                'installation ID: {} given by the pull request.'.format(
                    pull_request.number,
                    github_org.get_organization_installation_id(),
                    installation_id))
            cla.log.error(
                'PR: {}, Failed to update change request of repository {} - returning'
                .format(pull_request.number, github_repository_id))
            return

        # Retrieve project ID from the repository.
        project_id = repository.get_repository_project_id()

        # Find users who have signed and who have not signed.
        signed = []
        missing = []

        cla.log.debug(
            f'PR: {pull_request.number}, scanning users - determining who has signed a CLA an who has not.'
        )
        for commit_sha, author_info in commit_authors:
            # Extract the author info tuple details
            author_id = author_info[0]
            author_username = author_info[1]
            author_email = author_info[2]
            cla.log.debug(
                'PR: {}, processing sha: {} from author id: {}, username: {}, email: {}'
                .format(pull_request.number, commit_sha, author_id,
                        author_username, author_email))
            handle_commit_from_user(project_id, commit_sha, author_info,
                                    signed, missing)

        cla.log.debug(
            'PR: {}, updating github pull request for repo: {}, '
            'with signed authors: {} with missing authors: {}'.format(
                pull_request.number, github_repository_id, signed, missing))
        update_pull_request(installation_id,
                            github_repository_id,
                            pull_request,
                            signed=signed,
                            missing=missing)
    def update_change_request(self, installation_id, github_repository_id,
                              change_request_id):
        pull_request = self.get_pull_request(github_repository_id,
                                             change_request_id,
                                             installation_id)
        # Get all unique users involved in this PR.
        commit_authors = get_pull_request_commit_authors(pull_request)
        # Get existing repository info using the repository's external ID, which is the repository ID assigned by github.

        try:
            repository = Repository().get_repository_by_external_id(
                github_repository_id, "github")
        except DoesNotExist:
            cla.log.error(
                'Could not find repository with the repository ID: %s',
                github_repository_id)
            cla.log.error(
                'Failed to update change request %s of repository %s',
                change_request_id, github_repository_id)
            return

        # Get Github Organization name that the repository is configured to.
        organization_name = repository.get_repository_organization_name()

        # Check that the Github Organization exists.
        github_org = GitHubOrg()
        try:
            github_org.load(organization_name)
        except DoesNotExist:
            cla.log.error(
                'Could not find Github Organization with the following organization name: %s',
                organization_name)
            cla.log.error(
                'Failed to update change request %s of repository %s',
                change_request_id, github_repository_id)
            return

        # Ensure that installation ID for this organization matches the given installation ID
        if github_org.get_organization_installation_id() != installation_id:
            cla.log.error(
                'The installation ID: %s of this organization does not match installation ID: %s given by the pull request.',
                github_org.get_organization_installation_id(), installation_id)
            cla.log.error(
                'Failed to update change request %s of repository %s',
                change_request_id, github_repository_id)
            return

        # Retrieve project ID from the repository.
        project_id = repository.get_repository_project_id()

        # Find users who have signed and who have not signed.
        signed = []
        missing = []
        for commit, commit_author in commit_authors:
            # cla.log.info("Author: " + commit_author)
            if isinstance(commit_author, github.NamedUser.NamedUser):
                # Handle GitHub user.
                cla.log.info("Handle GitHub user")
                handle_commit_from_github_user(project_id, commit,
                                               commit_author, signed, missing)
            elif isinstance(commit_author, github.GitAuthor.GitAuthor):
                # Handle non-github user (just email and name in commit).
                cla.log.info(
                    "Handle non-github user (just email and name in commit)")
                handle_commit_from_git_author(project_id, commit,
                                              commit_author, signed, missing)
            else:
                # Couldn't find any author information.
                cla.log.info("Couldn't find any author information.")
                if commit_author is not None:
                    missing.append((commit.sha, commit_author))
                else:
                    missing.append((commit.sha, None))

        update_pull_request(installation_id,
                            github_repository_id,
                            pull_request,
                            signed=signed,
                            missing=missing)