Browse Source

Tools: automatically handle changelog updates (#5889)

Sarah Norris 3 years ago
parent
commit
813267c2fa
1 changed files with 73 additions and 10 deletions
  1. 73 10
      theme-utils.mjs

+ 73 - 10
theme-utils.mjs

@@ -40,6 +40,29 @@ function showHelp(){
 	console.log('Help info can go here');
 	console.log('Help info can go here');
 }
 }
 
 
+/*
+ Create list of changes from git logs
+ Optionally pass in a deployed hash or default to calling getLastDeployedHash()
+ Optionally pass in boolean bulletPoints to add bullet points to each commit log
+*/
+async function getCommitLogs(hash, bulletPoints) {
+	if (!hash) {
+		hash = await getLastDeployedHash();
+	}
+
+	let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
+
+	if (bulletPoints) {
+		// Add a '*' to the start of each log (used in changelogs)
+		logs = await executeCommand(`git log --reverse --pretty=format:"* %s" ${hash}..HEAD`);
+	}
+
+	// Remove any double quotes from commit messages
+	logs = logs.replace(/"/g, '');
+
+	return logs;
+}
+
 /*
 /*
  Determine what changes would be deployed
  Determine what changes would be deployed
 */
 */
@@ -58,9 +81,7 @@ async function deployPreview() {
 	let changedThemes = await getChangedThemes(hash);
 	let changedThemes = await getChangedThemes(hash);
 	console.log(`The following themes have changes:\n${changedThemes}`);
 	console.log(`The following themes have changes:\n${changedThemes}`);
 
 
-	let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
-	// Remove any double quotes from commit messages
-	logs = logs.replace(/"/g, '');
+	let logs = await getCommitLogs(hash);
 	console.log(`\n\nCommit log of changes to be deployed:\n\n${logs}\n\n`);
 	console.log(`\n\nCommit log of changes to be deployed:\n\n${logs}\n\n`);
 }
 }
 
 
@@ -129,7 +150,7 @@ async function pushButtonDeploy() {
 		if( thingsWentBump ){
 		if( thingsWentBump ){
 			prompt = await inquirer.prompt([{
 			prompt = await inquirer.prompt([{
 				type: 'confirm',
 				type: 'confirm',
-				message: 'Are you good with the version bump changes? Make any manual adjustments now if necessary.',
+				message: 'Are you good with the version bump and changelog updates? Make any manual adjustments now if necessary.',
 				name: "continue",
 				name: "continue",
 				default: false
 				default: false
 			}]);
 			}]);
@@ -429,6 +450,7 @@ async function updateLastDeployedHash() {
  Version bump (increment version patch) any theme project that has had changes since the last deployment.
  Version bump (increment version patch) any theme project that has had changes since the last deployment.
  If a theme's version has already been changed since that last deployment then do not version bump it.
  If a theme's version has already been changed since that last deployment then do not version bump it.
  If any theme projects have had a version bump also version bump the parent project.
  If any theme projects have had a version bump also version bump the parent project.
+ If a theme has changes also update its changelog.
  Commit the change.
  Commit the change.
 */
 */
 async function versionBumpThemes() {
 async function versionBumpThemes() {
@@ -453,6 +475,7 @@ async function versionBumpThemes() {
 		}
 		}
 
 
 		await versionBumpTheme(theme, true);
 		await versionBumpTheme(theme, true);
+		await updateThemeChangelog(theme, true);
 		changesWereMade = true;
 		changesWereMade = true;
 	}
 	}
 
 
@@ -482,6 +505,50 @@ export function getThemeMetadata(styleCss, attribute) {
 	}
 	}
 }
 }
 
 
+/*
+ Update theme changelog using current commit logs.
+ Used by versionBumpThemes to update each theme changelog.
+*/
+async function updateThemeChangelog(theme, addChanges) {
+	console.log(`Updating ${theme} changelog`);
+
+	// Get theme version
+ 	let styleCss = fs.readFileSync(`${theme}/style.css`, 'utf8');
+ 	let version = getThemeMetadata(styleCss, 'Version');
+
+	// Get list of updates with bullet points
+ 	let logs = await getCommitLogs('', true);
+
+	// Get theme readme.txt
+	let readmeFile = `${theme}/readme.txt`;
+
+	// Build changelog entry
+	let newChangelogEntry = `== Changelog ==
+
+= ${version} =
+${logs}`;
+
+	if (!readmeFile) {
+		console.log(`Unable to find a readme.txt for ${theme}. Aborted Automated Deploy Process at changelog step.`);
+		return;
+	}
+
+	// Update readme.txt
+	fs.readFile(readmeFile, 'utf8', function(err, data) {
+		let changelogSection = '== Changelog ==';
+		let regex = new RegExp('^.*' + changelogSection + '.*$', 'gm');
+		let formattedChangelog = data.replace(regex, newChangelogEntry);
+
+		fs.writeFile(readmeFile, formattedChangelog, 'utf8', function(err) {
+			if (err) return console.log(err);
+		});
+	});
+
+	// Stage readme.txt
+	if (addChanges) {
+		await executeCommand(`git add ${readmeFile}`);
+	}
+}
 
 
 /*
 /*
  Version Bump a Theme.
  Version Bump a Theme.
@@ -760,9 +827,7 @@ async function syncCoreTheme(theme, sinceRevision) {
 async function buildPhabricatorCommitMessageSince(hash){
 async function buildPhabricatorCommitMessageSince(hash){
 
 
 	let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
 	let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
-	let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
-	// Remove any double quotes from commit messages
-	logs = logs.replace(/"/g, '');
+	let logs = await getCommitLogs(hash);
 	return `Deploy Themes ${projectVersion} to wpcom
 	return `Deploy Themes ${projectVersion} to wpcom
 
 
 Summary:
 Summary:
@@ -834,9 +899,7 @@ async function tagDeployment(options={}) {
 		workInTheOpenPhabricatorUrl = `Phabricator: ${options.diffId}-code`;
 		workInTheOpenPhabricatorUrl = `Phabricator: ${options.diffId}-code`;
 	}
 	}
 	let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
 	let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
-	let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
-	// Remove any double quotes from commit messages
-	logs = logs.replace(/"/g, '');
+	let logs = await getCommitLogs(hash);
 	let tag = `v${projectVersion}`;
 	let tag = `v${projectVersion}`;
 	let message = `Deploy Themes ${tag} to wpcom. \n\n${logs} \n\n${workInTheOpenPhabricatorUrl}`;
 	let message = `Deploy Themes ${tag} to wpcom. \n\n${logs} \n\n${workInTheOpenPhabricatorUrl}`;