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

Approve a PR by leaving "/lgtm" comment #153

Open
wants to merge 40 commits into
base: main
Choose a base branch
from

Conversation

lugi0
Copy link
Collaborator

@lugi0 lugi0 commented Feb 19, 2025

Description

This add a GHA to approve a PR when a user leaves a "/lgtm" comment

How Has This Been Tested?

Merge criteria:

  • The commits are squashed in a cohesive manner and have meaningful messages.
  • Testing instructions have been added in the PR body (for PRs involving changes that are not immediately obvious).
  • The developer has manually tested the changes and verified that the changes work

@lugi0 lugi0 requested a review from a team as a code owner February 19, 2025 14:45
Copy link
Collaborator Author

@lugi0 lugi0 left a comment

Choose a reason for hiding this comment

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

testing /lgtm

Copy link

The following are automatically added/executed:

Available user actions:

  • To mark a PR as WIP, add /wip in a comment. To remove it from the PR comment /wip cancel to the PR.
  • To block merging of a PR, add /hold in a comment. To un-block merging of PR comment /hold cancel.
  • To mark a PR as approved, add /lgtm in a comment. To remove, add /lgtm cancel.
    lgtm label removed on each new commit push.
  • To mark PR as verified comment /verified to the PR, to un-verify comment /verified cancel to the PR.
    verified label removed on each new commit push.
Supported labels

{'/wip', '/verified', '/hold', '/lgtm'}

@lugi0 lugi0 requested a review from rnetser February 19, 2025 15:22
@github-actions github-actions bot added size/s and removed size/xs labels Feb 19, 2025
@lugi0
Copy link
Collaborator Author

lugi0 commented Feb 19, 2025

see lugi0#2 for testing of the workflow

on:
workflow_dispatch: #manual testing
issue_comment:
types: [created]
Copy link
Collaborator

Choose a reason for hiding this comment

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

please add condition to trigger only if "lgtm in comment"
e/g

    if: |
      contains(github.event.comment.body, '/lgtm')

also should be triggered on edited

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The actions already checks if /lgtm is int he comment: https://github.com/opendatahub-io/opendatahub-tests/pull/153/files#diff-8adeafbcbe79cc05a3ae753fd95a88451697a3356e53b468f8a7f565c5c737d1R13
Why would we edit a comment to add a lgtm? that seems unlikely to be honest

Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. i think adding the check on the content of the comment at the top makes it more clear
  2. a user can edit a comment and add "lgtm"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've added the edit event, I still think it is not a likely scenario though :)
The if conditional is not supported on the definition of the event as far as I can see.

@github-actions github-actions bot added size/m and removed size/s labels Mar 14, 2025
permissions:
pull-requests: write
if: >
(github.event.issue.pull_request && contains(github.event.comment.body, '/lgtm')) ||
Copy link
Collaborator

Choose a reason for hiding this comment

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

what if i use /lgtm cancel or delete my comment? add and remove pr approval should be done in our python code

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done in python code, now both options work

@github-actions github-actions bot added size/l and removed size/m labels Mar 18, 2025
@github-actions github-actions bot added size/m and removed size/l labels Mar 18, 2025
@lugi0 lugi0 requested a review from rnetser March 18, 2025 17:35

def dismiss_pr_approval(self) -> None:
all_reviews = self.pr.get_reviews()
current_user = self.gh_client.get_user().login
Copy link
Collaborator

Choose a reason for hiding this comment

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

isn't this self.user_login?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, the approval through the action is left by the bot, so we need to dismiss the bot's review.
We could expand it to also remove for the user who has left the comment if an approval by them exists, but we run into the issue that we don't have a token for each user and we won't be able to dismiss it using the bot's token

if label_in_pr:
LOGGER.info(f"Removing label {label}")
self.pr.remove_from_labels(label=label)

elif not label_in_pr:
self.add_pr_label(label=label)
self.approve_pr()
Copy link
Collaborator

Choose a reason for hiding this comment

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

wouldn't this approave the pr for all user labels?
pr should be approved only if self.user_login != pr owner

Copy link
Collaborator Author

@lugi0 lugi0 Mar 18, 2025

Choose a reason for hiding this comment

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

wouldn't this approave the pr for all user labels?

shoot, yeah, I was sure it was indented under the first if block, will fix tomorrow

pr should be approved only if self.user_login != pr owner

let's discuss about this, I think there are more scenarios to consider

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Checking for team membership now. I've done a workaround until we can get the bot into the org but the logic is there.

@github-actions github-actions bot added size/l and removed size/m labels Mar 19, 2025
@lugi0 lugi0 requested a review from rnetser March 19, 2025 11:07
adolfo-ab
adolfo-ab previously approved these changes Mar 19, 2025
@@ -31,19 +36,41 @@ jobs:
comment-id: ${{ github.event.comment.id }}
reactions: '+1'

# This currently fails with either the bot PAT or the standard github token secret
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you want to remove these?

allowed_users = self.get_allowed_users()
if self.user_login not in allowed_users:
LOGGER.info(f"User {self.user_login} is not allowed for this action. Exiting.")
sys.exit(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should you exit or add a comment? Exiting would just fail the action.

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

Successfully merging this pull request may close these issues.

4 participants