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

Enhanced Auto-PR: Add profanity filter, assign review tag to PRs with multiple file changes and more... #73431

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

Conversation

Esh07
Copy link
Contributor

@Esh07 Esh07 commented Sep 21, 2023

TL;DR

This PR adds an action that merges PRs that only change Contributors.md, after checking some criteria and giving feedback. It also improves the security of the token permissions by assigning them based on the steps.

💡 New features:

  • Delete previous comments posted by the script on the PR.
  • Check multiple line changes in the PR (PR replacing text also flag as multiple changes) 🆕
  • Implemented text check (profanity filter) but not link check (Google Safe Browsing APIs is not effectively compare to VirusTotal APIs but there is limitation of making request in day on VirustTotal. To be discuss further) 🆕
  • Add automatic closing on offensive content. 🆕
  • Drop a message to the contributor if the PR is closed due to offensive content. 🆕
  • Add reopened event to trigger GitHub-action. 🆕
  • Post comment on PR that contains multiple file changes. 🆕
  • Add review 👩🏼‍💻 Waiting for review to PR which contains multiple file changes. 🆕

🆕 The script will mark the PR with a “ Requested Changes” comment if there is action needed from user side. This will indicate that the PR is still pending.

Description

This PR adds a GitHub action that will automatically merge pull requests that only modify the Contributors.md file. The action will check if the pull request only changes one line in the file, and if the added content is safe and respectful. The action will also post comments on the pull request to provide feedback and guidance to the contributors.

Changes

This PR includes the following changes from the previous version of the script:

📈 The script now uses output and env variables to share job results, eliminating redundant requests and enhancing efficiency. This is an improvement from the previous version, which made redundant requests in each step.

  • Token permissions are assigned based on step requirements, enhancing security by limiting write access to necessary steps only, such as merging pull requests, and defaulting to read access otherwise.
  • Delete any previous message by github-actions bot.
  • Add review label to PR if changes multiple files.
  • Added a new job for checking offensive and malicious added content, using an external service and a regular expression.
  • Added a step job to close PR on offensive content.
  • Added some logic for retrying the request for checking the mergeability of the pull request, using a while loop and a counter:
    • GitHub does have internal checks going on when new PR created . A good discussion here and also some time it takes time to update status of pull request.
    • Script uses JS promise to make 3 attempt (with pause of 3 sec interval) if mergeability value is null.
    • Replace normal comment to review comment such as 'Request Change" comment with links to follow to solve the merge conflict.
  • The script now specifically retrieves the additions, deletions, and changes properties from the Contributors.md file. And not overall one. (Previous script checking overall additions, deletions and changes done in PR).
  • The comment messages have been enhanced and the formatting has been improved using markdown.

Permissions

🚨 Do not check out, build, or run untrusted code from the pull request withpull_request_target event. Use pull_request instead for these purposes. Good example by GitGuardian video

This PR sets minimal permissions for each job and step, to ensure that they only have access to what they need. For example:

  • checking all modified files and content: The job for checking all modified files and content only has read permissions for contents and pull requests, and write permissions for issues.

    • This is because it needs to read the contents of the repository and pull requests to check the changes, and it may need to comment on the PR (which requires issues: write permission) if multiple files or lines were changed.
  • checking offensive and malicious: The job for checking offensive and malicious links has read permissions for contents and issues to write.

    • Write permissions for issues are necessary to close them and post comments.
  • merging PRs: The job for merging PRs has write permissions for contents, pull requests and issues.

    • This is because it needs to modify the contents of the repository (which requires contents: write permission) and merge pull requests (which requires pull requests: write permission).
    • Posting a congratulatory message upon successful merges requires issues: write permission.

The steps that use GitHub scripts only have access to the GitHub token with the necessary scopes. These permissions limit the exposure of sensitive data (console log of GITHUB_TOKEN) and prevent unauthorised actions on the repository.

Testing

These are test cases I performed:

  1. Replace line in Contributors.md
  2. Multiple files changed in PR
  3. Only new added in Contributors.md file
  4. 2 Additions and 2 Deletions lines in Contributors.md file
  5. 3 new lines additions in Contributors.md file
  6. Check for inappropriate use of words in PR

Checklist

Before submitting this PR, please make sure:

  • You have read and followed the contributing guidelines.
  • You have tested your script on a forked repository.
  • You have updated the documentation if needed.
  • You have resolved any merge conflicts with the main branch.
    - need to resolve conflict on Contributors.md file.

Feedback

Thank you for taking the time to read this lo-o-o-o-o-o-o-ng PR description 😄. I appreciate your patience.

Please feel free to share your suggestions and improvements. I value your feedback and I want to know how we can improve it further.

Thank you again for your support and encouragement.

Esh07 and others added 30 commits September 13, 2023 15:50
I have refactor the script and separate the content checking, fetching PR and modified file as separate job in runner
 Additionally, the script is now more cover the use case such if user replace one line and add their line then script will accept that as well.

Co-Authored-By: Roshan Jossy <8488446+Roshanjossey@users.noreply.github.com>
Implemented code to check offensive content with purgoman API. There is no implementation (commented out) for checking URLs
Merge code is been separated into new job and it will have substitute steps jobs.

Added sub-job for checking mergeability
If the content contain offensive text then it will close the PR and one variable will update to true/false.
Here I break down the code to only perform single action such as merge request.
A separate job for only publishing comment with token permission to issue:write.

There are 3 types of comments
- comment about multiples files modified
- Comment on merge conflict
- Comment on multiple line changes in Contributors.md
- Comment about Code of Conduct in PR which used offensive language

Script also add review label to PR if there multiple file changes
PR will get message once PR get merged.

This code will randomly generate GIF and tag user.

Thanks to
-  @rp-1701  - share link dev.io
- @Roshanjossey - review
- @rossana87 - for reddit logo
- @NextThread - For share link to hackernews

Co-Authored-By: Roshan Jossy <8488446+Roshanjossey@users.noreply.github.com>
Co-Authored-By: Rossana Ventrella <99794555+rossana87@users.noreply.github.com>
Co-Authored-By: Rohan Patil <107268275+rp-1701@users.noreply.github.com>
Co-authored-by: Anurag Roy <binarybeast@Anurags-MacBook-Air.local>
There was format error
'Invalid workflow file
You have an error in your yaml syntax on line 416'

Script: | 
and underneath script was not align properly
Jobs does not have an id attribute and it is not required to mention. 

The job name itself is ID 

here is the issue I faced:

**Invalid workflow file:** .github/workflows/auto-pr-merge.yml#L11

The workflow is not valid. .github/workflows/auto-pr-merge.yml (Line: 11, Col: 5): Unexpected value 'id' .github/workflows/auto-pr-merge.yml (Line: 113, Col: 5): Unexpected value 'id'
There were invalid job references via IDs. 

Here is the error: 
The workflow is not valid. .github/workflows/auto-pr-merge.yml 

(Line: 365, Col: 25): Unrecognized named-value: 'check-non-safe-content'. 

Located at position 1 within expression: check-non-safe-content.check-non-safe-content.outputs.is_safe_contents .github/workflows/auto-pr-merge.yml 


**Before:**
'check-non-safe-content.check-non-safe-content.output.is_safe_content'

**After:**
'check-non-safe-content.output.is_safe_content'

(Line: 368, Col: 24): Unrecognized named-value: 'merge-pr'. Located at position 1 within expression: merge-pr.merge-pr.outputs.is_pr_mergeable

**Before:**
'merge-pr.merge-pr.is_pr_mergeable'

**After:**
'merge-pr.is_pr_mergeable'

The issue resolved in this commit
fixing this error 

The workflow is not valid. .github/workflows/auto-pr-merge.yml 

(Line: 365, Col: 25): Unrecognized named-value: 'check-non-safe-content'. Located at position 1 within expression: check-non-safe-content.outputs.is_safe_contents .github/workflows/auto-pr-merge.yml 

(Line: 368, Col: 24): Unrecognized named-value: 'merge-pr'. Located at position 1 within expression: merge-pr.outputs.is_pr_mergeable
Jobs if statement cannot utilize the env instead needs. 

Fix it by replacing `.env` with `.needs`
@Esh07 Esh07 marked this pull request as ready for review October 2, 2023 21:43
@Esh07 Esh07 mentioned this pull request Oct 6, 2023
@Esh07
Copy link
Contributor Author

Esh07 commented Oct 6, 2023

Hi @Roshanjossey, this version of the script leaves a ‘Requested change’ comment when it detects replaced lines or multiple changes. Do you think we should also close PRs when such changes are detected?

I’d appreciate your thoughts on this.

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