Skip to content

Commit

Permalink
fix(pr-labels): reduce the time to find touched projects (#2186)
Browse files Browse the repository at this point in the history
## Proposed change
Reduce the number of spawn used, thanks to [nx graph
command](https://nx.dev/nx-api/nx/documents/dep-graph#graph)
Not using `--print` because it's not working anymore
nrwl/nx#27682

## Related issues

- 🐛 Fixes #2185
  • Loading branch information
matthieu-crouzet authored Sep 23, 2024
2 parents 09b2e1c + 3533e1a commit 557904d
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions tools/@o3r/build-helpers/scripts/pr-labels.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { EOL } from 'node:os';
import { spawnSync } from 'node:child_process';
import { promises as fs, existsSync } from 'node:fs';
import { promises as fs, existsSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import minimist from 'minimist'

Expand Down Expand Up @@ -37,9 +38,9 @@ const messageTagMaps = {
* Get labels from the git log output command
*
* @param {string} targetBranch
* @return {Promise<string[]>}
* @return {string[]}
*/
async function getLabelsFromMessage(targetBranch, config) {
function getLabelsFromMessage(targetBranch, config) {
if (!config.enableCommitMessageLabel) {
return [];
}
Expand Down Expand Up @@ -71,28 +72,25 @@ async function getLabelsFromProjects(targetBranch, config) {
const /** @type {string[]} */ listTouchedFiles = spawnSync('git', ['log', `${targetBranch}..HEAD`, '--name-only', '--pretty=format:""'], { encoding: 'utf-8', shell: true }).stdout.trim()
.split(EOL)
.map((file) => file.replace(/\\/g, '/')) || [];
const commitMessages = spawnSync('yarn', ['nx', 'show', 'projects', '--affected', `--base=${targetBranch}`], { encoding: 'utf-8', shell: true }).stdout.trim() || '';
const lines = commitMessages?.split(EOL).filter((line) => !!line) || [];

const tempDirPath = join(tmpdir(), 'pr-labels');
const graphJsonPath = join(tempDirPath, 'graph.json')
spawnSync('yarn', ['nx', 'graph', '--file', graphJsonPath], { encoding: 'utf-8', shell: true });
const { graph } = JSON.parse(await fs.readFile(graphJsonPath, { encoding: 'utf-8' }));
rmSync(tempDirPath, { recursive: true });
const projects = Object.entries(graph.nodes);
const labels = [];
// console.log(lines);

for(const projectName of lines) {
const projectString = spawnSync('yarn', ['nx', 'show', 'project', projectName, '--json'], { encoding: 'utf-8', shell: true }).stdout.trim();
try {
const project = JSON.parse(projectString);
if (listTouchedFiles.some((file) => file.startsWith(project.root))) {
const packageJson = join(project.root, 'package.json');
const /** @type {string | undefined} */ packageName = JSON.parse(await fs.readFile(packageJson, { encoding: 'utf-8' })).name;
if (!packageName) {
process.stderr.write(`No package name found for ${projectName}${EOL}`);
continue;
}
if (!config.ignoredProjects.includes(packageName)) {
labels.push(`${config.projectLabelPrefix}${packageName}`);
}
for(const [projectName, { data: project }] of projects) {
if (listTouchedFiles.some((file) => file.startsWith(project.root))) {
const packageJson = join(project.root, 'package.json');
const /** @type {string | undefined} */ packageName = JSON.parse(await fs.readFile(packageJson, { encoding: 'utf-8' })).name;
if (!packageName) {
process.stderr.write(`No package name found for ${projectName}${EOL}`);
continue;
}
if (!config.ignoredProjects.includes(packageName)) {
labels.push(`${config.projectLabelPrefix}${packageName}`);
}
} catch(e) {
process.stderr.write(`Failed to analyze ${projectName}${EOL}`);
}
}

Expand Down Expand Up @@ -127,7 +125,7 @@ void(async () => {

const config = await getConfig();
const target = `remotes/origin/${args.target}`;
const labelFromMessage = await getLabelsFromMessage(target, config);
const labelFromMessage = getLabelsFromMessage(target, config);
const labelFromProject = !config.ignoreProjectForLabels.some((label) => labelFromMessage.includes(label)) ?
await getLabelsFromProjects(target, config) :
[];
Expand Down

0 comments on commit 557904d

Please sign in to comment.