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

refactor: allow more information for pull request creation #119

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vohoanglong0107
Copy link
Contributor

Context

I'm working on un-approving pr whenever new commits were pushed against the branch, but it requires adding a branch column to pull_request table. New pr creation will need branch information, so I made this refactor to make the changes easier

@vohoanglong0107 vohoanglong0107 force-pushed the refactor-create-with-more-information branch from 3393e5f to 7996aec Compare July 24, 2024 09:17
Copy link
Contributor

@Kobzol Kobzol left a comment

Choose a reason for hiding this comment

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

Just to clarify, why does unapproving on commit needs to know the branch? I mean, we will need to know the branch for actually merging, but it seems to me that when a commit is pushed, we should just unapprove unconditionally?

@@ -30,7 +30,7 @@ pub(super) async fn command_approve(
Approver::Myself => author.username.clone(),
Approver::Specified(approver) => approver.clone(),
};
db.approve(repo_state.repository(), pr.number, approver.as_str())
db.approve(repo_state.repository(), pr, approver.as_str())
Copy link
Contributor

@Kobzol Kobzol Jul 24, 2024

Choose a reason for hiding this comment

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

I understand that we need to store more data about PRs in the database. First, I assumed that we will rely on the PR edited event to update data in our DB, and then just approve based on the PR number. But if I understand this correctly, you want to directly do PR update + approval in the DB, so that it stays atomic?

This sounds like a good idea, to avoid situations where we get a race condition and approve something else than what you see on GH. Just wanted to confirm that this was the motivation :)

Copy link
Contributor Author

@vohoanglong0107 vohoanglong0107 Jul 24, 2024

Choose a reason for hiding this comment

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

I didn't think of it like that 😅 My intention was only because we need to use get_or_create in approve, we would need the entire PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Well 😆 In any case, I think that using the latest information from the PR struct, since we get it from the webhook anyway, is a better idea, to be 100% sure of what commit we are approving.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this pr instance was retrieved from either the webhook or by ourselves using gh_client.get_pull_request so it's 100% sure that the latest data was presented when we performed approval

Copy link
Contributor

Choose a reason for hiding this comment

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

Using a GH API call could actually lead to a race condition (approve -> push -> get PR from API). That's why I like reading the PR from the approval webhook directly :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a real issue.
Hmm, how can we fix it then, given that webhook payload sent to us when we approve pr (issue_comment) doesn't include the pull request information inside of it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, we can't :) I thought that the PR payload is in the webhook, but since it isn't, we just need to do the GH API call, or depend on what we have in the DB. Let's keep it as it is now and do the API call. The race condition should be rare, and on GH you can check which SHA was approved, since the bot should include it in the "approved" comment.

@vohoanglong0107
Copy link
Contributor Author

Actually, when there is push event we aren't informed of which pr the push event belonged to, only the ref of the branch, so I need to store the branch name to perform pr extraction by name

@vohoanglong0107 vohoanglong0107 marked this pull request as ready for review July 24, 2024 09:42
@Kobzol
Copy link
Contributor

Kobzol commented Jul 24, 2024

I think we can use the synchronize PR event to detect that: https://docs.github.com/en/webhooks/webhook-events-and-payloads?actionType=synchronize#pull_request That should be much easier.

@vohoanglong0107
Copy link
Contributor Author

This is the first time I've heard about synchronize event. Let me try it. If it's possible to use it, perhaps this PR could be put on wait until we need to perform merge events

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants