From 7caed858b0fc994d8472da9ab41a9b916ef57538 Mon Sep 17 00:00:00 2001 From: Aron Novak Date: Fri, 24 Nov 2023 09:57:30 +0100 Subject: [PATCH 1/6] notify after failure --- .travis.yml | 2 ++ ci-scripts/notify_after_failure.sh | 49 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 ci-scripts/notify_after_failure.sh diff --git a/.travis.yml b/.travis.yml index 282de2c62..31c502012 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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" diff --git a/ci-scripts/notify_after_failure.sh b/ci-scripts/notify_after_failure.sh new file mode 100755 index 000000000..2c8bad108 --- /dev/null +++ b/ci-scripts/notify_after_failure.sh @@ -0,0 +1,49 @@ +#!/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/Giza/WMO/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." + + github_api_url="https://api.github.com/repos/Giza/WMO/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 ]; 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 From 1aa57dd46f1af0a3be708eec58a6f89dc37ff881 Mon Sep 17 00:00:00 2001 From: Aron Novak Date: Fri, 24 Nov 2023 09:57:53 +0100 Subject: [PATCH 2/6] add to the template too --- .travis.template.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.template.yml b/.travis.template.yml index 232bb9585..19f01e6ae 100644 --- a/.travis.template.yml +++ b/.travis.template.yml @@ -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" From 1161730d834bd6d8e16410969a2c5bf77bb72fdf Mon Sep 17 00:00:00 2001 From: Aron Novak Date: Fri, 24 Nov 2023 08:59:11 +0000 Subject: [PATCH 3/6] Apply suggestions from code review --- ci-scripts/notify_after_failure.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci-scripts/notify_after_failure.sh b/ci-scripts/notify_after_failure.sh index 2c8bad108..adb1afcf5 100755 --- a/ci-scripts/notify_after_failure.sh +++ b/ci-scripts/notify_after_failure.sh @@ -36,7 +36,7 @@ fi 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." - github_api_url="https://api.github.com/repos/Giza/WMO/issues/$issue_number/comments" + github_api_url="https://api.github.com/repos/Giza/drupal-starter/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}') From 68157d24af706fec859af59882878de35bcaff32 Mon Sep 17 00:00:00 2001 From: Aron Novak Date: Fri, 24 Nov 2023 08:59:33 +0000 Subject: [PATCH 4/6] Update ci-scripts/notify_after_failure.sh --- ci-scripts/notify_after_failure.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci-scripts/notify_after_failure.sh b/ci-scripts/notify_after_failure.sh index adb1afcf5..f87e6e75c 100755 --- a/ci-scripts/notify_after_failure.sh +++ b/ci-scripts/notify_after_failure.sh @@ -17,7 +17,7 @@ else pr_number=${BASH_REMATCH[1]} # Retrieve PR information from GitHub API - pr_info=$(curl -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/repos/Giza/WMO/pulls/$pr_number") + pr_info=$(curl -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/repos/Giza/drupal-starter/pulls/$pr_number") # Extract issue number from PR body if [[ $pr_info =~ \#([0-9]+) ]]; then From 2cf79d3b6aea5603958f7a6b5a2156c3ebecad45 Mon Sep 17 00:00:00 2001 From: Aron Novak Date: Fri, 24 Nov 2023 10:34:51 +0100 Subject: [PATCH 5/6] use repo slug variable --- ci-scripts/notify_after_failure.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ci-scripts/notify_after_failure.sh b/ci-scripts/notify_after_failure.sh index 2c8bad108..f45a1ffb0 100755 --- a/ci-scripts/notify_after_failure.sh +++ b/ci-scripts/notify_after_failure.sh @@ -17,7 +17,7 @@ else pr_number=${BASH_REMATCH[1]} # Retrieve PR information from GitHub API - pr_info=$(curl -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/repos/Giza/WMO/pulls/$pr_number") + 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 @@ -35,12 +35,12 @@ 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." - - github_api_url="https://api.github.com/repos/Giza/WMO/issues/$issue_number/comments" +i + 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 ]; then + 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 From dcfb0a281ed2e8b3e4a41d3ef05023b8527fc4ec Mon Sep 17 00:00:00 2001 From: Aron Novak Date: Fri, 1 Dec 2023 21:43:40 +0100 Subject: [PATCH 6/6] address pr review comment --- ci-scripts/notify_after_failure.sh | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/ci-scripts/notify_after_failure.sh b/ci-scripts/notify_after_failure.sh index fc943cb6f..5de72aa31 100755 --- a/ci-scripts/notify_after_failure.sh +++ b/ci-scripts/notify_after_failure.sh @@ -3,8 +3,9 @@ # 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 +# Initialize variables issue_number="" +pr_number="" # Try to extract the issue number from the commit message. issue_matches=() @@ -34,7 +35,13 @@ 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." + # Determine the message based on whether PR number is available + if [ -n "$pr_number" ]; then + message="Could not deploy PR #$pr_number to Pantheon properly." + else + message="Could not deploy the last PR / commit to Pantheon properly." + fi + 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}')