create-preview-links.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * This function creates a WordPress Playground blueprint JSON string for a theme.
  3. *
  4. * @param {string} themeSlug - The slug of the theme to create a blueprint for.
  5. * @param {string} branch - The branch where the theme changes are located.
  6. * @returns {string} - A JSON string representing the blueprint.
  7. */
  8. function createBlueprint(themeSlug, branch) {
  9. const template = {
  10. steps: [
  11. {
  12. step: 'login',
  13. username: 'admin',
  14. password: 'password',
  15. },
  16. {
  17. step: 'installTheme',
  18. themeZipFile: {
  19. resource: 'url',
  20. url: `https://github-proxy.com/proxy.php?action=partial&repo=Automattic/themes&directory=${themeSlug}&branch=${branch}`,
  21. },
  22. },
  23. {
  24. step: 'activateTheme',
  25. themeFolderName: themeSlug,
  26. },
  27. ],
  28. };
  29. return JSON.stringify(template);
  30. }
  31. /*
  32. * This function creates a comment on a PR with preview links for the changed themes.
  33. * It is used by `preview-theme` workflow.
  34. *
  35. * @param {object} github - An authenticated instance of the GitHub API.
  36. * @param {object} context - The context of the event that triggered the action.
  37. * @param {string} changedThemeSlugs - A space-separated string of theme slugs that have changed.
  38. */
  39. async function createPreviewLinksComment(github, context, changedThemeSlugs) {
  40. const changedThemes = changedThemeSlugs.split(',');
  41. const previewLinks = changedThemes
  42. .map((theme) => {
  43. const [themeName, themeDir] = theme.split(':');
  44. const themeSlug = themeDir.split('/')[0];
  45. const parentThemeSlug = themeName.split('_childof_')[1];
  46. return `- [Preview changes for **${
  47. themeName.split('_childof_')[0]
  48. }**](https://playground.wordpress.net/#${createBlueprint(
  49. themeSlug,
  50. context.payload.pull_request.head.ref
  51. )})${parentThemeSlug ? ` (child of **${parentThemeSlug}**)` : ''}`;
  52. })
  53. .join('\n');
  54. const includesChildThemes = previewLinks.includes('child of');
  55. const comment = `
  56. I've detected changes to the following themes in this PR: ${changedThemes
  57. .map(
  58. (changedThemes) => changedThemes.split(':')[0].split('_childof_')[0]
  59. )
  60. .join(', ')}.
  61. You can preview these changes by following the links below:
  62. ${previewLinks}
  63. I will update this comment with the latest preview links as you push more changes to this PR.
  64. **⚠️ Note:** The preview sites are created using [WordPress Playground](https://wordpress.org/playground/). You can add content, edit settings, and test the themes as you would on a real site, but please note that changes are not saved between sessions.
  65. ${
  66. includesChildThemes
  67. ? '\n**⚠️ Note:** Child themes are dependent on their parent themes. You will have to install the parent theme as well for the preview to work correctly.'
  68. : ''
  69. }
  70. `;
  71. const repoData = {
  72. owner: context.repo.owner,
  73. repo: context.repo.repo,
  74. };
  75. // Check if a comment already exists and update it if it does
  76. const { data: comments } = await github.rest.issues.listComments({
  77. issue_number: context.payload.pull_request.number,
  78. ...repoData,
  79. });
  80. const existingComment = comments.find(
  81. (comment) =>
  82. comment.user.login === 'github-actions[bot]' &&
  83. comment.body.startsWith('### Preview changes')
  84. );
  85. const commentObject = {
  86. body: `### Preview changes\n${comment}`,
  87. ...repoData,
  88. };
  89. if (existingComment) {
  90. await github.rest.issues.updateComment({
  91. comment_id: existingComment.id,
  92. ...commentObject,
  93. });
  94. return;
  95. }
  96. // Create a new comment if one doesn't exist
  97. github.rest.issues.createComment({
  98. issue_number: context.payload.pull_request.number,
  99. ...commentObject,
  100. });
  101. }
  102. module.exports = createPreviewLinksComment;