Skip to content

Commit

Permalink
feat: automatically create a migration file
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re committed Feb 13, 2024
1 parent fcd70b2 commit c0519d5
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function run(): Promise<void> {

// 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';
Expand Down Expand Up @@ -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?`)
Expand All @@ -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 }
) {
Expand Down

0 comments on commit c0519d5

Please sign in to comment.