allow skipping trash for retention

This commit is contained in:
Ell 2023-07-16 22:47:57 +02:00
parent b190def36f
commit bf9abe3e46
4 changed files with 43 additions and 8 deletions

View file

@ -1,2 +1,4 @@
# WoodpeckerPlugins # WoodpeckerPlugins
Simple plugins I created and use for Woodpecker CI. Check subdirectories for specific plugin READMEs. Simple plugins I created and use for Woodpecker CI. Check subdirectories for specific plugin READMEs.
*Note: 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 # 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 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 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 # misc optional settings
basedir: "." # local base directory for files, defaults to . basedir: "." # local base directory for files, defaults to .
chunksize: # chunk size in bytes, defaults to 10485760, or 10 MiB 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 tags: # a set of tags to apply to uploaded files, tag is expected to already exist
- mytag - 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 baseDir = process.env.PLUGIN_BASEDIR || ".";
const chunkSizeEnv = process.env.PLUGIN_CHUNKSIZE || 10 * 1024 * 1024; 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 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 retentionBase = process.env.PLUGIN_RETENTIONBASE || "";
const retentionAmount = process.env.PLUGIN_RETENTIONAMOUNT || 0; const retentionAmount = process.env.PLUGIN_RETENTIONAMOUNT || 0;
const retentionSkipTrash = process.env.PLUGIN_RETENTIONSKIPTRASH == "true";
upload(); upload();
@ -53,7 +54,8 @@ async function upload() {
// sort directories by last modified // 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"])); 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)) { while (dirs.length > parseInt(retentionAmount)) {
let dir = `${serverEnv}${dirs[0]["d:href"]}`; let dir = serverEnv + dirs[0]["d:href"];
let dirName = dir.substring(retentionPath.length - retentionBase.length).replace(/\/$/, "");
await axios.request({ await axios.request({
method: "delete", method: "delete",
url: dir, url: dir,
@ -62,7 +64,36 @@ async function upload() {
password: tokenEnv 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); dirs.splice(0, 1);
} }
} }

View file

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