Skip to content

Commit

Permalink
Pull request feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lszomoru committed Apr 18, 2024
1 parent 61223f3 commit e375bbf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ module.exports = function (argv: string[]): void {
process.env['VSCE_PAT']
)
.option('--azure-credential', 'Use Microsoft Entra ID for authentication')
.action((name, { pat, azureCredential }) => main(verifyPat(pat, azureCredential, name)));
.action((publisherName, { pat, azureCredential }) => main(verifyPat({ publisherName, pat, azureCredential })));

program
.command('show <extensionid>')
Expand Down
4 changes: 2 additions & 2 deletions src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as semver from 'semver';
import { ExtensionQueryFlags, PublishedExtension } from 'azure-devops-node-api/interfaces/GalleryInterfaces';
import { pack, readManifest, versionBump, prepublish } from './package';
import * as tmp from 'tmp';
import { getPublisher } from './store';
import { IVerifyPatOptions, getPublisher } from './store';
import { getGalleryAPI, read, getPublishedUrl, log, getHubUrl, patchOptionsWithManifest, getAzureCredentialAccessToken } from './util';
import { Manifest } from './manifest';
import { readVSIXPackage } from './zip';
Expand Down Expand Up @@ -314,7 +314,7 @@ function validateMarketplaceRequirements(manifest: Manifest, options: IInternalP
}
}

export async function getPAT(publisher: string, options: IPublishOptions | IUnpublishOptions): Promise<string> {
export async function getPAT(publisher: string, options: IPublishOptions | IUnpublishOptions | IVerifyPatOptions): Promise<string> {
if (options.pat) {
return options.pat;
}
Expand Down
32 changes: 12 additions & 20 deletions src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { homedir } from 'os';
import { read, getGalleryAPI, getSecurityRolesAPI, log, getMarketplaceUrl, getAzureCredentialAccessToken } from './util';
import { validatePublisher } from './validation';
import { readManifest } from './package';
import { getPAT } from './publish';

export interface IPublisher {
readonly name: string;
Expand Down Expand Up @@ -39,7 +40,7 @@ export class FileStore implements IStore {
return this.publishers.length;
}

private constructor(readonly path: string, private publishers: IPublisher[]) {}
private constructor(readonly path: string, private publishers: IPublisher[]) { }

private async save(): Promise<void> {
await fs.promises.writeFile(this.path, JSON.stringify({ publishers: this.publishers }), { mode: '0600' });
Expand Down Expand Up @@ -92,7 +93,7 @@ export class KeytarStore implements IStore {
private readonly keytar: typeof import('keytar'),
private readonly serviceName: string,
private publishers: IPublisher[]
) {}
) { }

get(name: string): IPublisher {
return this.publishers.filter(p => p.name === name)[0];
Expand All @@ -113,24 +114,15 @@ export class KeytarStore implements IStore {
}
}

export async function verifyPat(pat: string, azureCredential?: boolean, publisherName?: string): Promise<void> {
if (!pat && !azureCredential) {
throw new Error('The Personal Access Token or the `--azure-credential` option is mandatory.');
}

if (azureCredential) {
pat = await getAzureCredentialAccessToken();
}
export interface IVerifyPatOptions {
readonly publisherName?: string;
readonly pat?: string;
readonly azureCredential?: boolean;
}

if (!publisherName) {
try {
publisherName = (await readManifest()).publisher;
} catch (error) {
throw new Error(
`Can not read the publisher's name. Either supply it as an argument or run vsce from the extension folder. Additional information:\n\n${error}`
);
}
}
export async function verifyPat(options: IVerifyPatOptions): Promise<void> {
const publisherName = options.publisherName ?? (await readManifest()).publisher;
const pat = await getPAT(publisherName, options);

try {
// If the caller of the `getRoleAssignments` API has any of the roles
Expand All @@ -149,7 +141,7 @@ async function requestPAT(publisherName: string): Promise<string> {
console.log(`${getMarketplaceUrl()}/manage/publishers/`);

const pat = await read(`Personal Access Token for publisher '${publisherName}':`, { silent: true, replace: '*' });
await verifyPat(pat, false, publisherName);
await verifyPat({ publisherName, pat });
return pat;
}

Expand Down

0 comments on commit e375bbf

Please sign in to comment.