-
Notifications
You must be signed in to change notification settings - Fork 6
106 lines (90 loc) · 4.57 KB
/
commit-preview.yml
File metadata and controls
106 lines (90 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
name: Commit Preview
on:
workflow_run:
workflows: ["PR Preview"]
types:
- completed
jobs:
commit:
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success'
permissions:
contents: write
pull-requests: write
steps:
- name: Download preview artifact
uses: dawidd6/action-download-artifact@v7
with:
run_id: ${{ github.event.workflow_run.id }}
name: preview-image
path: .
- name: Download PR info artifact
uses: dawidd6/action-download-artifact@v7
with:
run_id: ${{ github.event.workflow_run.id }}
name: pr-info
path: .
- name: Read PR info
id: pr-info
run: |
echo "pr_number=$(cat pr_number.txt)" >> $GITHUB_OUTPUT
echo "commit_sha=$(cat commit_sha.txt)" >> $GITHUB_OUTPUT
echo "new_themes=$(cat new_themes.txt)" >> $GITHUB_OUTPUT
echo "theme=$(cat theme.txt)" >> $GITHUB_OUTPUT
- name: Setup preview branch
run: |
# Configure git
git config --global user.name 'github-actions[bot]'
git config --global user.email 'github-actions[bot]@users.noreply.github.com'
# Create a temporary directory for git operations
mkdir -p temp_git
cd temp_git
REPO_URL="https://github.com/${{ github.repository }}.git"
# Clone only the preview branch, or create new if doesn't exist
if git ls-remote --heads $REPO_URL previews | grep -q 'refs/heads/previews'; then
git clone --branch previews --single-branch $REPO_URL .
else
git clone $REPO_URL .
git checkout --orphan previews
git rm -rf .
git clean -fxd
fi
# Setup the preview branch
mkdir -p previews
# Copy the new preview with PR number and commit SHA from parent directory
cp ../preview.png "previews/pr-${{ steps.pr-info.outputs.pr_number }}-${{ steps.pr-info.outputs.commit_sha }}.png"
# Commit and push
git add previews/
git commit -m "Update preview for PR #${{ steps.pr-info.outputs.pr_number }} commit ${{ steps.pr-info.outputs.commit_sha }}" || echo "No changes to commit"
git push https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}.git previews
- name: Find Comment
uses: peter-evans/find-comment@v3
id: find-comment
with:
issue-number: ${{ steps.pr-info.outputs.pr_number }}
comment-author: "github-actions[bot]"
body-includes: "### GitRoll Preview Cards"
- name: Create comment body
id: create-comment
run: |
IMAGE_URL="https://raw.githubusercontent.com/${{ github.repository }}/previews/previews/pr-${{ steps.pr-info.outputs.pr_number }}-${{ steps.pr-info.outputs.commit_sha }}.png"
echo "body<<EOF" >> $GITHUB_OUTPUT
echo "### GitRoll Preview Cards" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
if [[ -n "${{ steps.pr-info.outputs.new_themes }}" ]]; then
echo "New theme(s) detected: \`${{ steps.pr-info.outputs.new_themes }}\`" >> $GITHUB_OUTPUT
else
echo "No new theme detected, using: \`${{ steps.pr-info.outputs.theme }}\`" >> $GITHUB_OUTPUT
fi
echo "" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "" >> $GITHUB_OUTPUT
echo "These are preview cards showing possible ratings. Get your real score at [GitRoll.io](https://gitroll.io)" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create or update comment
uses: peter-evans/create-or-update-comment@v4
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ steps.pr-info.outputs.pr_number }}
body: ${{ steps.create-comment.outputs.body }}
edit-mode: replace