Skip to content

chore(ci): add label workflow #2003

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
82 changes: 82 additions & 0 deletions .github/workflows/label.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: Label Pull Requests

on:
pull_request:
types:
- opened
- reopened
- unlabeled

permissions:
pull-requests: write

jobs:
external-contributor:
runs-on: ubuntu-latest
steps:
- name: Check Author Association and Label PR
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const pr = context.payload.pull_request;
if (!pr) {
console.log("Could not get PR from context");
Copy link
Member

Choose a reason for hiding this comment

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

This should probably fail and indicate an error, right? Maybe we want a label like 'UNLABELABLE' or something? But i guess without the PR there is nothing to label?

return;
}

// Define associations considered "internal"
const internalAssociations = ["MEMBER"];

// Label to add if the author is external
const externalLabel = "external-contributor";

const authorAssociation = pr.author_association;
const isExternal = !internalAssociations.includes(authorAssociation);

const prLabels = pr.labels.map(label => label.name);
const hasExternalLabel = prLabels.includes(externalLabel);

console.log(`Event: ${context.eventName}, Action: ${context.payload.action}`);
console.log(`Author: ${pr.user.login}, Association: ${authorAssociation}, Is External: ${isExternal}`);
console.log(`Current PR Labels: ${prLabels.join(', ')}`);

// Logic for 'unlabeled' event: only re-add if *our* label was removed and author is still external
if (context.eventName === 'pull_request_target' && context.payload.action === 'unlabeled') {
const removedLabel = context.payload.label.name;
console.log(`Label removed: ${removedLabel}`);
if (removedLabel === externalLabel && isExternal) {
console.log(`External label was removed, author is still external. Re-adding label: ${externalLabel}`);
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: [externalLabel]
});
console.log(`Label "${externalLabel}" re-added successfully.`);
} else {
console.log(`Removed label was not the external label or author is not external. No action needed for unlabeled event.`);
}
}
// Logic for 'opened' or 'reopened' events: add label if external and not already present
else if (['opened', 'reopened'].includes(context.payload.action)) {
if (isExternal && !hasExternalLabel) {
console.log(`Author association "${authorAssociation}" is external and label is missing. Adding label: ${externalLabel}`);
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: [externalLabel]
});
console.log(`Label "${externalLabel}" added successfully.`);
} else if (isExternal && hasExternalLabel) {
console.log(`Author is external, but label "${externalLabel}" is already present.`);
} else {
console.log(`Author association "${authorAssociation}" is internal or label already present. No label added.`);
Copy link
Member

Choose a reason for hiding this comment

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

You already handle the case where the label is present above. You probably want to remove the label if it is labelled as external, but maybe just note that it is?

Suggested change
console.log(`Author association "${authorAssociation}" is internal or label already present. No label added.`);
if (hasExternalLabel) {
console.log(`Author association "${authorAssociation}" is internal. However, external contributor label is present.`);
} else {
console.log(`Author association "${authorAssociation}" is internal. No label added.`);
}

}
}
// Optional: Add logic for 'synchronize' if you uncomment it in the 'on:' section
// else if (context.payload.action === 'synchronize') { ... }
else {
console.log(`Unhandled action type: ${context.payload.action}. No labeling action taken.`);
Copy link
Member

Choose a reason for hiding this comment

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

Maybe error log messages such as this one should step summaries or otherwise easier to access?

}
Loading