From bc91a5c12527213c9210f6b8af8d54abdbee3499 Mon Sep 17 00:00:00 2001 From: kavitha186 Date: Sat, 18 Nov 2023 17:07:02 -0500 Subject: [PATCH] feat: added options to create pr from fork to upstream --- src/adapters/base.ts | 2 +- src/adapters/git.ts | 2 +- src/adapters/github.test.ts | 6 +++--- src/adapters/github.ts | 19 +++++++++++++++---- src/cli.ts | 7 +++++++ src/commands/pr.ts | 4 ++-- src/migration-context.ts | 1 + 7 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/adapters/base.ts b/src/adapters/base.ts index 5873fce99..73d82e8ec 100644 --- a/src/adapters/base.ts +++ b/src/adapters/base.ts @@ -29,7 +29,7 @@ interface IRepoAdapter { pushRepo(repo: IRepo, force: boolean): Promise; - createPullRequest(repo: IRepo, message: string): Promise; + createPullRequest(repo: IRepo, message: string, upstreamOwner: string): Promise; getPullRequestStatus(repo: IRepo): Promise; diff --git a/src/adapters/git.ts b/src/adapters/git.ts index 144d1a095..90f5d62f4 100644 --- a/src/adapters/git.ts +++ b/src/adapters/git.ts @@ -74,7 +74,7 @@ abstract class GitAdapter implements IRepoAdapter { await this.git(repo).push('origin', 'HEAD', options); } - public abstract createPullRequest(repo: IRepo, message: string): Promise; + public abstract createPullRequest(repo: IRepo, message: string, upstreamOwner: string): Promise; public abstract getPullRequestStatus(repo: IRepo): Promise; diff --git a/src/adapters/github.test.ts b/src/adapters/github.test.ts index 587af56d2..65a57ccdd 100644 --- a/src/adapters/github.test.ts +++ b/src/adapters/github.test.ts @@ -199,7 +199,7 @@ describe('GithubAdapter', () => { service.listPullRequests.mockResolvedValue([]); const adapter = new GithubAdapter(context, service); - await adapter.createPullRequest(REPO, 'Test PR message'); + await adapter.createPullRequest(REPO, 'Test PR message', 'NerdWallet'); expect(service.listPullRequests).toBeCalledWith({ owner: 'NerdWallet', @@ -227,7 +227,7 @@ describe('GithubAdapter', () => { }, ]); const adapter = new GithubAdapter(context, service); - await adapter.createPullRequest(REPO, 'Test PR message, part 2'); + await adapter.createPullRequest(REPO, 'Test PR message, part 2', 'NerdWallet'); expect(service.updatePullRequest).toBeCalledWith({ owner: 'NerdWallet', @@ -249,7 +249,7 @@ describe('GithubAdapter', () => { }, ]); const adapter = new GithubAdapter(context, service); - await expect(adapter.createPullRequest(REPO, 'Test PR message, part 2')).rejects.toThrow(); + await expect(adapter.createPullRequest(REPO, 'Test PR message, part 2', 'NerdWallet')).rejects.toThrow(); expect(service.updatePullRequest).not.toBeCalled(); }); }); diff --git a/src/adapters/github.ts b/src/adapters/github.ts index bc443ba18..b4d5bc453 100644 --- a/src/adapters/github.ts +++ b/src/adapters/github.ts @@ -134,12 +134,18 @@ class GithubAdapter extends GitAdapter { await super.pushRepo(repo, force || shouldForce); } - public async createPullRequest(repo: IRepo, message: string): Promise { + public async createPullRequest(repo: IRepo, message: string, upstreamOwner: string): Promise { const { migration: { spec }, } = this.migrationContext; const { owner, name, defaultBranch } = repo; + let baseOwner = owner; + + if(upstreamOwner) { + baseOwner = upstreamOwner; + } + // Let's check if a PR already exists const pullRequests = await this.githubService.listPullRequests({ owner, @@ -153,7 +159,7 @@ class GithubAdapter extends GitAdapter { if (pullRequest.state === 'open') { // A pull request exists and is open, let's update it await this.githubService.updatePullRequest({ - owner, + owner: baseOwner, repo: name, pull_number: pullRequest.number, title: spec.title, @@ -166,10 +172,11 @@ class GithubAdapter extends GitAdapter { } } else { // No PR yet - we have to create it + await this.githubService.createPullRequest({ - owner, + owner: baseOwner, repo: name, - head: this.branchName, + head: `${owner}:${this.branchName}`, base: defaultBranch, title: spec.title, body: message, @@ -279,6 +286,10 @@ class GithubAdapter extends GitAdapter { ); } + public getOwnerName(owner: string): string { + return owner; + } + public getBaseBranch(repo: IRepo): string { return repo.defaultBranch; } diff --git a/src/cli.ts b/src/cli.ts index 3d343f66c..75d373a56 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -45,6 +45,7 @@ type CommandHandler = (context: IMigrationContext, options: any) => Promise { ); }; +const addUpstreamOwnerOption = (command: program.Command) => { + return command.option('--upstreamOwner ', 'Upstream Owner can be passed incase of trying to raise PR from fork to upstream'); +} + const addCommand = (name: string, description: string, repos: boolean, handler: CommandHandler) => { const subprogram = buildCommand(name, description); if (repos) { addReposOption(subprogram); + addUpstreamOwnerOption(subprogram); } subprogram.action(handleCommand(handler)); }; diff --git a/src/commands/pr.ts b/src/commands/pr.ts index 769e638d3..0348f656f 100644 --- a/src/commands/pr.ts +++ b/src/commands/pr.ts @@ -5,7 +5,7 @@ import { generatePrMessageWithFooter } from '../util/generate-pr-message'; export default async (context: IMigrationContext) => { const { - migration: { spec }, + migration: { spec , upstreamOwner }, logger, } = context; @@ -31,7 +31,7 @@ export default async (context: IMigrationContext) => { const prSpinner = logger.spinner('Creating pull request'); try { - await context.adapter.createPullRequest(repo, message); + await context.adapter.createPullRequest(repo, message, upstreamOwner); prSpinner.succeed('Pull request created'); } catch (e: any) { logger.error(e); diff --git a/src/migration-context.ts b/src/migration-context.ts index df9f849f5..e6680f6a1 100644 --- a/src/migration-context.ts +++ b/src/migration-context.ts @@ -11,6 +11,7 @@ export interface IMigrationInfo { migrationDirectory: string; workingDirectory: string; repos: IRepo[] | null; + upstreamOwner: string; selectedRepos?: IRepo[]; }