diff --git a/src/extension.ts b/src/extension.ts index 98de4cf6..102b2d2a 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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); } } diff --git a/src/git.ts b/src/git.ts index f6626464..f0dea5cf 100644 --- a/src/git.ts +++ b/src/git.ts @@ -42,22 +42,24 @@ async function getGitHubOwnerAndRepositoryFromGitConfig(cwd: string): Promise { - 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 { - 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 { + return (await execa('git', ['log', '-n', '1', '--format=%s', sha], {cwd})).stdout.trim(); +} + +export async function getFirstCommitOnBranch(branch: string, cwd: string): Promise { + 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 { + return (await execa('git', ['log', '--format=%b', '-n', '1', sha], {cwd})).stdout.trim(); } export async function checkout(cwd: string, branch: string): Promise { - return execa('git', ['checkout', branch], {cwd}) - .then(() => undefined); + await execa('git', ['checkout', branch], {cwd}); } diff --git a/src/github-manager.ts b/src/github-manager.ts index 332e5f94..1d01860a 100644 --- a/src/github-manager.ts +++ b/src/github-manager.ts @@ -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); } @@ -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, ' '));