From 0da9aba0bbb783c23b09343a224a18b806a2fbb0 Mon Sep 17 00:00:00 2001 From: Saraph1nes Date: Sat, 13 Jul 2024 00:27:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20#836=20PR=E5=90=88=E5=B9=B6=E5=90=8E?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=B8=85=E9=99=A4PR=E5=9C=A8=E7=BA=BF?= =?UTF-8?q?=E9=A2=84=E8=A7=88=E7=9B=B8=E5=85=B3=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pr-merge.yml | 79 ++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .github/workflows/pr-merge.yml diff --git a/.github/workflows/pr-merge.yml b/.github/workflows/pr-merge.yml new file mode 100644 index 000000000..f4651ed4d --- /dev/null +++ b/.github/workflows/pr-merge.yml @@ -0,0 +1,79 @@ +name: PR Closed + +on: + pull_request_target: + types: + - closed + +jobs: + remove_assets: + runs-on: ubuntu-latest + + steps: + # 检出仓库代码 + - name: Checkout repository + uses: actions/checkout@v2 + + # 打印 PR 详细信息 + - name: Print PR details + run: | + echo "The PR ID is ${{ github.event.pull_request.id }}" + echo "The PR number is ${{ github.event.pull_request.number }}" + echo "The PR title is ${{ github.event.pull_request.title }}" + echo "The PR branch is ${{ github.event.pull_request.head.ref }}" + + # 安装 cos-nodejs-sdk-v5 + - name: Install cos-nodejs-sdk-v5 + run: npm install cos-nodejs-sdk-v5 + + # 删除对应的资源 + - name: Delete Resources On COS + uses: actions/github-script@v7 + with: + script: | + const COS = require('cos-nodejs-sdk-v5'); + + const cos = new COS({ + SecretId: process.env.COS_SECRETID, + SecretKey: process.env.COS_SECRETKEY, + }); + + const bucket = 'cherrymd-1301618266'; + const region = 'ap-singapore'; + const prNumber = '${{ github.event.pull_request.number }}'; + const prefix = `pr${prNumber}/`; + + // List objects in the bucket with the specified prefix + cos.getBucket({ + Bucket: bucket, + Region: region, + Prefix: prefix, + }, (err, data) => { + if (err) { + console.error('Error listing objects:', err); + return; + } + + const objectsToDelete = data.Contents.map(item => ({ Key: item.Key })); + + if (objectsToDelete.length === 0) { + console.log('No objects to delete.'); + return; + } + + // Delete the listed objects + cos.deleteMultipleObject({ + Bucket: bucket, + Region: region, + Objects: objectsToDelete, + }, (err, data) => { + if (err) { + console.error('Error deleting objects:', err); + } else { + console.log('Successfully deleted objects:', data); + } + }); + }); + env: + COS_SECRETID: ${{ secrets.COS_SECRETID }} + COS_SECRETKEY: ${{ secrets.COS_SECRETKEY }}