From 7a23e5d221e240aa893e43282c47116b15b741b3 Mon Sep 17 00:00:00 2001 From: Lee Date: Tue, 10 Dec 2024 17:16:48 +0800 Subject: [PATCH] add action.yml --- .github/workflows/comment-pr-preview-link.yml | 65 ------------- README.md | 35 ++----- action.yml | 97 +++++++++++++++++++ 3 files changed, 107 insertions(+), 90 deletions(-) delete mode 100644 .github/workflows/comment-pr-preview-link.yml create mode 100644 action.yml diff --git a/.github/workflows/comment-pr-preview-link.yml b/.github/workflows/comment-pr-preview-link.yml deleted file mode 100644 index c731b98..0000000 --- a/.github/workflows/comment-pr-preview-link.yml +++ /dev/null @@ -1,65 +0,0 @@ -name: 发送预览链接到评论区 - -on: - workflow_call: - inputs: - previewUrl: - required: true - type: string - pageUrls: - required: false - type: string - -jobs: - deploy: - runs-on: ubuntu-latest - steps: - - name: 使用 GitHub App 进行身份验证 - id: auth - uses: actions/create-github-app-token@v1 - with: - app-id: ${{ vars.BOT_APP_ID }} - private-key: ${{ secrets.BOT_APP_SECRET }} - owner: ${{ github.repository_owner }} - # 获取预览链接并发送到 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 previewUrl = "${{ inputs.previewUrl }}"; - const pageUrls = "${{ inputs.pageUrls }}"; - const reviewBody = `🚀 预览部署完成!访问链接: ${previewUrl}\n\n✨ 本 PR 修改了以下页面: ✨${pageUrls}`; - - // 获取现有 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", - }); - } - diff --git a/README.md b/README.md index 1bfb701..a25e51f 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,13 @@ -## 使用说明 - -本说明适用于部署在 Cloudflare 的场景。对于其他部署方案,默认导出的 URL 变量名可能不是 `url`,需结合文档对 `steps.deploy.outputs.url` 后的变量名稍作修改。 - -1. 在 `deploy` 层级下使用 `outputs` 导出生成的 URL -2. 在 `jobs` 层级下引用工作流 - -## 代码样例 - -```yaml -jobs: - deploy: - runs-on: ubuntu-latest - outputs: - preview_url: ${{ steps.deploy.outputs.url }} - steps: - # 下方部署过程略 -``` +### 使用说明 ```yaml - # 上方部署过程略 - comment_on_pr: - needs: deploy - uses: project-trans/actions/.github/workflows/comment-pr-preview-link.yml@main - secrets: inherit - with: - previewUrl: ${{ needs.deploy.outputs.preview_url }} +- name: bot + uses: project-trans/Display-Preview-URL-in-PR@SHA + with: + previewUrl: ${{ steps.deploy.outputs.deployment-url }} + BOT_APP_ID: ${{ vars.BOT_APP_ID }} + BOT_APP_SECRET: ${{ secrets.BOT_APP_SECRET }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REMOVE_PREFIX: docs + REMOVE_SUFFIX: .md ``` \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..0bd6278 --- /dev/null +++ b/action.yml @@ -0,0 +1,97 @@ +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 + echo -e "All File URLs: ${{ env.all_file_urls }}" + 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", + }); + }