Skip to content

On Create: Remove Governed Label #49562

On Create: Remove Governed Label

On Create: Remove Governed Label #49562

name: 'On Create: Remove Governed Label'
# Trigger workflow whenever a discussion is created
on:
discussion:
types: [created]
jobs:
check_labels:
name: Check labels
runs-on: ubuntu-latest
# Step 1 is to check if the discussion creator 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: |
console.log(context.payload);
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 grabs the label id of the label we want to remove and stores it in the GITHUB_ENV. We use the 'TOPIC' to define the label in plain text
- name: Fetch label id
id: fetch_label_id
if: ${{ steps.check_employee.outputs.result == 'false' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
TOPIC: 'more information needed'
run: |
gh api graphql -F owner=$OWNER -F name=$REPO -F topic="$TOPIC" -f query='
query($owner: String!, $name: String!, $topic: String) {
repository(owner: $owner, name: $name){
labels(first: 3, query: $topic) {
edges {
node {
id
name
}
}
}
}
}' > repository_label_data.json
echo 'LABEL_ID='$(jq '.data.repository.labels.edges[].node | .id ' repository_label_data.json) >> $GITHUB_ENV
# Step 3 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' }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
TOPIC: 'more information needed'
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 4 recalls the appropriate discussion id and label id from the GITHUB_ENV and removes the label from the discussion
- name: Unlabel the discussion
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
DISCUSSION_ID: ${{ env.DISCUSSION_ID }}
LABEL_IDS: ${{ env.LABEL_ID }}
run: |
IFS=' ' read -ra labelIds <<< "$LABEL_IDS"
for labelId in "${labelIds[@]}"; do
gh api graphql -f query='
mutation($labelableId: ID!, $labelIds: [ID!]!) {
removeLabelsFromLabelable(input: {labelableId: $labelableId, labelIds: $labelIds}) {
labelable {
labels(first: 10) {
edges {
node {
id
name
}
}
}
}
}
}' -f labelableId=$DISCUSSION_ID -f labelIds=$labelId
done