-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
209 lines (187 loc) · 6.66 KB
/
index.ts
File metadata and controls
209 lines (187 loc) · 6.66 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { exec, logger, newCommand } from "../utils"
import scanner from "sonarqube-scanner"
import fs from "fs"
import path from "path"
export const sonarCommand = newCommand(
"sonar",
"Run a Sonar analysis and uploads to the Sonarcloud server. This can be used from with GitHub or command line, but when using command line options need to be supplied which are derived automatically from the GitHub Action Environment/Secrets."
)
.argument("package-json", {
description: "The package.json of the project (or root of the workspace)",
type: "string",
optional: true,
default: "./package.json",
})
.option("sonar-url", {
description: "Sonar Server to connect to",
type: "string",
default: "https://sonarcloud.io",
})
.option("sonar-token", {
description: "Sonar Token to use to authenticate to the API",
type: "string",
})
.option("project-key", {
description:
"Sonar Project key (which can be seen in the UI). If not, supplied derived from the project name or from the GitHub Repo",
type: "string",
})
.option("organisation", {
description: "Sonar organisation",
default: "committed",
type: "string",
})
.option("exclude-coverage", {
description: "Exclude glob paths from coverage (e.g **/SwaggerApi.ts)",
default: "",
type: "array",
})
.action(async (options) => {
const sonarToken = options["sonar-token"] ?? process.env.SONAR_TOKEN
const packageJson = openPackageJson(options["package-json"])
// If we are running in GitHub CI we can complete the information
let projectKey = ""
if (options["project-key"]) {
projectKey = options["project-key"]
} else if (process.env.GITHUB_REPOSITORY) {
projectKey = process.env.GITHUB_REPOSITORY.replace("/", "_")
logger.debug(`Derived project key ${projectKey} from repository name`)
} else {
// Guess the name of the registry (and hence the sonar key)
projectKey = `commitd_${packageJson.name}`
}
let githubToken: string | undefined = undefined
if (process.env.GITHUB_TOKEN) {
githubToken = process.env.GITHUB_TOKEN
}
let githubPullRequest: string | undefined = undefined
if (process.env.GITHUB_PR) {
githubPullRequest = process.env.GITHUB_PR
}
const rootDir = path.dirname(path.resolve(options["package-json"]))
const packageJsonDirs = await findWorkspacePackageDirs(rootDir)
const sourceDirs = findSourceDir(packageJsonDirs)
const allTestExecutions = getTestExecutions(packageJsonDirs)
logger.debug(options["package-json"])
logger.debug(rootDir)
logger.debug(packageJsonDirs)
logger.debug(sourceDirs)
logger.debug(allTestExecutions)
const parameters = {
serverUrl: options["sonar-url"],
token: sonarToken,
options: {
// Connecting to Sonarcloud
"sonar.projectKey": projectKey,
"sonar.organization": options.organisation,
// GitHub stuff
"sonar.github.oauth": githubToken,
"sonar.github.pullRequest": githubPullRequest,
"sonar.projectName": packageJson.name,
"sonar.projectDescription": packageJson.description || "",
"sonar.sourceEncoding": "UTF-8",
"sonar.javascript.lcov.reportPaths": "coverage/lcov.info",
"sonar.python.coverage.reportPaths": "**/coverage.xml",
"sonar.testExecutionReportPaths": allTestExecutions.join(","),
"sonar.sources": sourceDirs.join(","),
"sonar.tests": sourceDirs.join(","),
"sonar.test.inclusions": [
"**/*.spec.ts",
"**/*.spec.tsx",
"**/*.test.ts",
"**/*.test.tsx",
"**/*.spec.js",
"**/*.spec.jsx",
"**/*.test.js",
"**/*.test.jsx",
].join(","),
"sonar.coverage.exclusions": [
"**/*.test.js",
"**/*.spec.js",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.ts",
"**/*.spec.ts",
"**/*.mock.ts",
"**/*.stories.tsx",
"scripts/**/*.*",
"**/generators/plopfile.js",
"**/mockServiceWorker.js",
"**/src/setupTests.tsx",
"**/tests/*.*",
"**/e2e/**/*.ts",
"**/*.mjs",
...options["exclude-coverage"],
].join(","),
},
}
logger.debug("Parameters %o", parameters)
if (options["dry-run"]) {
logger.info("DRY-RUN: Not running sonarqube scanner")
} else {
scanner(parameters, () => process.exit())
}
})
function openPackageJson(file: string): Record<string, string> {
const f = fs.readFileSync(file)
return JSON.parse(f.toString())
}
async function findWorkspacePackageDirs(rootDir: string): Promise<string[]> {
// As Sonar does not support wildcards in certain files we calculate the answer for it.
if (fs.existsSync(path.join(rootDir, "yarn.lock"))) {
// If Yarn
const r = await exec("yarn", ["workspaces", "--json", "info"], {
pipe: false,
})
if (r.exitCode === 0) {
const output = r.stdio
const raw = JSON.parse(JSON.parse(output).data) as Record<
string,
{
location: string
workspaceDependencies: string[]
mismatchedWorkspaceDependencies: string[]
}
>
return Object.values(raw).map((p) => p.location)
}
} else {
// npm
const r = await exec("npm", ["exec", "-ws", "-c", "echo $PWD"], {
pipe: false,
})
if (r.exitCode === 0) {
const output = r.stdio
return output.split("\n").map((f) => path.relative(rootDir, f))
}
}
// Assume this is not a workspace project, and that we are in the module that counts
// "" = relative(rootDir, rootDir)
return [""]
}
// Final all sources
function findSourceDir(packageJsonDirs: string[]): string[] {
return (
packageJsonDirs
// TODO src is a convention... perhaps we need something smarter here (read tsconfig, look for py files, etc))
.map((p) => path.join(p, "src"))
.filter((f) => fs.existsSync(f))
)
}
function getTestExecutions(packageJsonDirs: string[]): string[] {
// Get the location of every test-executions.xml
const allTestExecutions = packageJsonDirs
.map((p) => path.join(p, "coverage/test-executions.xml"))
.filter((f) => fs.existsSync(f))
// Sonar seems to need paths relative to the route
// The test-execution are set up to be full path, so we remove the cwd
// TODO: relative to the cwd() or relative to the root of project?
const cwd = process.cwd() + "/"
const pattern = new RegExp(cwd, "g")
for (const f of allTestExecutions) {
const input = fs.readFileSync(f, "utf8")
const replaced = input.replace(pattern, "")
fs.writeFileSync(f, replaced, "utf8")
}
return allTestExecutions
}