theme-utils.mjs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. import { spawn } from 'child_process';
  2. import fs from 'fs';
  3. import open from 'open';
  4. import inquirer from 'inquirer';
  5. const remoteSSH = 'wpcom-sandbox';
  6. const sandboxPublicThemesFolder = '/home/wpdev/public_html/wp-content/themes/pub';
  7. const sandboxRootFolder = '/home/wpdev/public_html/';
  8. const isWin = process.platform === 'win32';
  9. const directoriesToIgnore = [ 'variations', 'videomaker', 'videomaker-white' ];
  10. (async function start() {
  11. let args = process.argv.slice(2);
  12. let command = args?.[0];
  13. switch (command) {
  14. case "push-button-deploy-git": return pushButtonDeploy('git');
  15. case "push-button-deploy-svn": return pushButtonDeploy('svn');
  16. case "clean-sandbox-git": return cleanSandboxGit();
  17. case "clean-sandbox-svn": return cleanSandboxSvn();
  18. case "clean-all-sandbox-git": return cleanAllSandboxGit();
  19. case "clean-all-sandbox-svn": return cleanAllSandboxSvn();
  20. case "push-to-sandbox": return pushToSandbox();
  21. case "push-changes-to-sandbox": return pushChangesToSandbox();
  22. case "push-premium-to-sandbox": return pushPremiumToSandbox();
  23. case "version-bump-themes": return versionBumpThemes();
  24. case "land-diff-git": return landChangesGit(args?.[1]);
  25. case "land-diff-svn": return landChangesSvn(args?.[1]);
  26. case "deploy-preview": return deployPreview();
  27. case "deploy-theme": return deployThemes([args?.[1]]);
  28. case "build-com-zip": return buildComZip([args?.[1]]);
  29. }
  30. return showHelp();
  31. })();
  32. function showHelp(){
  33. // TODO: make this helpful
  34. console.log('Help info can go here');
  35. }
  36. /*
  37. Determine what changes would be deployed
  38. */
  39. async function deployPreview() {
  40. console.clear();
  41. console.log('To ensure accuracy clean your sandbox before previewing. (It is not automatically done).');
  42. console.log('npm run sandbox:clean:git OR npm run sandbox:clean:svn')
  43. let message = await checkForDeployability();
  44. if (message) {
  45. console.log(`\n${message}\n\n`);
  46. }
  47. let hash = await getLastDeployedHash();
  48. console.log(`Last deployed hash: ${hash}`);
  49. let changedThemes = await getChangedThemes(hash);
  50. console.log(`The following themes have changes:\n${changedThemes}`);
  51. let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
  52. console.log(`\n\nCommit log of changes to be deployed:\n\n${logs}\n\n`);
  53. }
  54. /*
  55. Execute the first phase of a deployment.
  56. Leverages git on the sandbox.
  57. * Gets the last deployed hash from the sandbox
  58. * Version bump all themes have have changes since the last deployment
  59. * Commit the version bump change to github
  60. * Clean the sandbox and ensure it is up-to-date
  61. * Push all changed files (including removal of deleted files) since the last deployment
  62. * Update the 'last deployed' hash on the sandbox
  63. * Create a phabricator diff based on the changes since the last deployment. The description including the commit messages since the last deployment.
  64. * Open the Phabricator Diff in your browser
  65. * Create a tag in the github repository at this point of change which includes the phabricator link in the description
  66. */
  67. async function pushButtonDeploy(repoType) {
  68. console.clear();
  69. let prompt = await inquirer.prompt([{
  70. type: 'confirm',
  71. message: 'You are about to deploy /trunk. Are you ready to continue?',
  72. name: "continue",
  73. default: false
  74. }]);
  75. if(!prompt.continue){
  76. return;
  77. }
  78. if (repoType != 'svn' && repoType != 'git' ) {
  79. return console.log('Specify a repo type to use push-button deploy');
  80. }
  81. let message = await checkForDeployability();
  82. if (message) {
  83. return console.log(`\n\n${message}\n\n`);
  84. }
  85. try {
  86. if (repoType === 'git' ) {
  87. await cleanSandboxGit();
  88. }
  89. else {
  90. await cleanSandboxSvn();
  91. }
  92. let hash = await getLastDeployedHash();
  93. let diffUrl;
  94. let thingsWentBump = await versionBumpThemes();
  95. let changedThemes = await getChangedThemes(hash);
  96. await pushChangesToSandbox();
  97. await updateLastDeployedHash();
  98. //push changes (from version bump)
  99. if( thingsWentBump ){
  100. prompt = await inquirer.prompt([{
  101. type: 'confirm',
  102. message: 'Are you ready to push this version bump change to the source repository (Github)?',
  103. name: "continue",
  104. default: false
  105. }]);
  106. if(!prompt.continue){
  107. console.log(`Aborted Automated Deploy Process at version bump push change.` );
  108. return;
  109. }
  110. await executeCommand(`
  111. git commit -m "Version Bump";
  112. git push
  113. `, true);
  114. }
  115. if (repoType === 'git' ) {
  116. diffUrl = await createGitPhabricatorDiff(hash);
  117. }
  118. else {
  119. diffUrl = await createSvnPhabricatorDiff(hash);
  120. }
  121. let diffId = diffUrl.split('a8c.com/')[1];
  122. await tagDeployment({
  123. hash: hash,
  124. diffId: diffId
  125. });
  126. console.log(`\n\nPhase One Complete\n\nYour sandbox has been updated and the diff is available for review.\nPlease give your sandbox a smoke test to determine that the changes work as expected.\nThe following themes have had changes: \n\n${changedThemes.join(' ')}\n\n\n`);
  127. prompt = await inquirer.prompt([{
  128. type: 'confirm',
  129. message: 'Are you ready to land these changes?',
  130. name: "continue",
  131. default: false
  132. }]);
  133. if(!prompt.continue){
  134. console.log(`Aborted Automated Deploy Process Landing Phase\n\nYou will have to land these changes manually. The ID of the diff to land: ${diffId}` );
  135. return;
  136. }
  137. if (repoType === 'git' ) {
  138. await landChangesGit(diffId);
  139. }
  140. else {
  141. await landChangesSvn(diffId);
  142. }
  143. await deployThemes(changedThemes);
  144. await buildComZips(changedThemes);
  145. console.log(`The following themes have changed:\n${changedThemes.join('\n')}`)
  146. console.log('\n\nAll Done!!\n\n');
  147. }
  148. catch (err) {
  149. console.log("ERROR with deply script: ", err);
  150. }
  151. }
  152. /*
  153. Build .zip file for .com
  154. */
  155. async function buildComZip(themeSlug) {
  156. console.log( `Building ${themeSlug} .zip` );
  157. let styleCss = fs.readFileSync(`${themeSlug}/style.css`, 'utf8');
  158. // Gets the theme version (Version:) and minimum WP version (Tested up to:) from the theme's style.css
  159. let themeVersion = getThemeMetadata(styleCss, 'Version');
  160. let wpVersionCompat = getThemeMetadata(styleCss, 'Requires at least');
  161. if (themeVersion && wpVersionCompat) {
  162. await executeOnSandbox(`php ${sandboxRootFolder}bin/themes/theme-downloads/build-theme-zip.php --stylesheet=pub/${themeSlug} --themeversion=${themeVersion} --wpversioncompat=${wpVersionCompat}`, true);
  163. }
  164. else {
  165. console.log('Unable to build theme .zip.');
  166. if (!themeVersion) {
  167. console.log('Could not find theme version (Version:) in the theme style.css.');
  168. }
  169. if (!wpVersionCompat) {
  170. console.log('Could not find WP compat version (Tested up to:) in the theme style.css.');
  171. }
  172. console.log('Please build the .zip file for the theme manually.', themeSlug);
  173. open('https://mc.a8c.com/themes/downloads/');
  174. }
  175. }
  176. async function buildComZips(themes) {
  177. for ( let theme of themes ) {
  178. await buildComZip(theme);
  179. }
  180. }
  181. /*
  182. Check to ensure that:
  183. * The current branch is /trunk
  184. * That trunk is up-to-date with origin/trunk
  185. */
  186. async function checkForDeployability(){
  187. let branchName = await executeCommand('git symbolic-ref --short HEAD');
  188. if(branchName !== 'trunk' ) {
  189. return 'Only the /trunk branch can be deployed.';
  190. }
  191. await executeCommand('git remote update', true);
  192. let localMasterHash = await executeCommand('git rev-parse trunk')
  193. let remoteMasterHash = await executeCommand('git rev-parse origin/trunk')
  194. if(localMasterHash !== remoteMasterHash) {
  195. return 'Local /trunk is out-of-date. Pull changes to continue.'
  196. }
  197. return null;
  198. }
  199. /*
  200. Land the changes from the given diff ID. This is the "production merge".
  201. This is the git version of that action.
  202. */
  203. async function landChangesGit(diffId){
  204. return await executeOnSandbox(`cd ${sandboxPublicThemesFolder};arc patch ${diffId};arc land;exit;`, true, true);
  205. }
  206. /*
  207. Land the changes from the given diff ID. This is the "production merge".
  208. This is the svn version of that action.
  209. */
  210. async function landChangesSvn(diffId){
  211. return await executeOnSandbox(`
  212. cd ${sandboxPublicThemesFolder};
  213. svn ci -m ${diffId}
  214. `, true );
  215. }
  216. async function getChangedThemes(hash) {
  217. console.log('Determining all changed themes');
  218. let themes = await getActionableThemes();
  219. let changedThemes = [];
  220. for (let theme of themes) {
  221. let hasChanges = await checkThemeForChanges(theme, hash);
  222. if(hasChanges){
  223. changedThemes.push(theme);
  224. }
  225. }
  226. return changedThemes;
  227. }
  228. /*
  229. Deploy a collection of themes.
  230. Part of the push-button-deploy process.
  231. Can also be triggered to deploy a single theme with the command:
  232. node ./theme-utils.mjs deploy-theme THEMENAME
  233. */
  234. async function deployThemes( themes ) {
  235. let response;
  236. for ( let theme of themes ) {
  237. console.log( `Deploying ${theme}` );
  238. let deploySuccess = false;
  239. let attempt = 0;
  240. while ( ! deploySuccess) {
  241. attempt++;
  242. console.log(`\nattempt #${attempt}\n\n`);
  243. response = await executeOnSandbox( `deploy pub ${theme};exit;`, true, true );
  244. deploySuccess = response.includes( 'successfully deployed to' );
  245. if( ! deploySuccess ) {
  246. console.log( 'Deploy was not successful. Trying again in 10 seconds...' );
  247. await new Promise(resolve => setTimeout(resolve, 10000));
  248. }
  249. else {
  250. console.log( "Deploy successful." );
  251. }
  252. }
  253. }
  254. }
  255. /*
  256. Provide the hash of the last managed deployment.
  257. This hash is used to determine all the changes that have happened between that point and the current point.
  258. */
  259. async function getLastDeployedHash() {
  260. let result = await executeOnSandbox(`
  261. cat ${sandboxPublicThemesFolder}/.pub-git-hash
  262. `);
  263. return result;
  264. }
  265. /*
  266. Update the 'last deployed hash' on the server with the current hash.
  267. */
  268. async function updateLastDeployedHash() {
  269. let hash = await executeCommand(`git rev-parse HEAD`);
  270. await executeOnSandbox(`
  271. echo '${hash}' > ${sandboxPublicThemesFolder}/.pub-git-hash
  272. `);
  273. }
  274. /*
  275. Version bump (increment version patch) any theme project that has had changes since the last deployment.
  276. If a theme's version has already been changed since that last deployment then do not version bump it.
  277. If any theme projects have had a version bump also version bump the parent project.
  278. Commit the change.
  279. */
  280. async function versionBumpThemes() {
  281. console.log("Version Bumping");
  282. let themes = await getActionableThemes();
  283. let hash = await getLastDeployedHash();
  284. let changesWereMade = false;
  285. let versionBumpCount = 0;
  286. for (let theme of themes) {
  287. let hasChanges = await checkThemeForChanges(theme, hash);
  288. if( ! hasChanges){
  289. // console.log(`${theme} has no changes`);
  290. continue;
  291. }
  292. versionBumpCount++;
  293. let hasVersionBump = await checkThemeForVersionBump(theme, hash);
  294. if( hasVersionBump ){
  295. continue;
  296. }
  297. await versionBumpTheme(theme, true);
  298. changesWereMade = true;
  299. }
  300. //version bump the root project if there were changes to any of the themes
  301. let rootHasVersionBump = await checkProjectForVersionBump(hash);
  302. console.log('root check', rootHasVersionBump, versionBumpCount, changesWereMade);
  303. if ( versionBumpCount > 0 && ! rootHasVersionBump ) {
  304. await executeCommand(`npm version patch --no-git-tag-version && git add package.json package-lock.json`);
  305. changesWereMade = true;
  306. }
  307. return changesWereMade;
  308. }
  309. function getThemeMetadata(styleCss, attribute) {
  310. if ( !styleCss || !attribute ) {
  311. return null;
  312. }
  313. switch ( attribute ) {
  314. case 'Version':
  315. return styleCss
  316. .match(/(?<=Version:\s*).*?(?=\s*\r?\n|\rg)/gs)[0]
  317. .trim()
  318. .replace('-wpcom', '');
  319. case 'Requires at least':
  320. return styleCss
  321. .match(/(?<=Requires at least:\s*).*?(?=\s*\r?\n|\rg)/gs);
  322. }
  323. }
  324. /*
  325. Version Bump a Theme.
  326. Used by versionBumpThemes to do the work of version bumping.
  327. First increment the patch version in style.css
  328. Then update any of these files with the new version: [package.json, style.scss, style-child-theme.scss]
  329. */
  330. async function versionBumpTheme(theme, addChanges){
  331. console.log(`${theme} needs a version bump`);
  332. await executeCommand(`perl -pi -e 's/Version: ((\\d+\\.)*)(\\d+)(.*)$/"Version: ".$1.($3+1).$4/ge' ${theme}/style.css`, true);
  333. await executeCommand(`git add ${theme}/style.css`);
  334. let styleCss = fs.readFileSync(`${theme}/style.css`, 'utf8');
  335. let currentVersion = getThemeMetadata(styleCss, 'Version');
  336. let filesToUpdate = await executeCommand(`find ${theme} -name package.json -o -name style.scss -o -name style-child-theme.scss -maxdepth 2`);
  337. filesToUpdate = filesToUpdate.split('\n').filter(item => item != '');
  338. for ( let file of filesToUpdate ) {
  339. await executeCommand(`perl -pi -e 's/Version: (.*)$/"Version: '${currentVersion}'"/ge' ${file}`);
  340. await executeCommand(`perl -pi -e 's/\\"version\\": (.*)$/"\\"version\\": \\"'${currentVersion}'\\","/ge' ${file}`);
  341. if (addChanges){
  342. await executeCommand(`git add ${file}`);
  343. }
  344. }
  345. }
  346. /*
  347. Determine if a theme has had a version bump since a given hash.
  348. Used by versionBumpThemes
  349. Compares the value of 'version' in style.css between the hash and current value
  350. */
  351. async function checkThemeForVersionBump(theme, hash){
  352. return executeCommand(`
  353. git show ${hash}:${theme}/style.css 2>/dev/null
  354. `)
  355. .catch( ( error ) => {
  356. //This is a new theme, no need to bump versions so we'll just say we've already done it
  357. return true;
  358. } )
  359. .then( ( previousStyleString ) => {
  360. if( previousStyleString === true) {
  361. return previousStyleString;
  362. }
  363. let previousVersion = getThemeMetadata(previousStyleString, 'Version');
  364. let styleCss = fs.readFileSync(`${theme}/style.css`, 'utf8');
  365. let currentVersion = getThemeMetadata(styleCss, 'Version');
  366. return previousVersion != currentVersion;
  367. });
  368. }
  369. /*
  370. Determine if the project has had a version bump since a given hash.
  371. Used by versionBumpThemes
  372. Compares the value of 'version' in package.json between the hash and current value
  373. */
  374. async function checkProjectForVersionBump(hash){
  375. let previousPackageString = await executeCommand(`
  376. git show ${hash}:./package.json 2>/dev/null
  377. `);
  378. let previousPackage = JSON.parse(previousPackageString);
  379. let currentPackage = JSON.parse(fs.readFileSync(`./package.json`))
  380. return previousPackage.version != currentPackage.version;
  381. }
  382. /*
  383. Determine if a theme has had changes since a given hash.
  384. Used by versionBumpThemes
  385. */
  386. async function checkThemeForChanges(theme, hash){
  387. let uncomittedChanges = await executeCommand(`git diff-index --name-only HEAD -- ${theme}`);
  388. let comittedChanges = await executeCommand(`git diff --name-only ${hash} HEAD -- ${theme}`);
  389. return uncomittedChanges != '' || comittedChanges != '';
  390. }
  391. /*
  392. Provide a list of 'actionable' themes (those themes that have style.css files)
  393. */
  394. async function getActionableThemes() {
  395. let result = await executeCommand(`for d in */; do
  396. if test -f "./$d/style.css"; then
  397. echo $d;
  398. fi
  399. done`);
  400. return result
  401. .split('\n')
  402. .map(item=>item.replace('/', ''));
  403. }
  404. /*
  405. Clean the theme sandbox.
  406. Assumes sandbox is in 'git' mode
  407. checkout origin/develop and ensure it's up-to-date.
  408. Remove any other changes.
  409. */
  410. async function cleanSandboxGit() {
  411. console.log('Cleaning the Themes Sandbox');
  412. await executeOnSandbox(`
  413. cd ${sandboxPublicThemesFolder};
  414. git reset --hard HEAD;
  415. git clean -fd;
  416. git checkout develop;
  417. git pull;
  418. echo;
  419. git status
  420. `, true);
  421. console.log('All done cleaning.');
  422. }
  423. /*
  424. Clean the entire sandbox.
  425. Assumes sandbox is in 'git' mode
  426. checkout origin/develop and ensure it's up-to-date.
  427. Remove any other changes.
  428. */
  429. async function cleanAllSandboxGit() {
  430. console.log('Cleaning the Entire Sandbox');
  431. let response = await executeOnSandbox(`
  432. cd ${sandboxRootFolder};
  433. git reset --hard HEAD;
  434. git clean -fd;
  435. git checkout develop;
  436. git pull;
  437. echo;
  438. git status
  439. `, true);
  440. console.log('All done cleaning.');
  441. }
  442. /*
  443. Clean the theme sandbox.
  444. Assumes sandbox is in 'svn' mode
  445. ensure trunk is up-to-date
  446. Remove any other changes
  447. */
  448. async function cleanSandboxSvn() {
  449. console.log('Cleaning the theme sandbox');
  450. await executeOnSandbox(`
  451. cd ${sandboxPublicThemesFolder};
  452. svn revert -R .;
  453. svn cleanup --remove-unversioned;
  454. svn up;
  455. `, true);
  456. console.log('All done cleaning.');
  457. }
  458. /*
  459. Clean the entire sandbox.
  460. Assumes sandbox is in 'svn' mode
  461. ensure trunk is up-to-date
  462. Remove any other changes
  463. */
  464. async function cleanAllSandboxSvn() {
  465. console.log('Cleaning the entire sandbox');
  466. await executeOnSandbox(`
  467. cd ${sandboxRootFolder};
  468. svn revert -R .;
  469. svn cleanup --remove-unversioned;
  470. svn up .;
  471. `, true);
  472. console.log('All done cleaning.');
  473. }
  474. /*
  475. Push exactly what is here (all files) up to the sandbox (with the exclusion of files noted in .sandbox-ignore)
  476. */
  477. function pushToSandbox() {
  478. executeCommand(`
  479. rsync -av --no-p --no-times --exclude-from='.sandbox-ignore' ./ wpcom-sandbox:${sandboxPublicThemesFolder}/
  480. `);
  481. }
  482. /*
  483. Push exactly what is here (all files) up to the sandbox (with the exclusion of files noted in .sandbox-ignore)
  484. This pushes only the folders noted as "premiumThemes" into the premium themes directory.
  485. This is the only part of the deploy process that is automated; the rest must be done manually including:
  486. * Creating a Phabricator Diff
  487. * Landing (comitting) the change
  488. * Deploying the theme
  489. * Triggering the .zip builds
  490. */
  491. function pushPremiumToSandbox() {
  492. const premiumThemes = [
  493. 'videomaker',
  494. 'videomaker-white'
  495. ]
  496. executeCommand(`
  497. rsync -av --no-p --no-times --exclude-from='.sandbox-ignore' --exclude='sass/' ./${premiumThemes.join(' ./')} wpcom-sandbox:${sandboxRootFolder}/wp-content/themes/premium/
  498. `, true);
  499. }
  500. /*
  501. Push only (and every) change since the point-of-diversion from /trunk
  502. Remove files from the sandbox that have been removed since the last deployed hash
  503. */
  504. async function pushChangesToSandbox() {
  505. console.log("Pushing Changes to Sandbox.");
  506. let hash = await getLastDeployedHash();
  507. let deletedFiles = await getDeletedFilesSince(hash);
  508. let changedFiles = await getComittedChangesSinceHash(hash);
  509. //remove deleted files from changed files
  510. changedFiles = changedFiles.filter( item => {
  511. return false === deletedFiles.includes(item);
  512. });
  513. if(deletedFiles.length > 0) {
  514. console.log('deleting from sandbox: ', deletedFiles);
  515. await executeOnSandbox(`
  516. cd ${sandboxPublicThemesFolder};
  517. rm -f ${deletedFiles.join(' ')}
  518. `, true);
  519. }
  520. if(changedFiles.length > 0) {
  521. console.log('pushing changed files to sandbox:', changedFiles);
  522. await executeCommand(`
  523. rsync -avR --no-p --no-times --exclude-from='.sandbox-ignore' ${changedFiles.join(' ')} wpcom-sandbox:${sandboxPublicThemesFolder}/
  524. `, true);
  525. }
  526. }
  527. /*
  528. Provide a collection of all files that have changed since the given hash.
  529. Used by pushChangesToSandbox
  530. */
  531. async function getComittedChangesSinceHash(hash) {
  532. const directoriesToIgnoreString = directoriesToIgnore.map( directory => ':^' + directory ).join(' ');
  533. let comittedChanges = await executeCommand(`git diff ${hash} HEAD --name-only -- . ${directoriesToIgnoreString}`);
  534. comittedChanges = comittedChanges.replace(/\r?\n|\r/g, " ").split(" ");
  535. let uncomittedChanges = await executeCommand(`git diff HEAD --name-only -- . ${directoriesToIgnoreString}`);
  536. uncomittedChanges = uncomittedChanges.replace(/\r?\n|\r/g, " ").split(" ");
  537. return comittedChanges.concat(uncomittedChanges);
  538. }
  539. /*
  540. Provide a collection of all files that have been deleted since the given hash.
  541. Used by pushChangesToSandbox
  542. */
  543. async function getDeletedFilesSince(hash){
  544. let deletedSinceHash = await executeCommand(`
  545. git log --format=format:"" --name-only -M100% --diff-filter=D ${hash}..HEAD
  546. `);
  547. deletedSinceHash = deletedSinceHash.replace(/\r?\n|\r/g, " ").trim().split(" ");
  548. let deletedAndUncomitted = await executeCommand(`
  549. git diff HEAD --name-only --diff-filter=D
  550. `);
  551. deletedAndUncomitted = deletedAndUncomitted.replace(/\r?\n|\r/g, " ").trim().split(" ");
  552. return deletedSinceHash.concat(deletedAndUncomitted).filter( item => {
  553. return item != '';
  554. });
  555. }
  556. /*
  557. Build the Phabricator commit message.
  558. This message contains the logs from all of the commits since the given hash.
  559. Used by create*PhabricatorDiff
  560. */
  561. async function buildPhabricatorCommitMessageSince(hash){
  562. let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
  563. let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
  564. // Remove any double quotes from commit messages
  565. logs.replace(/"/g, '');
  566. return `Deploy Themes ${projectVersion} to wpcom
  567. Summary:
  568. ${logs}
  569. Test Plan: Execute Smoke Test
  570. Reviewers:
  571. Subscribers:
  572. `;
  573. }
  574. /*
  575. Create a (git) Phabricator diff from a given hash.
  576. Open the phabricator diff in your browser.
  577. Provide the URL of the phabricator diff.
  578. */
  579. async function createGitPhabricatorDiff(hash) {
  580. console.log('creating Phabricator Diff');
  581. let commitMessage = await buildPhabricatorCommitMessageSince(hash);
  582. let result = await executeOnSandbox(`
  583. cd ${sandboxPublicThemesFolder};
  584. git branch -D deploy
  585. git checkout -b deploy
  586. git add --all
  587. git commit -m "${commitMessage}"
  588. arc diff --create --verbatim
  589. `, true);
  590. let phabricatorUrl = getPhabricatorUrlFromResponse(result);
  591. console.log('Diff Created at: ', phabricatorUrl);
  592. if(phabricatorUrl) {
  593. open(phabricatorUrl);
  594. }
  595. return phabricatorUrl;
  596. }
  597. /*
  598. Create a (svn) Phabricator diff from a given hash.
  599. Open the phabricator diff in your browser.
  600. Provide the URL of the phabricator diff.
  601. */
  602. async function createSvnPhabricatorDiff(hash) {
  603. console.log('creating Phabricator Diff');
  604. const commitTempFileLocation = '/tmp/theme-deploy-comment.txt';
  605. const commitMessage = await buildPhabricatorCommitMessageSince(hash);
  606. console.log(commitMessage);
  607. const result = await executeOnSandbox(`
  608. cd ${sandboxPublicThemesFolder};
  609. echo "${commitMessage}" > ${commitTempFileLocation};
  610. svn add --force * --auto-props --parents --depth infinity -q;
  611. svn status | grep "^\!" | sed 's/^\! *//g' | xargs svn rm;
  612. arc diff --create --message-file ${commitTempFileLocation}
  613. `, true);
  614. const phabricatorUrl = getPhabricatorUrlFromResponse(result);
  615. console.log('Diff Created at: ', phabricatorUrl);
  616. if(phabricatorUrl) {
  617. open(phabricatorUrl);
  618. }
  619. return phabricatorUrl;
  620. }
  621. /*
  622. Utility to pull the Phabricator URL from the diff creation command.
  623. Used by createGitPhabricatorDiff
  624. */
  625. function getPhabricatorUrlFromResponse(response){
  626. return response
  627. ?.split('\n')
  628. ?.find( item => {
  629. return item.includes('Revision URI: ');
  630. })
  631. ?.split("Revision URI: ")[1];
  632. }
  633. /*
  634. Create a git tag at the current hash.
  635. In the description include the commit logs since the given hash.
  636. Include the (cleansed) Phabricator link.
  637. */
  638. async function tagDeployment(options={}) {
  639. console.log('tagging deployment');
  640. let hash = options.hash || await getLastDeployedHash();
  641. let workInTheOpenPhabricatorUrl = '';
  642. if (options.diffId) {
  643. workInTheOpenPhabricatorUrl = `Phabricator: ${options.diffId}-code`;
  644. }
  645. let projectVersion = await executeCommand(`node -p "require('./package.json').version"`);
  646. let logs = await executeCommand(`git log --reverse --pretty=format:%s ${hash}..HEAD`);
  647. // Remove any double quotes from commit messages
  648. logs.replace(/"/g, '');
  649. let tag = `v${projectVersion}`;
  650. let message = `Deploy Themes ${tag} to wpcom. \n\n${logs} \n\n${workInTheOpenPhabricatorUrl}`;
  651. await executeCommand(`
  652. git tag -a ${tag} -m "${message}"
  653. git push origin ${tag}
  654. `, true);
  655. }
  656. /*
  657. Execute a command on the sandbox.
  658. Expects the following to be configured in your ~/.ssh/config file:
  659. Host wpcom-sandbox
  660. User wpdev
  661. HostName SANDBOXURL.wordpress.com
  662. ForwardAgent yes
  663. */
  664. function executeOnSandbox(command, logResponse, enablePsudoterminal){
  665. if(enablePsudoterminal){
  666. return executeCommand(`ssh -tt -A ${remoteSSH} << EOF
  667. ${command}
  668. EOF`, logResponse);
  669. }
  670. return executeCommand(`ssh -TA ${remoteSSH} << EOF
  671. ${command}
  672. EOF`, logResponse);
  673. }
  674. /*
  675. Execute a command locally.
  676. */
  677. async function executeCommand(command, logResponse) {
  678. return new Promise((resolove, reject) => {
  679. let child;
  680. let response = '';
  681. let errResponse = '';
  682. if (isWin) {
  683. child = spawn('cmd.exe', ['/s', '/c', '"' + command + '"'], {
  684. windowsVerbatimArguments: true,
  685. stdio: [process.stdin, 'pipe', 'pipe'],
  686. })
  687. } else {
  688. child = spawn(process.env.SHELL, ['-c', command]);
  689. }
  690. child.stdout.on('data', (data) => {
  691. response += data;
  692. if(logResponse){
  693. console.log(data.toString());
  694. }
  695. });
  696. child.stderr.on('data', (data) => {
  697. errResponse += data;
  698. if(logResponse){
  699. console.log(data.toString());
  700. }
  701. });
  702. child.on('exit', (code) => {
  703. if (code !== 0) {
  704. reject(errResponse.trim());
  705. }
  706. resolove(response.trim());
  707. });
  708. });
  709. }