Skip to content

Commit

Permalink
fix: renamed and removed folder moving part
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Mar 13, 2024
1 parent 3d0135f commit 0e94f45
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
23 changes: 12 additions & 11 deletions components/git/security.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import CLI from '../../lib/cli.js';
import SecurityReleaseSteward from '../../lib/prepare_security.js';
import FinalizeSecurityRelease from '../../lib/finalize_security_release.js';
import UpdateSecurityRelease from '../../lib/update_security_release.js';

export const command = 'security [options]';
export const describe = 'Manage an in-progress security release or start a new one.';
Expand All @@ -10,9 +10,9 @@ const securityOptions = {
describe: 'Start security release process',
type: 'boolean'
},
finalize: {
describe: 'Finalize the security release of Node.js',
type: 'boolean'
'update-date': {
describe: 'Updates the target date of the security release',
type: 'string'
}
};

Expand All @@ -24,26 +24,27 @@ export function builder(yargs) {
'git node security --start',
'Prepare a security release of Node.js')
.example(
'git node security --finalize',
'Finalize the date of the security release of Node.js'
'git node security --update-date=31/12/2023',
'Updates the target date of the security release'
);
}

export function handler(argv) {
if (argv.start) {
return startSecurityRelease(argv);
}
if (argv.finalize) {
return finalizeSecurityRelease(argv);
if (argv['update-date']) {
return updateReleaseDate(argv);
}
yargsInstance.showHelp();
}

async function finalizeSecurityRelease() {
async function updateReleaseDate(argv) {
const releaseDate = argv['update-date'];
const logStream = process.stdout.isTTY ? process.stdout : process.stderr;
const cli = new CLI(logStream);
const finalize = new FinalizeSecurityRelease(cli);
return finalize.start();
const update = new UpdateSecurityRelease(cli);
return update.updateReleaseDate(releaseDate);
}

async function startSecurityRelease() {
Expand Down
7 changes: 5 additions & 2 deletions lib/security-release/security-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ export const PLACEHOLDERS = {
export function checkRemote(cli, repository) {
const remote = runSync('git', ['ls-remote', '--get-url', 'origin']).trim();
const { owner, repo } = repository;
const securityReleaseOrigin = `https://github.com/${owner}/${repo}.git`;
const securityReleaseOrigin = [
`https://github.com/${owner}/${repo}.git`,
`git@github.com:${owner}/${repo}.git`
];

if (remote !== securityReleaseOrigin) {
if (!securityReleaseOrigin.includes(remote)) {
cli.error(`Wrong repository! It should be ${securityReleaseOrigin}`);
process.exit(1);
}
Expand Down
43 changes: 12 additions & 31 deletions lib/finalize_security_release.js → lib/update_security_release.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,56 +13,37 @@ export default class UpdateSecurityRelease {
this.cli = cli;
}

async start() {
async updateReleaseDate(releaseDate) {
const { cli } = this;

const releaseDate = await this.promptReleaseDate(cli);

// checkout on the next-security-release branch
checkoutOnSecurityReleaseBranch(cli, this.repository);

// update the release date in the vulnerabilities.json file
const updatedVulnerabilitiesFiles = await this.updateReleaseDate(releaseDate, { cli });
const updatedVulnerabilitiesFiles = await this.updateVulnerabilitiesJSON(releaseDate, { cli });

const commitMessage = `chore: update the release date to ${releaseDate}`;
commitAndPushVulnerabilitiesJSON(updatedVulnerabilitiesFiles,
commitMessage, { cli, repository: this.repository });
cli.ok('Done!');
}

async updateReleaseDate(releaseDate, { cli }) {
async updateVulnerabilitiesJSON(releaseDate) {
const vulnerabilitiesJSONPath = path.join(process.cwd(),
NEXT_SECURITY_RELEASE_FOLDER, 'vulnerabilities.json');

const content = JSON.parse(fs.readFileSync(vulnerabilitiesJSONPath, 'utf8'));
content.releaseDate = releaseDate;

fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));

cli.ok(`Updated the release date in vulnerabilities.json: ${releaseDate}`);
const exists = fs.existsSync(vulnerabilitiesJSONPath);

const newFolderPath = path.join(process.cwd(),
'security-release', releaseDate.replaceAll('/', '-'));

try {
await fs.accessSync(newFolderPath);
} catch (error) {
await fs.mkdirSync(newFolderPath, { recursive: true });
if (!exists) {
this.cli.error(`The file vulnerabilities.json does not exist at ${vulnerabilitiesJSONPath}`);
process.exit(1);
}

const newPath = path.join(newFolderPath, 'vulnerabilities.json');

fs.renameSync(vulnerabilitiesJSONPath, newPath);
const content = JSON.parse(fs.readFileSync(vulnerabilitiesJSONPath, 'utf8'));
content.releaseDate = releaseDate;

cli.ok(`Moved vulnerabilities.json to ${newPath}`);
// return old path and new path to commit and push
return [vulnerabilitiesJSONPath, newPath];
}
fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));

async promptReleaseDate(cli) {
return cli.prompt('Enter the final release date in YYYY-MM-DD format:', {
questionType: 'input',
defaultAnswer: 'DD-MM-YYYY'
});
this.cli.ok(`Updated the release date in vulnerabilities.json: ${releaseDate}`);
return [vulnerabilitiesJSONPath];
}
}

0 comments on commit 0e94f45

Please sign in to comment.