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

Improve trigger condition in github actions #828

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions docs/docs/installation/github.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ You can use our pre-built Github Action Docker image to run PR-Agent as a Github
```yaml
on:
pull_request:
types: [opened,reopened,ready_for_review,review_requested]
issue_comment:
jobs:
pr_agent_job:
Copy link
Collaborator

Choose a reason for hiding this comment

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

/ask can i limit the 'issue_comment' to ignore bots ? can I limit it to ignore message not starting with '/' ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you can limit the issue_comment trigger to ignore bots and to only act on comments that start with a /. However, these configurations are not directly available through the on.issue_comment syntax in the GitHub Actions YAML file. Instead, you would need to implement conditional checks within the jobs of your GitHub Action workflow. Here's how you can approach it:

  1. Ignore Bots: GitHub Actions provides a github context which contains information about the event that triggered the workflow. You can use the github.actor attribute to check if the actor is a bot. Bots typically have [bot] at the end of their username.

  2. Filter Comments Starting with '/': You can use the github.event.comment.body attribute to access the content of the comment and then check if it starts with a /.

Here is an example of how you might implement these checks in a job within your GitHub Actions workflow:

jobs:
  pr_agent_job:
    if: >-
      !contains(github.actor, '[bot]') &&
      startsWith(github.event.comment.body, '/')
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2
    # Add other steps as needed

In this example:

  • The if condition for the job pr_agent_job checks two things:
    • It uses !contains(github.actor, '[bot]') to ensure the actor is not a bot.
    • It uses startsWith(github.event.comment.body, '/') to ensure the comment starts with a /.
  • If both conditions are true, the job proceeds; otherwise, it's skipped.

This approach allows you to dynamically control the execution of your GitHub Actions based on the content and source of issue comments.

Expand All @@ -28,6 +29,7 @@ jobs:
```yaml
on:
pull_request:
types: [opened,reopened,ready_for_review,review_requested]
issue_comment:

jobs:
Expand Down
2 changes: 2 additions & 0 deletions pr_agent/servers/github_action_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ async def run_action():
await PRReviewer(pr_url).run()
if auto_improve is None or is_true(auto_improve):
await PRCodeSuggestions(pr_url).run()
else:
get_logger().info(f"Skipping action: {action}")

# Handle issue comment event
elif GITHUB_EVENT_NAME == "issue_comment" or GITHUB_EVENT_NAME == "pull_request_review_comment":
Expand Down