diff --git a/README.md b/README.md index 18f12ec..a8dffde 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # WoodpeckerPlugins 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!* diff --git a/nextcloud-upload/README.md b/nextcloud-upload/README.md index ff1a84b..f6607b1 100644 --- a/nextcloud-upload/README.md +++ b/nextcloud-upload/README.md @@ -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 ``` diff --git a/nextcloud-upload/run.js b/nextcloud-upload/run.js index 66c9dc0..1104c67 100644 --- a/nextcloud-upload/run.js +++ b/nextcloud-upload/run.js @@ -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(); @@ -53,7 +54,8 @@ async function upload() { // 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"]}`; + 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: ` + + + + + ` + }); + 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); } } diff --git a/nextcloud-upload/test.sh b/nextcloud-upload/test.sh index a0da691..e02c986 100644 --- a/nextcloud-upload/test.sh +++ b/nextcloud-upload/test.sh @@ -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