Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(pr-comments): generalization for triggering merged PR comments workflow #71039

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/sentry/integrations/github/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,25 @@ def get_commit(self, repo: str, sha: str) -> Any:
"""
return self.get_cached(f"/repos/{repo}/commits/{sha}")

def get_merge_commit_sha_from_commit(self, repo: str, sha: str) -> str | None:
"""
Get the merge commit sha from a commit sha.
"""
response = self.get_pullrequest_from_commit(repo, sha)
cathteng marked this conversation as resolved.
Show resolved Hide resolved
if not response or (isinstance(response, list) and len(response) != 1):
# the response should return a single merged PR, return if multiple
cathteng marked this conversation as resolved.
Show resolved Hide resolved
return None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does it mean if we return on multiple? Should we be logging it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


(pull_request,) = response
if pull_request["state"] == "open":
metrics.incr(
"github_pr_comment.queue_comment_check.open_pr",
sample_rate=1.0,
)
return None

return pull_request.get("merge_commit_sha")

def get_pullrequest_from_commit(self, repo: str, sha: str) -> Any:
"""
https://docs.github.com/en/rest/commits/commits#list-pull-requests-associated-with-a-commit
Expand Down
38 changes: 14 additions & 24 deletions src/sentry/tasks/commit_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
PR_COMMENT_TASK_TTL = timedelta(minutes=5).total_seconds()
PR_COMMENT_WINDOW = 14 # days

PR_COMMENT_SUPPORTED_PROVIDERS = {"integrations:github"}
Copy link
Member Author

@cathteng cathteng Jul 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when all SCMs have implemented the get_merge_commit_sha_from_commit function AND we have a generalized merged PR comment workflow, we can remove this


logger = logging.getLogger(__name__)


Expand All @@ -63,35 +65,23 @@ def queue_comment_task_if_needed(

# client will raise an Exception if the request is not successful
try:
response = installation.get_client().get_pullrequest_from_commit(
repo=repo.name, sha=commit.key
)
client = installation.get_client()
merge_commit_sha = client.get_merge_commit_sha_from_commit(repo=repo.name, sha=commit.key)
except Exception as e:
sentry_sdk.capture_exception(e)
return

if not isinstance(response, list) or len(response) != 1:
# the response should return a single PR, return if multiple
if len(response) > 1:
logger.info(
"github.pr_comment.queue_comment_check.commit_not_in_default_branch",
extra={
"organization_id": commit.organization_id,
"repository_id": repo.id,
"commit_sha": commit.key,
},
)
return

if response[0]["state"] == "open":
metrics.incr(
"github_pr_comment.queue_comment_check.open_pr",
sample_rate=1.0,
if merge_commit_sha is None:
logger.info(
"github.pr_comment.queue_comment_check.commit_not_in_default_branch",
extra={
"organization_id": commit.organization_id,
"repository_id": repo.id,
"commit_sha": commit.key,
},
)
return

merge_commit_sha = response[0]["merge_commit_sha"]

pr_query = PullRequest.objects.filter(
organization_id=commit.organization_id,
repository_id=commit.repository_id,
Expand Down Expand Up @@ -288,13 +278,13 @@ def process_commit_context(
"github.pr_comment",
extra={"organization_id": project.organization_id},
)
repo = Repository.objects.filter(id=commit.repository_id)
repo = Repository.objects.filter(id=commit.repository_id).order_by("-date_added")
group = Group.objects.get_from_cache(id=group_id)
if (
group.level is not logging.INFO # Don't comment on info level issues
and installation is not None
and repo.exists()
and repo.get().provider == "integrations:github"
and repo.get().provider in PR_COMMENT_SUPPORTED_PROVIDERS
):
queue_comment_task_if_needed(commit, group_owner, repo.get(), installation)
else:
Expand Down
34 changes: 34 additions & 0 deletions tests/sentry/integrations/github/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,40 @@ def test_get_comment_reactions(self, get_jwt):
del stored_reactions["url"]
assert reactions == stored_reactions

@mock.patch("sentry.integrations.github.client.get_jwt", return_value="jwt_token_1")
@responses.activate
def test_get_merge_commit_sha_from_commit(self, get_jwt):
merge_commit_sha = "jkl123"
pull_requests = [{"merge_commit_sha": merge_commit_sha, "state": "closed"}]
commit_sha = "asdf"
responses.add(
responses.GET,
f"https://api.github.com/repos/{self.repo.name}/commits/{commit_sha}/pulls",
json=pull_requests,
)

sha = self.github_client.get_merge_commit_sha_from_commit(
repo=self.repo.name, sha=commit_sha
)
assert sha == merge_commit_sha

@mock.patch("sentry.integrations.github.client.get_jwt", return_value="jwt_token_1")
@responses.activate
def test_get_merge_commit_sha_from_commit_open_pr(self, get_jwt):
merge_commit_sha = "jkl123"
pull_requests = [{"merge_commit_sha": merge_commit_sha, "state": "open"}]
commit_sha = "asdf"
responses.add(
responses.GET,
f"https://api.github.com/repos/{self.repo.name}/commits/{commit_sha}/pulls",
json=pull_requests,
)

sha = self.github_client.get_merge_commit_sha_from_commit(
repo=self.repo.name, sha=commit_sha
)
assert sha is None

@responses.activate
def test_disable_email(self):
with self.tasks():
Expand Down
Loading