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

[#1065] Fixed failing GitHub notifications. #1361

Merged
merged 1 commit into from
Aug 17, 2024
Merged
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
3 changes: 2 additions & 1 deletion .lagoon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ tasks:
DREVOPS_NOTIFY_EVENT=post_deployment \
DREVOPS_NOTIFY_PROJECT=$LAGOON_PROJECT \
DREVOPS_NOTIFY_ENVIRONMENT_URL=$LAGOON_ROUTE \
./scripts/drevops/notify.sh
./scripts/drevops/notify.sh || true
Copy link
Member Author

Choose a reason for hiding this comment

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

do not fail the deployment if the notifications fail

service: cli
shell: bash
#;> !NOTIFICATIONS

environments:
# Branch name that represents production environment.
main:
Expand Down
5 changes: 5 additions & 0 deletions .scaffold/.ahoy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ commands:
ahoy test-common
ahoy test-docs
test-bats:
cmd: |
[ ! -d tests/node_modules ] && ahoy install
tests/node_modules/.bin/bats "$@"
test-common:
cmd: ./tests/test.common.sh

Expand Down
8 changes: 4 additions & 4 deletions .scaffold/docs/content/workflows/variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -931,11 +931,11 @@ Defined in: `scripts/drevops/mirror-code.sh`

### `DREVOPS_NOTIFY_BRANCH`

Deployment reference, such as a git SHA.
Deployment reference branch.

Default value: `UNDEFINED`

Defined in: `scripts/drevops/notify-jira.sh`
Defined in: `scripts/drevops/notify-github.sh`, `scripts/drevops/notify-jira.sh`

### `DREVOPS_NOTIFY_CHANNELS`

Expand Down Expand Up @@ -1167,11 +1167,11 @@ Defined in: `scripts/drevops/notify-webhook.sh`, `scripts/drevops/notify.sh`

### `DREVOPS_NOTIFY_REF`

Deployment reference, such as a git SHA.
Git reference to notify about.

Default value: `UNDEFINED`

Defined in: `scripts/drevops/notify-github.sh`, `scripts/drevops/notify-webhook.sh`
Defined in: `scripts/drevops/notify-webhook.sh`

### `DREVOPS_NOTIFY_REPOSITORY`

Expand Down
160 changes: 153 additions & 7 deletions .scaffold/tests/bats/notify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ load _helper.bash
declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for pre_deployment event."
"@curl -X POST -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments -d {\"ref\":\"mybranch\", \"environment\": \"PR\", \"auto_merge\": false} # {\"id\": \"${app_id}\", \"othervar\": \"54321\"}"
"@curl -X POST -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments -d {\"ref\":\"existingbranch\", \"environment\": \"PR\", \"auto_merge\": false} # {\"id\": \"${app_id}\", \"othervar\": \"54321\"}"
Copy link
Member Author

Choose a reason for hiding this comment

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

replacing to use existingbranch and nonexistingbranch

"Marked deployment as started."
"Finished GitHub notification for pre_deployment event."
"Finished dispatching notifications."
Expand All @@ -130,7 +130,7 @@ load _helper.bash
export DREVOPS_NOTIFY_EVENT="pre_deployment"
export DREVOPS_NOTIFY_GITHUB_TOKEN="token12345"
export DREVOPS_NOTIFY_REPOSITORY="myorg/myrepo"
export DREVOPS_NOTIFY_REF="mybranch"
export DREVOPS_NOTIFY_BRANCH="existingbranch"
run ./scripts/drevops/notify.sh
assert_success

Expand All @@ -139,6 +139,62 @@ load _helper.bash
popd >/dev/null || exit 1
}

@test "Notify: github, pre_deployment, longer ID" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

app_id="12345678987"

declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for pre_deployment event."
"@curl -X POST -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments -d {\"ref\":\"existingbranch\", \"environment\": \"PR\", \"auto_merge\": false} # {\"id\": \"${app_id}\", \"othervar\": \"54321\"}"
"Marked deployment as started."
"Finished GitHub notification for pre_deployment event."
"Finished dispatching notifications."
)

mocks="$(run_steps "setup")"

export DREVOPS_NOTIFY_CHANNELS="github"
export DREVOPS_NOTIFY_EVENT="pre_deployment"
export DREVOPS_NOTIFY_GITHUB_TOKEN="token12345"
export DREVOPS_NOTIFY_REPOSITORY="myorg/myrepo"
export DREVOPS_NOTIFY_BRANCH="existingbranch"
run ./scripts/drevops/notify.sh
assert_success

run_steps "assert" "${mocks[@]}"

popd >/dev/null || exit 1
}

@test "Notify: github, pre_deployment, failure" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for pre_deployment event."
"@curl -X POST -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments -d {\"ref\":\"nonexistingbranch\", \"environment\": \"PR\", \"auto_merge\": false} # {\"message\": \"No ref found for: nonexistingbranch\",\"documentation_url\": \"https://docs.github.com/rest/deployments/deployments#create-a-deployment\",\"status\": \"422\"}"
"Failed to get a deployment ID for a pre_deployment operation. Payload:"
"Wait for GitHub checks to finish and try again."
"-Marked deployment as finished."
)

mocks="$(run_steps "setup")"

export DREVOPS_NOTIFY_CHANNELS="github"
export DREVOPS_NOTIFY_EVENT="pre_deployment"
export DREVOPS_NOTIFY_GITHUB_TOKEN="token12345"
export DREVOPS_NOTIFY_REPOSITORY="myorg/myrepo"
export DREVOPS_NOTIFY_BRANCH="nonexistingbranch"
run ./scripts/drevops/notify.sh
assert_failure

run_steps "assert" "${mocks[@]}"

popd >/dev/null || exit 1
}

@test "Notify: github, post_deployment" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

Expand All @@ -148,8 +204,39 @@ load _helper.bash
declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for post_deployment event."
"@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=mybranch # [{\"id\": \"${app_id}\", \"othervar\": \"54321\"},{\"id\": \"98765432101\", \"othervar\": \"12345\"}]"
"@curl -X POST -H Accept: application/vnd.github.v3+json -H Authorization: token token12345 https://api.github.com/repos/myorg/myrepo/deployments/123456789/statuses -s -d {\"state\":\"success\", \"environment_url\": \"https://develop.testproject.com\"} # {\"state\": \"success\", \"othervar\": \"54321\"}"
"@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=existingbranch # [{\"id\": \"${app_id}\", \"othervar\": \"54321\"},{\"id\": \"98765432101\", \"othervar\": \"12345\"}]"
"@curl -X POST -H Accept: application/vnd.github.v3+json -H Authorization: token token12345 https://api.github.com/repos/myorg/myrepo/deployments/${app_id}/statuses -s -d {\"state\":\"success\", \"environment_url\": \"https://develop.testproject.com\"} # {\"state\": \"success\", \"othervar\": \"54321\"}"
"Marked deployment as finished."
"Finished GitHub notification for post_deployment event."
"Finished dispatching notifications."
)
mocks="$(run_steps "setup")"

export DREVOPS_NOTIFY_CHANNELS="github"
export DREVOPS_NOTIFY_EVENT="post_deployment"
export DREVOPS_NOTIFY_GITHUB_TOKEN="token12345"
export DREVOPS_NOTIFY_REPOSITORY="myorg/myrepo"
export DREVOPS_NOTIFY_BRANCH="existingbranch"
export DREVOPS_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com"
run ./scripts/drevops/notify.sh
assert_success

run_steps "assert" "${mocks[@]}"

popd >/dev/null || exit 1
}

@test "Notify: github, post_deployment, longer ID" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

app_id="12345678987"
mock_curl=$(mock_command "curl")

declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for post_deployment event."
"@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=existingbranch # [{\"id\": \"${app_id}\", \"othervar\": \"54321\"},{\"id\": \"98765432101\", \"othervar\": \"12345\"}]"
"@curl -X POST -H Accept: application/vnd.github.v3+json -H Authorization: token token12345 https://api.github.com/repos/myorg/myrepo/deployments/${app_id}/statuses -s -d {\"state\":\"success\", \"environment_url\": \"https://develop.testproject.com\"} # {\"state\": \"success\", \"othervar\": \"54321\"}"
"Marked deployment as finished."
"Finished GitHub notification for post_deployment event."
"Finished dispatching notifications."
Expand All @@ -160,7 +247,7 @@ load _helper.bash
export DREVOPS_NOTIFY_EVENT="post_deployment"
export DREVOPS_NOTIFY_GITHUB_TOKEN="token12345"
export DREVOPS_NOTIFY_REPOSITORY="myorg/myrepo"
export DREVOPS_NOTIFY_REF="mybranch"
export DREVOPS_NOTIFY_BRANCH="existingbranch"
export DREVOPS_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com"
run ./scripts/drevops/notify.sh
assert_success
Expand All @@ -170,6 +257,65 @@ load _helper.bash
popd >/dev/null || exit 1
}

@test "Notify: github, post_deployment, failure to get id" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

mock_curl=$(mock_command "curl")

declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for post_deployment event."
"@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=nonexistingbranch # []"
"Failed to get a deployment ID for a post_deployment operation. Payload:"
"Check that a pre_deployment notification was dispatched."
"-Marked deployment as finished."
)
mocks="$(run_steps "setup")"

export DREVOPS_NOTIFY_CHANNELS="github"
export DREVOPS_NOTIFY_EVENT="post_deployment"
export DREVOPS_NOTIFY_GITHUB_TOKEN="token12345"
export DREVOPS_NOTIFY_REPOSITORY="myorg/myrepo"
export DREVOPS_NOTIFY_BRANCH="nonexistingbranch"
export DREVOPS_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com"
run ./scripts/drevops/notify.sh
assert_failure

run_steps "assert" "${mocks[@]}"

popd >/dev/null || exit 1
}

@test "Notify: github, post_deployment, failure to set status" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

app_id="12345678987"
mock_curl=$(mock_command "curl")

declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for post_deployment event."
"@curl -X GET -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments?ref=existingbranch # [{\"id\": \"${app_id}\", \"othervar\": \"54321\"},{\"id\": \"98765432101\", \"othervar\": \"12345\"}]"
"@curl -X POST -H Accept: application/vnd.github.v3+json -H Authorization: token token12345 https://api.github.com/repos/myorg/myrepo/deployments/${app_id}/statuses -s -d {\"state\":\"success\", \"environment_url\": \"https://develop.testproject.com\"} # {\"state\": \"notsuccess\", \"othervar\": \"54321\"}"
"Previous deployment was found, but was unable to update the deployment status. Payload:"
"-Marked deployment as finished."
)
mocks="$(run_steps "setup")"

export DREVOPS_NOTIFY_CHANNELS="github"
export DREVOPS_NOTIFY_EVENT="post_deployment"
export DREVOPS_NOTIFY_GITHUB_TOKEN="token12345"
export DREVOPS_NOTIFY_REPOSITORY="myorg/myrepo"
export DREVOPS_NOTIFY_BRANCH="existingbranch"
export DREVOPS_NOTIFY_ENVIRONMENT_URL="https://develop.testproject.com"
run ./scripts/drevops/notify.sh
assert_failure

run_steps "assert" "${mocks[@]}"

popd >/dev/null || exit 1
}

@test "Notify: jira" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

Expand Down Expand Up @@ -215,7 +361,7 @@ load _helper.bash
popd >/dev/null || exit 1
}

@test "Notify: webhook success" {
@test "Notify: webhook" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

mock_curl=$(mock_command "curl")
Expand Down Expand Up @@ -245,7 +391,7 @@ load _helper.bash
popd >/dev/null || exit 1
}

@test "Notify: webhook failure" {
@test "Notify: webhook, failure" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

mock_curl=$(mock_command "curl")
Expand Down
2 changes: 1 addition & 1 deletion hooks/library/notify-deployment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ export DREVOPS_NOTIFY_REF="${ref}"
export DREVOPS_NOTIFY_SHA="${target_env}"
export DREVOPS_NOTIFY_ENVIRONMENT_URL="${url}"

./scripts/drevops/notify.sh
./scripts/drevops/notify.sh || true

popd >/dev/null || exit 1
31 changes: 21 additions & 10 deletions scripts/drevops/notify-github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
# Deployment repository.
DREVOPS_NOTIFY_REPOSITORY="${DREVOPS_NOTIFY_REPOSITORY:-}"

# Deployment reference, such as a git SHA.
DREVOPS_NOTIFY_REF="${DREVOPS_NOTIFY_REF:-}"
# Deployment reference branch.
DREVOPS_NOTIFY_BRANCH="${DREVOPS_NOTIFY_BRANCH:-}"

Check warning on line 26 in scripts/drevops/notify-github.sh

View check run for this annotation

Codecov / codecov/patch

scripts/drevops/notify-github.sh#L26

Added line #L26 was not covered by tests

# The event to notify about. Can be 'pre_deployment' or 'post_deployment'.
DREVOPS_NOTIFY_EVENT="${DREVOPS_NOTIFY_EVENT:-}"
Expand All @@ -50,7 +50,7 @@

[ -z "${DREVOPS_NOTIFY_GITHUB_TOKEN}" ] && fail "Missing required value for DREVOPS_NOTIFY_GITHUB_TOKEN" && exit 1
[ -z "${DREVOPS_NOTIFY_REPOSITORY}" ] && fail "Missing required value for DREVOPS_NOTIFY_REPOSITORY" && exit 1
[ -z "${DREVOPS_NOTIFY_REF}" ] && fail "Missing required value for DREVOPS_NOTIFY_REF" && exit 1
[ -z "${DREVOPS_NOTIFY_BRANCH}" ] && fail "Missing required value for DREVOPS_NOTIFY_BRANCH" && exit 1

Check warning on line 53 in scripts/drevops/notify-github.sh

View check run for this annotation

Codecov / codecov/patch

scripts/drevops/notify-github.sh#L53

Added line #L53 was not covered by tests
[ -z "${DREVOPS_NOTIFY_EVENT}" ] && fail "Missing required value for DREVOPS_NOTIFY_EVENT" && exit 1
[ -z "${DREVOPS_NOTIFY_ENVIRONMENT_TYPE}" ] && fail "Missing required value for DREVOPS_NOTIFY_ENVIRONMENT_TYPE" && exit 1

Expand Down Expand Up @@ -79,12 +79,16 @@
-H "Accept: application/vnd.github.v3+json" \
-s \
"https://api.github.com/repos/${DREVOPS_NOTIFY_REPOSITORY}/deployments" \
-d "{\"ref\":\"${DREVOPS_NOTIFY_REF}\", \"environment\": \"${DREVOPS_NOTIFY_ENVIRONMENT_TYPE}\", \"auto_merge\": false}")"
-d "{\"ref\":\"${DREVOPS_NOTIFY_BRANCH}\", \"environment\": \"${DREVOPS_NOTIFY_ENVIRONMENT_TYPE}\", \"auto_merge\": false}")"

deployment_id="$(echo "${payload}" | extract_json_value "id")"
deployment_id="$(echo "${payload}" | extract_json_value "id" || true)"

Check warning on line 84 in scripts/drevops/notify-github.sh

View check run for this annotation

Codecov / codecov/patch

scripts/drevops/notify-github.sh#L84

Added line #L84 was not covered by tests

# Check deployment ID.
{ [ "${#deployment_id}" -lt 9 ] || [ "${#deployment_id}" -gt 11 ] || [ "$(expr "x${deployment_id}" : "x[0-9]*$")" -eq 0 ]; } && fail "Failed to get a deployment ID for a started operation. Payload: ${payload}" && exit 1
if [ -z "${deployment_id}" ] || [ "${#deployment_id}" -lt 9 ] || [ "${#deployment_id}" -gt 11 ] || [ "$(expr "x${deployment_id}" : "x[0-9]*$")" -eq 0 ]; then
fail "Failed to get a deployment ID for a ${DREVOPS_NOTIFY_EVENT} operation. Payload: ${payload}"
fail "Wait for GitHub checks to finish and try again."
exit 1

Check warning on line 90 in scripts/drevops/notify-github.sh

View check run for this annotation

Codecov / codecov/patch

scripts/drevops/notify-github.sh#L87-L90

Added lines #L87 - L90 were not covered by tests
fi

note "Marked deployment as started."
else
Expand All @@ -96,12 +100,16 @@
-H "Authorization: token ${DREVOPS_NOTIFY_GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-s \
"https://api.github.com/repos/${DREVOPS_NOTIFY_REPOSITORY}/deployments?ref=${DREVOPS_NOTIFY_REF}")"
"https://api.github.com/repos/${DREVOPS_NOTIFY_REPOSITORY}/deployments?ref=${DREVOPS_NOTIFY_BRANCH}")"

deployment_id="$(echo "${payload}" | extract_json_first_value "id")"
deployment_id="$(echo "${payload}" | extract_json_first_value "id" || true)"

Check warning on line 105 in scripts/drevops/notify-github.sh

View check run for this annotation

Codecov / codecov/patch

scripts/drevops/notify-github.sh#L105

Added line #L105 was not covered by tests

# Check deployment ID.
{ [ "${#deployment_id}" -lt 9 ] || [ "${#deployment_id}" -gt 11 ] || [ "$(expr "x${deployment_id}" : "x[0-9]*$")" -eq 0 ]; } && fail "Failed to get a deployment ID for a finished operation. Payload: ${payload}" && exit 1
if [ -z "${deployment_id}" ] || [ "${#deployment_id}" -lt 9 ] || [ "${#deployment_id}" -gt 11 ] || [ "$(expr "x${deployment_id}" : "x[0-9]*$")" -eq 0 ]; then
fail "Failed to get a deployment ID for a ${DREVOPS_NOTIFY_EVENT} operation. Payload: ${payload}"
fail "Check that a pre_deployment notification was dispatched."
exit 1

Check warning on line 111 in scripts/drevops/notify-github.sh

View check run for this annotation

Codecov / codecov/patch

scripts/drevops/notify-github.sh#L108-L111

Added lines #L108 - L111 were not covered by tests
fi

# Post status update.
payload="$(curl \
Expand All @@ -114,7 +122,10 @@

status="$(echo "${payload}" | extract_json_value "state")"

[ "${status}" != "success" ] && fail "Unable to set deployment status" && exit 1
if [ "${status}" != "success" ]; then
fail "Previous deployment was found, but was unable to update the deployment status. Payload: ${payload}"
exit 1

Check warning on line 127 in scripts/drevops/notify-github.sh

View check run for this annotation

Codecov / codecov/patch

scripts/drevops/notify-github.sh#L125-L127

Added lines #L125 - L127 were not covered by tests
fi

note "Marked deployment as finished."
fi
Expand Down
Loading