diff --git a/gitea-package.sh b/gitea-package.sh index b05aaae..4c8943f 100755 --- a/gitea-package.sh +++ b/gitea-package.sh @@ -47,8 +47,46 @@ showSettings() { say "PLUGIN_UPDATE: $PLUGIN_UPDATE" "showSettings" } +testArtifact() { + tout=$(curl --output /dev/null --write-out "%{http_code}" \ + "$CI_FORGE_URL/api/packages/$PLUGIN_OWNER/generic/$PLUGIN_PACKAGE_NAME/$PLUGIN_PACKAGE_VERSION/$PLUGIN_FILE_NAME") + + if [ "$tout" = "200" ]; then + echo "true" + else + echo "false" + fi +} + +deleteArtifact() { + dout=$(curl --output /dev/null --write-out "%{http_code}" \ + --user "$PLUGIN_USER:$PLUGIN_PASSWORD" -X DELETE \ + "$CI_FORGE_URL/api/packages/$PLUGIN_OWNER/generic/$PLUGIN_PACKAGE_NAME/$PLUGIN_PACKAGE_VERSION/$PLUGIN_FILE_NAME") + + if [ "$dout" = "204" ]; then + say "Old package file deleted" "deleteArtifact" + elif [ "$dout" = "404" ]; then + sayE "The package or file was not found." "deleteArtifact" + exit 1 + else + sayE "Unknown curl response! ($dout)" "deleteArtifact" + exit 1 + fi +} + uploadArtifact() { - say "Starting file upload..." + say "Testing if the artifact already exists in give package version..." "uploadArtifact" + fexist=$(testArtifact) + if [ "$fexist" = "true" ]; then + if [ -n "$PLUGIN_UPDATE" ]; then + sayW "Given file already exists. Updating package file..." "uploadArtifact" + deleteArtifact + else + sayW "A file with the same name exist already in the package." "uploadArtifact" + exit 1 + fi + fi + say "Starting file upload..." "uploadArtifact" cout=$(curl --output /dev/null --write-out "%{http_code}" \ --user "$PLUGIN_USER:$PLUGIN_PASSWORD" \ --upload-file "$PLUGIN_FILE_SOURCE" \ @@ -62,8 +100,7 @@ uploadArtifact() { sayE "Upload failed! (Bad Request)" "uploadArtifact" exit 1 elif [ "$cout" = "409" ]; then - sayW "A file with the same name exist already in the package." "uploadArtifact" - exit 1 + sayE "File already exists in package version!" "uploadArtifact" else sayE "Unknown upload response! ($cout)" "uploadArtifact" exit 1