Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

feat: add pull request body #39

Merged
merged 1 commit into from
Nov 30, 2016
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
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Extension {
console.error(e.response);
vscode.window.showErrorMessage('GitHub error: ' + e.message);
} else {
console.error(e);
vscode.window.showErrorMessage('Error: ' + e.message);
}
}
Expand Down
28 changes: 15 additions & 13 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,24 @@ async function getGitHubOwnerAndRepositoryFromGitConfig(cwd: string): Promise<st
}

export async function getCurrentBranch(cwd: string): Promise<string|undefined> {
return execa('git', ['branch'], {cwd})
.then(result => {
const match = result.stdout.match(/^\* (.*)$/m);
return match ? match[1] : undefined;
});
const stdout = (await execa('git', ['branch'], {cwd})).stdout;
const match = stdout.match(/^\* (.*)$/m);
return match ? match[1] : undefined;
}

export async function getCommitMessage(cwd: string): Promise<string> {
return execa('git', ['log', '--oneline', '-1'], {cwd})
.then(result => {
const match = result.stdout.match(/^(?:.+?) (.*)/);
return match ? match[1] : result.stdout;
});
export async function getCommitMessage(sha: string, cwd: string): Promise<string> {
return (await execa('git', ['log', '-n', '1', '--format=%s', sha], {cwd})).stdout.trim();
}

export async function getFirstCommitOnBranch(branch: string, cwd: string): Promise<string> {
return (await execa('git', ['log', '--reverse', '--right-only', '--format=%h',
`origin/master..origin/${branch}`], {cwd})).stdout.trim().split('\n')[0];
}

export async function getCommitBody(sha: string, cwd: string): Promise<string> {
return (await execa('git', ['log', '--format=%b', '-n', '1', sha], {cwd})).stdout.trim();
}

export async function checkout(cwd: string, branch: string): Promise<void> {
return execa('git', ['checkout', branch], {cwd})
.then(() => undefined);
await execa('git', ['checkout', branch], {cwd});
}
16 changes: 14 additions & 2 deletions src/github-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export class GitHubManager {
this.channel = channel;
}

private log(message: string): void {
this.channel.appendLine(message);
console.log(message);
}

get connected(): boolean {
return Boolean(this.github);
}
Expand Down Expand Up @@ -80,10 +85,17 @@ export class GitHubManager {
}
const [owner, repository] = await git.getGitHubOwnerAndRepository(this.cwd);
const branch = await git.getCurrentBranch(this.cwd);
if (!branch) {
throw new Error('No current branch');
}
this.log(`Create pull request on branch ${branch}`);
const firstCommit = await git.getFirstCommitOnBranch(branch, this.cwd);
this.log(`First commit on branch ${firstCommit}`);
const body: CreatePullRequestBody = {
title: await git.getCommitMessage(this.cwd),
title: await git.getCommitMessage(firstCommit, this.cwd),
head: `${owner}:${branch}`,
base: await this.getDefaultBranch()
base: await this.getDefaultBranch(),
body: await git.getCommitBody(firstCommit, this.cwd)
};
this.channel.appendLine('Create pull request:');
this.channel.appendLine(JSON.stringify(body, undefined, ' '));
Expand Down