How to call Jira api from Connect app module via Forge

I am migrating from Connect App to Forge, however I noticed that after migrating I can no longer use Jwt or httpClient to call API.

Can I call api from Connect App via Forge to call Jira api?

If anyone has any advice I would appreciate it

Thanks

Hi, where is the origin of the http call? Is it in a forge function, forge frontend or your services backend? Also, were you using one of our supported frameworks in your Connect app backend or were you using your own custom framework?

Hi @rmassaioli
The api is called in the old Connect app backend. Currently, the Connect app is using User Impersonation and JWT to call the api. When migrating to Forge, these parts cannot be used because the addon is not registered.
Do you have any solution for this case?

Hi @HungTran, does the Forge version of your app have the lifecycle hook set up? Without it the Forge-based installation won’t be registered. Something like the following

connectModules:
  jira:lifecycle:
    - key: lifecycle-events
      installed: /installed
      uninstalled: /uninstalled

Hi @jhazelwood
I added the lifecycle part but the problem still doesn’t change.
The old way to use it in connect app is as below but now when migrating to forge it doesn’t work anymore

app.post('/event',
      async (req, res) => { 
            let bearerToken = await jUtil.getBearerToken(baseUrl, sharedSecret, userAccountId, oauthClientId)
     		let options = {
			method: 'POST',
			headers: {
				'Authorization': `Bearer ${bearerToken}`,
				Accept: 'application/json',
				'Content-Type': 'application/json',
			},
			body: JSON.stringify(data),
			redirect: 'manual',
			credentials: 'include'
		};
                 const response = await fetch(url, options);
      return response;
} )

When migrating to Forge, I tried to log out and the data used such as sharedSecret, oauthClientId, userAccountId no longer exists. Besides, addon.httpClient also returns null.
Do you have any suggestions in this case?

Hi @HungTran ,

Firstly, just want to check: did you re-deploy and re-install?

The connect parts of the app work the same way before and after adopting Forge. To take an example from this migration example app:

    app.get("/bookmarks", addon.authenticate(), async (req, res) => {
        const {pageId} = req.query;
        const userId = req.context.userAccountId;
        if (!pageId || !userId) {
            return res.status(400).json({error: "pageId and userId are required"});
        }

I can’t gather much from your code snippet - I’m not sure whether it’s from an app using our framework, I see no call to addon.authenticate(), and it’s not clear where or how baseUrl, sharedSecret, userAccountId or oauthClientId are defined or initialised.

Hi @jhazelwood
Last time i ran forge install but it didn’t report any error, so now i just tried uninstall and reinstall and got the following error:

Error: Error performing sync with Connect: LIFECYCLE_HOOK_FAILED: Request to /installed failed with a status code 404. Check the app host is available and try again.

Do we need any handle for this event?

Ah I have fixed this error.
However, when adding addon.authenticate() there will be an error :
Authentication verification error (401): Could not find stored client data for d5e089c5-ec98-3b5a-xxx. Is this client registered?
when calling that event.
About data such as baseUrl, userAccountId will be taken from the request.context
while sharedSecret and oauthClientId are taken from the method addon.loadClientInfo(req.context.clientKey)

The example you gave I have been referring to it before

That suggests the /installed handler isn’t saving the installation data properly; what did you do to fix the 404?

If the app is running in development mode with default configuration, it stores the data in-memory and will lose it on a restart. That might be the issue.

That suggests the /installed handler isn’t saving the installation data properly; what did you do to fix the 404?

This error is because I did not start the Connect app module before running forge install. So I just need to run npm run start first and the 404 error is gone.

I see - that does suggest that the data is going missing, e.g. due to a restart if you’re using the in-memory option. Have you tried another uninstall / re-install, or if you’re using something like sqlite, can you query the database to see what’s in there after an install, something like that? Is the app logging anything about saving an installation?

I tried running the example you gave and still saw the application registration part as shown in the picture:

But when I implement the same thing in my application, there is no such part:

Can you guess what the problem is?

I can’t guess, sorry. If you forge uninstall your application then install it via the descriptor URL, does it work? Your app isn’t intercepting requests to /installed, is it?

What steps did you take to migrate? Perhaps you could check out a pre-migration version of your app, run it, validate it installs and works, then go through the migration steps again, one-by-one.

I found the problem here. It will only register the app in Connect module when there is an install action on the Forge app side. So when you want to have data addons or restart the Connect module, you need to trigger a change in the manifest to be able to run forge install again on that site so that the Connect module can be registered.
Is there a way to develop more easily without having to trigger a re-install in Forge app every time start the Connect app?

Yes.

npm install sqlite3

and add something like the following to your config.json (merge with any existing config)

  "development": {
    "store": {
      "adapter": "sequelize",
      "logging": false,
      "type": "sqlite",
      "url": "sqlite://database.sqlite"
    }
  },

This way installation data will persist across restarts of the service.

3 Likes

@jhazelwood Thanks for your support! I have solved the problems I am currently having

3 Likes