Compare commits

...

3 commits

Author SHA1 Message Date
Ell 249b14c27e actually delete the right amount of files 2023-07-16 23:04:20 +02:00
Ell 02621d4d2e note 2023-07-16 22:53:38 +02:00
Ell bf9abe3e46 allow skipping trash for retention 2023-07-16 22:47:57 +02:00
4 changed files with 44 additions and 9 deletions

View file

@ -1,2 +1,4 @@
# WoodpeckerPlugins
Simple plugins I created and use for Woodpecker CI. Check subdirectories for specific plugin READMEs.
*The code for all of these is probably awful to look at because functionality is a lot more important to me in single-file scripts than proper conventions are. Sorry!*

View file

@ -28,12 +28,13 @@ steps:
# optional retention settings, useful if old builds should be deleted automatically
retentionamount: 7 # amount of children that retentionbase is allowed to have before oldest ones are deleted on upload
retentionbase: Uploads # directory that the retentionamount applies to
retentionskiptrash: false # whether retention-based deletion should skip the Nextcloud trash, defaults to false
# misc optional settings
basedir: "." # local base directory for files, defaults to .
chunksize: # chunk size in bytes, defaults to 10485760, or 10 MiB
quiet: false # whether to reduce output
quiet: false # whether to reduce output, defaults to false
tags: # a set of tags to apply to uploaded files, tag is expected to already exist
- mytag
flatten: false # whether to flatten directories, causing all files to be placed directly in dest
flatten: false # whether to flatten directories, causing all files to be placed directly in dest, defaults to false
```

View file

@ -22,11 +22,12 @@ if (!destEnv)
const baseDir = process.env.PLUGIN_BASEDIR || ".";
const chunkSizeEnv = process.env.PLUGIN_CHUNKSIZE || 10 * 1024 * 1024;
const quiet = process.env.PLUGIN_QUIET || false;
const quiet = process.env.PLUGIN_QUIET == "true";
const tags = process.env.PLUGIN_TAGS || "";
const flatten = process.env.PLUGIN_FLATTEN || false;
const flatten = process.env.PLUGIN_FLATTEN == "true";
const retentionBase = process.env.PLUGIN_RETENTIONBASE || "";
const retentionAmount = process.env.PLUGIN_RETENTIONAMOUNT || 0;
const retentionSkipTrash = process.env.PLUGIN_RETENTIONSKIPTRASH == "true";
upload();
@ -52,8 +53,9 @@ async function upload() {
let dirs = data["d:multistatus"]["d:response"].slice(1);
// sort directories by last modified
dirs.sort((a, b) => new Date(a["d:propstat"]["d:prop"]["d:getlastmodified"]) - new Date(b["d:propstat"]["d:prop"]["d:getlastmodified"]));
while (dirs.length > parseInt(retentionAmount)) {
let dir = `${serverEnv}${dirs[0]["d:href"]}`;
while (dirs.length >= parseInt(retentionAmount)) {
let dir = serverEnv + dirs[0]["d:href"];
let dirName = dir.substring(retentionPath.length - retentionBase.length).replace(/\/$/, "");
await axios.request({
method: "delete",
url: dir,
@ -62,7 +64,36 @@ async function upload() {
password: tokenEnv
}
});
console.log(`Deleted directory ${dir.substring(retentionPath.length + 1)} because retention amount of ${retentionAmount} was reached`);
// if we skip the trash, we actually also have to delete the item from the trash
if (retentionSkipTrash) {
let trashResponse = await axios.request({
method: "propfind",
url: `${basePath}/trashbin/${userEnv}/trash`,
auth: {
username: userEnv,
password: tokenEnv
},
data: `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<nc:trashbin-original-location/>
</d:prop>
</d:propfind>`
});
let trashContent = JSON.parse(xml.toJson(trashResponse.data))["d:multistatus"]["d:response"];
let trashItem = trashContent.find(e => e["d:propstat"]["d:prop"]["nc:trashbin-original-location"] == dirName);
await axios.request({
method: "delete",
url: serverEnv + trashItem["d:href"],
auth: {
username: userEnv,
password: tokenEnv
}
});
}
console.log(`Deleted directory ${dirName} because retention amount of ${retentionAmount} was reached${retentionSkipTrash ? " (skipped trash)" : ""}`);
dirs.splice(0, 1);
}
}

View file

@ -2,8 +2,9 @@ export PLUGIN_SERVER=https://cloud.ellpeck.de
export PLUGIN_USER=EllBot
export PLUGIN_FILES=**/*.md
export PLUGIN_DEST=Uploads/$(date '+%M:%S')
export PLUGIN_RETENTION_BASE=Uploads
export PLUGIN_RETENTION_AMOUNT=3
export PLUGIN_RETENTIONBASE=Uploads
export PLUGIN_RETENTIONAMOUNT=3
export PLUGIN_RETENTIONSKIPTRASH=true
npm install
node run.js