Clarity

 View Only

 Gelscript to Call REST API Jira from Clarity with Token Method

M179318's profile image
M179318 posted Jan 30, 2024 08:46 AM

Hello team

We need to create and  read some tasks  on Jira when they are created on Clarity PPM. i'm using this code to reach Rest API with ( Basic authentification) but i need to do it with Token that Jira administrator given to me. 

Do you know how to change the authentification method with token  ? 

<gel:script xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:core="jelly:core"
xmlns:gel="jelly:com.niku.union.gel.GELTagLibrary"
xmlns:sql="jelly:sql" xmlns:xog="http://www.niku.com/xog"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="jelly:util">
<core:invokeStatic className="java.lang.System"   method="currentTimeMillis" var="milStart00"/>
<core:invokeStatic className="com.niku.union.config.ConfigurationManager" method="getInstance" var="thisClarityConfig"/>
<core:set value="${thisClarityConfig.getVersionProperty('version')}" var="ClarityVersion"/>
<core:set value="${thisClarityConfig.getProperties().getWebServer().getWebServerInstance(0).getEntryUrl()}" var="AppEntryUrl"/>
<core:set value="${thisClarityConfig.getProperties().getWebServer().getWebServerInstance(0).getContext()}" var="AppContext"/>
<gel:parameter default="/ppm" var="AppRestContext"/>
<core:set value="${AppEntryUrl}${AppContext}" var="ClarityBaseUrl"/>
<core:set value="https://jiraexample.com/rest/api/2/issue" var="ClarityRestUrl"/>
<core:invoke method="getProperties" on="${thisClarityConfig}" var="property"/>
<core:invoke method="getMailServer" on="${property}" var="mailserver"/>
<core:invoke method="getUsername" on="${mailserver}" var="mailsender"/>
<gel:parameter default="true" var="DebugParameter"/>
<gel:parameter default="admin" var="cl_UserName"/>
<gel:parameter default="${thisClarityConfig.getProperties().getApplicationServer().getAdminPassword()}" secure="true" var="cl_UserPassword"/>
<core:if test="${cl_UserPassword==null}">
<gel:log level="ERROR">No Password set for user ${cl_UserName}</gel:log>
</core:if>    
<core:invokeStatic var="base64" className="com.niku.union.utility.Base64" method="encode"> 
    <core:arg type="java.lang.String" value="${cl_UserName}:${cl_UserPassword}" /> 
</core:invokeStatic> 
<core:set var="basicAuth" value="Basic ${base64}"/>
<gel:persist scope="INSTANCE" var="ClarityBaseUrl">${ClarityBaseUrl}</gel:persist>
<gel:persist scope="INSTANCE" var="ClarityRestUrl">${ClarityRestUrl}</gel:persist>
<gel:persist scope="INSTANCE" var="cl_UserName">${cl_UserName}</gel:persist>
<gel:persist scope="INSTANCE" var="cl_UserPassword">${cl_UserPassword}</gel:persist>
<gel:persist scope="INSTANCE" var="mailsender">${mailsender}</gel:persist>
<gel:persist scope="INSTANCE" var="ClarityVersion">${ClarityVersion}</gel:persist>
<gel:persist scope="INSTANCE" var="basicAuth">${basicAuth}</gel:persist>
<core:if test="${DebugParameter}">
      <gel:log level="INFO">Clarity Parameter's </gel:log>
      <gel:log level="INFO">ClarityBaseUrl        : ${ClarityBaseUrl} </gel:log>
      <gel:log level="INFO">ClarityRestUrl        : ${ClarityRestUrl}</gel:log>
      <gel:log level="INFO">cl_UserName           : ${cl_UserName}</gel:log>
      <gel:log level="INFO">mailsender            : ${mailsender}</gel:log>
      <gel:log level="INFO">ClarityVersion        : ${ClarityVersion}</gel:log>  
      <gel:log level="INFO">basicAuth             : ${basicAuth}</gel:log>   
</core:if>
<gel:parameter default="2038726" var="cl_ProjectInternalId"/>
<gel:parameter default="true" var="DebugClarity"/>
<core:catch var="SuperException">      
<core:invokeStatic className="java.lang.System"   method="currentTimeMillis" var="milStart00"/>
<gel:log><![CDATA[************ Clarity Project Information Extraction started  *********]]></gel:log>
<core:if test="${DebugClarity}">
<gel:log/>
<gel:log>Basic Auth Encoded 64: ${basicAuth}</gel:log>
</core:if>
<core:set var="httpRESTURL">${ClarityRestUrl}/${cl_ProjectInternalId}</core:set>
    <core:if test="${DebugClarity}"><gel:log/><gel:log>httpRESTURL: ${httpRESTURL}</gel:log>
</core:if>
<core:new className="java.net.URL" var="remoteURL" ><core:arg type="java.lang.String" value="${httpRESTURL}" />
</core:new>
<core:set var="ClarityConnection" value="${remoteURL.openConnection()}"/>  
<core:expr value="${ClarityConnection.setDoOutput(true)}" /> 
<core:expr value="${ClarityConnection.setDoInput(true)}" />
<core:expr value="${ClarityConnection.setConnectTimeout(180000)}" />
<core:expr value="${ClarityConnection.setReadTimeout(180000)}" />
<core:expr value='${ClarityConnection.setRequestMethod("GET")}'/> 
<core:expr value='${ClarityConnection.setRequestProperty("Content-type", "application/json")}'/>
<core:expr value='${ClarityConnection.setRequestProperty("Accept", "application/json")}'/>         
<core:expr value='${ClarityConnection.setRequestProperty("cache-control", "no-cache")}'/>
<core:expr value='${ClarityConnection.setRequestProperty("Authorization",  basicAuth)}'/> 
<core:set var="void" value="${ClarityConnection.connect()}"/>
<core:if test="${DebugClarity}"><gel:log>Connection Established: ${void}</gel:log>
</core:if>
<core:set var="CL_isAuthenticated" value="${ClarityConnection.getHeaderField(0)}" />
<core:set var="response_msg" value="${ClarityConnection.getResponseMessage()}" />
<core:set var="response_code" value="${ClarityConnection.getResponseCode()}" />
<core:if test="${DebugClarity}"><gel:log>Clarity isAuthenticated: ${CL_isAuthenticated}</gel:log>
<gel:log>Response Msg : ${response_msg}</gel:log>
<gel:log>Response Code : ${response_code}</gel:log>
</core:if>
<core:if test="${CL_isAuthenticated=='HTTP/1.1 200 OK'}">
 
<core:set var="httpInputStream1" value="${ClarityConnection.getInputStream()}" />
        <core:new className="java.io.InputStreamReader" var="v_Input1">
            <core:arg type="java.io.InputStream" value="${httpInputStream1}"/>
        </core:new>
        <core:new className="java.io.BufferedReader" var="v_InputData1">
            <core:arg type="java.io.InputStreamReader" value="${v_Input1}"/>
        </core:new>
<core:set value="${v_InputData1.readLine()}" var="ClarityProjectInfo"/>
        <core:if test="${DebugClarity}"><gel:log>httpData: ${ClarityProjectInfo}</gel:log>
</core:if>
<core:set var="void" value="${httpInputStream1.close()}" />
   <core:new className="org.json.JSONObject" var="J_ClarityPrjInfo">
            <core:arg type="java.lang.String" value="${ClarityProjectInfo}" />
        </core:new>
  <core:if test="${DebugClarity}"><gel:log>JSON Output: ${J_ClarityPrjInfo}</gel:log></core:if>
</core:if> 
<core:if test="${CL_isAuthenticated!='HTTP/1.1 200 OK'}">
        <gel:log><![CDATA[************ Error while reading Project Information *********]]></gel:log>                           
    </core:if> 
</core:catch>
<core:if test="${SuperException!=null}"><gel:log>Super Exception — ${SuperException}</gel:log>
</core:if>
</gel:script>

Thank you