Skip to content
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: added options to create pr from fork to upstream #633

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/adapters/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface IRepoAdapter {

pushRepo(repo: IRepo, force: boolean): Promise<void>;

createPullRequest(repo: IRepo, message: string): Promise<void>;
createPullRequest(repo: IRepo, message: string, upstreamOwner: string): Promise<void>;

getPullRequestStatus(repo: IRepo): Promise<string[]>;

Expand Down
6 changes: 5 additions & 1 deletion src/adapters/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
public abstract createPullRequest(
repo: IRepo,
message: string,
upstreamOwner: string
): Promise<void>;

public abstract getPullRequestStatus(repo: IRepo): Promise<string[]>;

Expand Down
10 changes: 6 additions & 4 deletions src/adapters/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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();
});
});
Expand Down
23 changes: 19 additions & 4 deletions src/adapters/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,22 @@ class GithubAdapter extends GitAdapter {
await super.pushRepo(repo, force || shouldForce);
}

public async createPullRequest(repo: IRepo, message: string): Promise<void> {
public async createPullRequest(
repo: IRepo,
message: string,
upstreamOwner: string
): Promise<void> {
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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -279,6 +290,10 @@ class GithubAdapter extends GitAdapter {
);
}

public getOwnerName(owner: string): string {
return owner;
}

public getBaseBranch(repo: IRepo): string {
return repo.defaultBranch;
}
Expand Down
10 changes: 10 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type CommandHandler = (context: IMigrationContext, options: any) => Promise<void

interface ICliOptions {
repos?: string[];
upstreamOwner: string;
}

const handleCommand =
Expand Down Expand Up @@ -78,6 +79,7 @@ const handleCommand =

// The list of repos will be null if migration hasn't started yet
migrationContext.migration.repos = await loadRepoList(migrationContext);
migrationContext.migration.upstreamOwner = options.upstreamOwner;

await handler(migrationContext, options);
} catch (e: any) {
Expand All @@ -96,10 +98,18 @@ const addReposOption = (command: Command) => {
);
};

const addUpstreamOwnerOption = (command: Command) => {
return command.option(
'--upstreamOwner <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));
};
Expand Down
2 changes: 1 addition & 1 deletion src/commands/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/pr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { generatePrMessageWithFooter } from '../util/generate-pr-message';

export default async (context: IMigrationContext) => {
const {
migration: { spec },
migration: { spec, upstreamOwner },
logger,
} = context;

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/migration-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface IMigrationInfo {
migrationDirectory: string;
workingDirectory: string;
repos: IRepo[] | null;
upstreamOwner: string;
selectedRepos?: IRepo[];
}

Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading