diff --git a/package-lock.json b/package-lock.json index d337335a..59c446fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3668,9 +3668,9 @@ } }, "node_modules/electron-to-chromium": { - "version": "1.4.627", - "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.627.tgz", - "integrity": "sha512-BPFdHKPzyGxYQpgiCoIGnkzlMlps3bRdnjeh3qd/Q2pSacL0YW81i4llqsTY/wNbN/Ztw++7HNfp8v4Rm8VDuA==", + "version": "1.4.628", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.628.tgz", + "integrity": "sha512-2k7t5PHvLsufpP6Zwk0nof62yLOsCf032wZx7/q0mv8gwlXjhcxI3lz6f0jBr0GrnWKcm3burXzI3t5IrcdUxw==", "dev": true }, "node_modules/emittery": { diff --git a/src/adapters/base.ts b/src/adapters/base.ts index 5873fce9..73d82e8e 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 144d1a09..0ad0c8ff 100644 --- a/src/adapters/git.ts +++ b/src/adapters/git.ts @@ -74,7 +74,11 @@ 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 587af56d..d4e2d2ae 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', @@ -209,7 +209,7 @@ describe('GithubAdapter', () => { expect(service.createPullRequest).toBeCalledWith({ owner: 'NerdWallet', repo: 'shepherd', - head: 'test-migration', + head: 'NerdWallet:test-migration', base: 'master', title: 'Test migration', body: 'Test PR message', @@ -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,9 @@ 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 bc443ba1..8febacb4 100644 --- a/src/adapters/github.ts +++ b/src/adapters/github.ts @@ -134,12 +134,22 @@ 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 +163,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 +176,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 +290,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 a2ab944c..3be91993 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -47,6 +47,7 @@ type CommandHandler = (context: IMigrationContext, options: any) => Promise { ); }; +const addUpstreamOwnerOption = (command: 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/checkout.ts b/src/commands/checkout.ts index 428ce57c..333587fe 100644 --- a/src/commands/checkout.ts +++ b/src/commands/checkout.ts @@ -78,7 +78,7 @@ export default async (context: IMigrationContext) => { logger.info(''); logger.info(`Checked out ${checkedOutRepos.length} out of ${repos.length} repos`); - const mappedCheckedOutRepos = []; + const mappedCheckedOutRepos: IRepo[] = []; for (const repo of checkedOutRepos) { mappedCheckedOutRepos.push(await adapter.mapRepoAfterCheckout(repo)); } diff --git a/src/commands/pr.ts b/src/commands/pr.ts index 769e638d..e3afa15a 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 df9f849f..e6680f6a 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[]; } diff --git a/yarn.lock b/yarn.lock index ebb2a6ae..368d9d14 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2259,9 +2259,9 @@ eastasianwidth@^0.2.0: integrity sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA== electron-to-chromium@^1.4.601: - version "1.4.627" - resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.627.tgz#86e47c33138cf37e1a05b9f3c95ffff666763f5d" - integrity sha512-BPFdHKPzyGxYQpgiCoIGnkzlMlps3bRdnjeh3qd/Q2pSacL0YW81i4llqsTY/wNbN/Ztw++7HNfp8v4Rm8VDuA== + version "1.4.628" + resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.628.tgz#97cefa4b2356d981875f19639885e4fc50ce6e82" + integrity sha512-2k7t5PHvLsufpP6Zwk0nof62yLOsCf032wZx7/q0mv8gwlXjhcxI3lz6f0jBr0GrnWKcm3burXzI3t5IrcdUxw== emittery@^0.13.1: version "0.13.1"