-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cli): add command for reviewing a pr #130
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,29 @@ | ||
import {Changes, CreatePullRequestUserOptions} from '../types'; | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import { | ||
CreatePullRequestUserOptions, | ||
CreateReviewCommentUserOptions, | ||
} from '../types'; | ||
import {Octokit} from '@octokit/rest'; | ||
import * as git from './handle-git-dir-change'; | ||
import {createPullRequest} from '../'; | ||
import {createPullRequest, reviewPullRequest} from '../'; | ||
import {logger, setupLogger} from '../logger'; | ||
import * as yargs from 'yargs'; | ||
|
||
export const CREATE_PR_COMMAND = 'pr'; | ||
export const REVIEW_PR_COMMAND = 'review'; | ||
|
||
/** | ||
* map yargs to user pull request otions | ||
|
@@ -25,22 +43,48 @@ export function coerceUserCreatePullRequestOptions(): CreatePullRequestUserOptio | |
}; | ||
} | ||
|
||
/** | ||
* map yargs to user pull request otions | ||
*/ | ||
export function coerceUserCreateReviewRequestOptions(): CreateReviewCommentUserOptions { | ||
return { | ||
repo: yargs.argv.upstreamRepo as string, | ||
owner: yargs.argv.upstreamOwner as string, | ||
pullNumber: yargs.argv.pullNumber as number, | ||
}; | ||
} | ||
|
||
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); | ||
} | ||
|
||
async function reviewCommand() { | ||
const reviewOptions = coerceUserCreateReviewRequestOptions(); | ||
const diffContents = await git.getDiffContents( | ||
yargs.argv['git-dir'] as string | ||
); | ||
const octokit = new Octokit({auth: process.env.ACCESS_TOKEN}); | ||
await reviewPullRequest(octokit, diffContents, reviewOptions, logger); | ||
} | ||
|
||
/** | ||
* main workflow entrance | ||
*/ | ||
export async function main() { | ||
try { | ||
setupLogger(); | ||
const options = coerceUserCreatePullRequestOptions(); | ||
if (!process.env.ACCESS_TOKEN) { | ||
throw Error('The ACCESS_TOKEN should not be undefined'); | ||
} | ||
const octokit = new Octokit({auth: process.env.ACCESS_TOKEN}); | ||
let changes: Changes; | ||
switch (yargs.argv._[0]) { | ||
case CREATE_PR_COMMAND: | ||
changes = await git.getChanges(yargs.argv['git-dir'] as string); | ||
await createPullRequest(octokit, changes, options, logger); | ||
createCommand(); | ||
break; | ||
case REVIEW_PR_COMMAND: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can get pretty fancy and provide a handler to the command itself, rather than using a switch like this: .command('command', 'description of command', (yargs) => {
yargs.option('foo', {
type: 'string'
})
}, (argv) => {
// this code runs when the command is invoked.
}) Here's an example of a really fancy command line application built with yargs, that encapsulates commands in their own files: https://github.com/redwoodjs/redwood/tree/main/packages/cli/src/commands |
||
reviewCommand(); | ||
break; | ||
default: | ||
// yargs should have caught this. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to describe all of the commands options inside its own JavaScript file, rather than here, see:
https://github.com/yargs/yargs/blob/master/docs/advanced.md#providing-a-command-module
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filing #133 to clean up separately as it actually appears non-trivial with typing.