Script wiki for linked workitems

Description
alinoras
Posts: 3
Joined: Wed May 24, 2017 9:55 am

Script wiki for linked workitems

Postby alinoras » Mon May 29, 2017 8:02 am

As a result of version changes, an old wiki script does not work anymore.
The macro was used to create one or more links between source and target documents in one or more specified items.

I have to correct it but I do not know where to start.
Can you help me ?

Code: Select all

#set($quote = '"')

<style type="text/css">
    #tableau {margin:2.5%;width:95%;  padding:0.5em; border:1px solid  #0046b0; }
    #tableau thead th {padding:0.5em; background-color: #E1F0FF;color: #0046b0;text-align:center; font-size:1.25em ;}
    #tableau tbody th {padding:0.5em; text-align:right; width:20%; color: #0046b0;  background-color: #f2f2f2; }
    #tableau tbody td {padding:0.5em;text-align:left; width:80%; }
    #tableau tfoot th {text-align:center; background-color: white;  font-size:1.25em ;padding:0.5em; background-color: #E1F0FF;}
</style>

1 Create links from references in Work Item fields

#GetDocuments($page.project $documentList)

{parameter:IdField|type=string|value=id}
{parameter:LinksField|type=string|value=linkitems}
{parameter:LinkSeparator|type=string|value=,}
{parameter:SourceDocument|type=string|allowed-values=$documentList|value="xx"}
{parameter:TargetDocument|type=string|allowed-values=$documentList|value="xxx"}
{parameter:LinkRole|type=enum:workitem-link-role|value=couvre}
{parameter:Simulate|type=boolean|value=true}
{parameter:Backlink|type=boolean|value=false}

1.1 Configuration

Pressing ~~Apply~~ will execute the link creation script with the given parameters.
While ~~Simulate~~ is checked the script will only display the links that would be created if ~~Simulate~~ was unchecked.

{parameter-form}
<table id="tableau">
    <thead>
        <tr>
            <th>Paramètres</th>
            <th>Valeurs</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th>ID Field:</th>
            <td>{parameter-editor:IdField}</td>
        </tr>
        <tr>
            <th>Link Field:</th>
            <td>{parameter-editor:LinksField}</td>
        </tr>
        <tr>
            <th>Link Separator:</th>
            <td>{parameter-editor:LinkSeparator}</td>
        </tr>
        <tr>
            <th>Source Document:</th>
            <td>{parameter-editor:SourceDocument|width=100%}</td>
        </tr>
        <tr>
            <th>Target Document:</th>
            <td>{parameter-editor:TargetDocument|width=100%}</td>
        </tr>
        <tr>
            <th>Linkrole:</th>
            <td>{parameter-editor:LinkRole}</td>
        </tr>
        <tr>
            <th>Simulate:</th>
            <td>{parameter-editor:Simulate}</td>
        </tr>
        <tr>
            <th>Create back links:</th>
            <td>{parameter-editor:Backlink}</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <th colspan="2">
                {parameter-form-submit}
                {parameter-form-save}
            </th>
        </tr>
    </tfoot>
</table>
{parameter-form}

#set($linkField =      $pageParameters.LinksField)
#set($idField=         $pageParameters.IdField)
#set($targetSeparator= $pageParameters.LinkSeparator)
#set($sourceDocument=  $pageParameters.SourceDocument)
#set($targetDocument=  $pageParameters.TargetDocument)
#set($simulates=       $pageParameters.Simulates)
#set($simulates=       $pageParameters.BackLink)

#if(!$linkField || !$idField || !$targetSeparator || !$sourceDocument)
  #info("You have to configure all parameters before you can continue.")
#else

1.1 Results

#if(!$pageParameters.LinkRole && !$pageParameters.Simulate)
  #error("Can't create link without selected link role.")
#else

#if($pageParameters.LinkRole)
  #set($linkRole = $trackerService.getTrackerProject($page.project).getWorkItemLinkRoleEnum().wrapOption($pageParameters.LinkRole.id))
#end

#set($project=$projectService.getProject($page.project))
#set($sourceQuery = "project.id:$page.project AND document.id:$quote$sourceDocument$quote AND NOT type:heading")
#set($sourceItems = $trackerService.queryWorkItems($sourceQuery,null))
number of Workitems= $sourceItems.size() \\
 
#set($targetType = $pageParameters.TargetWITyp)

#if(!$pageParameters.Simulate)
  #if($transactionService.txExists())
    $transactionService.rollbackTx()
  #end
  $transactionService.beginTx()
#end

#foreach($item in $sourceItems)
  #if($item.type.id != "heading")
* Links #if($pageParameters.Backlink)to#else from#end $item.id #if($pageParameters.Backlink)from#else to#end

  #set($targetIds = false)
  #set($linksString = $item.getValue($linkField))
  #if(!$linksString.isEmpty())
    #set($targetIds = $item.getValue($linkField).split($targetSeparator))
    #foreach($targetId in $targetIds)

      #if($targetId.trim().isEmpty())
        ** ERROR: Can't create link for external ID - targetId is empty.
      #else
        #set($targetQuery = "$idField:$quote$targetId$quote AND project.id:$page.project AND document.id:$quote$targetDocument$quote AND NOT type:heading")
        #set($potentialTargetItems = $trackerService.queryWorkItems($targetQuery, null))
        #if($potentialTargetItems.size() == 1)
          #set($targetItem = $potentialTargetItems.get(0))
          #if(!$targetType || $targetType.id.equals($potentialTargetItems.get(0).type.id))
** $targetItem.id ($targetItem.type.name) #if(!$pageParameters.Simulate)#link($item $targetItem)#end

          #end
#elseif($potentialTargetItems.size() > 1)

** ERROR: Can't create link  for external ID $targetId - multiple potential taget items found.
          #else

** ERROR: Can't create link for external ID $targetId - No potential target item found.
        #end
      #end
  #end

  #end

  #set($cfQuery="$pageParameters.TargetField:$item.getCustomField($pageParameters.SourceField)")
  #foreach($targetItem in $trackerService.queryWorkItems("project.id:$m_ProjectId AND type:$pageParameters.TargetWITyp.id AND $cfQuery", "id"))
    #LinkWIs($m_ProjectId $item.id $targetItem.id)
  #end
 #end ## if heading
#end

##
## end transaction, persist the work item changes
##
#if(!$pageParameters.Simulate)
$transactionService.commitTx()
#end

#end

#end


#macro(link $source $target)
{pre}
    #if($pageParameters.Backlink)
      #set($realSource = $target)
      #set($realTarget = $source)
    #else
      #set($realSource = $source)
      #set($realTarget = $target)
    #end
    ##
    #set($modifiedLink = false)
    #foreach($link in $realSource.getLinkedWorkItemsStructsDirect())
      #if($link.linkedItem.equals($realTarget ))
        #set($modifiedLink = true)
        #if($link.linkRole.id != $linkRole.id)
          $link.setValue("role", $linkRole)
          $realSource.save()
          <b>Link role modified</b>
        #else
          <b>Nothing changed</b>         
        #end           
      #end
    #end
    ##
    #if(!$modifiedLink)
      #set($result= $realSource.addLinkedItem($realTarget, $linkRole, null, false))
      #if($result == true)$realSource.save() <b>Link created</b>#end
    #end
{/pre}
#end

#macro(GetDocuments $p_ProjectId $p_Documents)
  #set($project = $projectService.getProject($p_ProjectId))
  #set ($allModules=[])
  #foreach ($space in $wikiService.getSpaces($p_ProjectId))
    #set($modulesLocation = $project.location.getLocation($space.id))
    #set($modules = $trackerService.moduleManager.getModules($project,$modulesLocation))
    #foreach($module in $modules)
      #set($temp = $allModules.add("$space.getId()/$module.id"))
    #end
  #end
  #set($p_Documents="")
  #foreach($document in $allModules)
    #set($p_Documents="$p_Documents#if($p_Documents.length() > 0),#end$document")
  #end
#end

Return to “Polarion Application Lifecycle Management (ALM)”

Who is online

Users browsing this forum: Bing [Bot] and 2 guests