Example #1
0
def get_project_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 project ID (contract group ID) of this repository
    return repository.get_repository_project_id()
Example #2
0
    def redirect_to_console(self, installation_id, repository_id,
                            pull_request_id, redirect, request):
        console_endpoint = cla.conf['CONTRIBUTOR_BASE_URL']
        # Get repository using github's repository ID.
        repository = Repository().get_repository_by_external_id(
            repository_id, "github")
        if repository is None:
            cla.log.warning(
                'Could not find repository with the following repository_id: %s',
                repository_id)
            return None

        # Get project ID from this repository
        project_id = repository.get_repository_project_id()

        user = self.get_or_create_user(request)
        # Ensure user actually requires a signature for this project.
        # TODO: Skipping this for now - we can do this for ICLAs but there's no easy way of doing
        # the check for CCLAs as we need to know in advance what the company_id is that we're checking
        # the CCLA signature for.
        # We'll have to create a function that fetches the latest CCLA regardless of company_id.
        # icla_signature = cla.utils.get_user_signature_by_github_repository(installation_id, user)
        # ccla_signature = cla.utils.get_user_signature_by_github_repository(installation_id, user, company_id=?)
        # try:
        # document = cla.utils.get_project_latest_individual_document(project_id)
        # except DoesNotExist:
        # cla.log.debug('No ICLA for project %s' %project_id)
        # if signature is not None and \
        # signature.get_signature_document_major_version() == document.get_document_major_version():
        # return cla.utils.redirect_user_by_signature(user, signature)
        # Store repository and PR info so we can redirect the user back later.
        cla.utils.set_active_signature_metadata(user.get_user_id(), project_id,
                                                repository_id, pull_request_id)
        # Generate console URL
        console_url = 'https://' + console_endpoint + \
                      '/#/cla/project/' + project_id + \
                      '/user/' + user.get_user_id() + \
                      '?redirect=' + redirect
        raise falcon.HTTPFound(console_url)
Example #3
0
    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)
Example #4
0
    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)