Skip to content

Commit

Permalink
feat: select auth method when exporting to github
Browse files Browse the repository at this point in the history
  • Loading branch information
senyai committed May 11, 2024
1 parent ecc0746 commit 294eef4
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 80 deletions.
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
211 changes: 139 additions & 72 deletions src/test/suite/gitExportSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class GitExportTestHelper {
gitUrl?: string;
configJson?: string;
createForAuthenticatedUser?: 'valid' | 'already exists' | 'error';
withToken?: boolean;
} = {}
) {
options = {
Expand All @@ -75,6 +76,7 @@ class GitExportTestHelper {
userName: 'mr. Test',
orgDescription: 'the great',
createForAuthenticatedUser: 'valid',
withToken: true,
...options,
};
this.sod = sandbox
Expand Down Expand Up @@ -156,6 +158,30 @@ class GitExportTestHelper {
return;
})()
);
})
.onCall(3)
.callsFake(items => {
assert.ok(items instanceof Array);
assert.equal(items.length, 2);
assert.equal(
items[0].label,
'$(github) Use https url with token'
);
assert.equal(
items[1].label,
'$(key) Use git url without token'
);
return Promise.resolve(
(() => {
switch (options.withToken) {
case true:
return items[0];
case false:
return items[1];
}
return;
})()
);
});

const fakeSession: AuthenticationSession = {
Expand Down Expand Up @@ -301,7 +327,8 @@ class GitExportTestHelper {
params: RestEndpointMethodTypes['repos']['createForAuthenticatedUser']['parameters']
) => ({
data: {
html_url: `https://examplegit.com/${params.name}`,
html_url: `https://examplegit.com/theuser/${params.name}`,
git_url: `git:examplegit.com/theuser/${params.name}.git`,
},
})
);
Expand Down Expand Up @@ -336,7 +363,86 @@ class GitExportTestHelper {
}
}

export function GetExportSuite(this: Suite): void {
function GitCancelSuite(this: Suite): void {
test('Cancel export directory selection', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
exportDirectory: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
});

test('Cancel repository name', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
repositoryName: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledOnce(helper.sib);
});

test('Cancel export git url', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
destination: '$(globe) Export using git url',
gitUrl: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledTwice(helper.sib);
});

test('Cancel destination (github/git)', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
destination: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledOnce(helper.sib);
sinon.assert.calledOnce(helper.sqp);
});

test('Cancel organization (github/git)', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
organization: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledOnce(helper.sib);
sinon.assert.calledTwice(helper.sqp);
});

test('Cancel description (github/git)', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
repositoryDescription: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledTwice(helper.sib);
sinon.assert.calledTwice(helper.sqp);
});

test('Cancel private (github/git)', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
private: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledTwice(helper.sib);
sinon.assert.calledThrice(helper.sqp);
});

test('Cancel auth type (github/git)', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
withToken: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledTwice(helper.sib);
sinon.assert.callCount(helper.sqp, 4);
});
}

export function GitExportSuite(this: Suite): void {
test('No session', async () => {
// warning! must be first test
const helper = new GitExportTestHelper(this.ctx.sandbox);
Expand All @@ -362,13 +468,39 @@ export function GetExportSuite(this: Suite): void {
const helper = new GitExportTestHelper(this.ctx.sandbox);
const term = helper.fakeTerminal();
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledThrice(helper.sqp);
sinon.assert.callCount(helper.sqp, 4);
sinon.assert.calledOnce(term.mkdir);
sinon.assert.calledOnce(term.odct);
sinon.assert.calledOnce(term.fakeDisposable.dispose);
sinon.assert.calledOnce(
helper.fakeOctokit.repos.createForAuthenticatedUser
);
sinon.assert.calledOnceWithExactly(
term.fakeTerminal.sendText,
' fossil git export /tmp/gitExport/spn --mainbranch main ' +
'--autopush https://fakeAccountLabel:fakeAccessToken@' +
'examplegit.com/theuser/spn'
);
});

test('Publish repository to github without token', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
withToken: false,
});
const term = helper.fakeTerminal();
await commands.executeCommand('fossil.gitPublish');
sinon.assert.callCount(helper.sqp, 4);
sinon.assert.calledOnce(term.mkdir);
sinon.assert.calledOnce(term.odct);
sinon.assert.calledOnce(term.fakeDisposable.dispose);
sinon.assert.calledOnce(
helper.fakeOctokit.repos.createForAuthenticatedUser
);
sinon.assert.calledOnceWithExactly(
term.fakeTerminal.sendText,
' fossil git export /tmp/gitExport/spn --mainbranch main ' +
'--autopush git:examplegit.com/theuser/spn.git'
);
});

test('Publish repository to git', async () => {
Expand Down Expand Up @@ -399,7 +531,7 @@ export function GetExportSuite(this: Suite): void {
});
const term = helper.fakeTerminal();
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledThrice(helper.sqp);
sinon.assert.callCount(helper.sqp, 4);
sinon.assert.calledOnce(term.mkdir);
sinon.assert.calledOnce(term.odct);
sinon.assert.calledOnce(term.fakeDisposable.dispose);
Expand All @@ -413,7 +545,7 @@ export function GetExportSuite(this: Suite): void {
const term = helper.fakeTerminal();
const sem = helper.stubShowErrorMessage();
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledThrice(helper.sqp);
sinon.assert.callCount(helper.sqp, 4);
sinon.assert.calledOnce(term.mkdir);
sinon.assert.calledOnce(term.odct);
sinon.assert.calledOnce(term.fakeDisposable.dispose);
Expand Down Expand Up @@ -458,73 +590,6 @@ export function GetExportSuite(this: Suite): void {
);
});

test('Cancel export directory selection', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
exportDirectory: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
});

test('Cancel repository name', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
repositoryName: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledOnce(helper.sib);
});

test('Cancel export git url', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
destination: '$(globe) Export using git url',
gitUrl: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledTwice(helper.sib);
});

test('Cancel destination (github/git)', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
destination: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledOnce(helper.sib);
sinon.assert.calledOnce(helper.sqp);
});

test('Cancel organization (github/git)', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
organization: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledOnce(helper.sib);
sinon.assert.calledTwice(helper.sqp);
});

test('Cancel description (github/git)', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
repositoryDescription: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledTwice(helper.sib);
sinon.assert.calledTwice(helper.sqp);
});

test('Cancel private (github/git)', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
private: undefined,
});
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnce(helper.sod);
sinon.assert.calledTwice(helper.sib);
sinon.assert.calledThrice(helper.sqp);
});

test('Full project name can be used', async () => {
const helper = new GitExportTestHelper(this.ctx.sandbox, {
repositoryName: undefined,
Expand All @@ -533,4 +598,6 @@ export function GetExportSuite(this: Suite): void {
await commands.executeCommand('fossil.gitPublish');
sinon.assert.calledOnceWithMatch(helper.sib, { value: 'pn' });
});

suite('Cancel', GitCancelSuite);
}

0 comments on commit 294eef4

Please sign in to comment.