From e13dc1051329562d002e7a01b307c2910c373a24 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20G=C3=B6bel?= <dgoebel@techfak.uni-bielefeld.de>
Date: Mon, 27 Mar 2023 16:31:58 +0200
Subject: [PATCH] Add button to deprecate a workflow version

#53
---
 .../workflow/services/WorkflowService.ts      | 31 +++++++++++
 .../services/WorkflowVersionService.ts        | 31 +++++++++++
 .../object-storage/modals/CopyObjectModal.vue |  2 +-
 src/views/workflows/WorkflowView.vue          | 51 ++++++++++++++++++-
 4 files changed, 113 insertions(+), 2 deletions(-)

diff --git a/src/client/workflow/services/WorkflowService.ts b/src/client/workflow/services/WorkflowService.ts
index 38cff14..d54923b 100644
--- a/src/client/workflow/services/WorkflowService.ts
+++ b/src/client/workflow/services/WorkflowService.ts
@@ -260,4 +260,35 @@ export class WorkflowService {
         });
     }
 
+    /**
+     * Deprecate a workflow version
+     * Deprecate a workflow version.
+     *
+     * Permission "workflow:update" required if you are the developer of the workflow,
+     * otherwise "workflow:read_status""
+     * @param gitCommitHash Git commit git_commit_hash of specific version.
+     * @param wid ID of a workflow
+     * @returns WorkflowVersionFull Successful Response
+     * @throws ApiError
+     */
+    public static workflowVersionDeprecateWorkflowVersion(
+        gitCommitHash: string,
+        wid: string,
+    ): CancelablePromise<WorkflowVersionFull> {
+        return __request(OpenAPI, {
+            method: 'POST',
+            url: '/workflows/{wid}/versions/{git_commit_hash}/deprecate',
+            path: {
+                'git_commit_hash': gitCommitHash,
+                'wid': wid,
+            },
+            errors: {
+                400: `Error decoding JWT Token`,
+                403: `Not authenticated`,
+                404: `Entity not Found`,
+                422: `Validation Error`,
+            },
+        });
+    }
+
 }
diff --git a/src/client/workflow/services/WorkflowVersionService.ts b/src/client/workflow/services/WorkflowVersionService.ts
index 18b2970..25687b7 100644
--- a/src/client/workflow/services/WorkflowVersionService.ts
+++ b/src/client/workflow/services/WorkflowVersionService.ts
@@ -108,4 +108,35 @@ export class WorkflowVersionService {
         });
     }
 
+    /**
+     * Deprecate a workflow version
+     * Deprecate a workflow version.
+     *
+     * Permission "workflow:update" required if you are the developer of the workflow,
+     * otherwise "workflow:read_status""
+     * @param gitCommitHash Git commit git_commit_hash of specific version.
+     * @param wid ID of a workflow
+     * @returns WorkflowVersionFull Successful Response
+     * @throws ApiError
+     */
+    public static workflowVersionDeprecateWorkflowVersion(
+        gitCommitHash: string,
+        wid: string,
+    ): CancelablePromise<WorkflowVersionFull> {
+        return __request(OpenAPI, {
+            method: 'POST',
+            url: '/workflows/{wid}/versions/{git_commit_hash}/deprecate',
+            path: {
+                'git_commit_hash': gitCommitHash,
+                'wid': wid,
+            },
+            errors: {
+                400: `Error decoding JWT Token`,
+                403: `Not authenticated`,
+                404: `Entity not Found`,
+                422: `Validation Error`,
+            },
+        });
+    }
+
 }
diff --git a/src/components/object-storage/modals/CopyObjectModal.vue b/src/components/object-storage/modals/CopyObjectModal.vue
index b722103..cfc05ee 100644
--- a/src/components/object-storage/modals/CopyObjectModal.vue
+++ b/src/components/object-storage/modals/CopyObjectModal.vue
@@ -187,7 +187,7 @@ onMounted(() => {
             </div>
           </form>
           <div class="col-5">
-            You can copy objects. You have to create destination container prior
+            You can copy objects. You have to create destination buckets prior
             to copy.<br />
             You can specify folder by using '/' at destination object field. For
             example, if you want to copy object under the folder named
diff --git a/src/views/workflows/WorkflowView.vue b/src/views/workflows/WorkflowView.vue
index efff546..583efb6 100644
--- a/src/views/workflows/WorkflowView.vue
+++ b/src/views/workflows/WorkflowView.vue
@@ -1,7 +1,11 @@
 <script setup lang="ts">
 import { computed, onMounted, reactive, watch } from "vue";
 import type { WorkflowOut, WorkflowVersionReduced } from "@/client/workflow";
-import { Status, WorkflowService } from "@/client/workflow";
+import {
+  Status,
+  WorkflowService,
+  WorkflowVersionService,
+} from "@/client/workflow";
 import { useRoute, useRouter } from "vue-router";
 import FontAwesomeIcon from "@/components/FontAwesomeIcon.vue";
 import {
@@ -9,6 +13,9 @@ import {
   sortedVersions,
 } from "@/utils/Workflow";
 import { determineGitIcon } from "@/utils/GitRepository";
+import { useAuthStore } from "@/stores/auth";
+
+const userRepository = useAuthStore();
 
 // Props
 // =============================================================================
@@ -98,6 +105,20 @@ const gitIcon = computed<string>(() =>
   determineGitIcon(workflowState.workflow?.repository_url)
 );
 
+const allowVersionDeprecation = computed<boolean>(() => {
+  if (activeVersion.value?.status === Status.PUBLISHED) {
+    if (userRepository.workflowReviewer || userRepository.admin) {
+      return true;
+    } else if (
+      userRepository.workflowDev &&
+      workflowState.workflow?.developer_id === userRepository.currentUID
+    ) {
+      return true;
+    }
+  }
+  return false;
+});
+
 // Functions
 // =============================================================================
 function updateWorkflow(workflowId: string) {
@@ -121,6 +142,24 @@ function updateWorkflow(workflowId: string) {
     });
 }
 
+function deprecateCurrentWorkflowVersion() {
+  if (props.versionId) {
+    WorkflowVersionService.workflowVersionDeprecateWorkflowVersion(
+      props.versionId,
+      props.workflowId
+    ).then((version) => {
+      if (workflowState.workflow) {
+        const versionIndex = workflowState.workflow.versions.findIndex(
+          (v) => v.git_commit_hash === version.git_commit_hash
+        );
+        if (versionIndex > -1) {
+          workflowState.workflow.versions[versionIndex].status = version.status;
+        }
+      }
+    });
+  }
+}
+
 // Lifecycle Events
 // =============================================================================
 onMounted(() => {
@@ -183,6 +222,16 @@ onMounted(() => {
         >
       </div>
       <div class="row align-items-center">
+        <div class="w-fit position-absolute start-0">
+          <button
+            v-if="props.versionId && allowVersionDeprecation"
+            type="button"
+            class="btn btn-warning"
+            @click="deprecateCurrentWorkflowVersion"
+          >
+            Deprecate version
+          </button>
+        </div>
         <router-link
           role="button"
           class="btn btn-success btn-lg w-fit mx-auto"
-- 
GitLab