Lehman-Blog #1609
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Issue 创建时自动标记“待处理”标签 | |
name: 【Open】Label and move to project on issue open | |
on: | |
issues: | |
types: [opened] | |
jobs: | |
label-issue: | |
runs-on: ubuntu-latest | |
permissions: | |
issues: write | |
steps: | |
- name: Label issue | |
uses: andymckay/labeler@master | |
with: | |
add-labels: "待处理" | |
repo-token: ${{ secrets.GITHUB_TOKEN }} | |
auto-check-assistant: | |
strategy: | |
fail-fast: false | |
max-parallel: 3 | |
defaults: | |
run: | |
shell: bash | |
working-directory: ./travellings-bot | |
runs-on: ubuntu-latest | |
outputs: | |
result: ${{ steps.gen_output.outputs.result }} | |
permissions: | |
issues: read | |
steps: | |
- name: Checkout travellings-link/travellings-bot repository | |
uses: actions/checkout@v4 | |
with: | |
repository: 'travellings-link/travellings-bot' | |
path: './travellings-bot' | |
- uses: pnpm/action-setup@v4 | |
name: Install pnpm | |
with: | |
version: latest | |
run_install: false | |
- name: Install Node.js | |
uses: actions/setup-node@v4 | |
# - name: ls | |
# run: ls | |
# 如果不先安装这个,下面安装会报错 | |
- name: Install simple-git-hooks | |
run: pnpm add simple-git-hooks | |
- name: Install package with pnpm | |
run: pnpm install --lockfile pnpm-lock.yaml --config.node-linker=isolated | |
- name: Read Issue Content | |
uses: actions/github-script@v4 | |
with: | |
script: | | |
async function getWebsiteURL(context, github) { | |
const issue = context.issue; | |
const issueResponse = await github.issues.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue.number | |
}); | |
const issueBody = issueResponse.data.body; | |
const lines = issueBody.split('\n'); | |
let websiteUrl = ''; | |
// 新申请拿到网址 | |
for (let i = 0; i < lines.length; i++) { | |
if (lines[i].trim() === '### 网站网址') { | |
websiteUrl = lines[i + 2].trim(); | |
break; | |
} | |
} | |
// 申请更改的拿到网址 | |
if (websiteUrl === '') { | |
for (let i = 0; i < lines.length; i++) { | |
if (lines[i].trim() === '### 新网址(不更改网址请留空)') { | |
websiteUrl = lines[i + 2].trim(); | |
break; | |
} | |
} | |
} | |
// 申请更改类,但是不改网址的 | |
if (websiteUrl === '_No response_') { | |
for (let i = 0; i < lines.length; i++) { | |
if (lines[i].trim() === '### 原网址') { | |
websiteUrl = lines[i + 2].trim(); | |
break; | |
} | |
} | |
} | |
// 还是没拿到网址啥的就退出 | |
if (websiteUrl === '_No response_' | websiteUrl === '' ) { | |
// core.setFailed('未能获取到有效的网址'); | |
return; | |
} | |
return websiteUrl; | |
} | |
const websiteUrl = await getWebsiteURL(context, github); | |
const fs = require('fs'); | |
if (websiteUrl === undefined){ | |
console.log('未能获取到有效的网址'); | |
fs.writeFileSync('./travellings-bot/url.txt', ""); | |
return; | |
} | |
console.log('Website URL:', websiteUrl); | |
fs.writeFileSync('./travellings-bot/url.txt', websiteUrl); | |
# - name: ls | |
# run: ls | |
- name: Run pnpm cli with websiteUrl | |
run: | | |
websiteUrl=$(head -n 1 url.txt) | |
if [ -z "$websiteUrl" ]; then | |
echo "Website URL is empty, skipping pnpm run cli." | |
exit 0 | |
fi | |
pnpm run cli ${websiteUrl} | |
# - name: ls | |
# run: ls | |
- name: Comment on issue | |
uses: actions/github-script@v4 | |
with: | |
script: | | |
const fs = require('fs'); | |
if (!fs.existsSync('./travellings-bot/results.json')) { | |
console.log('results.json file does not exist, skipping comment.'); | |
core.exportVariable('last_result', "null"); | |
return; | |
} | |
const json_result = JSON.parse(fs.readFileSync('./travellings-bot/results.json', { encoding: 'utf8' })); | |
console.log(JSON.stringify(json_result, null, 2)); | |
core.exportVariable('last_result', json_result); | |
- name: Generate output | |
id: gen_output | |
run: | | |
echo ${last_result} | |
echo "result=${last_result}" >> "$GITHUB_OUTPUT" | |
comment-auto-check-assistant-result: | |
runs-on: ubuntu-latest | |
needs: [auto-check-assistant] | |
steps: | |
- name: Comment on issue | |
uses: actions/github-script@v4 | |
env: | |
RESULT: ${{ needs.auto-check-assistant.outputs.result }} | |
with: | |
script: | | |
async function getWebsiteURL(context, github) { | |
const issue = context.issue; | |
const issueResponse = await github.issues.get({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: issue.number | |
}); | |
const issueBody = issueResponse.data.body; | |
const lines = issueBody.split('\n'); | |
let websiteUrl = ''; | |
// 新申请拿到网址 | |
for (let i = 0; i < lines.length; i++) { | |
if (lines[i].trim() === '### 网站网址') { | |
websiteUrl = lines[i + 2].trim(); | |
break; | |
} | |
} | |
// 申请更改的拿到网址 | |
if (websiteUrl === '') { | |
for (let i = 0; i < lines.length; i++) { | |
if (lines[i].trim() === '### 新网址(不更改网址请留空)') { | |
websiteUrl = lines[i + 2].trim(); | |
break; | |
} | |
} | |
} | |
// 申请更改类,但是不改网址的 | |
if (websiteUrl === '_No response_') { | |
for (let i = 0; i < lines.length; i++) { | |
if (lines[i].trim() === '### 原网址') { | |
websiteUrl = lines[i + 2].trim(); | |
break; | |
} | |
} | |
} | |
// 还是没拿到网址啥的就退出 | |
if (websiteUrl === '_No response_' | websiteUrl === '' ) { | |
// core.setFailed('未能获取到有效的网址'); | |
return; | |
} | |
return websiteUrl; | |
} | |
const websiteUrl = await getWebsiteURL(context, github); | |
if (websiteUrl === undefined){ | |
console.log('未能获取到有效的网址'); | |
// 此时 process.env.RESULT 为 null | |
const issue = context.issue; | |
const label = context.payload.label; | |
github.issues.createComment({ | |
issue_number: issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: "站点信息自动检查失败❌\n未能获取到有效的网址" | |
}); | |
return; | |
} | |
let json_result = JSON.parse(process.env.RESULT)["results"][websiteUrl]; | |
// console.log(json_result); | |
function replaceNodeValue(json_result){ | |
for (const key in json_result) { | |
if (typeof json_result[key] === 'object') { | |
replaceNodeValue(json_result[key]); | |
} else if (json_result[key] === "RUN") { | |
json_result[key] = '✅RUN'; | |
} else if (key === "status") { | |
json_result[key] = '❌' + json_result[key]; | |
} | |
} | |
} | |
replaceNodeValue(json_result); | |
let result = `站点信息自动检查完毕🎉 | |
自动检查结果: | |
- HTTP 请求检查:${json_result["axiosCheck"]["status"]}${json_result["axiosCheck"]["status"] === '✅RUN' ? '' : '、失败原因:' + json_result["axiosCheck"]["failedReason"] } | |
- 浏览器模拟检查:${json_result["browserCheck"]["status"]} | |
<details><summary>原始返回值 点击查看详情</summary> | |
${JSON.stringify(json_result, null, 2)} | |
</details>`; | |
const issue = context.issue; | |
const label = context.payload.label; | |
github.issues.createComment({ | |
issue_number: issue.number, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: result | |
}); |