2023-05-05 05:18:39 -05:00
|
|
|
name: Notify Slack channel based on new issue label
|
2022-06-22 09:22:18 -05:00
|
|
|
|
|
|
|
on:
|
|
|
|
issues:
|
|
|
|
types: [labeled]
|
|
|
|
|
|
|
|
jobs:
|
2023-08-02 06:25:23 -05:00
|
|
|
config:
|
|
|
|
runs-on: "ubuntu-latest"
|
|
|
|
outputs:
|
|
|
|
has-secrets: ${{ steps.check.outputs.has-secrets }}
|
|
|
|
steps:
|
|
|
|
- name: "Check for secrets"
|
|
|
|
id: check
|
|
|
|
shell: bash
|
|
|
|
run: |
|
|
|
|
if [ -n "${{ (secrets.SLACK_WEBHOOK_URL != '') || '' }}" ]; then
|
|
|
|
echo "has-secrets=1" >> "$GITHUB_OUTPUT"
|
|
|
|
fi
|
|
|
|
|
2022-06-22 09:22:18 -05:00
|
|
|
notify:
|
2023-08-02 06:25:23 -05:00
|
|
|
needs: config
|
|
|
|
if: needs.config.outputs.has-secrets
|
2022-06-22 09:22:18 -05:00
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
2022-06-23 05:31:33 -05:00
|
|
|
- name: "Download teams.yml to know which label is for which team"
|
|
|
|
run: wget https://raw.githubusercontent.com/grafana/grafana/main/.github/teams.yml
|
2022-06-22 09:22:18 -05:00
|
|
|
|
|
|
|
- name: "Determine which team to notify"
|
|
|
|
run: |
|
|
|
|
# Default to null values.
|
|
|
|
CHANNEL="null"
|
2023-05-05 05:18:39 -05:00
|
|
|
TEAM="null"
|
2022-06-22 09:22:18 -05:00
|
|
|
|
|
|
|
echo "${{ github.event.label.name }} label added"
|
|
|
|
export CURRENT_LABEL="${{ github.event.label.name }}" # Enable the use of the label in yq evaluations
|
|
|
|
# yq is installed by default in ubuntu-latest
|
2022-06-23 05:31:33 -05:00
|
|
|
if [[ $(yq e 'keys | .[] | select(. == env(CURRENT_LABEL))' teams.yml ) ]]; then
|
2022-06-22 09:22:18 -05:00
|
|
|
# Check if we have a channel set to notify on comments.
|
2022-06-23 05:31:33 -05:00
|
|
|
if [[ $(yq '.[env(CURRENT_LABEL)] | has("channel-label")' teams.yml ) == true ]]; then
|
|
|
|
CHANNEL=$(yq '.[env(CURRENT_LABEL)].channel-label' teams.yml)
|
2022-08-24 09:20:10 -05:00
|
|
|
echo "Ready to send issue to channel ID ${CHANNEL}"
|
2022-06-22 09:22:18 -05:00
|
|
|
fi
|
2023-05-05 05:18:39 -05:00
|
|
|
|
|
|
|
if [[ $(yq '.[env(CURRENT_LABEL)] | has("exclude-github-team")' teams.yml ) == true ]]; then
|
|
|
|
TEAM=$(yq '.[env(CURRENT_LABEL)].exclude-github-team' teams.yml)
|
|
|
|
echo "Will not send issue to channel if issue author is part of the team ${TEAM}"
|
|
|
|
fi
|
2022-06-22 09:22:18 -05:00
|
|
|
fi
|
|
|
|
|
2023-05-05 05:18:39 -05:00
|
|
|
# set environment for next steps
|
2022-06-22 09:22:18 -05:00
|
|
|
echo "CHANNEL=${CHANNEL}" >> $GITHUB_ENV
|
2023-05-05 05:18:39 -05:00
|
|
|
echo "TEAM=${TEAM}" >> $GITHUB_ENV
|
2022-06-22 09:22:18 -05:00
|
|
|
|
|
|
|
- name: "Prepare payload"
|
2024-03-14 04:20:24 -05:00
|
|
|
uses: frabert/replace-string-action@v2.5
|
2022-06-22 09:22:18 -05:00
|
|
|
id: preparePayload
|
|
|
|
with:
|
|
|
|
# replace double quotes with single quotes to avoid breaking the JSON payload sent to Slack
|
|
|
|
string: ${{ github.event.issue.title }}
|
|
|
|
pattern: '"'
|
|
|
|
replace-with: "'"
|
|
|
|
flags: 'g'
|
|
|
|
|
2023-10-06 05:25:01 -05:00
|
|
|
- name: Get Token
|
|
|
|
id: get_workflow_token
|
2024-03-14 05:25:36 -05:00
|
|
|
uses: peter-murray/workflow-application-token-action@v3
|
2023-10-06 05:25:01 -05:00
|
|
|
with:
|
|
|
|
application_id: ${{ secrets.APP_GRAFANA_TEAM_CHECKER_ID }}
|
|
|
|
application_private_key: ${{ secrets.APP_GRAFANA_TEAM_CHECKER_KEY }}
|
|
|
|
|
2023-05-05 05:18:39 -05:00
|
|
|
- name: "Check that issue author is not part of the team"
|
|
|
|
if: ${{ env.TEAM != 'null' }}
|
2023-09-29 10:36:52 -05:00
|
|
|
run: |
|
|
|
|
response=$(gh api /orgs/grafana/teams/${{ env.TEAM }}/memberships/${{ github.event.issue.user.login }} -i -H "Accept: application/vnd.github.v3+json")
|
|
|
|
STATUS_CODE=$(echo "$response" | head -n 1 | cut -d' ' -f2)
|
|
|
|
if [ "$status_code" -eq 404 ]; then
|
|
|
|
echo "The user was not found in the team."
|
|
|
|
echo "USER_FOUND=false" >> $GITHUB_ENV
|
|
|
|
else
|
|
|
|
echo "The user was potentially found in the team"
|
|
|
|
echo "USER_FOUND=maybe" >> $GITHUB_ENV
|
|
|
|
fi
|
|
|
|
env:
|
2023-10-06 05:25:01 -05:00
|
|
|
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
|
2023-05-05 05:18:39 -05:00
|
|
|
|
2022-06-22 09:22:18 -05:00
|
|
|
- name: "Send Slack notification"
|
2023-09-29 10:36:52 -05:00
|
|
|
if: ${{ (env.CHANNEL != 'null') && ((env.USER_FOUND == 'false') || (env.TEAM != 'null')) }}
|
2024-04-22 04:10:36 -05:00
|
|
|
uses: slackapi/slack-github-action@v1.26.0
|
2022-06-22 09:22:18 -05:00
|
|
|
with:
|
|
|
|
payload: >
|
|
|
|
{
|
|
|
|
"icon_emoji": ":grafana:",
|
|
|
|
"username": "Grafana issue labeled",
|
2023-05-05 05:18:39 -05:00
|
|
|
"text": "Issue \"${{ steps.preparePayload.outputs.replaced }}\" labeled \"${{ github.event.label.name }}\": ${{ github.event.issue.html_url }}, please triage.",
|
2022-06-22 09:22:18 -05:00
|
|
|
"channel": "${{ env.CHANNEL }}"
|
|
|
|
}
|
|
|
|
env:
|
|
|
|
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
|