diff --git a/src/commands/basemaps-github/make.cog.github.ts b/src/commands/basemaps-github/make.cog.github.ts index fa59b3f6..f7db0a51 100644 --- a/src/commands/basemaps-github/make.cog.github.ts +++ b/src/commands/basemaps-github/make.cog.github.ts @@ -9,7 +9,7 @@ import { fsa } from '@chunkd/fs'; import { logger } from '../../log.js'; import { DEFAULT_PRETTIER_FORMAT } from '../../utils/config.js'; -import { createPR, GithubApi } from '../../utils/github.js'; +import { GithubApi } from '../../utils/github.js'; import { prettyPrint } from '../format/pretty.print.js'; export enum Category { @@ -78,7 +78,7 @@ export class MakeCogGithub { const tileSetPath = fsa.joinAll('config', 'tileset', region, `${layer.name}.json`); const file = { path: tileSetPath, content }; // Github create pull request - await createPR(gh, branch, title, botEmail, [file]); + await gh.createPullRequest(branch, title, botEmail, [file]); } else { // Prepare new aerial tileset config const tileSetPath = fsa.joinAll('config', 'tileset', `${filename}.json`); @@ -91,7 +91,7 @@ export class MakeCogGithub { const content = await prettyPrint(JSON.stringify(newTileSet, null, 2), ConfigPrettierFormat); const file = { path: tileSetPath, content }; // Github create pull request - await createPR(gh, branch, title, botEmail, [file]); + await gh.createPullRequest(branch, title, botEmail, [file]); } } @@ -180,7 +180,7 @@ export class MakeCogGithub { const content = await prettyPrint(JSON.stringify(newTileSet, null, 2), ConfigPrettierFormat); const file = { path: tileSetPath, content }; // Github create pull request - await createPR(gh, branch, title, botEmail, [file]); + await gh.createPullRequest(branch, title, botEmail, [file]); } /** diff --git a/src/commands/stac-github-import/stac.github.import.ts b/src/commands/stac-github-import/stac.github.import.ts index de9eb7b6..d9ec48dd 100644 --- a/src/commands/stac-github-import/stac.github.import.ts +++ b/src/commands/stac-github-import/stac.github.import.ts @@ -5,7 +5,7 @@ import * as st from 'stac-ts'; import { CliInfo } from '../../cli.info.js'; import { logger } from '../../log.js'; import { DEFAULT_PRETTIER_FORMAT } from '../../utils/config.js'; -import { createPR, GithubApi } from '../../utils/github.js'; +import { GithubApi } from '../../utils/github.js'; import { config, registerCli, verbose } from '../common.js'; import { prettyPrint } from '../format/pretty.print.js'; @@ -91,7 +91,7 @@ export const commandStacGithubImport = command({ const collectionFile = { path: targetCollectionPath, content: collectionFileContent }; logger.info({ commit: `feat: import ${collection.title}`, branch: `feat/bot-${collection.id}` }, 'Git:Commit'); // create pull request - await createPR(gh, branch, title, botEmail, [collectionFile]); + await gh.createPullRequest(branch, title, botEmail, [collectionFile]); }, }); diff --git a/src/utils/github.ts b/src/utils/github.ts index c074c4d0..b8739f74 100644 --- a/src/utils/github.ts +++ b/src/utils/github.ts @@ -155,7 +155,34 @@ export class GithubApi { /** * Create a new pull request from the given branch and return pull request number */ - async createPullRequest(branch: string, title: string): Promise { + async createPullRequest(branch: string, title: string, botEmail: string, files: GithubFiles[]): Promise { + // git checkout -b + logger.info({ branch }, 'GitHub: Get branch'); + let sha = await this.getBranch(branch); + if (sha == null) { + logger.info({ branch }, 'GitHub: branch Not Found, create new branch'); + sha = await this.createBranch(branch); + } + + // git add + const blobs: Blob[] = []; + for (const file of files) { + logger.info({ path: file.path }, 'GitHub: Add change'); + const blob = await this.createBlob(file.content, file.path); + blobs.push(blob); + } + + // git commit + logger.info({ branch }, 'GitHub: Commit to Branch'); + const commitSha = await this.createCommit(blobs, title, botEmail, sha); + + // git push + logger.info({ branch }, 'GitHub: Push commit to Branch'); + await this.updateBranch(branch, commitSha); + + // git pr create + logger.info({ branch: branch }, 'GitHub: Create Pull Request'); + // Create pull request from the give head const response = await this.octokit.rest.pulls.create({ owner: this.owner, @@ -174,44 +201,3 @@ export interface GithubFiles { path: string; content: string; } - -/** - * Create github pull requests - * - * @returns pull request number - */ -export async function createPR( - gh: GithubApi, - branch: string, - title: string, - botEmail: string, - files: GithubFiles[], -): Promise { - // git checkout -b - logger.info({ branch }, 'GitHub: Get branch'); - let sha = await gh.getBranch(branch); - if (sha == null) { - logger.info({ branch }, 'GitHub: branch Not Found, create new branch'); - sha = await gh.createBranch(branch); - } - - // git add - const blobs: Blob[] = []; - for (const file of files) { - logger.info({ path: file.path }, 'GitHub: Add change'); - const blob = await gh.createBlob(file.content, file.path); - blobs.push(blob); - } - - // git commit - logger.info({ branch }, 'GitHub: Commit to Branch'); - const commitSha = await gh.createCommit(blobs, title, botEmail, sha); - - // git push - logger.info({ branch }, 'GitHub: Push commit to Branch'); - await gh.updateBranch(branch, commitSha); - - // git pr create - logger.info({ branch: branch }, 'GitHub: Create Pull Request'); - return await gh.createPullRequest(branch, title); -}