Skip to content

[ISSUE #1182]🔨Add remove-label-on-approve.yml #1183

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

Merged
merged 4 commits into from
Nov 17, 2024
Merged
Changes from 1 commit
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
49 changes: 49 additions & 0 deletions .github/workflows/remove-label-on-approve.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Remove Label on Approval

on:
pull_request_review:
types:
- submitted

jobs:
remove-label-on-approve:
if: github.event.review.state == 'approved' # Trigger only when the review is approved
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Remove label using github-script
uses: actions/github-script@v7
with:
github-token: ${{ secrets.BOT_TOKEN }}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Document required token permissions

The workflow uses BOT_TOKEN but doesn't specify the required permissions. This should be documented to ensure proper setup.

Add a comment explaining the required permissions:

+          # Requires a token with 'issues:write' permissions to manage labels
           github-token: ${{ secrets.BOT_TOKEN  }}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
github-token: ${{ secrets.BOT_TOKEN }}
# Requires a token with 'issues:write' permissions to manage labels
github-token: ${{ secrets.BOT_TOKEN }}

script: |
const labelToRemove = 'waiting-review'; // The name of the label to remove
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Make label name configurable

Consider making the label name configurable through workflow inputs rather than hardcoding it.

-            const labelToRemove = 'waiting-review'; // The name of the label to remove
+            const labelToRemove = core.getInput('label-name') || 'waiting-review';

Add this to the workflow inputs:

on:
  pull_request_review:
    types:
      - submitted

inputs:
  label-name:
    description: 'Name of the label to remove'
    required: false
    default: 'waiting-review'

const { context, github } = require('@actions/github');

// Get repository and PR context information
const { owner, repo } = context.repo;
const issue_number = context.payload.pull_request.number;

// Get all labels on the current PR
const { data: labels } = await github.rest.issues.listLabelsOnIssue({
owner,
repo,
issue_number,
});
Comment on lines +29 to +33
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add error handling for API calls

The API calls lack proper error handling, which could lead to silent failures.

-            const { data: labels } = await github.rest.issues.listLabelsOnIssue({
-              owner,
-              repo,
-              issue_number,
-            });
+            try {
+              const { data: labels } = await github.rest.issues.listLabelsOnIssue({
+                owner,
+                repo,
+                issue_number,
+              });
+              
+              // Check if the target label exists
+              const labelExists = labels.some(label => label.name === labelToRemove);
+              if (labelExists) {
+                // Remove the label
+                await github.rest.issues.removeLabel({
+                  owner,
+                  repo,
+                  issue_number,
+                  name: labelToRemove,
+                });
+                console.log(`Label '${labelToRemove}' has been removed.`);
+              } else {
+                console.log(`Label '${labelToRemove}' does not exist on this PR.`);
+              }
+            } catch (error) {
+              core.setFailed(`Failed to manage labels: ${error.message}`);
+            }

Also applies to: 40-46


// Check if the target label exists
const labelExists = labels.some(label => label.name === labelToRemove);
if (labelExists) {
// Remove the label
await github.rest.issues.removeLabel({
owner,
repo,
issue_number,
name: labelToRemove,
});
console.log(`Label '${labelToRemove}' has been removed.`);
} else {
console.log(`Label '${labelToRemove}' does not exist on this PR.`);
}
Loading