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 after deployment failure #595

Merged
merged 7 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 2 additions & 0 deletions .travis.template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ jobs:
- "$TRAVIS_BUILD_DIR/ci-scripts/test_phpunit.sh || travis_terminate 1;"
- "(travis_retry $TRAVIS_BUILD_DIR/ci-scripts/prepare_deploy.sh) || travis_terminate 1;"
- "(travis_retry ddev robo deploy:pantheon-sync live) || travis_terminate 1;"
after_failure:
- "ci-scripts/notify_after_failure.sh"
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ jobs:
- "$TRAVIS_BUILD_DIR/ci-scripts/test_phpunit.sh || travis_terminate 1;"
- "(travis_retry $TRAVIS_BUILD_DIR/ci-scripts/prepare_deploy.sh) || travis_terminate 1;"
- "(travis_retry ddev robo deploy:pantheon-sync live) || travis_terminate 1;"
after_failure:
- "ci-scripts/notify_after_failure.sh"
48 changes: 48 additions & 0 deletions ci-scripts/notify_after_failure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# Get the commit message from the environment variable or from git
git_commit_message=${TRAVIS_COMMIT_MESSAGE:-$(git log -1 --pretty=%B)}

# Initialize issue_number variable
issue_number=""

# Try to extract the issue number from the commit message.
issue_matches=()
if [[ $git_commit_message =~ from\ [a-zA-Z-_0-9]+/([0-9]+) ]]; then
issue_matches=("${BASH_REMATCH[@]}")
issue_number=${issue_matches[1]}
else
# Check for PR number in the format (#1234)
if [[ $git_commit_message =~ \(\#([0-9]+)\) ]]; then
pr_number=${BASH_REMATCH[1]}

# Retrieve PR information from GitHub API
pr_info=$(curl -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/repos/$TRAVIS_REPO_SLUG/pulls/$pr_number")

# Extract issue number from PR body
if [[ $pr_info =~ \#([0-9]+) ]]; then
issue_number=${BASH_REMATCH[1]}
else
echo "Could not determine the issue number from the PR description."
exit 1
fi
else
echo "Could not determine the issue or PR number from the commit message: $git_commit_message"
exit 1
fi
fi

# Check if the script should notify
if [ "$TRAVIS_BRANCH" == "main" ] && [ "$TRAVIS_EVENT_TYPE" == "push" ] && [ -z "$TRAVIS_TAG" ] && [ -n "$issue_number" ]; then
message="Could not deploy the last PR to Pantheon properly."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AronNovak Should we include the PR number in this message?

github_api_url="https://api.github.com/repos/$TRAVIS_REPO_SLUG/issues/$issue_number/comments"

exit_code=$(curl -X POST -H "Authorization: token $GITHUB_TOKEN" -d "{\"body\": \"$message\"}" "$github_api_url" -o /dev/null -w '%{http_code}')

if [ "$exit_code" -ne 200 ] && [ "$exit_code" -ne 201 ]; then
echo "Failed to post the message to GitHub. HTTP response code: $exit_code"
exit 1
fi
else
echo "Notification conditions not met or issue number not found. No action taken."
fi