-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
build-plugins-report.js
executable file
·183 lines (152 loc) · 4.85 KB
/
build-plugins-report.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
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
#!/usr/bin/env node
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fs = require('fs-extra');
const path = require('path');
const { execFile: execFileCb } = require('child_process');
const { promisify } = require('util');
const execFile = promisify(execFileCb);
const rootDirectory = path.resolve(__dirname, '..');
const pluginsDirectory = path.resolve(rootDirectory, 'plugins');
async function run(command, ...args) {
const { stdout, stderr } = await execFile(command, args, {
cwd: rootDirectory,
});
if (stderr) {
console.error(stderr);
}
return stdout.trim();
}
function findLatestValidCommit(commits, directoryPath) {
return commits.find(commit => {
const { author, message, files } = commit;
// exclude merge commits
if (message.startsWith('Merge pull request #')) {
return false;
}
// exclude commits authored by a bot
if (author.includes('[bot]')) {
return false;
}
// exclude core maintainers' commits
if (
[
].some(email => author.includes(email))
) {
return false;
}
// ignore multiple plugins changes
if (
files.some(file => {
const fileFullPath = path.resolve(rootDirectory, file);
if (!fileFullPath.startsWith(pluginsDirectory)) {
return false;
}
const pluginBasePath = directoryPath.replace(
/-(backend|common|react|node).*$/,
'',
);
return !fileFullPath.startsWith(pluginBasePath);
})
) {
return false;
}
return true;
});
}
async function getPluginDirectory(directoryName) {
const directoryPath = path.resolve(pluginsDirectory, directoryName);
const packageJson = await fs.readJson(
path.resolve(directoryPath, 'package.json'),
);
return { directoryName, directoryPath, packageJson };
}
function parseCommitsLog(log) {
const lines = log.split('\n');
return lines.reduce((commits, line) => {
if (!line) return commits;
if (line.includes(';')) {
const [author, message, date] = line.split(';');
return [...commits, { author, message, date, files: [] }];
}
const { files, ...commit } = commits.pop();
return [...commits, { ...commit, files: [...files, line] }];
}, []);
}
async function readDirectoryCommits(directoryName) {
const maxCount = 100;
const directoryPath = path.resolve(pluginsDirectory, directoryName);
const logOutput = await run(
'git',
'log',
'origin/master',
'--name-only',
`--max-count=${maxCount}`,
'--pretty=format:%an <%ae>;%s;%as',
'--',
// ignore changes on README and package.json files
path.posix.resolve(directoryPath, 'src'),
`:(exclude)${path.posix.resolve(directoryPath, 'src', '**', '*.test.*')}`,
);
return parseCommitsLog(logOutput);
}
async function getLatestDirectoryCommit({ directoryName, directoryPath }) {
console.log(`🔎 Reading data for ${directoryName}`);
const commits = await readDirectoryCommits(directoryName);
const commit = findLatestValidCommit(commits, directoryPath) ?? {
author: '-',
message: '-',
date: '-',
};
return { plugin: directoryName, ...commit };
}
function isValidDirectory({ packageJson }) {
const roles = [
'frontend-plugin',
'frontend-plugin-module',
'backend-plugin',
'backend-plugin-module',
];
return roles.includes(packageJson?.backstage?.role ?? '');
}
async function main() {
const directoryNames = fs
.readdirSync(pluginsDirectory, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
const directories = await Promise.all(directoryNames.map(getPluginDirectory));
const commits = await Promise.all(
directories.filter(isValidDirectory).map(getLatestDirectoryCommit),
);
const fileName = 'plugins-report.csv';
const fileContent = [
'Plugin;Author;Message;Date',
...commits.map(c => `${c.plugin};${c.author};${c.message};${c.date}`),
];
console.log(`📊 Generating plugins report...`);
fs.writeFile(fileName, fileContent.join('\n'), err => {
if (err) throw err;
});
console.log(`📄 Report generated at ${fileName}`);
}
main(process.argv.slice(2)).catch(error => {
console.error(error.stack || error);
process.exit(1);
});