From 7d33b742b45406d6da68ea3591124c49c089f95b Mon Sep 17 00:00:00 2001
From: Andre Anjos <andre.anjos@idiap.ch>
Date: Fri, 23 Jun 2017 11:24:50 +0200
Subject: [PATCH] Check if directories exist on the remote DAV server before
 deleting them

---
 gitlab/before_docs.sh |  4 ++--
 gitlab/functions.sh   | 12 ++++++++++--
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/gitlab/before_docs.sh b/gitlab/before_docs.sh
index 7fe4b0b..45941af 100755
--- a/gitlab/before_docs.sh
+++ b/gitlab/before_docs.sh
@@ -18,9 +18,9 @@ fi
 
 # prefix differs between private & public repos
 if [[ "${VISIBILITY}" == "public" ]]; then
-  DOCS_SERVER_PREFIX="public-upload/$CI_PROJECT_PATH/docs/"
+  DOCS_SERVER_PREFIX="public-upload/$CI_PROJECT_PATH/docs"
 else
-  DOCS_SERVER_PREFIX="private-upload/$CI_PROJECT_PATH/docs/"
+  DOCS_SERVER_PREFIX="private-upload/$CI_PROJECT_PATH/docs"
 fi
 
 # define possible upload paths
diff --git a/gitlab/functions.sh b/gitlab/functions.sh
index 608d3d8..a05dff2 100644
--- a/gitlab/functions.sh
+++ b/gitlab/functions.sh
@@ -204,9 +204,17 @@ dav_mkdir() {
 # Deletes a file/folder from our intranet location via curl
 # $1: Path to the file/folder to delete (e.g. dist/myfile.whl)
 dav_delete() {
-  log_info "curl: DELETE ${1}..."
+  log_info "curl: EXISTS ${1}?"
+
+  # checks if the directory exists before trying to remove it (use --head)
+  local code=$(curl --location --silent --fail --write-out "%{http_code}" --head --user "${DOCUSER}:${DOCPASS}" "$1")
+  if [[ ${code} == *404 ]]; then
+    log_info "Directory $1 does not exist. Skipping deletion"
+    return 0
+  fi
 
-  local code=$(curl --location --silent --fail --write-out "%{http_code}" --user "${DOCUSER}:${DOCPASS}" -X DELETE "$1")
+  log_info "curl: DELETE ${1}..."
+  code=$(curl --location --silent --fail --write-out "%{http_code}" --user "${DOCUSER}:${DOCPASS}" -X DELETE "$1")
 
   if [[ ${code} == *204 || ${code} == *201 ]]; then
     log_info "Successfully deleted ${1} with curl"
-- 
GitLab