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

Commit 589ba74

Browse files
committed
fix: handling of headers
headers are set on the Headers object now
1 parent 8409d61 commit 589ba74

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Extension {
2626
this.channel.appendLine('Visual Studio Code GitHub Extension');
2727

2828
this.githubManager = new GitHubManager(this.cwd, this.channel);
29-
this.statusBarManager = new StatusBarManager(context, this.githubManager);
29+
this.statusBarManager = new StatusBarManager(context, this.githubManager, this.channel);
3030

3131
const token = context.globalState.get<string|undefined>('token');
3232
if (token) {

src/github-manager.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,20 @@ export class GitHubManager {
6666
this.channel.appendLine('Create pull request:');
6767
this.channel.appendLine(JSON.stringify(body, undefined, ' '));
6868

69-
const result = await this.github.createPullRequest(owner, repository, body);
70-
// TODO: Pretend should optionally redirect
71-
const number = result.headers['location'][0]
72-
.match(/https:\/\/api.github.com\/repos\/[^\/]+\/[^\/]+\/pulls\/([0-9]+)/) as RegExpMatchArray;
73-
return (await this.github.getPullRequest(owner, repository, parseInt(number[1] as string, 10))).body;
69+
try {
70+
const result = await this.github.createPullRequest(owner, repository, body);
71+
// TODO: Pretend should optionally redirect
72+
const number = result.headers['location'][0]
73+
.match(/https:\/\/api.github.com\/repos\/[^\/]+\/[^\/]+\/pulls\/([0-9]+)/) as RegExpMatchArray;
74+
return (await this.github.getPullRequest(owner, repository, parseInt(number[1] as string, 10))).body;
75+
} catch (e) {
76+
if (e instanceof GitHubError) {
77+
console.log(e);
78+
this.channel.appendLine('Create pull request error:');
79+
this.channel.appendLine(JSON.stringify(e.response, undefined, ' '));
80+
}
81+
throw e;
82+
}
7483
}
7584

7685
public async listPullRequests(): Promise<PullRequest[]> {

src/github.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,7 @@ namespace impl {
118118
const entry = cache.get(request.url);
119119
if (entry) {
120120
// When we have a cache hit, send etag
121-
if (!request.options.headers) {
122-
request.options.headers = {};
123-
}
124-
request.options.headers['If-None-Match'] = entry.etag;
121+
(request.options.headers as any).set('If-None-Match', entry.etag);
125122
}
126123
const response = await chain(request);
127124
if (!entry || response.status !== 304) {
@@ -139,10 +136,7 @@ namespace impl {
139136

140137
export function githubTokenAuthenticator(token: string): IPretendRequestInterceptor {
141138
return request => {
142-
if (!request.options.headers) {
143-
request.options.headers = {};
144-
}
145-
request.options.headers['Authorization'] = `token ${token}`;
139+
(request.options.headers as any).set('Authorization', `token ${token}`);
146140
return request;
147141
};
148142
}

src/status-bar-manager.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import {PullRequest, PullRequestStatus} from './github';
2+
import {GitHubError, PullRequest, PullRequestStatus} from './github';
33
import {GitHubManager} from './github-manager';
44

55
const colors = {
@@ -15,8 +15,11 @@ export class StatusBarManager {
1515

1616
private githubManager: GitHubManager;
1717

18-
constructor(context: vscode.ExtensionContext, githubManager: GitHubManager) {
18+
private channel: vscode.OutputChannel;
19+
20+
constructor(context: vscode.ExtensionContext, githubManager: GitHubManager, channel: vscode.OutputChannel) {
1921
this.githubManager = githubManager;
22+
this.channel = channel;
2023

2124
this.statusBar = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
2225
this.statusBar.command = '';
@@ -35,17 +38,26 @@ export class StatusBarManager {
3538
}
3639

3740
public async updatePullRequestStatus(): Promise<void> {
38-
const pullRequest = await this.githubManager.getPullRequestForCurrentBranch();
39-
this.statusBar.show();
40-
if (pullRequest) {
41-
const status = await this.calculateMergableStatus(pullRequest);
42-
this.statusBar.color = colors[status];
43-
this.statusBar.tooltip = status === 'success' ? `Merge pull-request #${pullRequest.number}` : '';
44-
this.statusBar.command = status === 'success' ? 'extension.mergePullRequest' : '';
45-
} else {
46-
this.statusBar.color = colors.none;
47-
this.statusBar.tooltip = 'Create pull-request for current branch';
48-
this.statusBar.command = 'extension.createPullRequest';
41+
try {
42+
const pullRequest = await this.githubManager.getPullRequestForCurrentBranch();
43+
this.statusBar.show();
44+
if (pullRequest) {
45+
const status = await this.calculateMergableStatus(pullRequest);
46+
this.statusBar.color = colors[status];
47+
this.statusBar.tooltip = status === 'success' ? `Merge pull-request #${pullRequest.number}` : '';
48+
this.statusBar.command = status === 'success' ? 'extension.mergePullRequest' : '';
49+
} else {
50+
this.statusBar.color = colors.none;
51+
this.statusBar.tooltip = 'Create pull-request for current branch';
52+
this.statusBar.command = 'extension.createPullRequest';
53+
}
54+
} catch (e) {
55+
if (e instanceof GitHubError) {
56+
console.log(e);
57+
this.channel.appendLine('Update pull request status error:');
58+
this.channel.appendLine(JSON.stringify(e.response, undefined, ' '));
59+
}
60+
throw e;
4961
}
5062
}
5163

0 commit comments

Comments
 (0)