Skip to content
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

Write a workflow test #21

Merged
merged 2 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 66 additions & 0 deletions .github/workflows/validate-new-issue-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Validate new issue - test
on:
workflow_dispatch:
env:
GITHUB_TOKEN: ${{ secrets.PAT }}
jobs:
validate-new-issue-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: "It skips validation for Sentry org members."
run: |
set -euo pipefail

issue_number=$(
basename $(
gh issue create --title "Greetings, program!" --body "This is an issue, clearly."
)
)
echo "Checking issue #${issue_number}."

function download-logs {
echo "Downloading and extracting the log for the validation workflow run."
rm -rf log.zip log
run_id=$(
gh api "repos/:owner/:repo/actions/workflows/validate-new-issue.yml/runs" \
chadwhitacre marked this conversation as resolved.
Show resolved Hide resolved
| jq '.workflow_runs[0].id'
)
if [ -z "$run_id" ]; then
echo "No run_id."
return 1
fi
gh api "repos/:owner/:repo/actions/runs/${run_id}/logs" > log.zip
if [ $? -gt 0 ]; then
echo "Bad log.zip?"
return 1
fi
logfile="validate-new-issue/3_Validate issue against templates.txt"
unzip log.zip "$logfile"
mv "$logfile" log
rm -rf validate-new-issue
rm log.zip
echo "Are we looking at the right issue?"
expected="${issue_number}"
actual=$(grep 'Validating issue #[0-9]*\.[^"]' log | sed -Ee 's/.*#([0-9]*)\..*/\1/')
echo "Expected: ${expected}"
echo "Actual: ${actual}"
test $actual = $expected
}

function backoff {
echo "Trying $1 several times with backoff ..."
for i in {0..6}; do
[ $i -gt 0 ] && sleep $(( 2 ** $i ))
echo "::group::Trying $1 ..."
$1 && echo "::endgroup::" && return
echo "::endgroup::"
done
echo "$1 backed right off the edge! 😧"
exit 1
}

backoff download-logs
echo "Logs downloaded successfully, proceeding to test."

grep "Skipping validation" log > /dev/null
49 changes: 49 additions & 0 deletions .github/workflows/validate-new-issue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: Validate new issue
on:
issues:
types: ['opened']
jobs:
validate-new-issue:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: "Validate issue against templates"
shell: bash
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
echo "Validating issue #${{ github.event.issue.number }}."

# Trust users who belong to the getsentry org.
if gh api "https://api.github.com/orgs/getsentry/members/${{ github.actor }}" >/dev/null 2>&1; then
echo "Skipping validation, because ${{ github.actor }} is a member of the getsentry org."
exit 0
else
echo "${{ github.actor }} is not a member of the getsentry org. 🧐"
fi

# Look for a template where the headings match this issue's
jq -r .issue.body "$GITHUB_EVENT_PATH" > issue-body
for template in $(ls .github/ISSUE_TEMPLATE/*.md 2> /dev/null); do
echo -n "$(basename $template)? "
# <() is process substitution - https://superuser.com/a/1060002
if diff -rub <(grep '^#' $template) <(grep '^#' issue-body) > /dev/null; then
echo "👍 💃"
exit 0
else
echo "👎"
fi
done

# Failed to find a match! Close the issue.
cat << EOF > comment
{"body": "Sorry, friend. As far as this ol' bot can tell, your issue does not use one of this repo's available issue templates. Please [try again using a template](https://github.com/${{ github.repository }}/issues/new/choose) so that we have the best chance of understanding and addressing your issue. (And if I'm confused, please [let us know](https://github.com/getsentry/.github/issues/new?title=template+enforcer+is+confused&body=${{ github.event.issue.html_url }}). 😬)\n\n----\n\n[![Did you see the memo about this?](https://user-images.githubusercontent.com/134455/104515469-e04a9c80-55c0-11eb-8e15-ffe9c0b8dd7f.gif)](https://www.youtube.com/watch?v=Fy3rjQGc6lA)"}
EOF

# Might get `gh issue comment` some day - https://github.com/cli/cli/issues/517
chadwhitacre marked this conversation as resolved.
Show resolved Hide resolved
echo -n "Commented: "
gh api "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments" \
--method POST \
--input comment \
| jq .html_url
gh issue close ${{ github.event.issue.number }}