This repository was archived by the owner on Jul 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (134 loc) Β· 4.34 KB
/
script.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {appAuth} from '@stoe/octoherd-script-common'
import {composeCreatePullRequest} from 'octokit-plugin-create-pull-request'
import {readFileSync} from 'fs'
import {dirname, resolve} from 'path'
import {fileURLToPath} from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
/**
* @param {import('@octoherd/cli').Octokit} octokit
* @param {import('@octoherd/cli').Repository} repository
* @param {object} options
* @param {int} [options.appId=0]
* @param {string} [options.privateKey='']
* @param {boolean} [options.dryRun=false]
*/
export async function script(octokit, repository, {appId = 0, privateKey = '', dryRun = false}) {
const {
archived,
default_branch,
disabled,
fork,
language,
size,
name: repo,
owner: {login: owner},
clone_url: url,
} = repository
// skip archived, disabled, forked and empty repos
if (archived || disabled || fork || size === 0) return
// skip non JavaScript, Golang repos
const lang = language ? language.toLowerCase() : null
let path = null
switch (lang) {
case 'c':
case 'cpp':
case 'csharp':
case 'go':
case 'kotlin':
case 'java':
path = resolve(__dirname, `./codeql.compiled.yml`)
break
case 'javascript':
case 'python':
case 'ruby':
case 'typescript':
path = resolve(__dirname, `./codeql.yml`)
break
default:
break
}
if (!path || !lang) {
octokit.log.info({skipped: true, lang}, ` π no supported language found`)
return
}
let langReplace = lang
// use 'cpp' also for C
if (lang === 'c') langReplace = 'cpp'
// use 'java' also for Kotlin
if (lang === 'kotlin') langReplace = 'java'
// use 'javascript' also for TypeScript
if (lang === 'typescript') langReplace = 'javascript'
if (lang !== langReplace) octokit.log.info({lang, replace: langReplace}, ` β replace language`)
try {
let ok = octokit
if (appId && privateKey) {
try {
ok = await appAuth(repository, appId, privateKey)
octokit.log.info(` π€ authenticated as app`)
} catch (error) {
octokit.log.info({error}, ` β failed to authenticate as app`)
return
}
}
const newContentBuffer = readFileSync(path)
const newContentStr = Buffer.from(newContentBuffer, 'base64').toString('utf-8')
const newContent = newContentStr.replace('__LANGUAGE__', langReplace)
const options = {
owner,
repo,
title: 'π€ Update CodeQL GitHub Action workflow',
head: 'octoherd-script/codeql',
base: default_branch,
body: 'This pull request updates the CodeQL GitHub Action workflow',
createWhenEmpty: false,
changes: [
{
files: {
'.github/workflows/codeql.yml': newContent,
},
commit: `π€ Update .github/workflows/codeql.yml`,
emptyCommit: false,
},
],
update: true,
}
let create = false
let existingContent
try {
// https://docs.github.com/en/rest/reference/repos#get-repository-content
const {
data: {content: existingContentBase64},
} = await ok.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner,
repo,
path: '.github/workflows/codeql.yml',
})
existingContent = Buffer.from(existingContentBase64, 'base64').toString('utf-8')
} catch (error) {
create = true
}
if (newContent === existingContent) {
octokit.log.info({change: false}, ` π no changes`)
} else {
if (create) {
options.title = 'β¨ Create CodeQL GitHub Action workflow'
options.body = `- β¨ Create .github/workflows/codeql.yml`
options.changes[0].commit = `β¨ Create .github/workflows/codeql.yml`
}
if (dryRun) {
const {title, base, head, changes} = options
octokit.log.info({lang, title, base, head, changes}, ` π’ dry-run`)
} else {
const {
data: {html_url},
} = await composeCreatePullRequest(ok, options)
octokit.log.info({change: true, lang}, ` π¦ pull request created ${html_url}`)
}
}
} catch (error) {
octokit.log.error({error}, ` β ${error.message}`)
return
}
octokit.log.info(` β
${url}`)
return
}