Skip to content

Commit

Permalink
WIP: Legger til funksjonalitet for fjerning av eldre pakker i m2-repo
Browse files Browse the repository at this point in the history
  • Loading branch information
selfjell committed Oct 31, 2024
1 parent 5a525e9 commit 8d0d15c
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 1 deletion.
7 changes: 7 additions & 0 deletions __tests__/filterM2Repo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { filterM2Repo } from "../src/filterM2repo";

describe("test action", () => {
test("Should find latest version and keep", () => {
filterM2Repo();
});
});
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ inputs:
description: 'Whether to force path style URLs for S3 objects.'
required: false
default: 'false'
prune-m2-before-save:
description: 'Wheter to remove older packages from m2-repository before saving cache in S3'
required: false
default: 'false'
outputs:
cache-hit:
description: 'A boolean value to indicate an exact match was found for the primary key'
Expand Down
4 changes: 4 additions & 0 deletions save/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ inputs:
description: 'An optional boolean when enabled, allows windows runners to save caches that can be restored on other platforms'
default: 'false'
required: false
prune-m2-before-save:
description: 'Wheter to remove older packages from m2-repository before saving cache in S3'
required: false
default: 'false'
runs:
using: 'node16'
main: '../dist/save-only/index.js'
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export enum Inputs {
AWSRegion = "aws-region",
AWSEndpoint = "aws-endpoint",
AWSS3BucketEndpoint = "aws-s3-bucket-endpoint",
AWSS3ForcePathStyle = "aws-s3-force-path-style"
AWSS3ForcePathStyle = "aws-s3-force-path-style",
PruneM2BeforeSave = "prune-m2-before-save"
}

export enum Outputs {
Expand Down
61 changes: 61 additions & 0 deletions src/filterM2repo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable prettier/prettier */
import core from "@actions/core";
import { execSync } from "node:child_process";
import * as fs from "node:fs";
import path from "node:path";

export async function filterM2Repo() {
try {
execSync("cd ~/.m2/repository");
} catch (e) {
core.error("Could not change to m2-repo directory. Skipping..");
// fail gracefully
return;
}

const packagesWithVersions = recursivelyGetPackagesVersions(path.join(process.env.HOME!, '.m2/repository'));
packagesWithVersions.forEach((fileNames, parentFolder) => {
const sortedVersions = fileNames.sort((a: string, b: string) =>
a.localeCompare(b, undefined, { numeric: true })
)
const latest = sortedVersions.pop();
core.debug(`Newest version of ${parentFolder}: ${latest}`)

sortedVersions.forEach((fileName: string) => {
core.debug(`Deleting ${fileName} from ${parentFolder}`);
execSync(`echo ${path.join(parentFolder, fileName)}`) // TODO: replace echo with rm -rf when mocking is properly implemented
});
})
}

const recursivelyGetPackagesVersions = (pathSoFar: string) => {
const outMap: Map<string, string[]> = new Map();
fs.readdirSync(pathSoFar, { withFileTypes: true }).forEach(file => {
if (file.isDirectory()) {
// base case: version numbers in directory names
if (file.name.match(/.*\d+\.\d+.*/)?.length ?? 0 > 0) {
if (outMap.has(pathSoFar)) {
outMap.set(pathSoFar, [
...outMap.get(pathSoFar)!,
file.name
]);
} else {
outMap.set(pathSoFar, [file.name]);
}
} else {
recursivelyGetPackagesVersions(
path.join(pathSoFar, file.name)
)
.forEach((v: string[], k: string) => {
if(outMap.has(k)) {
outMap.set(k, [...outMap.get(k)!, ...v])
} else {
outMap.set(k, v)
}
})
}
} // no else: if files are not directories we are too deep
});
return outMap;
};

6 changes: 6 additions & 0 deletions src/saveImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as core from "@actions/core";
import { Events, Inputs, State } from "./constants";
import { IStateProvider } from "./stateProvider";
import * as utils from "./utils/actionUtils";
import { filterM2Repo } from './filterM2repo'

// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
Expand Down Expand Up @@ -56,6 +57,11 @@ async function saveImpl(stateProvider: IStateProvider): Promise<number | void> {
s3BucketName = s3BucketName.split(".")[0];
}

const pruneCache = core.getInput(Inputs.PruneM2BeforeSave);
if (pruneCache === "true") {
await filterM2Repo();
}

const s3config = utils.getInputS3ClientConfig();

const enableCrossOsArchive = utils.getInputAsBool(
Expand Down

0 comments on commit 8d0d15c

Please sign in to comment.