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

[ISSUE #542]🔨Add auto add label for pr approval #543

Merged
merged 4 commits into from
Jun 14, 2024
Merged
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
41 changes: 41 additions & 0 deletions .github/workflows/label-on-approval.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Label on Approval

on:
pull_request_review:
types: [ submitted ]

jobs:
add-label-on-approval:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Check for approval and add label
uses: actions/github-script@v6
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using GITHUB_TOKEN for authentication to follow best practices and ensure security.

-  GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
+  GITHUB_TOKEN: ${{ secrets.GITHUB_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.PAT_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

with:
script: |
const github = require('@actions/github');
const context = github.context;
const approved = context.payload.review.state === 'approved';
const owner = context.repo.owner;
const repo = context.repo.repo;
const pull_number = context.payload.pull_request.number;

if (approved) {
const { data: collaborators } = await github.repos.listCollaborators({ owner, repo });
const reviewer = context.payload.review.user.login;
const isOwner = collaborators.some(collab => collab.login === reviewer && collab.permissions.admin);

if (isOwner) {
await github.issues.addLabels({
owner,
repo,
issue_number: pull_number,
labels: ['approved','auto merge']
});
}
Comment on lines +26 to +38
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure robustness by handling potential errors during API calls, such as when adding labels or fetching collaborators.

+ try {
  await github.issues.addLabels({
    owner,
    repo,
    issue_number: pull_number,
    labels: ['approved','auto merge']
  });
+ } catch (error) {
+   console.error('Failed to add labels:', error);
+ }
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
if (approved) {
const { data: collaborators } = await github.repos.listCollaborators({ owner, repo });
const reviewer = context.payload.review.user.login;
const isOwner = collaborators.some(collab => collab.login === reviewer && collab.permissions.admin);
if (isOwner) {
await github.issues.addLabels({
owner,
repo,
issue_number: pull_number,
labels: ['approved','auto merge']
});
}
if (approved) {
const { data: collaborators } = await github.repos.listCollaborators({ owner, repo });
const reviewer = context.payload.review.user.login;
const isOwner = collaborators.some(collab => collab.login === reviewer && collab.permissions.admin);
if (isOwner) {
try {
await github.issues.addLabels({
owner,
repo,
issue_number: pull_number,
labels: ['approved','auto merge']
});
} catch (error) {
console.error('Failed to add labels:', error);
}
}

}
Loading