Skip to content

Commit

Permalink
feat: added options to create pr from fork to upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
kavitha186 authored and aorinevo committed Jan 10, 2024
1 parent 7a2a3a7 commit bc91a5c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 11 deletions.
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
2 changes: 1 addition & 1 deletion src/adapters/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>;
public abstract createPullRequest(repo: IRepo, message: string, upstreamOwner: string): Promise<void>;

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

Expand Down
6 changes: 3 additions & 3 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 Down Expand Up @@ -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,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();
});
});
Expand Down
19 changes: 15 additions & 4 deletions src/adapters/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,18 @@ 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 +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,
Expand All @@ -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,
Expand Down Expand Up @@ -279,6 +286,10 @@ class GithubAdapter extends GitAdapter {
);
}

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

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

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

const handleCommand =
Expand Down Expand Up @@ -76,6 +77,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 @@ -94,10 +96,15 @@ const addReposOption = (command: program.Command) => {
);
};

const addUpstreamOwnerOption = (command: program.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
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

0 comments on commit bc91a5c

Please sign in to comment.