forked from redwoodjs/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdetectChanges.mjs
More file actions
105 lines (84 loc) · 2.79 KB
/
detectChanges.mjs
File metadata and controls
105 lines (84 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import fs from 'node:fs'
import core from '@actions/core'
import { hasCodeChanges } from './cases/code_changes.mjs'
import { rscChanged } from './cases/rsc.mjs'
import { ssrChanged } from './cases/ssr.mjs'
const getPrNumber = (githubRef) => {
// Example GITHUB_REF refs/pull/9544/merge
const result = /refs\/pull\/(\d+)\/merge/g.exec(process.env.GITHUB_REF)
let prNumber = result?.[1]
if (!prNumber) {
try {
// Example GITHUB_EVENT_PATH
// /home/runner/work/_temp/_github_workflow/event.json
const ev = JSON.parse(
fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')
)
prNumber = ev.pull_request?.number
} catch {
// fall through
}
}
if (!prNumber) {
throw new Error('Could not find the PR number')
}
return prNumber
}
async function getChangedFiles(page = 1) {
const prNumber = getPrNumber()
console.log(`Getting changed files for PR ${prNumber} (page ${page})`)
let changedFiles = []
// Query the GitHub API to get the changed files in the PR
const githubToken = process.env.GITHUB_TOKEN
const url = `https://api.github.com/repos/redwoodjs/redwood/pulls/${prNumber}/files?per_page=100&page=${page}`
const resp = await fetch(url, {
headers: {
Authorization: githubToken ? `Bearer ${githubToken}` : undefined,
['X-GitHub-Api-Version']: '2022-11-28',
Accept: 'application/vnd.github+json',
},
})
const json = await resp.json()
const files = json?.map((file) => file.filename) || []
changedFiles = changedFiles.concat(files)
// Look at the headers to see if the result is paginated
const linkHeader = resp.headers.get('link')
if (linkHeader && linkHeader.includes('rel="next"')) {
const files = await getChangedFiles(page + 1)
changedFiles = changedFiles.concat(files)
}
return changedFiles
}
async function main() {
const branch = process.env.GITHUB_BASE_REF
// If there's no branch, we're not in a pull request.
if (!branch) {
core.setOutput('onlydocs', false)
core.setOutput('rsc', false)
core.setOutput('ssr', false)
return
}
const changedFiles = await getChangedFiles()
console.log(`${changedFiles.length} changed files`)
if (changedFiles.length === 0) {
console.log(
'No changed files found. Something must have gone wrong. Fall back to ' +
'running all tests.'
)
core.setOutput('onlydocs', false)
core.setOutput('rsc', true)
core.setOutput('ssr', true)
return
}
if (!hasCodeChanges(changedFiles)) {
console.log('No code changes detected, only docs')
core.setOutput('onlydocs', true)
core.setOutput('rsc', false)
core.setOutput('ssr', false)
return
}
core.setOutput('onlydocs', false)
core.setOutput('rsc', rscChanged(changedFiles))
core.setOutput('ssr', ssrChanged(changedFiles))
}
main()