Add function to save job artifacts in Artifactory

Related-Prod: PROD-28146 (PROD:28146)
Change-Id: Ic6a69958591ae6cee1deb7ee5094eae8757e4727
diff --git a/src/com/mirantis/mcp/MCPArtifactory.groovy b/src/com/mirantis/mcp/MCPArtifactory.groovy
index de4f81f..d253704 100644
--- a/src/com/mirantis/mcp/MCPArtifactory.groovy
+++ b/src/com/mirantis/mcp/MCPArtifactory.groovy
@@ -309,3 +309,67 @@
     }
     sh "rm -v ${queryFile}"
 }
+
+/**
+ * Save job artifacts to Artifactory server if available.
+ * Returns link to Artifactory repo, where saved job artifacts.
+ *
+ * @param config LinkedHashMap which contains next parameters:
+ *   @param artifactory String, Artifactory server id
+ *   @param artifactoryRepo String, repo to save job artifacts
+ *   @param buildProps ArrayList, additional props for saved artifacts. Optional, default: []
+ *   @param artifactory_not_found_fail Boolean, whether to fail if provided artifactory
+ *          id is not found or just print warning message. Optional, default: false
+ */
+def uploadJobArtifactsToArtifactory(LinkedHashMap config) {
+    def common = new com.mirantis.mk.Common()
+    def artifactsDescription = ''
+    def artifactoryServer
+    try {
+        artifactoryServer = Artifactory.server(config.get('artifactory'))
+    } catch (Exception e) {
+        if (config.get('artifactory_not_found_fail', false)) {
+            throw e
+        } else {
+            common.warningMsg(e)
+            return "Artifactory server is not found. Can't save artifacts in Artifactory."
+        }
+    }
+    def artifactDir = 'cur_build_artifacts'
+    def user = ''
+    wrap([$class: 'BuildUser']) {
+        user = env.BUILD_USER_ID
+    }
+    dir(artifactDir) {
+        try {
+            unarchive(mapping: ['*' : '.'])
+            // Mandatory and additional properties
+            def properties = getBinaryBuildProperties(config.get('buildProps', []) << "buildUser=${user}")
+
+            // Build Artifactory spec object
+            def uploadSpec = """{
+                "files":
+                    [
+                        {
+                            "pattern": "*",
+                            "target": "${config.get('artifactoryRepo')}/",
+                            "props": "${properties}"
+                        }
+                    ]
+                }"""
+
+            artifactoryServer.upload(uploadSpec, newBuildInfo())
+            def linkUrl = "${artifactoryServer.getUrl()}/artifactory/${config.get('artifactoryRepo')}"
+            artifactsDescription = "Job artifacts uploaded to Artifactory: <a href=\"${linkUrl}\">${linkUrl}</a>"
+        } catch (Exception e) {
+            if (e =~ /no artifacts/) {
+                artifactsDescription = 'Build has no artifacts saved.'
+            } else {
+                throw e
+            }
+        } finally {
+            deleteDir()
+        }
+    }
+    return artifactsDescription
+}