On Label: Remove Governed Label #6982
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: 'On Label: Remove Governed Label' | |
# Trigger workflow whenever a discussion is labeled | |
on: | |
discussion: | |
types: [labeled] | |
permissions: | |
contents: read | |
discussions: write | |
jobs: | |
action: | |
runs-on: ubuntu-latest | |
# Step 1 is to check if the user applying the label is an employee. If employee = 'true', later steps will not fire. If employee = 'false', later steps will continue | |
steps: | |
- name: Check if employee | |
id: check_employee | |
uses: actions/github-script@v6 | |
with: | |
github-token: ${{ secrets.READ_GITHUB_ORG_MEMBERS_TOKEN }} | |
result-encoding: string | |
script: | | |
try { | |
const response = await github.rest.orgs.checkMembershipForUser({ | |
org: `github`, | |
username: context.payload.sender.login | |
}); | |
if (response.status === 204) { | |
return 'true'; | |
} else { | |
return 'false'; | |
} | |
} catch (error) { | |
console.log(error); | |
return 'false'; | |
} | |
# Step 2 is to record the discussion id of the discussion that triggered the workflow in the GITHUB_ENV so the workflow knows which discussion post to then act on later | |
- name: Fetch discussion ID | |
id: fetch_discussion_id | |
if: ${{ steps.check_employee.outputs.result == 'false' && github.event.repository && github.event.discussion }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
OWNER: ${{ github.repository_owner }} | |
REPO: ${{ github.event.repository.name }} | |
run: | | |
gh api graphql -F owner=$OWNER -F name=$REPO -f query=' | |
query($owner: String!, $name: String!) { | |
repository(owner: $owner, name: $name) { | |
discussion(number: ${{ github.event.discussion.number }}) { | |
id | |
} | |
} | |
}' > discussion_data.json | |
echo 'DISCUSSION_ID='$(jq '.data.repository.discussion.id' discussion_data.json) >> $GITHUB_ENV | |
# Step 3 references the label-actions.yml file for instructions. That config file tells the action what to do next. | |
# config file will reference the GITHUB_ENV and will only process the 'unlabel' action on 'discussions' and will unlabel the named labels | |
- name: Label Actions | |
if: ${{ steps.check_employee.outputs.result == 'false' }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
OWNER: ${{ github.repository_owner }} | |
REPO: ${{ github.event.repository.name }} | |
config-path: '.github/label-actions.yml' | |
process-only: 'discussions' | |
uses: dessant/label-actions@v3 |