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

For v2 templates, use the index.json for template versioning and construct the url ourselves #4297

Merged
merged 1 commit into from
Oct 7, 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
4 changes: 1 addition & 3 deletions src/templates/script/PysteinTemplateProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import { AzExtFsExtra, type IActionContext } from '@microsoft/vscode-azext-utils';
import * as path from 'path';
import { ProjectLanguage } from '../../constants';
import { type IBundleMetadata } from '../../funcConfig/host';
import { bundleFeedUtils } from '../../utils/bundleFeedUtils';
import { feedUtils } from '../../utils/feedUtils';
import { verifyTemplateIsV2 } from '../../utils/templateVersionUtils';
Expand All @@ -33,8 +32,7 @@ export class PysteinTemplateProvider extends ScriptBundleTemplateProvider {
protected _language: string;

public async getLatestTemplates(context: IActionContext, latestTemplateVersion: string): Promise<ITemplates> {
const bundleMetadata: IBundleMetadata | undefined = await this.getBundleInfo();
const release: bundleFeedUtils.ITemplatesReleaseV2 = await bundleFeedUtils.getReleaseV2(context, bundleMetadata, latestTemplateVersion);
const release: bundleFeedUtils.ITemplatesReleaseV2 = await bundleFeedUtils.getReleaseV2(latestTemplateVersion);
const language = this.getResourcesLanguage();
const resourcesUrl: string = release.resources.replace('{locale}', language);
const urls: string[] = [release.userPrompts ?? release.bindings, resourcesUrl, release.functions];
Expand Down
2 changes: 1 addition & 1 deletion src/templates/script/ScriptBundleTemplateProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ScriptBundleTemplateProvider extends ScriptTemplateProvider {

public async getLatestTemplateVersion(context: IActionContext): Promise<string> {
const bundleMetadata: IBundleMetadata | undefined = await this.getBundleInfo();
return await bundleFeedUtils.getLatestTemplateVersion(context, bundleMetadata, this.templateSchemaVersion);
return await bundleFeedUtils.getLatestTemplateVersion(context, bundleMetadata);
}

public async getLatestTemplates(context: IActionContext, latestTemplateVersion: string): Promise<ITemplates> {
Expand Down
24 changes: 14 additions & 10 deletions src/utils/bundleFeedUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { type IBundleMetadata, type IHostJsonV2 } from '../funcConfig/host';
import { localize } from '../localize';
import { type IBindingTemplate } from '../templates/IBindingTemplate';
import { type FunctionTemplateBase, type IFunctionTemplate } from '../templates/IFunctionTemplate';
import { type TemplateSchemaVersion } from '../templates/TemplateProviderBase';
import { feedUtils } from './feedUtils';
import { nugetUtils } from './nugetUtils';

Expand Down Expand Up @@ -46,20 +45,19 @@ export namespace bundleFeedUtils {

export interface ITemplatesReleaseV2 extends ITemplatesReleaseBase {
userPrompts: string;
// it is not supposed to exist in the v2 schema, but sometimes userPrompts accidentally gets replaced with bindings
// for v3 runtimes, it still uses bindings for user prompts
bindings?: string;
}

export async function getLatestTemplateVersion(context: IActionContext, bundleMetadata: IBundleMetadata | undefined, templateSchemaVersion: TemplateSchemaVersion): Promise<string> {
export async function getLatestTemplateVersion(context: IActionContext, bundleMetadata: IBundleMetadata | undefined): Promise<string> {
bundleMetadata = bundleMetadata || {};

const feed: IBundleFeed = await getBundleFeed(context, bundleMetadata);
const validVersions: string[] = Object.keys(feed.templates[templateSchemaVersion]).filter((v: string) => !!semver.valid(v));
const versionArray: string[] = await feedUtils.getJsonFeed(context, 'https://aka.ms/azFuncBundleVersions');
const validVersions: string[] = versionArray.filter((v: string) => !!semver.valid(v));
const bundleVersion: string | undefined = nugetUtils.tryGetMaxInRange(bundleMetadata.version || await getLatestVersionRange(context), validVersions);
if (!bundleVersion) {
throw new Error(localize('failedToFindBundleVersion', 'Failed to find bundle version satisfying range "{0}".', bundleMetadata.version));
} else {
return feed.bundleVersions[bundleVersion].templates;
return bundleVersion;
}
}

Expand All @@ -68,9 +66,15 @@ export namespace bundleFeedUtils {
return feed.templates.v1[templateVersion];
}

export async function getReleaseV2(context: IActionContext, bundleMetadata: IBundleMetadata | undefined, templateVersion: string): Promise<ITemplatesReleaseV2> {
const feed: IBundleFeed = await getBundleFeed(context, bundleMetadata);
return feed.templates.v2[templateVersion];
export async function getReleaseV2(templateVersion: string): Promise<ITemplatesReleaseV2> {
// build the url ourselves because the index-v2.json file is no longer publishing version updates for v2 templates
const functionsCdn: string = 'https://functionscdn.azureedge.net/public/ExtensionBundles/Microsoft.Azure.Functions.ExtensionBundle/';
return {
functions: `${functionsCdn}${templateVersion}/StaticContent/v2/templates/templates.json`,
bindings: `${functionsCdn}${templateVersion}/StaticContent/v2/bindings/userPrompts.json`,
userPrompts: `${functionsCdn}${templateVersion}/StaticContent/v2/bindings/userPrompts.json`,
resources: `${functionsCdn}${templateVersion}/StaticContent/v2/resources/Resources.{locale}.json`,
}
}

export function isBundleTemplate(template: FunctionTemplateBase | IBindingTemplate): boolean {
Expand Down
Loading