Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select auth method when exporting to github #182

Merged
merged 5 commits into from
May 11, 2024
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
299 changes: 138 additions & 161 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1139,16 +1139,16 @@
},
"devDependencies": {
"@microsoft/eslint-formatter-sarif": "^3.0.0",
"@octokit/rest": "^20.1.1",
"@types/mocha": "~10.0.1",
"@types/node": "^16.18.32",
"@types/node": "^18",
"@types/sinon": "~17.0.3",
"@types/vscode": "^1.36.0",
"@types/vscode-webview": "^1.57.0",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/parser": "^7.6.0",
"@vscode/test-electron": "~2.3.9",
"@vscode/vsce": "~2.26.0",
"@octokit/rest": "~20.1.0",
"@vscode/vsce": "~2.26.1",
"c8": "^8.0.1",
"esbuild": "~0.20.2",
"eslint": "^8.52.0",
Expand Down
35 changes: 23 additions & 12 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { done } from './util';
export function memoize<Return>(
target: any,
key: string,
descriptor: PropertyDescriptor
descriptor: TypedPropertyDescriptor<Return>
) {
const memoizeKey = `$memoize$${key}`;
const fn = descriptor.get!;
Expand Down Expand Up @@ -73,7 +73,7 @@ export function memoize<Return>(

// return trigger;
// }
export function throttle(
export function throttle<Args extends any[], T>(
target: any,
key: string,
descriptor: PropertyDescriptor
Expand All @@ -83,9 +83,9 @@ export function throttle(
const fn = descriptor.value!;

descriptor.value = function (
this: Record<string, Promise<any> | undefined>,
...args: any
): Promise<any> {
this: Record<string, Promise<T> | undefined>,
...args: Args
): Promise<T> {
if (this[nextKey]) {
return this[nextKey]!;
}
Expand All @@ -109,7 +109,7 @@ export function throttle(
}

// Make sure asynchronous functions are called one after another.
// type ThisPromise = Record<string, Promise<any>>;
type ThisPromise = Record<string, Promise<any>>;

// export function sequentialize<Args extends any[]>(
// target: (this: ThisPromise, ...args: Args) => Promise<any>,
Expand All @@ -126,15 +126,20 @@ export function throttle(
// };
// }

export function sequentialize(
export function sequentialize<Args extends any[]>(
target: any,
key: string,
descriptor: PropertyDescriptor
descriptor: TypedPropertyDescriptor<
(this: ThisPromise, ...args: Args) => Promise<any>
>
) {
const currentKey = `$s11e$${key}`; // sequentialize
const fn = descriptor.value!;

descriptor.value = function (this: any, ...args: any): Promise<any> {
descriptor.value = function (
this: ThisPromise,
...args: Args
): Promise<any> {
const currentPromise =
(this[currentKey] as Promise<any>) || Promise.resolve(null);
const run = async () => await fn.apply(this, args);
Expand All @@ -143,7 +148,7 @@ export function sequentialize(
};
}

// type ThisTimer = Record<string, ReturnType<typeof setTimeout>>;
type ThisTimer = Record<string, ReturnType<typeof setTimeout>>;

// export function debounce(delay: number) {
// return function <Args extends any[]>(
Expand All @@ -159,11 +164,17 @@ export function sequentialize(
// };
// }
export function debounce(delay: number) {
return function (target: any, key: string, descriptor: PropertyDescriptor) {
return function (
target: any,
key: string,
descriptor: TypedPropertyDescriptor<
(this: ThisTimer, ...args: []) => void
>
) {
const timerKey = `$d6e$${key}`; // debounce
const fn = descriptor.value!;

descriptor.value = function (this: any, ...args: any): void {
descriptor.value = function (this: ThisTimer, ...args: []): void {
clearTimeout(this[timerKey]);
this[timerKey] = setTimeout(() => fn.apply(this, args), delay);
};
Expand Down
38 changes: 31 additions & 7 deletions src/gitExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ export async function inputExportOptions(
description,
private: selectedPrivacy === privateItem,
};
let response: { data: { html_url: string } };
type Response =
| Awaited<ReturnType<typeof octokit.repos.createForAuthenticatedUser>>
| Awaited<ReturnType<typeof octokit.repos.createInOrg>>
| Awaited<ReturnType<typeof octokit.repos.get>>;
let response: Response;
try {
if (orgToUse === userItem) {
response = await octokit.repos.createForAuthenticatedUser(
Expand Down Expand Up @@ -272,13 +276,33 @@ export async function inputExportOptions(
return;
}
}
// add token to url
const remoteUri = Uri.parse(response.data.html_url) as AutoPushURISafe;
const remoteUriWithToken = remoteUri.with({
authority: `${session.account.label}:${session.accessToken}@${remoteUri.authority}`,
}) as AutoPushURIUnsafe;

return { path: exportPath, url: remoteUri, urlUnsafe: remoteUriWithToken };
// ask: auth
const withToken = {
label: '$(github) Use https url with token',
};
const withGit = {
label: '$(key) Use git url without token',
};
const auth = await window.showQuickPick([withToken, withGit]);
if (!auth) {
return;
}

let url: AutoPushURISafe;
let urlUnsafe: AutoPushURIUnsafe;
if (auth === withToken) {
// add token to url
url = Uri.parse(response.data.html_url) as AutoPushURISafe;
urlUnsafe = url.with({
authority: `${session.account.label}:${session.accessToken}@${url.authority}`,
}) as AutoPushURIUnsafe;
} else {
urlUnsafe = (url = Uri.parse(
response.data.git_url
) as AutoPushURISafe) as unknown as AutoPushURIUnsafe;
}
return { path: exportPath, url, urlUnsafe };
}

export async function exportGit(
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { PatchSuite, StageSuite, StashSuite, UpdateSuite } from './stateSuite';
import { RenameSuite } from './renameSuite';
import { BranchSuite } from './branchSuite';
import { RevertSuite } from './revertSuite';
import { GetExportSuite as GitExportSuite } from './gitExportSuite';
import { GitExportSuite } from './gitExportSuite';

suite('Fossil.OpenedRepo', function (this: Suite) {
this.ctx.sandbox = sinon.createSandbox();
Expand Down
Loading