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

Trevor Clokie March 29, 2023

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
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
March 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