Skip to content

Instantly share code, notes, and snippets.

@suzuki-shunsuke
Created August 13, 2020 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suzuki-shunsuke/11b74e51a4f7826eec20b744119ba1ff to your computer and use it in GitHub Desktop.
Save suzuki-shunsuke/11b74e51a4f7826eec20b744119ba1ff to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -eu
if [ -z "${CODEBUILD_WEBHOOK_EVENT:-}" ]; then
repo=$(echo "${CODEBUILD_SOURCE_REPO_URL}" | sed -E "s|https://github\.com/(.*)$|\1|" | sed -E "s|\.git$||")
# https://docs.github.com/en/rest/reference/repos#list-pull-requests-associated-with-a-commit
body=$(curl \
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.groot-preview+json" \
"https://api.github.com/repos/$repo/commits/$CODEBUILD_SOURCE_VERSION/pulls")
if [ "$(echo "$body" | jq ". | length")" -eq 0 ]; then
echo "no pull requests"
exit 0
else
pr_number=$(echo "$body" | jq ".[0].number")
base=$(echo "$body" | jq -r ".[0].base.ref")
head=$(echo "$body" | jq -r ".[0].head.ref")
echo "$pr_number"
echo "$base"
echo "$head"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment