Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,511
Community Members
 
Community Events
184
Community Groups

How do I make a REST API call from a Forge Trigger?

Edited

Hi! I would like to trigger a REST API call every time a Confluence page is loaded. In particular, I'd like to use Atlassian Forge to identify which macros are present on a given page. I'm using the `/wiki/rest/api/content/${contentId}?expand=body.storage` call to just get the HTML contents of the page, but I'm happy to consider another API call if a more fitting one exists.

However, when I make this call, it just... never returns. It doesn't throw an exception, it doesn't quit, it just gives up. I've tried a number of modifications and debugging, but nothing seems to change the result. Why might this be, and how can I fix it?

Here's my code (src/index.js):

import api, { route } from "@forge/api";

const getPageContent = async (contentId) => {
console.log("Debug 1");
const response = await api
.asApp()
.requestConfluence(
route`/wiki/rest/api/content/${contentId}?expand=body.storage`
);
console.log("Debug 2");
const data = await response.json();
return data;
};

export async function run(event, _context) {
const contentId = event?.content?.id;
if (contentId) {
console.log(contentId);
getPageContent(contentId);
}
}

In particular, running this results in the console output: ${contentId} and "Debug 1", and nothing more.

Here's the manifest:

modules:
  trigger:
    - key: hello-world
      function: main
      events:
        - avi:confluence:viewed:page
  function:
    - key: main
      handler: index.run
app:
  id: ari:cloud:ecosystem::app/uuiduuid-uuid-uuid-uuid-uuiduuiduuid
permissions:
  scopes:
    - read:confluence-content.summary
    - read:confluence-content.all
    - read:content-details:confluence
    - read:page:confluence

 

1 answer

1 accepted

4 votes
Answer accepted
Jens Schumacher - Released_so
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.
Mar 29, 2023

This is a guess, haven't been able to validate it. However, it looks like the reason the code never returns a result is that the getPageContent function returns a promise, but the function run does not wait for the promise to resolve.

In the run function, when getPageContent is called, it starts executing the async function and immediately moves on to the next line of code, without waiting for the promise to resolve. This means that the run function returns before the getPageContent function has a chance to complete.

To fix this, you can add an await keyword before getPageContent(contentId) to make sure that the run function waits for the getPageContent function to complete before it returns. Here's an example of how you can modify the run function:

export async function run(event, _context) {

const contentId = event?.content?.id;

if (contentId) {

console.log(contentId);

const pageContent = await getPageContent(contentId);

console.log(pageContent);

return pageContent;

}

}


FYI: For development related questions, check out the developer community. You're more likely to get an answer there: https://community.developer.atlassian.com


Cheers,
Jens

Released.so - Release notes powered by Jira

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events