Skip to content

[Github Action] Label community issues #1746

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
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
58 changes: 58 additions & 0 deletions .github/workflows/label-community-issues.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Label Community Issues

on:
issues:
types: [opened]

jobs:
label-community-issues:
runs-on: ubuntu-latest
permissions:
issues: write
contents: read

steps:
- name: Check organization membership
id: check-membership
uses: actions/github-script@v7
with:
github-token: ${{ secrets.ORG_MEMBER_READ_TOKEN }}
script: |
const issueAuthor = '${{ github.event.issue.user.login }}';
const orgName = 'elastic';

try {
await github.rest.orgs.getMembershipForUser({
Copy link
Member

Choose a reason for hiding this comment

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

unfortunately, I'm afraid this won't work with the standard github token.

You need a PAT that is able to read org membership

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Argh yes I didn't consider most of our users have private membership.

Can we use a PAT that we store somewhere safe and invoke it here?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. Adding it to the GitHub repository secrets should be sufficient for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, is this something you can help with? I don't have any permissions to do such things I believe

Copy link
Member

Choose a reason for hiding this comment

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

image

I created the ORG_MEMBER_READ_TOKEN which can be accessed with secrets.ORG_MEMBER_READ_TOKEN.

For now it's configured to our max TTL. We can revisit this in a couple of months.

The platform team is working on ephemeral token service that we can utilize in the future.

Copy link
Member

Choose a reason for hiding this comment

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

The token can only read the membership. Hence you might need to split the script into two steps. One for reading and one for adding the label

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok thank you :) will look into it later!

org: orgName,
username: issueAuthor
});

console.log(`${issueAuthor} is a member of ${orgName} organization`);
return 'member';

} catch (error) {
if (error.status === 404) {
console.log(`${issueAuthor} is not a member of ${orgName} organization`);
return 'non-member';
} else {
console.log('Error checking organization membership:', error);
return 'error';
}
}

- name: Add community label
if: steps.check-membership.outputs.result == 'non-member'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const issueAuthor = '${{ github.event.issue.user.login }}';

await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ['community']
});

console.log(`Added "community" label to issue by ${issueAuthor}`);
Loading