Skip to content

Commit

Permalink
Merge pull request DIYgod#6650 from NeverBehave/pr-auto-url-test
Browse files Browse the repository at this point in the history
flow: pr auto test template
  • Loading branch information
NeverBehave authored Jan 10, 2021
2 parents 51c3f05 + 201eaae commit f9bdb3a
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
20 changes: 18 additions & 2 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,29 @@ Close #
## 完整路由地址 / Example for the proposed route(s)

<!--
为方便测试,请附上完整路由地址,包括所有必选与可选参数,否则将导致 PR 被关闭。
To simplify the testing workflow, please include the complete route, with all required and optional parameters, otherwise your pull request will be closed.
请按照如下格式填写`routes`区域: 我们将会根据你的参数展开自动测试. 一行一个路由
Please fill the `routes` block follow the format below, as we will perform automatic test based on this information. one route per line.
```
/some/route
/some/other/route
```
如果与路由无关, 请写`NOROUTE`
```
NOROUTE
```
-->

<!-- FILL HERE -->
```routes
```


## 新RSS检查列表 / New RSS Script Checklist

<!--
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/pr-deploy-route-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PR route test

on: pull_request

jobs:
testRoute:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Fetch affected routes
id: fetchRoute
uses: actions/github-script@v3
with:
# by default, JSON format returned
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const body = context.payload.pull_request.body
const number = context.payload.pull_request.number
const script = require(`${process.env.GITHUB_WORKSPACE}/scripts/workflow/test-route/identify.js`)
return script({github, context}, body, number)
- name: Waiting for 200 from the Vercel Preview
uses: patrickedqvist/wait-for-vercel-preview@master
id: waitFor200
with:
token: ${{ secrets.GITHUB_TOKEN }}
max_timeout: 120
# fetch link from pr and test link
- name: Generate previews
uses: actions/github-script@v3
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const link = context.steps.waitFor200.outputs.url
const routes = JSON.parse(context.steps.fetchRoute.outputs.result)
const number = context.payload.pull_request.number
const script = require(`${process.env.GITHUB_WORKSPACE}/scripts/workflow/test-route/test.js`)
return await script({github, context}, link, routes, number)
35 changes: 35 additions & 0 deletions scripts/workflow/test-route/identify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = ({github, context}, body, number) => {
const m = body.match(/```routes\n((.|\n)*)```/)
let res = null

if (m && m[1]) {
res = m.split("\n")
console.log("routes detected: ", res)

if (res.length > 0) {
return res
}

if (res[0] === "NOROUTE") {
console.log("PR stated no route, passing")
github.issues.addLabels({
issue_number: number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['Auto: No Route Needed']
})
return res
}
}

console.log("seems no route found, failing")

github.issues.addLabels({
issue_number: number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['Auto: Route No Found']
})

throw "Please follow the PR rules: failed to detect route"
}
51 changes: 51 additions & 0 deletions scripts/workflow/test-route/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module.exports = async ({github, context}, baseUrl, routes, number) => {
if (res[0] === 'NOROUTE') {
return
}

const links = res.map(e => {
const l = e.startsWith('/') ? e : `/${e}`
return `${baseUrl}${l}`
})

let com = 'Successfully generated as following:\n\n'

for (lks in links) {
const prev = await fetch(lks).catch(err => {
com+= `
<details>
<summary>${lks} - **Failed**</summary>
\`\`\`
${err}
\`\`\`
</details>
`
})
if (prev) {
com += `
<details>
<summary>${lks} - Success</summary>
\`\`\`
${prev.text().split("\n").slice(0, 30).join("\n")}
\`\`\`
</details>
`
}
}
github.issues.addLabels({
issue_number: number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: ['Auto: Route Test Complete']
})
github.issues.createComment({
issue_number: number,
owner: context.repo.owner,
repo: context.repo.repo,
body: com
});
}

0 comments on commit f9bdb3a

Please sign in to comment.