Skip to content

Commit

Permalink
refactor!: move custom logger configuration into options (#212)
Browse files Browse the repository at this point in the history
Options should be made as optional object properties rather than positional arguments.
  • Loading branch information
chingor13 committed Apr 26, 2021
1 parent ecc7b25 commit 89a1482
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
8 changes: 5 additions & 3 deletions src/bin/workflow.ts
Expand Up @@ -41,6 +41,7 @@ export function coerceUserCreatePullRequestOptions(): CreatePullRequestUserOptio
maintainersCanModify: yargs.argv.maintainersCanModify as boolean,
fork: yargs.argv.fork as boolean,
labels: yargs.argv.labels as string[],
logger,
};
}

Expand All @@ -52,29 +53,30 @@ export function coerceUserCreateReviewRequestOptions(): CreateReviewCommentUserO
repo: yargs.argv.upstreamRepo as string,
owner: yargs.argv.upstreamOwner as string,
pullNumber: yargs.argv.pullNumber as number,
logger,
};
}

async function createCommand() {
const options = coerceUserCreatePullRequestOptions();
const changes = await git.getChanges(yargs.argv['git-dir'] as string);
const octokit = new Octokit({auth: process.env.ACCESS_TOKEN});
await createPullRequest(octokit, changes, options, logger);
await createPullRequest(octokit, changes, options);
}

async function reviewCommand() {
const reviewOptions = coerceUserCreateReviewRequestOptions();
const diffContents = await git.getDiffString(yargs.argv['git-dir'] as string);
const octokit = new Octokit({auth: process.env.ACCESS_TOKEN});
await reviewPullRequest(octokit, diffContents, reviewOptions, logger);
await reviewPullRequest(octokit, diffContents, reviewOptions);
}

/**
* main workflow entrance
*/
export async function main() {
try {
setupLogger();
setupLogger(console);
if (!process.env.ACCESS_TOKEN) {
throw Error('The ACCESS_TOKEN should not be undefined');
}
Expand Down
13 changes: 4 additions & 9 deletions src/index.ts
Expand Up @@ -22,7 +22,6 @@ import {
FileData,
FileDiffContent,
CreateReviewCommentUserOptions,
Logger,
} from './types';
export {Changes} from './types';
import {Octokit} from '@octokit/rest';
Expand All @@ -47,16 +46,14 @@ import * as retry from 'async-retry';
* @param octokit The authenticated octokit instance, instantiated with an access token having permissiong to create a fork on the target repository.
* @param diffContents A set of changes. The changes may be empty.
* @param options The configuration for interacting with GitHub provided by the user.
* @param loggerOption The logger instance (optional).
* @returns the created review's id number, or null if there are no changes to be made.
*/
export async function reviewPullRequest(
octokit: Octokit,
diffContents: Map<string, FileDiffContent> | string,
options: CreateReviewCommentUserOptions,
loggerOption?: Logger
options: CreateReviewCommentUserOptions
): Promise<number | null> {
setupLogger(loggerOption);
setupLogger(options.logger);
// if null undefined, or the empty map then no changes have been provided.
// Do not execute GitHub workflow
if (
Expand Down Expand Up @@ -104,16 +101,14 @@ export async function reviewPullRequest(
* @param {Octokit} octokit The authenticated octokit instance, instantiated with an access token having permissiong to create a fork on the target repository
* @param {Changes | null | undefined} changes A set of changes. The changes may be empty
* @param {CreatePullRequestUserOptions} options The configuration for interacting with GitHub provided by the user.
* @param {Logger} logger The logger instance (optional).
* @returns {Promise<number>} the pull request number. Returns 0 if unsuccessful.
*/
async function createPullRequest(
octokit: Octokit,
changes: Changes | null | undefined,
options: CreatePullRequestUserOptions,
loggerOption?: Logger
options: CreatePullRequestUserOptions
): Promise<number> {
setupLogger(loggerOption);
setupLogger(options.logger);
// if null undefined, or the empty map then no changes have been provided.
// Do not execute GitHub workflow
if (changes === null || changes === undefined || changes.size === 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/types.ts
Expand Up @@ -99,6 +99,8 @@ export interface CreatePullRequestUserOptions {
retry?: number;
// Create a DRAFT pull request.
draft?: boolean;
// Optional logger to set
logger?: Logger;
}

/**
Expand Down Expand Up @@ -137,6 +139,8 @@ export interface CreateReviewCommentUserOptions {
pullNumber: number;
// The number of files to return per pull request list files query. Used when getting data on the remote PR's files.
pageSize?: number;
// Optional logger to set
logger?: Logger;
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/cli.ts
Expand Up @@ -105,6 +105,7 @@ describe('Mapping pr yargs to create PR options', () => {
maintainersCanModify: true,
fork: true,
labels: ['automerge'],
logger: console,
};
sandbox.stub(yargs, 'argv').value({_: ['pr'], ...options});
assert.deepStrictEqual(coerceUserCreatePullRequestOptions(), options);
Expand Down

0 comments on commit 89a1482

Please sign in to comment.