Skip to content

Commit

Permalink
fix(changelog): add functionality to match release by name
Browse files Browse the repository at this point in the history
When changelog is generated independently, it can result in duplicates created while using the
release command as the hash id never matches as its a full collection opposed to whats in the
ealrlier release. This inturn fixes it, where the  release can be matched by name provided the
artifacts are already part of an existing release
  • Loading branch information
azlam-abdulsalam committed Jul 14, 2023
1 parent 43d7550 commit dc9ca23
Showing 1 changed file with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class ReleaseChangelogUpdater {
private packagesToChangelogFilePaths: { [p: string]: string },
private workItemFilters: string[],
private org: string
) {}
) { }

update(): ReleaseChangelog {
let buildNumber: number;
Expand All @@ -27,8 +27,11 @@ export default class ReleaseChangelogUpdater {

let latestRelease: Release = this.initLatestRelease(this.releaseName, buildNumber, this.artifactsToSfpPackage);

let releaseWithMatchingHashId = this.findRelease(this.releaseChangelog.releases, latestRelease.hashId);
let releaseWithMatchingHashId = this.findRelease(this.releaseChangelog.releases, latestRelease);
if (!releaseWithMatchingHashId) {



let artifactsToLatestCommitId: { [P: string]: string };
if (this.releaseChangelog.releases.length > 0) {
artifactsToLatestCommitId = this.getArtifactsToLatestCommitId(this.releaseChangelog, latestRelease);
Expand Down Expand Up @@ -94,17 +97,51 @@ export default class ReleaseChangelogUpdater {
* @param latestRelease
* @returns
*/
private findRelease(releases: Release[], hashId: string): Release {
private findRelease(releases: Release[], latestRelease: Release): Release | null {
let foundRelease: Release | null = null;

if (releases.length > 0) {
for (let release of releases) {
if (release.hashId === hashId) {
return release;
// First level matching with hashId
foundRelease = releases.find(release => release.hashId === latestRelease.hashId);

// If not found by hashId, proceed to next level matching with names and artifacts
if (foundRelease == null) {
// Create a map for constant time lookup of all release's artifacts
const allArtifacts = new Map<string, string[]>();
for (let release of releases) {
for (let artifact of release.artifacts) {
if (allArtifacts.has(artifact.name)) {
allArtifacts.get(artifact.name).push(artifact.version);
} else {
allArtifacts.set(artifact.name, [artifact.version]);
}
}
}

// Check if all artifacts in the latest release exist in previous releases
let isAllArtifactsAlreadyAvailablePreviously: boolean = true;
for (let artifact of latestRelease.artifacts) {
if (!allArtifacts.has(artifact.name) || !allArtifacts.get(artifact.name).includes(artifact.version)) {
isAllArtifactsAlreadyAvailablePreviously = false;
break;
}
}

// If all artifacts match, check for names
if (isAllArtifactsAlreadyAvailablePreviously) {
for (let release of releases) {
if (release.names.includes(latestRelease.names[0])) {
foundRelease = release;
break;
}
}
}
}
}

return null;
return foundRelease;
}


/**
* Initalise latest release
Expand Down

0 comments on commit dc9ca23

Please sign in to comment.