Skip to content

OS, workflow, and pull request support #4

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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!
1. Enjoy!
15 changes: 15 additions & 0 deletions get_pull_request_title.rb
Original file line number Diff line number Diff line change
@@ -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
40 changes: 31 additions & 9 deletions send.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand All @@ -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
}
],
Expand Down
5 changes: 5 additions & 0 deletions testing_utils/README.md
Original file line number Diff line number Diff line change
@@ -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!
11 changes: 11 additions & 0 deletions testing_utils/local_env_run.sh
Original file line number Diff line number Diff line change
@@ -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