diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 3bdf9cce..56500256 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -3,6 +3,11 @@ name: PR Preview on: pull_request: +permissions: + contents: read # Read repository content for checkout 读取仓库内容用于检出代码 + pull-requests: write # Write PR comments and status 写入PR评论和状态 + issues: write # Create and update issue comments 创建和更新issue评论 + jobs: build-and-deploy: runs-on: ubuntu-latest @@ -33,16 +38,16 @@ jobs: - name: Merge output directories run: | - # 合并文件 + # Merge files 合并文件 cp -R output_resource/* output/page/ - # 创建目标目录 + # Create target directories 创建目标目录 mkdir -p merged_output/mobile/react/arco-design/pc mkdir -p merged_output/mobile/react/arco-design/mobile - # 移动目录内容 + # Move directory contents 移动目录内容 mv output/page/home/* merged_output/mobile/react/ mv output/page/pc/* merged_output/mobile/react/arco-design/pc/ mv output/page/mobile/* merged_output/mobile/react/arco-design/mobile/ - # 创建重定向 HTML 文件 + # Create redirect HTML file 创建重定向 HTML 文件 echo '' > merged_output/index.html - name: Install Surge @@ -55,21 +60,43 @@ jobs: run: | surge ./merged_output ${{ env.ARCO_SITE_DOMAIN }} --token $SURGE_TOKEN - - name: Find Comment - uses: peter-evans/find-comment@v2 - id: fc - with: - issue-number: ${{ github.event.pull_request.number }} - comment-author: 'github-actions[bot]' - body-includes: PR Preview Link - - - name: Comment PR with Preview Link - uses: peter-evans/create-or-update-comment@v2 + - name: Create/Update PR Comment + uses: actions/github-script@v6 env: ARCO_SITE_DOMAIN: ${{ env.ARCO_SITE_DOMAIN }} with: - issue-number: ${{ github.event.pull_request.number }} - comment-id: ${{ steps.fc.outputs.comment-id }} - edit-mode: replace - body: | - ✨ **PR Preview Link**: [https://${{ env.ARCO_SITE_DOMAIN }}](https://${{ env.ARCO_SITE_DOMAIN }}) + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const previewUrl = `https://${process.env.ARCO_SITE_DOMAIN}`; + const commentBody = `✨ **PR Preview Link**: [${previewUrl}](${previewUrl})`; + + // Retrieve existing comments 获取已有的评论 + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number + }); + + // Find if there's a comment created by us before 查找是否有我们之前创建的评论 + const botComment = comments.find(comment => { + return comment.user.type === 'Bot' && + comment.body.includes('PR Preview Link'); + }); + + if (botComment) { + // Update existing comment 更新已有评论 + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: botComment.id, + body: commentBody + }); + } else { + // Create new comment 创建新评论 + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body: commentBody + }); + }