from pyartifactory import Artifactory
import sys
import os

# Check if exist the same version of the artifact

# Variables
ARTIFACTORY_URL = "https://artifactory.finboot.com/artifactory"
PATH = "libs-snapshot-local/com/finboot"
FOLDER = sys.argv[1]
USER = os.environ.get('ARTIFACTORY_USER')
PASSWORD = os.environ.get('ARTIFACTORY_PASSWORD')
VERSION = sys.argv[2]

# ------- Main ------------------
# Authentication
art = Artifactory(url=ARTIFACTORY_URL, auth=(USER,PASSWORD))
print ("")
print ("Debug: "+PATH+'/'+FOLDER+'/'+VERSION)
print ("-------------------------------------------")
try:
    #artifact_properties = art.artifacts.properties(PATH+VERSION)
    artifact_properties = art.artifacts.properties(PATH+'/'+FOLDER+'/'+VERSION)
    print("ERROR - Sorry, there are already the same version uploaded in Artifactory.")
    print("If it is a new version you should change the POM.XML")
    print (ARTIFACTORY_URL+'/'+PATH+'/'+FOLDER+'/')
    #print(artifact_properties.json)
    exit(1)
except Exception as e:
    print("OK - Version: "+VERSION+" not found in "+ARTIFACTORY_URL+'/'+PATH+'/'+FOLDER+'/')
    print("You can continue with the pipeline :)")
    exit(0)
    

