Repository navigation
feat: preview picked pull requests and email the author from a setup PR's run #152
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
74c005d
feat: preview picked pull requests and email the author from a setup …
ivanmilevtues cf89b25
feat: name each preview artifact for its pull request and head
ivanmilevtues 9f6cc29
style: format setup preview tests with Black
ivanmilevtues 3bdbdca
fix: satisfy shellcheck in setup preview scripts
ivanmilevtues File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| #!/usr/bin/env bash | ||
| # Tells the author of a CodeBoarding setup PR that its preview map is ready. | ||
| # | ||
| # The review comment is sticky: it is created when the run starts and edited when it | ||
| # ends, and GitHub sends no notification for an edit. A new comment does notify the | ||
| # PR's author, who for a setup PR is the person who asked for the map (the app opens | ||
| # it with their token), so this one comment is what turns "you can close this tab" | ||
| # into an email from GitHub. Failed runs need nothing here: GitHub already emails the | ||
| # person who triggered a failed run. | ||
| # | ||
| # When the setup PR asked for previews of other pull requests, this runs after the first | ||
| # one is ready (FIRST_PR) and links it, since a coloured pull request is the point; the | ||
| # rest of PREVIEW_PRS are named as still running. Posted once per PR, found again by its | ||
| # marker, so pushing new settings to the setup PR does not email again. Best effort throughout. | ||
| set -euo pipefail | ||
| [ "${HEAD_REF:-}" = "codeboarding/setup-action" ] || exit 0 | ||
| [ -n "${PR_NUMBER:-}" ] && [ -n "${REPOSITORY:-}" ] || exit 0 | ||
| export GH_HOST="${GH_HOST:-github.com}" | ||
| GH_HOST="${GH_HOST#*://}" | ||
|
|
||
| marker="<!-- codeboarding-setup-ready -->" | ||
| existing="$(gh api --paginate "repos/$REPOSITORY/issues/$PR_NUMBER/comments?per_page=100" \ | ||
| --jq ".[] | select((.body // \"\") | contains(\"$marker\")) | .id")" | ||
| [ -z "$existing" ] || exit 0 | ||
|
|
||
| # A mention notifies even an author who unsubscribed from the thread. Bots are not mentioned. | ||
| greeting="Your" | ||
| case "${PR_AUTHOR:-}" in | ||
| "" | *"[bot]") ;; | ||
| *) greeting="@$PR_AUTHOR your" ;; | ||
| esac | ||
| if [ -n "${BASE_REF:-}" ]; then | ||
| branch="\`$BASE_REF\`" | ||
| else | ||
| branch="your default branch" | ||
| fi | ||
| link() { printf 'https://app.codeboarding.org/%s/pull/%s?utm_source=github&utm_medium=setup_ready&utm_campaign=gh_action' "$REPOSITORY" "$1"; } | ||
| if [ -n "${FIRST_PR:-}" ]; then | ||
| headline="$greeting CodeBoarding map of $branch is ready, and #$FIRST_PR is reviewed on it." | ||
| cta="**[Open #$FIRST_PR in CodeBoarding]($(link "$FIRST_PR"))**${FIRST_TITLE:+ · $FIRST_TITLE}" | ||
| rest="" | ||
| IFS=',' read -r -a all <<< "${PREVIEW_PRS:-}" | ||
| for pr in "${all[@]}"; do | ||
| if [ -n "$pr" ] && [ "$pr" != "$FIRST_PR" ]; then rest+="${rest:+, }#$pr"; fi | ||
| done | ||
| [ -z "$rest" ] || cta+=$'\n\n'"$rest follow in a few minutes. [Your map]($(link "$PR_NUMBER")) is open already." | ||
| else | ||
| headline="$greeting CodeBoarding map of $branch is ready." | ||
| cta="**[Open it in CodeBoarding]($(link "$PR_NUMBER"))**" | ||
| fi | ||
| body="$(printf '%s\n\n%s\n\n%s\n\n%s' \ | ||
| "$headline" \ | ||
| "$cta" \ | ||
| "This is a preview. Merge this PR to keep the map up to date and review every pull request." \ | ||
| "$marker")" | ||
| gh api -X POST "repos/$REPOSITORY/issues/$PR_NUMBER/comments" -f body="$body" >/dev/null |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| #!/usr/bin/env bash | ||
| # Reads which pull requests a CodeBoarding setup PR should preview, from a line the app | ||
| # writes into its description: | ||
| # | ||
| # <!-- codeboarding:preview 412,409,401 --> | ||
| # | ||
| # Before the setup PR merges, its own run is the only one GitHub starts for us, so this is | ||
| # where a new user's open pull requests get their first review. The description is read | ||
| # now, after the map is built, not from the event payload, so a list changed while the map | ||
| # was building still counts. Each entry must be an open pull request from this repository | ||
| # into the same branch; anything else is skipped with a notice. At most MAX_PREVIEWS. | ||
| # | ||
| # Writes one JSON file per preview to $RUNNER_TEMP/codeboarding-preview/<n>.json and | ||
| # outputs count= and prs= (comma-separated, in order). Best effort throughout. | ||
| set -euo pipefail | ||
| out="${RUNNER_TEMP:?}/codeboarding-preview" | ||
| rm -rf "$out" | ||
| mkdir -p "$out" | ||
| count=0 prs="" | ||
| finish() { printf 'count=%s\nprs=%s\n' "$count" "$prs" >> "${GITHUB_OUTPUT:-/dev/null}"; exit 0; } | ||
| [ "${HEAD_REF:-}" = "codeboarding/setup-action" ] || finish | ||
| [ -n "${PR_NUMBER:-}" ] && [ -n "${REPOSITORY:-}" ] && [ -n "${BASE_REF:-}" ] || finish | ||
| export GH_HOST="${GH_HOST:-github.com}" | ||
| GH_HOST="${GH_HOST#*://}" | ||
| max="${MAX_PREVIEWS:-3}" | ||
|
|
||
| body="$(gh api "repos/$REPOSITORY/pulls/$PR_NUMBER" --jq '.body // ""' 2>/dev/null || true)" | ||
| line="$(grep -oE '<!-- codeboarding:preview [0-9, ]+ -->' <<< "$body" | head -n 1 || true)" | ||
| [ -n "$line" ] || finish | ||
|
|
||
| skip() { echo "::notice::Not previewing #$1: $2"; } | ||
| seen=" " | ||
| while IFS= read -r pr; do | ||
| [ "$count" -lt "$max" ] || { skip "$pr" "at most $max pull requests are previewed."; continue; } | ||
| [ "$pr" != "$PR_NUMBER" ] || continue | ||
| case "$seen" in *" $pr "*) continue ;; esac | ||
| seen+="$pr " | ||
| if ! json="$(gh api "repos/$REPOSITORY/pulls/$pr" 2>/dev/null)"; then | ||
| skip "$pr" "it could not be read." | ||
| continue | ||
| fi | ||
| IFS=$'\t' read -r state head_repo base_ref base_sha head_sha < <(jq -r \ | ||
| '[.state, (.head.repo.full_name // ""), .base.ref, .base.sha, .head.sha] | @tsv' <<< "$json") | ||
| [ "$state" = open ] || { skip "$pr" "it is not open."; continue; } | ||
| [ "$head_repo" = "$REPOSITORY" ] || { skip "$pr" "it comes from a fork."; continue; } | ||
| [ "$base_ref" = "$BASE_REF" ] || { skip "$pr" "it targets $base_ref, not $BASE_REF."; continue; } | ||
| # The pull request's own changes are what it adds since its merge base, the same | ||
| # comparison GitHub's Files tab makes. | ||
| merge_base="$(gh api "repos/$REPOSITORY/compare/$base_sha...$head_sha" --jq '.merge_base_commit.sha // ""' 2>/dev/null || true)" | ||
| [ -n "$merge_base" ] || { skip "$pr" "its merge base could not be resolved."; continue; } | ||
| count=$(( count + 1 )) | ||
| jq -n --arg pr "$pr" --arg head_sha "$head_sha" --arg merge_base "$merge_base" \ | ||
| --arg title "$(jq -r '.title // ""' <<< "$json")" \ | ||
| '{pr_number: $pr, head_sha: $head_sha, merge_base_sha: $merge_base, title: $title}' > "$out/$count.json" | ||
| prs="${prs:+$prs,}$pr" | ||
| done < <(grep -oE '[0-9]+' <<< "${line#*preview}") | ||
| finish |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.