Example #1
0
    def __call__(self, integration, organization, event):
        repo = self.get_repo(integration, organization, event)
        if repo is None:
            return

        # TODO(kmclb): turn this back on once tri-level repo problem has been solved
        # while we're here, make sure repo data is up to date
        # self.update_repo_data(repo, event)

        try:
            number = event["object_attributes"]["iid"]
            title = event["object_attributes"]["title"]
            body = event["object_attributes"]["description"]
            created_at = event["object_attributes"]["created_at"]
            merge_commit_sha = event["object_attributes"]["merge_commit_sha"]

            last_commit = event["object_attributes"]["last_commit"]
            author_email = None
            author_name = None
            if last_commit:
                author_email = last_commit["author"]["email"]
                author_name = last_commit["author"]["name"]
        except KeyError as e:
            logger.info(
                "gitlab.webhook.invalid-merge-data",
                extra={
                    "integration_id": integration.id,
                    "error": str(e)
                },
            )

        if not author_email:
            raise Http404()

        author = CommitAuthor.objects.get_or_create(
            organization_id=organization.id,
            email=author_email,
            defaults={"name": author_name})[0]

        try:
            PullRequest.create_or_save(
                organization_id=organization.id,
                repository_id=repo.id,
                key=number,
                values={
                    "title":
                    title,
                    "author":
                    author,
                    "message":
                    body,
                    "merge_commit_sha":
                    merge_commit_sha,
                    "date_added":
                    dateutil.parser.parse(created_at).astimezone(timezone.utc),
                },
            )
        except IntegrityError:
            pass
Example #2
0
    def __call__(self, integration, organization, event):
        repo = self.get_repo(integration, organization, event)
        if repo is None:
            return
        try:
            number = event['object_attributes']['iid']
            title = event['object_attributes']['title']
            body = event['object_attributes']['description']
            created_at = event['object_attributes']['created_at']
            merge_commit_sha = event['object_attributes']['merge_commit_sha']

            last_commit = event['object_attributes']['last_commit']
            author_email = None
            author_name = None
            if last_commit:
                author_email = last_commit['author']['email']
                author_name = last_commit['author']['name']
        except KeyError as e:
            logger.info('gitlab.webhook.invalid-merge-data',
                        extra={
                            'integration_id': integration.id,
                            'error': six.string_type(e)
                        })

        if not author_email:
            raise Http404()

        author = CommitAuthor.objects.get_or_create(
            organization_id=organization.id,
            email=author_email,
            defaults={'name': author_name})[0]

        try:
            PullRequest.create_or_save(
                organization_id=organization.id,
                repository_id=repo.id,
                key=number,
                values={
                    'title':
                    title,
                    'author':
                    author,
                    'message':
                    body,
                    'merge_commit_sha':
                    merge_commit_sha,
                    'date_added':
                    dateutil.parser.parse(created_at).astimezone(timezone.utc),
                },
            )
        except IntegrityError:
            pass
 def test_pull_request_url(self):
     response = self.create_repository(self.default_repository_config,
                                       self.integration.id)
     repo = Repository.objects.get(pk=response.data['id'])
     pull = PullRequest(key=99)
     result = self.provider.pull_request_url(repo, pull)
     assert result == 'https://example.gitlab.com/getsentry/projects/example-repo/merge_requests/99'
Example #4
0
    def __call__(self, integration, organization, event):
        repo = self.get_repo(integration, organization, event)
        if repo is None:
            return
        try:
            number = event['object_attributes']['iid']
            title = event['object_attributes']['title']
            body = event['object_attributes']['description']
            created_at = event['object_attributes']['created_at']
            merge_commit_sha = event['object_attributes']['merge_commit_sha']

            author = event['object_attributes']['last_commit']['author']
            author_email = author['email']
            author_name = author['name']
        except KeyError as e:
            logger.info(
                'gitlab.webhook.invalid-merge-data',
                extra={
                    'integration_id': integration.id,
                    'error': six.string_type(e)
                })
            raise Http404()

        author = CommitAuthor.objects.get_or_create(
            organization_id=organization.id,
            email=author_email,
            defaults={'name': author_name}
        )[0]

        try:
            PullRequest.create_or_save(
                organization_id=organization.id,
                repository_id=repo.id,
                key=number,
                values={
                    'title': title,
                    'author': author,
                    'message': body,
                    'merge_commit_sha': merge_commit_sha,
                    'date_added': dateutil.parser.parse(
                        created_at).astimezone(timezone.utc),
                },
            )
        except IntegrityError:
            pass
Example #5
0
    def _handle(self, integration, event, organization, repo, host=None):
        # while we're here, make sure repo data is up to date
        self.update_repo_data(repo, event)

        pull_request = event["pull_request"]
        number = pull_request["number"]
        title = pull_request["title"]
        body = pull_request["body"]
        user = pull_request["user"]

        # The value of the merge_commit_sha attribute changes depending on the state of the pull request. Before a pull request is merged, the merge_commit_sha attribute holds the SHA of the test merge commit. After a pull request is merged, the attribute changes depending on how the pull request was merged:
        # - If the pull request was merged as a merge commit, the attribute represents the SHA of the merge commit.
        # - If the pull request was merged via a squash, the attribute represents the SHA of the squashed commit on the base branch.
        # - If the pull request was rebased, the attribute represents the commit that the base branch was updated to.
        # https://developer.github.com/v3/pulls/#get-a-single-pull-request
        merge_commit_sha = pull_request["merge_commit_sha"] if pull_request[
            "merged"] else None

        author_email = "{}@localhost".format(user["login"][:65])
        try:
            commit_author = CommitAuthor.objects.get(
                external_id=self.get_external_id(user["login"]),
                organization_id=organization.id)
            author_email = commit_author.email
        except CommitAuthor.DoesNotExist:
            try:
                identity = Identity.objects.get(
                    external_id=user["id"],
                    idp__type=self.provider,
                    idp__external_id=self.get_idp_external_id(
                        integration, host),
                )
            except Identity.DoesNotExist:
                pass
            else:
                author_email = identity.user.email

        try:
            author = CommitAuthor.objects.get(organization_id=organization.id,
                                              external_id=self.get_external_id(
                                                  user["login"]))
        except CommitAuthor.DoesNotExist:
            try:
                author = CommitAuthor.objects.get(
                    organization_id=organization.id, email=author_email)
            except CommitAuthor.DoesNotExist:
                author = CommitAuthor.objects.create(
                    organization_id=organization.id,
                    email=author_email,
                    external_id=self.get_external_id(user["login"]),
                    name=user["login"][:128],
                )

        try:
            PullRequest.create_or_save(
                organization_id=organization.id,
                repository_id=repo.id,
                key=number,
                values={
                    "organization_id": organization.id,
                    "title": title,
                    "author": author,
                    "message": body,
                    "merge_commit_sha": merge_commit_sha,
                },
            )
        except IntegrityError:
            pass
 def test_pull_request_url(self):
     pull = PullRequest(key=99)
     result = self.provider.pull_request_url(self.repository, pull)
     assert result == 'https://github.com/getsentry/example-repo/pull/99'
Example #7
0
    def _handle(self, integration, event, organization, repo, host=None):
        pull_request = event['pull_request']
        number = pull_request['number']
        title = pull_request['title']
        body = pull_request['body']
        user = pull_request['user']

        # The value of the merge_commit_sha attribute changes depending on the state of the pull request. Before a pull request is merged, the merge_commit_sha attribute holds the SHA of the test merge commit. After a pull request is merged, the attribute changes depending on how the pull request was merged:
        # - If the pull request was merged as a merge commit, the attribute represents the SHA of the merge commit.
        # - If the pull request was merged via a squash, the attribute represents the SHA of the squashed commit on the base branch.
        # - If the pull request was rebased, the attribute represents the commit that the base branch was updated to.
        # https://developer.github.com/v3/pulls/#get-a-single-pull-request
        merge_commit_sha = pull_request['merge_commit_sha'] if pull_request[
            'merged'] else None

        author_email = u'{}@localhost'.format(user['login'][:65])
        try:
            commit_author = CommitAuthor.objects.get(
                external_id=self.get_external_id(user['login']),
                organization_id=organization.id,
            )
            author_email = commit_author.email
        except CommitAuthor.DoesNotExist:
            try:
                identity = Identity.objects.get(
                    external_id=user['id'],
                    idp__type=self.provider,
                    idp__external_id=self.get_idp_external_id(
                        integration, host))
            except Identity.DoesNotExist:
                pass
            else:
                author_email = identity.user.email

        try:
            author = CommitAuthor.objects.get(
                organization_id=organization.id,
                external_id=self.get_external_id(user['login']),
            )
        except CommitAuthor.DoesNotExist:
            try:
                author = CommitAuthor.objects.get(
                    organization_id=organization.id,
                    email=author_email,
                )
            except CommitAuthor.DoesNotExist:
                author = CommitAuthor.objects.create(
                    organization_id=organization.id,
                    email=author_email,
                    external_id=self.get_external_id(user['login']),
                    name=user['login'][:128])

        try:
            PullRequest.create_or_save(
                organization_id=organization.id,
                repository_id=repo.id,
                key=number,
                values={
                    'organization_id': organization.id,
                    'title': title,
                    'author': author,
                    'message': body,
                    'merge_commit_sha': merge_commit_sha,
                },
            )
        except IntegrityError:
            pass
Example #8
0
    def _handle(self, integration, event, organization, repo, host=None):
        pull_request = event['pull_request']
        number = pull_request['number']
        title = pull_request['title']
        body = pull_request['body']
        user = pull_request['user']

        # The value of the merge_commit_sha attribute changes depending on the state of the pull request. Before a pull request is merged, the merge_commit_sha attribute holds the SHA of the test merge commit. After a pull request is merged, the attribute changes depending on how the pull request was merged:
        # - If the pull request was merged as a merge commit, the attribute represents the SHA of the merge commit.
        # - If the pull request was merged via a squash, the attribute represents the SHA of the squashed commit on the base branch.
        # - If the pull request was rebased, the attribute represents the commit that the base branch was updated to.
        # https://developer.github.com/v3/pulls/#get-a-single-pull-request
        merge_commit_sha = pull_request['merge_commit_sha'] if pull_request['merged'] else None

        author_email = u'{}@localhost'.format(user['login'][:65])
        try:
            commit_author = CommitAuthor.objects.get(
                external_id=self.get_external_id(user['login']),
                organization_id=organization.id,
            )
            author_email = commit_author.email
        except CommitAuthor.DoesNotExist:
            try:
                identity = Identity.objects.get(
                    external_id=user['id'], idp__type=self.provider, idp__external_id=self.get_idp_external_id(integration, host))
            except Identity.DoesNotExist:
                pass
            else:
                author_email = identity.user.email

        try:
            author = CommitAuthor.objects.get(
                organization_id=organization.id,
                external_id=self.get_external_id(user['login']),
            )
        except CommitAuthor.DoesNotExist:
            try:
                author = CommitAuthor.objects.get(
                    organization_id=organization.id,
                    email=author_email,
                )
            except CommitAuthor.DoesNotExist:
                author = CommitAuthor.objects.create(
                    organization_id=organization.id,
                    email=author_email,
                    external_id=self.get_external_id(user['login']),
                    name=user['login'][:128]
                )

        try:
            PullRequest.create_or_save(
                organization_id=organization.id,
                repository_id=repo.id,
                key=number,
                values={
                    'organization_id': organization.id,
                    'title': title,
                    'author': author,
                    'message': body,
                    'merge_commit_sha': merge_commit_sha,
                },
            )
        except IntegrityError:
            pass