Skip to content

Commit

Permalink
Merge pull request #645 from rfennell/issue644-xplat-relnote-add-hand…
Browse files Browse the repository at this point in the history
…lebars-libs

Add support for handlebar-helpers and custom handlebars extensions
  • Loading branch information
rfennell authored Mar 11, 2020
2 parents fd03070 + e54b592 commit 1dab4cf
Show file tree
Hide file tree
Showing 6 changed files with 1,848 additions and 45 deletions.
17 changes: 16 additions & 1 deletion Extensions/XplatGenerateReleaseNotes/V2/GenerateReleaseNotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ async function run(): Promise<number> {

var stopOnRedeploy = tl.getBoolInput("stopOnRedeploy");
var sortWi = tl.getBoolInput("SortWi");
var customHandlebarsExtensionCode = tl.getInput("customHandlebarsExtensionCode");
var customHandlebarsExtensionFile = tl.getInput("customHandlebarsExtensionFile");
var customHandlebarsExtensionFolder = tl.getInput("customHandlebarsExtensionFolder");

let credentialHandler: vstsInterfaces.IRequestHandler = util.getCredentialHandler();
let vsts = new webApi.WebApi(tpcUri, credentialHandler);
Expand Down Expand Up @@ -274,7 +277,19 @@ async function run(): Promise<number> {
currentBuild = await buildApi.getBuild(buildId);

var template = util.getTemplate (templateLocation, templateFile, inlineTemplate);
var outputString = util.processTemplate(template, fullWorkItems, globalCommits, currentBuild, currentRelease, mostRecentSuccessfulDeploymentRelease, emptyDataset, delimiter, fieldEquality, anyFieldContent);
var outputString = util.processTemplate(
template,
fullWorkItems,
globalCommits,
currentBuild,
currentRelease,
mostRecentSuccessfulDeploymentRelease,
emptyDataset,
delimiter,
fieldEquality,
anyFieldContent,
customHandlebarsExtensionCode);

util.writeFile(outputfile, outputString);

agentApi.writeVariable(outputVariableName, outputString.toString());
Expand Down
32 changes: 29 additions & 3 deletions Extensions/XplatGenerateReleaseNotes/V2/ReleaseNotesFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { GitCommit } from "vso-node-api/interfaces/GitInterfaces";
import { HttpClient } from "typed-rest-client/HttpClient";
import { WorkItem } from "vso-node-api/interfaces/WorkItemTrackingInterfaces";
import { type } from "os";
const Handlebars = require("handlebars");

let agentApi = new AgentSpecificApi();

Expand Down Expand Up @@ -174,7 +173,18 @@ export function getTemplate(
}

// The Argument compareReleaseDetails is used in the template processing. Renaming or removing will break the templates
export function processTemplate(template, workItems: WorkItem[], commits: Change[], buildDetails: Build, releaseDetails: Release, compareReleaseDetails: Release, emptySetText, delimiter, fieldEquality, anyFieldContent): string {
export function processTemplate(
template,
workItems: WorkItem[],
commits: Change[],
buildDetails: Build,
releaseDetails: Release,
compareReleaseDetails: Release,
emptySetText,
delimiter,
fieldEquality,
anyFieldContent,
customHandlebarsExtensionCode): string {

var widetail = undefined;
var csdetail = undefined;
Expand Down Expand Up @@ -434,9 +444,25 @@ export function processTemplate(template, workItems: WorkItem[], commits: Change
} else {
// it is a handlebar template
agentApi.logDebug("Processing handlebar template");
const handlebars = require("handlebars");
// load the extension library so it can be accessed in templates
agentApi.logInfo("Loading handlebars-helpers extension");
const helpers = require("handlebars-helpers")({
handlebars: handlebars
});

var customHandlebarsExtensionFile = "customHandlebarsExtension";
var customHandlebarsExtensionFolder = process.env.Agent_TempDirectory;
if (typeof customHandlebarsExtensionCode !== undefined && customHandlebarsExtensionCode && customHandlebarsExtensionCode.length > 0) {
agentApi.logInfo("Loading custom handlebars extension");
writeFile(`${customHandlebarsExtensionFolder}/${customHandlebarsExtensionFile}.js`, customHandlebarsExtensionCode);
var tools = require(`${customHandlebarsExtensionFolder}/${customHandlebarsExtensionFile}`);
handlebars.registerHelper(tools);
}

// compile the template
var handlebarsTemplate = Handlebars.compile(template);
var handlebarsTemplate = handlebars.compile(template);

// execute the compiled template
output = handlebarsTemplate({ "workItems": workItems, "commits": commits, "buildDetails": buildDetails, "releaseDetails": releaseDetails, "compareReleaseDetails": compareReleaseDetails });
}
Expand Down
Loading

0 comments on commit 1dab4cf

Please sign in to comment.