diff --git a/src/index.ts b/src/index.ts index aefae23..4b0e3a3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,7 +20,7 @@ async function run(): Promise { // Otherwise, run verify and leave a PR comment const issue_number = payload.pull_request?.number - const { success, message } = await verify(); + const { success, message } = await verify(github.context); if (!issue_number) { const method = success ? 'info' : 'setFailed'; @@ -55,7 +55,7 @@ async function push() { await execa(bin, ['db', 'push'], { encoding: 'utf8', detached: true, reject: false }) } -async function verify() { +async function verify(context: typeof github.context) { const root = resolve('astro', process.cwd()) if (!root) { throw new Error(`Unable to locate the "astro" package. Did you remember to run install?`) @@ -65,12 +65,24 @@ async function verify() { const status = JSON.parse(stdout); switch (status.state) { case 'no-migrations-found': return { success: false, message: 'No migrations found!\nTo scaffold your migrations folder, run `astro db sync`.\n' } - case 'ahead': return { success: false, message: 'Changes detected! To create the necessary migration file, run `astro db sync`.\n' } + case 'ahead': { + let instructions = ''; + if (status.newFileContent) { + instructions = `[Commit a migration file](${getAddMigrationURL(context, status)}).` + } else { + instructions = `Create the necessary migration file by running \`astro db sync\`.` + } + return { success: false, message: `Changes detected! ${instructions}\n` } + } case 'up-to-date': return { success: true, message: 'No migrations needed!\nYour database is up to date.' } } return { success: false, message: 'Unable to run `astro db verify`! Does your action install the `astro` package?' } } +function getAddMigrationURL(context: typeof github.context, status: any) { + return `${context.payload.pull_request!.head.repo.html_url}/new/${context.payload.pull_request!.head.ref}?filename=./migrations/${status.newFilename}&value=${encodeURIComponent(status.newFileContent)}`; +} + async function getCommentId( params: { repo: string; owner: string; issue_number: number } ) {