Browse Source

feat(integrations): Added a client call to get a single commit for gitlab (#36746)

API will be used to check if a commit exists in a repository during ingest
Aniket Das 2 years ago
parent
commit
21bb1a04b7

+ 32 - 0
fixtures/gitlab.py

@@ -397,3 +397,35 @@ COMMIT_DIFF_RESPONSE = r"""
     }
 ]
 """
+
+# Example taken from https://docs.gitlab.com/ee/api/commits.html#get-a-single-commit
+GET_COMMIT_RESPONSE = """
+{
+  "id": "6104942438c14ec7bd21c6cd5bd995272b3faff6",
+  "short_id": "6104942438c",
+  "title": "Sanitize for network graph",
+  "author_name": "randx",
+  "author_email": "user@example.com",
+  "committer_name": "Dmitriy",
+  "committer_email": "user@example.com",
+  "created_at": "2021-09-20T09:06:12.300+03:00",
+  "message": "Sanitize for network graph",
+  "committed_date": "2021-09-20T09:06:12.300+03:00",
+  "authored_date": "2021-09-20T09:06:12.420+03:00",
+  "parent_ids": [
+    "ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"
+  ],
+  "last_pipeline" : {
+    "id": 8,
+    "ref": "master",
+    "sha": "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
+    "status": "created"
+  },
+  "stats": {
+    "additions": 15,
+    "deletions": 10,
+    "total": 25
+  },
+  "status": "running",
+  "web_url": "https://gitlab.example.com/thedude/gitlab-foss/-/commit/6104942438c14ec7bd21c6cd5bd995272b3faff6"
+}"""

+ 1 - 0
src/sentry/integrations/bitbucket/client.py

@@ -26,6 +26,7 @@ class BitbucketAPIPath:
 
     repository = "/2.0/repositories/{repo}"
     repositories = "/2.0/repositories/{username}"
+    repository_commit = "/2.0/repositories/{repo}/commit/{sha}"
     repository_commits = "/2.0/repositories/{repo}/commits/{revision}"
     repository_diff = "/2.0/repositories/{repo}/diff/{spec}"
     repository_hook = "/2.0/repositories/{repo}/hooks/{uid}"

+ 1 - 0
src/sentry/integrations/bitbucket_server/client.py

@@ -21,6 +21,7 @@ class BitbucketServerAPIPath:
     repository_hook = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks/{id}"
     repository_hooks = "/rest/api/1.0/projects/{project}/repos/{repo}/webhooks"
     repository_commits = "/rest/api/1.0/projects/{project}/repos/{repo}/commits"
+    repository_commit_details = "/rest/api/1.0/projects/{project}/repos/{repo}/commits/{commit}"
     commit_changes = "/rest/api/1.0/projects/{project}/repos/{repo}/commits/{commit}/changes"
 
 

+ 7 - 0
src/sentry/integrations/gitlab/client.py

@@ -258,6 +258,13 @@ class GitLabApiClient(ApiClient):
         path = GitLabApiClientPath.commits.format(project=project_id)
         return self.get(path, params={"until": end_date})
 
+    def get_commit(self, project_id, sha):
+        """
+        Get the details of a commit
+        See https://docs.gitlab.com/ee/api/commits.html#get-a-single-commit
+        """
+        return self.get_cached(GitLabApiClientPath.commit.format(project=project_id, sha=sha))
+
     def compare_commits(self, project_id, start_sha, end_sha):
         """Compare commits between two SHAs
 

+ 14 - 1
tests/sentry/integrations/gitlab/test_client.py

@@ -4,7 +4,7 @@ from unittest import mock
 import pytest
 import responses
 
-from fixtures.gitlab import GitLabTestCase
+from fixtures.gitlab import GET_COMMIT_RESPONSE, GitLabTestCase
 from sentry.auth.exceptions import IdentityNotValid
 from sentry.models import Identity
 from sentry.shared_integrations.exceptions import ApiError
@@ -23,6 +23,7 @@ class GitlabRefreshAuthTest(GitLabTestCase):
     def setUp(self):
         super().setUp()
         self.client = self.installation.get_client()
+        self.client.base_url = "https://example.gitlab.com/"
         self.request_data = {"id": "user_id"}
         self.request_url = "https://example.gitlab.com/api/v4/user"
         self.refresh_url = "https://example.gitlab.com/oauth/token"
@@ -197,3 +198,15 @@ class GitlabRefreshAuthTest(GitLabTestCase):
         )
 
         assert result == GITLAB_CODEOWNERS
+
+    @responses.activate
+    def test_get_commit(self):
+        commit = "a" * 40
+        responses.add(
+            method=responses.GET,
+            url=f"https://example.gitlab.com/api/v4/projects/{self.gitlab_id}/repository/commits/{commit}",
+            json=json.loads(GET_COMMIT_RESPONSE),
+        )
+
+        resp = self.client.get_commit(self.gitlab_id, commit)
+        assert resp == json.loads(GET_COMMIT_RESPONSE)