-
Notifications
You must be signed in to change notification settings - Fork 3
Add reusable workflow to check external contributor status #78
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
base: main
Are you sure you want to change the base?
Changes from all commits
f12a17a
316e24a
05ce417
fdedc15
94b4caf
472a5cc
19dd374
fdf1dea
1fa523c
8ad13fa
993bf3d
e9d7d6c
62e994b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Check External Contributor Action | ||
|
|
||
| Automatically labels pull requests created by users who are not members of a specified GitHub team. | ||
|
|
||
| ## Usage | ||
|
|
||
| ```yaml | ||
| - uses: SolaceDev/solace-public-workflows/check-external-contributor@main | ||
| with: | ||
| github_team_slug: solace-ai | ||
| label_name: "external contributor" | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| ``` | ||
|
|
||
| ## Inputs | ||
|
|
||
| | Input | Description | Required | Default | | ||
| |-------|-------------|----------|---------| | ||
| | `github_team_slug` | GitHub team slug to check membership against (e.g., `solace-ai`) | Yes | - | | ||
| | `label_name` | Label to add to PR if creator is not in the team | No | `"external contributor"` | | ||
| | `github-token` | GitHub token for API access | Yes | - | | ||
|
|
||
| ## How it Works | ||
|
|
||
| 1. Checks if the PR creator is a member of the specified GitHub team | ||
| 2. If not a member, adds the specified label to the PR | ||
| 3. Logs the results for debugging | ||
|
|
||
| ## Workflow Trigger | ||
|
|
||
| This action is designed to work with `pull_request_target` to safely handle external contributors: | ||
|
|
||
| ```yaml | ||
| on: | ||
| pull_request_target: | ||
| types: [opened, reopened] | ||
|
|
||
| jobs: | ||
| check-external: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| pull-requests: write | ||
| issues: write | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Basic Example | ||
|
|
||
| ```yaml | ||
| name: Check External Contributor | ||
| on: | ||
| pull_request_target: | ||
| types: [opened, reopened] | ||
|
|
||
| jobs: | ||
| check: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| pull-requests: write | ||
| issues: write | ||
| steps: | ||
| - uses: SolaceDev/solace-public-workflows/check-external-contributor@main | ||
| with: | ||
| github_team_slug: my-team | ||
| label_name: "external contributor" | ||
| github-token: ${{ secrets.GITHUB_TOKEN }} | ||
| ``` | ||
|
|
||
| ## Permissions Required | ||
|
|
||
| The GitHub token must have the following permissions: | ||
| - `pull-requests: write` - To access PR information | ||
| - `issues: write` - To add labels to PRs | ||
|
|
||
| ## Notes | ||
|
|
||
| - Use `pull_request_target` instead of `pull_request` for security when running workflows on external PRs | ||
| - The action gracefully handles errors when checking team membership | ||
| - If the label doesn't exist, GitHub will automatically create it when adding it to the PR |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| name: Check External Contributor | ||
| description: Checks if PR creator is in a GitHub team and adds a label if not | ||
|
|
||
| inputs: | ||
| github_team_slug: | ||
| description: "GitHub team slug to check membership against (e.g., 'solace-ai')" | ||
| required: true | ||
| label_name: | ||
| description: "Label to add to PR if creator is not in the team" | ||
| required: false | ||
| default: "external contributor" | ||
| github-token: | ||
| description: "GitHub token for API access" | ||
| required: true | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Check if PR creator is in team | ||
| id: check-team | ||
| uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
| with: | ||
| github-token: ${{ inputs.github-token }} | ||
| script: | | ||
| const teamSlug = '${{ inputs.github_team_slug }}'; | ||
| const org = context.repo.owner; | ||
| const prCreator = context.payload.pull_request.user.login; | ||
|
|
||
| console.log(`🔍 Checking team membership for PR creator...`); | ||
| console.log(` - Team slug: ${teamSlug}`); | ||
| console.log(` - PR creator: ${prCreator}`); | ||
| console.log(` - Organization: ${org}`); | ||
|
|
||
| try { | ||
| const { data } = await github.rest.teams.getMembershipForUserInOrg({ | ||
| org: org, | ||
| team_slug: teamSlug, | ||
| username: prCreator | ||
| }); | ||
|
|
||
| core.setOutput('is_member', 'true'); | ||
| console.log(`✅ PR creator is a member of the team`); | ||
| } catch (error) { | ||
| if (error.status === 404) { | ||
| core.setOutput('is_member', 'false'); | ||
| console.log(`⚠️ PR creator is not a member of the team`); | ||
| } else { | ||
| console.log(`⚠️ Could not verify team membership (${error.message}), assuming external contributor`); | ||
| core.setOutput('is_member', 'false'); | ||
| } | ||
|
Comment on lines
+44
to
+50
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @johnvincentcorpuz when running this with Note that I have tried @clarkbains suggestion here to use See thread with Clark: https://solacedotcom.slack.com/archives/C084S3XTD8S/p1772498735212189?thread_ts=1772496075.926289&cid=C084S3XTD8S and used |
||
| } | ||
|
|
||
| - name: Add external contributor label | ||
| if: steps.check-team.outputs.is_member == 'false' | ||
| uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
| with: | ||
| github-token: ${{ inputs.github-token }} | ||
| script: | | ||
| const labelName = '${{ inputs.label_name }}'; | ||
| const prNumber = context.issue.number; | ||
|
|
||
| console.log(`🏷️ Adding label "${labelName}" to PR #${prNumber}...`); | ||
|
|
||
| await github.rest.issues.addLabels({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| labels: [labelName] | ||
| }); | ||
|
|
||
| console.log(`✅ Added label "${labelName}" to PR #${prNumber}`); | ||
Uh oh!
There was an error while loading. Please reload this page.