|
| 1 | + |
| 2 | +import {randomUUID} from 'node:crypto' |
| 3 | +import {createRequire} from 'node:module' |
| 4 | + |
| 5 | +import {findUp} from 'find-up' |
| 6 | + |
| 7 | +import {withOctokit} from './github-utils.js' |
| 8 | + |
| 9 | +const require = createRequire(import.meta.url) |
| 10 | +const {readFile} = require('fs-extra') |
| 11 | + |
| 12 | +const REPO_OWNER = 'Shopify' |
| 13 | +const REPO_NAME = 'static-cdn-assets' |
| 14 | +const NOTIFICATIONS_PATH = 'static-24h/cli/notifications.json' |
| 15 | + |
| 16 | +async function createPR() { |
| 17 | + const version = await versionToRelease() |
| 18 | + console.log(`Creating notification PR for version ${version}`) |
| 19 | + |
| 20 | + await withOctokit('shopify', async (octokit) => { |
| 21 | + const {data} = await octokit.rest.repos.getContent({ |
| 22 | + owner: REPO_OWNER, |
| 23 | + repo: REPO_NAME, |
| 24 | + path: NOTIFICATIONS_PATH, |
| 25 | + }) |
| 26 | + |
| 27 | + const currentContent = Buffer.from(data.content, 'base64').toString('utf-8') |
| 28 | + |
| 29 | + const newNotification = { |
| 30 | + id: randomUUID(), |
| 31 | + type: 'info', |
| 32 | + title: `Release notes for ${version}`, |
| 33 | + frequency: 'once', |
| 34 | + message: 'Release highlights:\n\n - [app] Example \n - [theme] Example', |
| 35 | + minVersion: version, |
| 36 | + maxVersion: version, |
| 37 | + ownerChannel: '#devtools-dev-experience', |
| 38 | + cta: { |
| 39 | + label: 'Read the complete release notes', |
| 40 | + url: `https://github.com/Shopify/cli/releases/tag/${version}`, |
| 41 | + }, |
| 42 | + } |
| 43 | + |
| 44 | + const BASE_INDENT = ' ' |
| 45 | + const formattedEntry = JSON.stringify(newNotification, null, 2) |
| 46 | + .split('\n') |
| 47 | + .map((line) => BASE_INDENT + line) |
| 48 | + .join('\n') |
| 49 | + |
| 50 | + // Append the new entry right before the closing "]" of the notifications array, |
| 51 | + // preserving the rest of the file as-is. |
| 52 | + const updatedContent = currentContent.replace( |
| 53 | + /(\})\s*(\]\s*\}\s*)$/, |
| 54 | + `$1,\n${formattedEntry}\n $2`, |
| 55 | + ) |
| 56 | + |
| 57 | + const response = await octokit.createPullRequest({ |
| 58 | + owner: REPO_OWNER, |
| 59 | + repo: REPO_NAME, |
| 60 | + title: `Add notification for CLI v${version}`, |
| 61 | + body: [ |
| 62 | + `Adds a notification to be shown in the latest version of the CLI [${version}](https://github.com/Shopify/cli/releases/tag/${version}).`, |
| 63 | + '', |
| 64 | + '**Please update the release highlights before merging.**', |
| 65 | + '', |
| 66 | + '### How to test', |
| 67 | + '- `npx http-server ~/src/github.com/Shopify/static-cdn-assets`', |
| 68 | + '- `SHOPIFY_CLI_NOTIFICATIONS_URL=http://127.0.0.1:8080/static-24h/cli/notifications.json shopify version`', |
| 69 | + "- You may need to clear the CLI cache with `shopify cache clear` and run the command twice to see the notification (it's fetched in the background).", |
| 70 | + '', |
| 71 | + '### Checklist', |
| 72 | + "- [ ] I've updated the message template in the notification with the most important changes", |
| 73 | + "- [ ] I've tested the notification", |
| 74 | + ].join('\n'), |
| 75 | + head: `cli-${version}-notification`, |
| 76 | + base: 'main', |
| 77 | + update: true, |
| 78 | + forceFork: false, |
| 79 | + changes: [ |
| 80 | + { |
| 81 | + files: { |
| 82 | + [NOTIFICATIONS_PATH]: updatedContent, |
| 83 | + }, |
| 84 | + commit: `Add notification for CLI v${version}`, |
| 85 | + }, |
| 86 | + ], |
| 87 | + createWhenEmpty: false, |
| 88 | + }) |
| 89 | + |
| 90 | + if (response) { |
| 91 | + console.log(`PR URL: ${response.data.html_url}`) |
| 92 | + } else { |
| 93 | + console.log('No changes detected, PR not created.') |
| 94 | + } |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +async function versionToRelease() { |
| 99 | + const cliKitPackageJsonPath = await findUp('packages/cli-kit/package.json', {type: 'file'}) |
| 100 | + return JSON.parse(await readFile(cliKitPackageJsonPath)).version |
| 101 | +} |
| 102 | + |
| 103 | +await createPR() |
0 commit comments