Skip to content

Commit ba3e74b

Browse files
authored
Merge pull request #124 from rossjrw/test-comment
Add integration test for comment content
2 parents 8e38e8c + 9c9ad94 commit ba3e74b

7 files changed

Lines changed: 217 additions & 20 deletions

File tree

.github/workflows/preview-example.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ jobs:
1717
uses: actions/checkout@v4
1818

1919
- name: Deploy preview
20-
uses: rossjrw/pr-preview-action@v1
20+
uses: ./
2121
with:
22-
source-dir: test
22+
source-dir: test/fixtures/sample

.github/workflows/test.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,41 @@ jobs:
2222
- name: Run tests
2323
run: bash test/run-unit-tests.sh
2424

25+
test-comment-content:
26+
name: Comment content
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v4
31+
32+
- name: Test deploy comment
33+
env:
34+
GITHUB_ACTION_PATH: ${{ github.workspace }}
35+
umbrella_path: pr-preview
36+
pages_base_url: ""
37+
pages_base_path: ""
38+
pr_number: "12345"
39+
github_action_ref: ${{ github.ref }}
40+
github_action_repository: ${{ github.repository }}
41+
deployment_repository: test-owner/test-repo
42+
token: fake-token
43+
deprecated_custom_url: ""
44+
run: bash test/integration/test-comment-content.sh deploy
45+
46+
- name: Test remove comment
47+
env:
48+
GITHUB_ACTION_PATH: ${{ github.workspace }}
49+
umbrella_path: pr-preview
50+
pages_base_url: ""
51+
pages_base_path: ""
52+
pr_number: "12345"
53+
github_action_ref: ${{ github.ref }}
54+
github_action_repository: ${{ github.repository }}
55+
deployment_repository: test-owner/test-repo
56+
token: fake-token
57+
deprecated_custom_url: ""
58+
run: bash test/integration/test-comment-content.sh remove
59+
2560
# Git-level tests verify repository state without waiting for Pages deployment
2661
test-git-lifecycle:
2762
name: "[Git] Lifecycle"

action.yml

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,39 @@ runs:
177177
wait_for_pages_deployment "${{ inputs.deploy-repository }}" "${{ steps.deployed-commit.outputs.deployed_commit_sha }}" "${{ inputs.preview-branch }}" "${{ inputs.token }}"
178178
shell: bash
179179

180-
- name: Leave a comment after deployment
180+
- name: Generate comment content for deployment
181+
id: deploy-comment
181182
if: |
182183
env.deployment_action == 'deploy' &&
183184
(inputs.comment == 'true' || inputs.comment == true) &&
184185
(env.deployment_status == 'success' || env.deployment_status == 'skipped')
186+
run: |
187+
CONTENT=$($GITHUB_ACTION_PATH/lib/generate-comment.sh \
188+
"${{ env.action_repository }}" \
189+
"${{ env.action_version }}" \
190+
"${{ env.preview_url }}" \
191+
"${{ inputs.preview-branch }}" \
192+
"${{ github.server_url }}" \
193+
"${{ inputs.deploy-repository }}" \
194+
"${{ env.action_start_time }}" \
195+
"deploy")
196+
{
197+
echo "content<<EOF"
198+
echo "$CONTENT"
199+
echo "EOF"
200+
} >> $GITHUB_OUTPUT
201+
shell: bash
202+
203+
- name: Leave a comment after deployment
204+
if: |
205+
env.deployment_action == 'deploy' &&
206+
(inputs.comment == 'true' || inputs.comment == true) &&
207+
(env.deployment_status == 'success' || env.deployment_status == 'skipped') &&
208+
steps.deploy-comment.outcome == 'success'
185209
uses: marocchino/sticky-pull-request-comment@67d0dec7b07ed060a405f9b2a64b8ab319fdd7db # v2.9.2
186210
with:
187211
header: pr-preview
188-
message: |
189-
[PR Preview Action](https://github.com/${{ env.action_repository }}) ${{ env.action_version }}
190-
:---:
191-
| <p></p> :rocket: View preview at <br> ${{ env.preview_url }} <br><br>
192-
| <h6>Built to branch [`${{ inputs.preview-branch }}`](${{ github.server_url }}/${{ inputs.deploy-repository }}/tree/${{ inputs.preview-branch }}) at ${{ env.action_start_time }}. <br> Preview will be ready when the [GitHub Pages deployment](${{ github.server_url }}/${{ inputs.deploy-repository }}/deployments) is complete. <br><br> </h6>
212+
message: ${{ steps.deploy-comment.outputs.content }}
193213

194214
# REMOVAL
195215

@@ -227,16 +247,36 @@ runs:
227247
wait_for_pages_deployment "${{ inputs.deploy-repository }}" "${{ steps.removed-commit.outputs.deployed_commit_sha }}" "${{ inputs.preview-branch }}" "${{ inputs.token }}"
228248
shell: bash
229249

230-
- name: Leave a comment after removal
250+
- name: Generate comment content for removal
251+
id: remove-comment
231252
if: |
232253
env.deployment_action == 'remove' &&
233254
(inputs.comment == 'true' || inputs.comment == true) &&
234255
(env.deployment_status == 'success' || env.deployment_status == 'skipped')
256+
run: |
257+
CONTENT=$($GITHUB_ACTION_PATH/lib/generate-comment.sh \
258+
"${{ env.action_repository }}" \
259+
"${{ env.action_version }}" \
260+
"${{ env.preview_url }}" \
261+
"${{ inputs.preview-branch }}" \
262+
"${{ github.server_url }}" \
263+
"${{ inputs.deploy-repository }}" \
264+
"${{ env.action_start_time }}" \
265+
"remove")
266+
{
267+
echo "content<<EOF"
268+
echo "$CONTENT"
269+
echo "EOF"
270+
} >> $GITHUB_OUTPUT
271+
shell: bash
272+
273+
- name: Leave a comment after removal
274+
if: |
275+
env.deployment_action == 'remove' &&
276+
(inputs.comment == 'true' || inputs.comment == true) &&
277+
(env.deployment_status == 'success' || env.deployment_status == 'skipped') &&
278+
steps.remove-comment.outcome == 'success'
235279
uses: marocchino/sticky-pull-request-comment@67d0dec7b07ed060a405f9b2a64b8ab319fdd7db # v2.9.2
236280
with:
237281
header: pr-preview
238-
message: |
239-
[PR Preview Action](https://github.com/${{ env.action_repository }}) ${{ env.action_version }}
240-
:---:
241-
Preview removed because the pull request was closed.
242-
${{ env.action_start_time }}
282+
message: ${{ steps.remove-comment.outputs.content }}

lib/find-current-git-tag.sh

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env bash
22

3+
set -euo pipefail
4+
35
help() {
46
echo ""
57
echo "Usage: $0 -p github_repository -f git_ref"
@@ -21,12 +23,26 @@ if [ -z "$github_repository" ] || [ -z "$git_ref" ]; then
2123
help
2224
fi
2325

24-
echo >&2 "Determining Git tag for $github_repository/$git_ref"
25-
26-
echo >&2 "Cloning repository $github_repository at ref $git_ref"
27-
git clone --bare "https://github.com/$github_repository" bare_pr_preview
28-
29-
cd bare_pr_preview || exit 1
26+
echo >&2 "Determining Git tag for $github_repository@$git_ref"
27+
28+
case $git_ref in
29+
refs/pull/*)
30+
# If the ref is a github virtual pull request merge, this is not a valid git ref
31+
# Although this script is meant to be portable, action_ref being a PR merge can only happen for PRs to the action repo - in which case it is already checked out
32+
echo >&2 "Assuming that's *this* repository..."
33+
if eval "$(git rev-parse --is-shallow-repository)"; then
34+
git fetch --prune --unshallow
35+
fi
36+
git_ref=HEAD
37+
;;
38+
*)
39+
echo >&2 "Cloning repository $github_repository"
40+
clone_dir="$(mktemp -d)"
41+
trap 'rm -rf "$clone_dir"' EXIT
42+
git clone --bare "https://github.com/$github_repository" "$clone_dir"
43+
cd "$clone_dir"
44+
;;
45+
esac
3046

3147
action_version=$(git describe --tags --match "v*.*.*" "$git_ref" || git describe --tags "$git_ref" || git rev-parse HEAD)
3248

lib/generate-comment.sh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
# Generate comment content for PR preview deployment
5+
# Usage: generate-comment.sh <action_repository> <action_version> <preview_url> <preview_branch> <server_url> <deploy_repository> <action_start_time> <deployment_action>
6+
7+
action_repository=$1
8+
action_version=$2
9+
preview_url=$3
10+
preview_branch=$4
11+
server_url=$5
12+
deploy_repository=$6
13+
action_start_time=$7
14+
deployment_action=$8
15+
16+
if [ "$deployment_action" = "deploy" ]; then
17+
cat << EOF
18+
[PR Preview Action](https://github.com/${action_repository}) ${action_version}
19+
:---:
20+
| <p></p> :rocket: View preview at <br> ${preview_url} <br><br>
21+
| <h6>Built to branch [\`${preview_branch}\`](${server_url}/${deploy_repository}/tree/${preview_branch}) at ${action_start_time}. <br> Preview will be ready when the [GitHub Pages deployment](${server_url}/${deploy_repository}/deployments) is complete. <br><br> </h6>
22+
EOF
23+
24+
elif [ "$deployment_action" = "remove" ]; then
25+
cat << EOF
26+
[PR Preview Action](https://github.com/${action_repository}) ${action_version}
27+
:---:
28+
Preview removed because the pull request was closed.
29+
${action_start_time}
30+
EOF
31+
fi
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
source "$(dirname "$0")/../lib/assert.sh"
6+
7+
# Test comment generation by running setup and generating comments
8+
# This simulates what the action does without actually deploying anything
9+
10+
test_deployment_action="${1:-deploy}"
11+
12+
echo "Testing comment generation for action: $test_deployment_action"
13+
14+
# Set up environment for main.sh - all must be provided
15+
export GITHUB_ACTION_PATH="${GITHUB_ACTION_PATH:?missing GITHUB_ACTION_PATH}"
16+
export deployment_action="$test_deployment_action"
17+
export umbrella_path="${umbrella_path:?missing umbrella_path}"
18+
export pages_base_url="${pages_base_url-}"
19+
export pages_base_path="${pages_base_path-}"
20+
export pr_number="${pr_number:?missing pr_number}"
21+
export github_action_ref="${github_action_ref:?missing github_action_ref}"
22+
export github_action_repository="${github_action_repository:?missing github_action_repository}"
23+
export deployment_repository="${deployment_repository:?missing deployment_repository}"
24+
export token="${token:?missing token}"
25+
export deprecated_custom_url="${deprecated_custom_url-}"
26+
27+
# Create temporary files for GitHub Actions environment if not in GHA
28+
if [ -z "$GITHUB_ENV" ]; then
29+
export GITHUB_ENV=$(mktemp)
30+
trap "rm -f $GITHUB_ENV" EXIT
31+
fi
32+
if [ -z "$GITHUB_OUTPUT" ]; then
33+
export GITHUB_OUTPUT=$(mktemp)
34+
trap "rm -f $GITHUB_OUTPUT" EXIT
35+
fi
36+
37+
# Run the setup script to generate environment variables
38+
source lib/main.sh
39+
40+
# Read environment variables from GITHUB_ENV (handle values with spaces)
41+
while IFS='=' read -r key value; do
42+
[ -n "$key" ] && export "$key=$value"
43+
done < "$GITHUB_ENV"
44+
45+
# Generate the comment using the same script the action uses
46+
comment_file="comment-generated-${test_deployment_action}.md"
47+
bash lib/generate-comment.sh \
48+
"$action_repository" \
49+
"$action_version" \
50+
"$preview_url" \
51+
"gh-pages" \
52+
"https://github.com" \
53+
"$deployment_repository" \
54+
"$action_start_time" \
55+
"$test_deployment_action" \
56+
> "$comment_file"
57+
58+
echo "=== Generated $test_deployment_action comment content ==="
59+
cat "$comment_file"
60+
echo "========================================"
61+
62+
# Verify the comment contains expected content
63+
if [ "$test_deployment_action" = "deploy" ]; then
64+
assert_file_contains "$comment_file" "PR Preview Action"
65+
assert_file_contains "$comment_file" "$action_version"
66+
assert_file_contains "$comment_file" "$preview_url"
67+
assert_file_contains "$comment_file" "Built to branch"
68+
assert_file_contains "$comment_file" "pr-$pr_number"
69+
echo "✓ Deploy comment content verified"
70+
elif [ "$test_deployment_action" = "remove" ]; then
71+
assert_file_contains "$comment_file" "PR Preview Action"
72+
assert_file_contains "$comment_file" "$action_version"
73+
assert_file_contains "$comment_file" "Preview removed"
74+
echo "✓ Removal comment content verified"
75+
fi

0 commit comments

Comments
 (0)