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

Notify upon successful deployment #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
89 changes: 89 additions & 0 deletions .github/workflows/notify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/bin/bash

set -e

# GitHub API URL
AronNovak marked this conversation as resolved.
Show resolved Hide resolved
API_URL="https://api.github.com"

# Function to extract issue number from commit message
AronNovak marked this conversation as resolved.
Show resolved Hide resolved
extract_issue_number() {
local commit_message="$1"
local issue_number
local pr_number

# Try to extract from "Merge pull request ... from branch/123"
issue_number=$(echo "$commit_message" | grep -oP 'from [a-zA-Z-_0-9]+/\K([0-9]+)')

if [ -z "$issue_number" ]; then
# Try to extract from "... (#123)"
pr_number=$(echo "$commit_message" | grep -oP '\(#\K([0-9]+)(?=\))')

if [ -n "$pr_number" ]; then
# Fetch PR description
pr_description=$(curl -s -H "Authorization: token $GH_TOKEN" "$API_URL/repos/$GITHUB_REPOSITORY/pulls/$pr_number" | jq -r .body)

if [ -n "$pr_description" ]; then
# Extract issue numbers from PR description
issue_numbers=($(echo "$pr_description" | grep -oP '#\K([0-9]+)(?=\s)'))

if [ ${#issue_numbers[@]} -gt 0 ]; then
issue_number=${issue_numbers[0]}
fi
fi
fi
fi

echo "$issue_number $pr_number"
}

print_debug_info() {
echo "Debug Information:"
echo "COMMIT_MESSAGE: $COMMIT_MESSAGE"
echo "ISSUE_NUMBER: $ISSUE_NUMBER"
echo "PR_NUMBER: $PR_NUMBER"
echo "GITHUB_REPOSITORY: $GITHUB_REPOSITORY"
echo "URL_QA: $URL_QA"
echo "API_URL: $API_URL"
echo "Environment Variables:"
env
}

# Get the commit message
AronNovak marked this conversation as resolved.
Show resolved Hide resolved
COMMIT_MESSAGE=$(git log -1 --pretty=format:"%s")

# Extract the issue number and PR number
read ISSUE_NUMBER PR_NUMBER <<< $(extract_issue_number "$COMMIT_MESSAGE")

if [ -z "$ISSUE_NUMBER" ]; then
echo "Could not determine the issue number from the commit message."
exit 0
fi

# Prepare the comment body
AronNovak marked this conversation as resolved.
Show resolved Hide resolved
if [ -n "$PR_NUMBER" ]; then
COMMENT_BODY="The latest changes (#$PR_NUMBER) have been deployed successfully to [$URL_QA]($URL_QA)"
else
COMMENT_BODY="The latest changes have been deployed successfully to [$URL_QA]($URL_QA)"
fi

# Make the API call and capture both the HTTP status code and the response body
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
-H "Authorization: token $GH_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
"$API_URL/repos/$GITHUB_REPOSITORY/issues/$ISSUE_NUMBER/comments" \
-d "{\"body\":\"$COMMENT_BODY\"}")

# Extract the response body and status code
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
STATUS_CODE=$(echo "$RESPONSE" | tail -n1)

# Check if the request was successful
AronNovak marked this conversation as resolved.
Show resolved Hide resolved
if [ "$STATUS_CODE" -ne 201 ]; then
echo "Failed to post comment to issue #$ISSUE_NUMBER. HTTP status code: $STATUS_CODE"
echo "Response body:"
echo "$RESPONSE_BODY"
print_debug_info
exit 1
fi

echo "Comment posted to issue #$ISSUE_NUMBER"
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,8 @@ jobs:
direnvVersion: 2.32.3

- name: Deploy
env:
GITHUB_REPOSITORY: ${{ github.repository }}
run: |
deploy-to-nixos ${{ env.PROJECT_NAME}}-${{ env.ENV }}
.github/workflows/notify.sh