-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction.yml
96 lines (87 loc) · 3.16 KB
/
action.yml
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
name: 'pr preview action'
description: 'Generate and post a preview URL as a PR comment.'
branding:
icon: 'award'
color: 'green'
inputs:
previewUrl:
required: true
BOT_APP_ID:
required: true
BOT_APP_SECRET:
required: true
GITHUB_TOKEN:
required: true
REMOVE_PREFIX:
required: false
REMOVE_SUFFIX:
required: false
runs:
using: "composite"
steps:
- name: 使用 GitHub App 进行身份验证
id: auth
uses: actions/create-github-app-token@v1
with:
app-id: ${{ inputs.BOT_APP_ID }}
private-key: ${{ inputs.BOT_APP_SECRET }}
owner: ${{ github.repository_owner }}
- name: List PR files using GitHub CLI
id: url
run: |
# 使用 GitHub CLI 获取 PR 文件列表并提取文件路径
files=$(gh api \
-H "Accept: application/vnd.github+json" \
/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files \
| jq -r '.[].filename') # 使用 -r 获取原始文本输出
# 处理文件路径,拼接成完整的 URL
for file in $files; do
if [[ $file == *.md ]]; then
# 去掉路径前缀和后缀
modified_file="${file#${{ inputs.REMOVE_PREFIX }}}" # 删除路径前缀
modified_file="${modified_file%${{ inputs.REMOVE_SUFFIX }}}" # 删除路径后缀
# 拼接 URL
file_url="${{ inputs.previewUrl }}${modified_file}"
all_file_urls="${all_file_urls}\n${file_url}"
fi
done
echo "all_file_urls=${all_file_urls}" >> $GITHUB_ENV
shell: bash
env:
GH_TOKEN: ${{ inputs.GITHUB_TOKEN }}
# 获取预览链接并发送到 PR
- name: 发送整体 PR review
uses: actions/github-script@v6
with:
github-token: ${{ steps.auth.outputs.token }}
script: |
const prNumber = context.payload.pull_request.number;
const reviewBody = `🚀 预览部署完成!访问链接: ${{ inputs.previewUrl }}\n\n✨ 本 PR 修改了以下页面: ✨${{ env.all_file_urls }}`;
// 获取现有 review
const { data: reviews } = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
// 查找已有的评论 review
const existingReview = reviews.find(review =>
review.body.includes('🚀 预览部署完成!'));
if (existingReview) {
// 如果已经有 review,更新它
await github.rest.pulls.updateReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
review_id: existingReview.id,
body: reviewBody,
});
} else {
// 如果没有 review,创建新的 review
await github.rest.pulls.createReview({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
body: reviewBody,
event: "COMMENT",
});
}