Skip to content

Commit 95f969e

Browse files
committed
Handle remote creation of repository.
1 parent 465385a commit 95f969e

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/service/githubService.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use strict";
22

33
import * as GitHubApi from "@octokit/rest";
4+
import { ReposCreateParams } from "@octokit/rest";
45
import * as HttpsProxyAgent from "https-proxy-agent";
56
import * as vscode from "vscode";
67
import { File } from "./fileService";
@@ -154,4 +155,10 @@ export class GitHubService {
154155
await this.github.gists.edit(gistObject);
155156
return true;
156157
}
158+
159+
public async MakeRepository(
160+
options: GitHubApi.ReposCreateParams
161+
): Promise<GitHubApi.Response<GitHubApi.ReposCreateResponse>> {
162+
return await this.github.repos.create(options);
163+
}
157164
}

src/sync.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,55 @@ export class Sync {
262262
await localgit.Add(allSettingFiles);
263263
await localgit.Commit(dateNow.toString());
264264
if (syncSetting.repoUrl && customSettings.repoPush) {
265-
await localgit.Push();
265+
try {
266+
await localgit.Push();
267+
} catch (err) {
268+
if (err.message.indexOf("Repository not found")) {
269+
if (customSettings.token === "") {
270+
if (customSettings.openTokenLink) {
271+
vscode.commands.executeCommand(
272+
"vscode.open",
273+
vscode.Uri.parse("https://github.com/settings/tokens")
274+
);
275+
}
276+
await common.GetTokenAndSave(customSettings);
277+
}
278+
279+
const repoName = (
280+
(await vscode.window.showInputBox({
281+
placeHolder:
282+
"Enter repository name, for ex. vscode-settings",
283+
password: false,
284+
prompt:
285+
"Enter name for repository which will be created on your account before changes will be pushed.",
286+
ignoreFocusOut: true
287+
})) || ""
288+
).trim();
289+
290+
const uriFormat = (
291+
(await vscode.window.showQuickPick(["ssh", "https"], {
292+
ignoreFocusOut: true
293+
})) || ""
294+
).trim();
295+
296+
if (uriFormat !== "") {
297+
const res = await github.MakeRepository({
298+
name: repoName
299+
});
300+
301+
syncSetting.repoUrl =
302+
uriFormat === "https"
303+
? res.data.html_url
304+
: res.data.ssh_url;
305+
await common.SaveSettings(syncSetting);
306+
307+
await localgit.Init(syncSetting.repoUrl);
308+
await localgit.Push();
309+
}
310+
} else {
311+
throw err;
312+
}
313+
}
266314
}
267315
completed = true;
268316
} else {

0 commit comments

Comments
 (0)