Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Need assistance in a user macro script

Aravind Koushik Gobburu
Contributor
February 14, 2022

Hi we have a user macro named "UPDATE HISTROY". This has been hadr coded for only particular space only. So i need guidance in this script so that it will be used for all spaces in confluence. Do kindly let me know if any body can help with this. Thanks in advance. Here is the script what i have

## @noparams

 

#set ( $descendantPages = $pageManager.getPage("NTGSDHSI","Project Status").getChildren() )

#set ( $compareDate = $action.dateFormatter.getCalendar() )

$compareDate.setTime($content.getCurrentDate())

$compareDate.add(6, -7)

<table>

<tr>

<th>Status Page</th>

<th>Last Modified By</th>

<th>Last Modified Date</th>

<th>Modified in last seven days?</th>

</tr>

#foreach ( $page in $descendantPages )

<tr>

<td><a href="$

{req.contextPath}

 

$childPage.urlPath"> $page.title </a></td>

<td> $page.lastModifier.name </td>

<td> $action.dateFormatter.format($page.lastModificationDate) </td>

<td>

#set ($pageDate = $action.dateFormatter.getCalendar())

$pageDate.setTime($page.getLastModificationDate())

#if ( $pageDate.before($compareDate) )

<span class="aui-lozenge aui-lozenge-error">NO</span>

#else

<span class="aui-lozenge aui-lozenge-success">YES</span>

#end

</td>

</tr>

#end

</table>

2 answers

0 votes
Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 22, 2022

Ok, I think I found the solution. But be aware, this solution uses methods which are depricated in version 7.3. This means, they might work for longer (worked with 7.13), but atlassian can always delete this methods.

What I did:

  • Get all spaces
  • get all spacekeys
  • iterate over all spaces to find pages with "Project Status"
  • iterate over all descendants

Hope this is what you want

## Macro title: My Macro
## Macro has a body: Y or N
## Body processing: Selected body processing option
## Output: Selected output option
##
## Developed by: My Name
## Date created: dd/mm/yyyy
## Installed by: My Name

## This is an example macro
## @noparams

#set( $spaceManager = $action.getSpaceManager())
#set( $actualSpace = $spaceManager.getSpace($space.key))
#set ($spaces = $spaceManager.getAllSpaces())

#set( $allSpaceKeys=[] )
#foreach ($space in $spaces)
#set ($key = $space.getKey())
#set( $dummy = $allSpaceKeys.add($key) )
#end

#set( $descendantPages=[] )
#foreach ($spacekey in $allSpaceKeys)
#set ( $dumm = $descendantPages.add($pageManager.getPage($spacekey,"Project Status").getChildren() ))
#end


#set ( $compareDate = $action.dateFormatter.getCalendar() )

$compareDate.setTime($content.getCurrentDate())

$compareDate.add(6, -7)

<table>

<tr>

<th>Status Page</th>

<th>Last Modified By</th>

<th>Last Modified Date</th>

<th>Modified in last seven days?</th>

</tr>

#foreach ($descendants in $descendantPages)
#foreach ( $page in $descendants )
<tr>
<td><a href="$
{req.contextPath}$childPage.urlPath"> $page.title </a></td>

<td> $page.lastModifier.name </td>
<td> $action.dateFormatter.format($page.lastModificationDate) </td>
<td>

#set ($pageDate = $action.dateFormatter.getCalendar())
$pageDate.setTime($page.getLastModificationDate())

#if ( $pageDate.before($compareDate) )
<span class="aui-lozenge aui-lozenge-error">NO</span>
#else
<span class="aui-lozenge aui-lozenge-success">YES</span>
#end

</td>
</tr>
#end
#end

</table>
0 votes
Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 14, 2022

Hi @Aravind Koushik Gobburu 

On almost the first line, you go and get the page with the Spacekey NTGSDHSI and the title "Project Status". (see documentation)

#set ( $descendantPages = $pageManager.getPage("NTGSDHSI","Project Status").getChildren())

So it will always get these pages, doesn't matter where you put your macro.

Regards, Dominic

Aravind Koushik Gobburu
Contributor
February 14, 2022

Hi @Dominic Lagger ,

I think you have wrongly understood my problem. Let me explain you clearly. This update history macro is hard coded for only particular space(like mentioned NTGSDHSI). Now we need to modify the code so that it works for all the spaces present in confluence. So final conclusion is that update history macro must be used for all spaces not only for a single space. And many thanks for the reply. Awaiting for your response.

Regards,

Aravind

Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 15, 2022

I understand you correctly, I just wanted to point out, why it's just working for that space. Because you hard code the key in your marco.

If you want to work it for all spaces, then perhaps this could help you.

#set( $containerManagerClass=$content.class.forName('com.atlassian.spring.container.ContainerManager') )
#set( $getInstanceMethod=$containerManagerClass.getDeclaredMethod('getInstance',null) )
#set( $containerManager=$getInstanceMethod.invoke(null,null) )
#set( $containerContext=$containerManager.containerContext )
#set( $pageManager=$containerContext.getComponent('pageManager') )
#set( $page = $pageManager.getPage($content.getId()) )

This give you the actual page, where the macro is sitting. From there, you should be able to do the rest.

Aravind Koushik Gobburu
Contributor
February 15, 2022

Hi @Dominic Lagger ,

Really sorry for that i misunderstood you. Thanks for the code. I am testing it out. Will tell you once if it is an successful one or not. Thankyou so much for the code.

Thanks,

Aravind

Aravind Koushik Gobburu
Contributor
February 16, 2022

Hi @Dominic Lagger ,

I tried this out but it's not working as expected.Its not giving us the update history of all the spaces or in particular space.Can you briefly modify the code what I have provided you and modify that to work for all spaces? If it's possible try that once.Or a big thanks for helping me out in this.

Regards,

Aravind

Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 18, 2022

Hey @Aravind Koushik Gobburu 

I'm not sure what is your goal. What should this macro do?

And also, what do you mean by: "update history of all the spaces or in particular space". You can't have a space in a space. Do you mean pages? 

And last question, what history do you want to update? The whole user macro just display a table, and do not update anything, it never did.

Thanks and Regards, Dominic

Aravind Koushik Gobburu
Contributor
February 18, 2022

Hi @Dominic Lagger ,

When you see the code it gives us a table present in project NTGSDHI. This means that it is hardcoded for particular space only right. Now i want a modification to the code that works for the remaining spaces as well not for particular space NTGSDHI. Can we do this is what my question is.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events