Skip to content

Commit

Permalink
fix(pipeline): action version and update npmRegistry (#2376)
Browse files Browse the repository at this point in the history
## Proposed change

The version for Otter GitHub actions is incorrect.

Also, if a registry is already present in `.npmrc` but a value is passed
to the ng add schematics, it should be updated.

## Related issues

<!--
Please make sure to follow the [contribution
guidelines](https://github.com/amadeus-digital/Otter/blob/main/CONTRIBUTING.md)
-->

*- No issue associated -*

<!-- * 🐛 Fix #issue -->
<!-- * 🐛 Fix resolves #issue -->
<!-- * 🚀 Feature #issue -->
<!-- * 🚀 Feature resolves #issue -->
<!-- * :octocat: Pull Request #issue -->
  • Loading branch information
vscaiceanu-1a authored Oct 31, 2024
2 parents 4df55d5 + d2fa60f commit ca17508
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
18 changes: 18 additions & 0 deletions packages/@o3r/pipeline/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,22 @@ describe('ng-add', () => {

});

it('should generate a GitHub workflow with custom parameters when npmrc exists', async () => {
const runner = new SchematicTestRunner('@o3r/pipeline', collectionPath);
initialTree.create('.npmrc', 'registry=http://public.registry.com');
const tree = await runner.runSchematic('ng-add', {
toolchain: 'github',
runner: 'windows-latest',
npmRegistry: 'http://private.registry.com'
} as NgAddSchematicsSchema, initialTree);

expect(tree.exists('.github/actions/setup/action.yml')).toBe(true);
expect(tree.exists('.github/workflows/main.yml')).toBe(true);
expect(tree.exists('.npmrc')).toBe(true);

expect(tree.readText('.github/workflows/main.yml')).toContain('windows-latest');
expect(tree.readText('.npmrc')).toBe('registry=http://private.registry.com');

});

});
16 changes: 10 additions & 6 deletions packages/@o3r/pipeline/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {

return async (tree, context) => {
const packageJsonPath = path.resolve(__dirname, '..', '..', 'package.json');
const ownPackageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' })) as PackageJson & { o3rConfig?: { commitHash?: string } };
const commitHash = ownPackageJson.o3rConfig?.commitHash;
const ownPackageJson = JSON.parse(fs.readFileSync(packageJsonPath, { encoding: 'utf8' })) as PackageJson & { config?: { o3r?: { commitHash?: string } } };
const commitHash = ownPackageJson.config?.o3r?.commitHash;
const ownVersion = ownPackageJson.version;
const actionVersionString = commitHash ? `${commitHash} # v${ownVersion}` : ownVersion;
const actionVersionString = commitHash ? `${commitHash} # v${ownVersion}` : `v${ownVersion}`;
let packageManager = 'npm';
try {
const schematics = await import('@o3r/schematics');
Expand Down Expand Up @@ -52,12 +52,16 @@ function ngAddFn(options: NgAddSchematicsSchema): Rule {
}
} else if (packageManager === 'npm') {
const npmrcPath = '/.npmrc';
const npmRegistry = `registry=${options.npmRegistry}`;
if (!tree.exists(npmrcPath)) {
tree.create(npmrcPath, npmRegistry);
tree.create(npmrcPath, `registry=${options.npmRegistry}`);
} else {
const npmrcContent = tree.readText(npmrcPath);
tree.overwrite(npmrcPath, npmrcContent.concat(`\n${npmRegistry}`));
const registryPattern = /^registry=.*$/m;
const newRegistryLine = `registry=${options.npmRegistry}`;
const newContent = registryPattern.test(npmrcContent)
? npmrcContent.replace(registryPattern, newRegistryLine)
: `${npmrcContent}\n${newRegistryLine}`;
tree.overwrite(npmrcPath, newContent);
}
}
return tree;
Expand Down
6 changes: 4 additions & 2 deletions tools/@o3r/build-helpers/scripts/write-git-hash.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ try {
const data = readFileSync(packageJsonPath, { encoding: 'utf-8' });
const packageJson = JSON.parse(data);

packageJson.o3rConfig = packageJson.o3rConfig || {};
packageJson.o3rConfig.commitHash = commitHash;

packageJson.config ||= {};
packageJson.config.o3r ||= {};
packageJson.config.o3r.commitHash = commitHash;

writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2), { encoding: 'utf-8' });
console.log(`package.json updated successfully with commit hash '${commitHash}'`);
Expand Down

0 comments on commit ca17508

Please sign in to comment.