Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Bugfixes for generate changelog #154

Merged
merged 8 commits into from
Sep 24, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import tl = require("azure-pipelines-task-lib/task");
import ArtifactFilePathFetcher, { ArtifactFilePaths } from "@dxatscale/sfpowerscripts.core/lib/artifacts/ArtifactFilePathFetcher";
import PackageMetadata from "@dxatscale/sfpowerscripts.core/lib/PackageMetadata";
import generateMarkdown from "@dxatscale/sfpowerscripts.core/lib/changelog/GenerateChangelogMarkdown";
import { ReleaseChangelog, Release } from "@dxatscale/sfpowerscripts.core/lib/changelog/interfaces/ReleaseChangelogInterfaces";
import { ReleaseChangelog, Release, Artifact } from "@dxatscale/sfpowerscripts.core/lib/changelog/interfaces/ReleaseChangelogInterfaces";
import { Changelog as PackageChangelog } from "@dxatscale/sfpowerscripts.core/lib/changelog/interfaces/GenericChangelogInterfaces";
import authVCS from "../Common/VersionControlAuth";
import simplegit, { SimpleGit } from "simple-git/promise";
Expand Down Expand Up @@ -43,51 +43,61 @@ async function run() {
artifacts: []
};



if (taskType === "Release") {
// Use artifact type from Release API
console.log(
`Fetching artifacts from artifact directory ${tl.getVariable("system.artifactsDirectory")}`
);
} else {
// Default to Pipeline artifact type for Build task type
console.log(
`Build pipeline detected.`,
`Fetching artifacts from pipeline workspace ${tl.getVariable("pipeline.workspace")}`
);
}

let artifacts_filepaths: ArtifactFilePaths[] = ArtifactFilePathFetcher.fetchArtifactFilePaths(
ArtifactHelper.getArtifactDirectory(ArtifactHelper.getArtifactDirectory(artifactDir))
if (taskType === "Release") {
// Use artifact type from Release API
console.log(
`Fetching artifacts from artifact directory ${tl.getVariable("system.artifactsDirectory")}`
);
} else {
// Default to Pipeline artifact type for Build task type
console.log(
`Build pipeline detected.`,
`Fetching artifacts from pipeline workspace ${tl.getVariable("pipeline.workspace")}`
);
}

for (let artifactFilepaths of artifacts_filepaths) {
let packageMetadata: PackageMetadata = JSON.parse(
fs.readFileSync(artifactFilepaths.packageMetadataFilePath, 'utf8')
);

latestReleaseDefinition["artifacts"].push({
name: packageMetadata["package_name"],
from: undefined,
to: packageMetadata["sourceVersion"]?.slice(0,8) || packageMetadata["sourceVersionTo"]?.slice(0,8),
version: packageMetadata["package_version_number"],
latestCommitId: undefined,
commits: undefined
});
let artifacts_filepaths: ArtifactFilePaths[] = ArtifactFilePathFetcher.fetchArtifactFilePaths(
ArtifactHelper.getArtifactDirectory(artifactDir)
);

if (artifacts_filepaths.length === 0) {
throw new Error(`No artifacts found at ${ArtifactHelper.getArtifactDirectory(artifactDir)}`);
}

let missingChangelogs: Error[] = [];
for (let artifactFilepaths of artifacts_filepaths) {
let packageMetadata: PackageMetadata = JSON.parse(
fs.readFileSync(artifactFilepaths.packageMetadataFilePath, 'utf8')
);

packageChangelogMap[packageMetadata["package_name"]] = artifactFilepaths.changelogFilePath;
let artifact: Artifact = {
name: packageMetadata["package_name"],
from: undefined,
to: packageMetadata["sourceVersion"]?.slice(0,8) || packageMetadata["sourceVersionTo"]?.slice(0,8),
version: packageMetadata["package_version_number"],
latestCommitId: undefined,
commits: undefined
}


console.log("Generating changelog...");
latestReleaseDefinition["artifacts"].push(artifact);

// Check if any packages are missing changelog
Object.keys(packageChangelogMap).forEach( (pkg) => {
if (!fs.existsSync(packageChangelogMap[pkg])) {
throw Error("Artifact is missing changelog. Check build task version compatability");
if (!fs.existsSync(artifactFilepaths.changelogFilePath)) {
missingChangelogs.push(
new Error(`No changelog found in artifact ${packageMetadata["package_name"]} ${packageMetadata["package_version_number"]}`)
);
}
});

packageChangelogMap[packageMetadata["package_name"]] = artifactFilepaths.changelogFilePath;
}

if (missingChangelogs.length > 0) {
throw missingChangelogs;
}

console.log("Generating changelog...");

// Get artifact versions from previous release definition
let releaseChangelog: ReleaseChangelog;
Expand Down Expand Up @@ -126,11 +136,11 @@ async function run() {
commit["commitId"] === prevReleaseLatestCommitId[artifact["name"]]
);
if (fromIdx === -1)
throw Error(`Cannot find commit Id ${prevReleaseLatestCommitId[artifact["name"]]} in ${artifact["name"]} changelog`);
throw new Error(`Cannot find commit Id ${prevReleaseLatestCommitId[artifact["name"]]} in ${artifact["name"]} changelog`);
}

if (fromIdx > 0) {
artifact["commits"] = packageChangelog["commits"].slice(0, fromIdx+1);
artifact["commits"] = packageChangelog["commits"].slice(0, fromIdx);
} else if (fromIdx === 0) {
// Artifact verison has not changed
artifact["commits"] = [];
Expand All @@ -145,7 +155,8 @@ async function run() {
// Compute work items for latest release
let workItemFilter: RegExp = RegExp(tl.getInput("workItemFilter", true), 'gi');
for (let commit of artifact["commits"]) {
let workItems: RegExpMatchArray = commit["body"].match(workItemFilter) || commit["message"].match(workItemFilter);
let commitMessage: String = commit["message"] + "\n" + commit["body"];
let workItems: RegExpMatchArray = commitMessage.match(workItemFilter);
if (workItems) {
for (let item of workItems) {
if (latestReleaseDefinition["workItems"][item] == null) {
Expand Down Expand Up @@ -186,7 +197,7 @@ async function run() {
);

fs.writeFileSync(
path.join(repoTempDir,`releasechangelog.md`),
path.join(repoTempDir,`Release-Changelog.md`),
payload
);

Expand All @@ -206,7 +217,16 @@ async function run() {
} catch (err) {
// Cleanup temp directories
tempDir.removeCallback();
tl.setResult(tl.TaskResult.Failed, err.message);

let errorMessage: string = "";
if (err instanceof Array) {
for (let e of err) {
errorMessage += e.message + `\n`;
}
} else {
errorMessage = err.message;
}
tl.setResult(tl.TaskResult.Failed, errorMessage);
} finally {
tempDir.removeCallback();
tl.setResult(tl.TaskResult.Succeeded, 'Finished');
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sfpowerscripts-generatechangelog-task",
"description": "sfpowerscripts-generatechangelog-task",
"version": "1.0.0",
"version": "1.0.1",
"private": true,
"repository": {
"type": "git",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"version": {
"Major": 1,
"Minor": 0,
"Patch": 0
"Patch": 1
},
"runsOn": [
"Agent"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"name": "target_org",
"aliases": [
"envname"
],
],
"type": "string",
"label": "Alias or username of the target org",
"defaultValue": "scratchorg",
Expand Down Expand Up @@ -97,4 +97,4 @@
"target": "lib/InstallSourcePackageTask/InstallSourcePackageToOrg.js"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"name": "target_org",
"aliases": [
"envname"
],
],
"type": "string",
"label": "Alias or username of the target environment",
"defaultValue": "",
Expand Down
2 changes: 1 addition & 1 deletion packages/azpipelines/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/azpipelines/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@dxatscale/sfpowerscripts.azpipelines",
"private": true,
"version": "17.1.0",
"version": "17.1.1",
"description": "CI/CD extensions for Salesforce",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/azpipelines/vss-extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"id": "sfpowerscripts",
"publisher": "AzlamSalam",
"name": "sfpowerscripts",
"version": "17.1.0",
"version": "17.1.1",
"description": "Azure Pipelines tasks for working with Salesforce",
"tags": [
"Extension",
Expand Down
2 changes: 1 addition & 1 deletion packages/sfpowerscripts-cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sfpowerscripts-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@dxatscale/sfpowerscripts",
"description": "Simple wrappers around sfdx commands to help set up CI/CD quickly",
"version": "0.13.1",
"version": "0.13.2",
"author": "dxatscale",
"bin": {
"readVars": "./scripts/readVars.sh"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { flags, SfdxCommand } from '@salesforce/command';
import { Messages } from '@salesforce/core';
import PackageMetadata from "@dxatscale/sfpowerscripts.core/lib/PackageMetadata";
import { ReleaseChangelog, Release } from "@dxatscale/sfpowerscripts.core/lib/changelog/interfaces/ReleaseChangelogInterfaces";
import { ReleaseChangelog, Release, Artifact } from "@dxatscale/sfpowerscripts.core/lib/changelog/interfaces/ReleaseChangelogInterfaces";
import { Changelog as PackageChangelog } from "@dxatscale/sfpowerscripts.core/lib/changelog/interfaces/GenericChangelogInterfaces";
import generateMarkdown from "@dxatscale/sfpowerscripts.core/lib/changelog/GenerateChangelogMarkdown";
import fs = require("fs-extra");
Expand Down Expand Up @@ -92,6 +92,10 @@ export default class GenerateChangelog extends SfdxCommand {
}
);

if (packageMetadataFilepaths.length === 0) {
throw new Error(`No artifacts found at ${path.resolve(process.cwd(), this.flags.artifactdir)}`);
}

let packageChangelogMap: {[P:string]: string} = {};
let latestReleaseDefinition: Release = {
name: this.flags.releasename,
Expand All @@ -100,31 +104,41 @@ export default class GenerateChangelog extends SfdxCommand {
};

// Read artifacts for latest release definition
let missingChangelogs: Error[] = [];
for (let packageMetadataFilepath of packageMetadataFilepaths ) {

let packageMetadata: PackageMetadata = JSON.parse(fs.readFileSync(packageMetadataFilepath, 'utf8'));
latestReleaseDefinition["artifacts"].push({

let artifact: Artifact = {
name: packageMetadata["package_name"],
from: undefined,
to: packageMetadata["sourceVersion"]?.slice(0,8) || packageMetadata["sourceVersionTo"]?.slice(0,8),
version: packageMetadata["package_version_number"],
latestCommitId: undefined,
commits: undefined
});
}

latestReleaseDefinition["artifacts"].push(artifact);

packageChangelogMap[packageMetadata["package_name"]] = path.join(
let changelogFilepath: string = path.join(
path.dirname(packageMetadataFilepath),
`changelog.json`
);

if (!fs.existsSync(changelogFilepath)) {
missingChangelogs.push(
new Error(`No changelog found in artifact ${packageMetadata["package_name"]} ${packageMetadata["package_version_number"]}`)
);
}

packageChangelogMap[packageMetadata["package_name"]] = changelogFilepath;
}

console.log("Generating changelog...");
if (missingChangelogs.length > 0) {
throw missingChangelogs;
}

// Check if any packages are missing changelog
Object.values(packageChangelogMap).forEach( (changelogPath) => {
if (!fs.existsSync(changelogPath)) {
throw Error("Artifact is missing changelog. Check build task version compatability");
}
});
console.log("Generating changelog...");

// Get artifact versions from previous release definition
let prevReleaseDefinition: Release;
Expand Down Expand Up @@ -162,12 +176,12 @@ export default class GenerateChangelog extends SfdxCommand {
commit["commitId"] === prevReleaseLatestCommitId[artifact["name"]]
);
if (fromIdx === -1)
throw Error(`Cannot find commit Id ${prevReleaseLatestCommitId[artifact["name"]]} in ${artifact["name"]} changelog`);
throw new Error(`Cannot find commit Id ${prevReleaseLatestCommitId[artifact["name"]]} in ${artifact["name"]} changelog`);
}


if (fromIdx > 0) {
artifact["commits"] = packageChangelog["commits"].slice(0, fromIdx+1);
artifact["commits"] = packageChangelog["commits"].slice(0, fromIdx);
} else if (fromIdx === 0) {
// Artifact verison has not changed
artifact["commits"] = [];
Expand All @@ -182,7 +196,8 @@ export default class GenerateChangelog extends SfdxCommand {
// Compute work items for latest release
let workItemFilter: RegExp = RegExp(this.flags.workitemfilter, 'gi');
for (let commit of artifact["commits"]) {
let workItems: RegExpMatchArray = commit["body"].match(workItemFilter) || commit["message"].match(workItemFilter);
let commitMessage: String = commit["message"] + "\n" + commit["body"];
let workItems: RegExpMatchArray = commitMessage.match(workItemFilter);
if (workItems) {
for (let item of workItems) {
if (latestReleaseDefinition["workItems"][item] == null) {
Expand Down Expand Up @@ -218,7 +233,7 @@ export default class GenerateChangelog extends SfdxCommand {

let payload: string = generateMarkdown(releaseChangelog, this.flags.workitemurl, this.flags.limit);
fs.writeFileSync(
path.join(repoTempDir,`releasechangelog.md`),
path.join(repoTempDir,`Release-Changelog.md`),
payload
);

Expand All @@ -236,9 +251,20 @@ export default class GenerateChangelog extends SfdxCommand {
}

} catch (err) {
console.log(err.message);
tempDir.removeCallback();
process.exit(1);

let errorMessage: string = "";
if (err instanceof Array) {
for (let e of err) {
errorMessage += e.message + `\n`;
}
} else {
errorMessage = err.message;
}
console.log(errorMessage);

tempDir.removeCallback();

process.exit(1);
} finally {
tempDir.removeCallback();
console.log(`Successfully generated changelog`);
Expand Down