Skip to content

Commit

Permalink
refactor: Move createPR implementation to GitHub API method
Browse files Browse the repository at this point in the history
This makes mocking the functionality much easier; see
<nodejs/help#4298>.

Co-Authored-By: Alice Fage <[email protected]>
  • Loading branch information
l0b0 and amfage committed Mar 13, 2024
1 parent 125cfd5 commit 71bd626
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 48 deletions.
8 changes: 4 additions & 4 deletions src/commands/basemaps-github/make.cog.github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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`);
Expand All @@ -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]);
}
}

Expand Down Expand Up @@ -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]);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/commands/stac-github-import/stac.github.import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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]);
},
});

Expand Down
70 changes: 28 additions & 42 deletions src/utils/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
async createPullRequest(branch: string, title: string, botEmail: string, files: GithubFiles[]): Promise<number> {
// 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,
Expand All @@ -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<number> {
// 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);
}

0 comments on commit 71bd626

Please sign in to comment.