diff --git a/README.md b/README.md index 17751b3..8859aac 100644 --- a/README.md +++ b/README.md @@ -2,16 +2,19 @@ ## Setup 1. In Github secrets, add a `WEBHOOK_URL` variable with the Discord web hook URL 1. In your Github actions yml file, add this to reference the variable you just created: - - To see a real example, visit [here](https://github.com/unthreaded/git-hooks/blob/80e914105c0a9d94282f8f1b0a1b39ea8d59dc33/.github/workflows/build.yml#L36). + - To see a real example, visit [here](https://github.com/unthreaded/git-hooks/blob/92ea6bde348431fbe25d05c33398c969eec5d3ee/.github/workflows/build.yml#L48). ```yaml + - uses: actions/setup-ruby@v1 - name: Send Webhook Notification if: always() env: JOB_STATUS: ${{ job.status }} WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }} + HOOK_OS_NAME: ${{ runner.os }} + WORKFLOW_NAME: ${{ github.workflow }} run: | - wget https://raw.githubusercontent.com/DiscordHooks/github-actions-discord-webhook/master/send.sh - chmod +x send.sh - ./send.sh $JOB_STATUS $WEBHOOK_URL + git clone https://github.com/DiscordHooks/github-actions-discord-webhook.git webhook + bash webhook/send.sh $JOB_STATUS $WEBHOOK_URL + shell: bash ``` -1. Enjoy! \ No newline at end of file +1. Enjoy! diff --git a/get_pull_request_title.rb b/get_pull_request_title.rb new file mode 100644 index 0000000..dbc3192 --- /dev/null +++ b/get_pull_request_title.rb @@ -0,0 +1,15 @@ +require 'net/http' +require 'json' + +if ARGV.length != 1 then + puts "Expected only one argument: ENDPOINT_URL" + exit -1 +end + +responce = Net::HTTP.get_response(URI(ARGV[0])) +if responce.code == "200" + puts JSON.parse(responce.body)["title"] +else + puts "Had an issue calling endpoint..." + exit -1 +end diff --git a/send.sh b/send.sh index 346def2..28c6d4d 100644 --- a/send.sh +++ b/send.sh @@ -37,28 +37,50 @@ COMMIT_URL="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" # If, for example, $GITHUB_REF = refs/heads/feature/example-branch # Then this sed command returns: feature/example-branch -BRANCH_NAME="$(echo $GITHUB_REF | sed 's/^.*\/.*\///g')" -BRANCH_URL="https://github.com/$GITHUB_REPOSITORY/tree/$BRANCH_NAME" - +BRANCH_NAME="$(echo $GITHUB_REF | sed 's/^[^/]*\/[^/]*\///g')" +REPO_URL="https://github.com/$GITHUB_REPOSITORY" +BRANCH_OR_PR="Branch" +BRANCH_OR_PR_URL="$REPO_URL/tree/$BRANCH_NAME" +ACTION_URL="$COMMIT_URL/checks" +COMMIT_OR_PR_URL=$COMMIT_URL if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then CREDITS="$AUTHOR_NAME authored & committed" else CREDITS="$AUTHOR_NAME authored & $COMMITTER_NAME committed" fi -TIMESTAMP=$(date --utc +%FT%TZ) +if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then + BRANCH_OR_PR="Pull Request" + + PR_NUM=$(sed 's/\/.*//g' <<< $BRANCH_NAME) + BRANCH_OR_PR_URL="$REPO_URL/pull/$PR_NUM" + BRANCH_NAME="#${PR_NUM}" + + # Call to GitHub API to get PR title + PULL_REQUEST_ENDPOINT="https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$PR_NUM" + + WORK_DIR=$(dirname ${BASH_SOURCE[0]}) + PULL_REQUEST_TITLE=$(ruby $WORK_DIR/get_pull_request_title.rb $PULL_REQUEST_ENDPOINT) + + COMMIT_SUBJECT=$PULL_REQUEST_TITLE + COMMIT_MESSAGE="Pull Request #$PR_NUM" + ACTION_URL="$BRANCH_OR_PR_URL/checks" + COMMIT_OR_PR_URL=$BRANCH_OR_PR_URL +fi + +TIMESTAMP=$(date -u +%FT%TZ) WEBHOOK_DATA='{ "username": "", "avatar_url": "'$AVATAR'", "embeds": [ { "color": '$EMBED_COLOR', "author": { - "name": "Build '"$STATUS_MESSAGE"' - '"$GITHUB_REPOSITORY"'", - "url": "'$COMMIT_URL'", + "name": "'"$STATUS_MESSAGE"': '"$WORKFLOW_NAME"' ('"${HOOK_OS_NAME}"') - '"$GITHUB_REPOSITORY"'", + "url": "'$ACTION_URL'", "icon_url": "'$AVATAR'" }, "title": "'"$COMMIT_SUBJECT"'", - "url": "'"$URL"'", + "url": "'"$COMMIT_OR_PR_URL"'", "description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'", "fields": [ { @@ -67,8 +89,8 @@ WEBHOOK_DATA='{ "inline": true }, { - "name": "Branch", - "value": "'"[\`${BRANCH_NAME}\`](${BRANCH_URL})"'", + "name": "'"$BRANCH_OR_PR"'", + "value": "'"[\`${BRANCH_NAME}\`](${BRANCH_OR_PR_URL})"'", "inline": true } ], diff --git a/testing_utils/README.md b/testing_utils/README.md new file mode 100644 index 0000000..5df2b44 --- /dev/null +++ b/testing_utils/README.md @@ -0,0 +1,5 @@ +# Testing utils +Testing this on GitHub Actions can be annoying due to wait times. While you will eventually have to do so, when developing locally, it's nice to have these tool(s). + +## Tools +- [`local_env_run.sh`](local_env_run.sh) Setup dummy variables and run send.sh! \ No newline at end of file diff --git a/testing_utils/local_env_run.sh b/testing_utils/local_env_run.sh new file mode 100644 index 0000000..702145b --- /dev/null +++ b/testing_utils/local_env_run.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +export GITHUB_SHA="69a3b64e5b36db68b54ca7291d8acfde261dda76" +# Enter your url.....don't commit this +export DISCORD_URL="" +export GITHUB_REPOSITORY="DiscordHooks/github-actions-discord-webhook" +export GITHUB_REF="refs/heads/4/merge" # refs/heads/feature/new-stuff +export HOOK_OS_NAME="Windows" +export WORKFLOW_NAME="Main Pipeline" +export GITHUB_EVENT_NAME="pull_request" # Could also be push, ext. +./send.sh Success $DISCORD_URL \ No newline at end of file