Read Customfield Configuration Context

Hello everyone here,

I’m trying to read customfield configuration context from a forge app but getting the error below.

ERROR   23:24:24.528  6767fbca72100eed  [NEEDS_AUTHENTICATION_ERR: Authentication required] {
  serviceKey: 'atlassian-token-service-key',
  status: 401
}

I used the sample code from here https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-custom-field-configuration--apps-/
and the code snippet, I used is below

export async function update(event, context) {
  console.log('Hello World! update event: ', event, ' context: ', context);

  console.log('change log', event.changelog.items.reduce((acc, item) => {
    acc[item.field] = item.toString;
    return acc;
  }));

  for (const changelogItem in event.changelog.items) {
      const customFieldContext = await api.asUser().requestJira(route`/rest/api/3/app/field/${changelogItem.fieldId}/context/configuration`, {
          headers: {
              'Accept': 'application/json'
          }
      });

      console.log(`Response: ${customFieldContext.status} ${customFieldContext.statusText}`);
      console.log(await customFieldContext.json());
  }
}

it runs on update event as it’s obvious from its name. and I already added below scopes for permissions.

permissions:
  scopes:
    - 'read:jira-user'
    - 'read:jira-work'
    - 'write:jira-work'
    - 'manage:jira-configuration'
    - 'read:custom-field-contextual-configuration:jira'

a/o with any ideas?

Hi @masterkeylog,

There is one thing to consider here:

  • asUser() vs asApp() in a product event: I see that the example code uses asUser() in what looks like a product event function. Based on my test, only asApp() is supported in this context and I’ve also confirmed that with engineering now. Because no user is available in this context, only asApp() can be used.

A note about {fieldIdOrKey} for Forge apps
Let me also share that the {fieldIdOrKey} for Forge apps is defined in the following way:
{app-id-uuid}__{environment}__{customField-key}

Where:

  • {app-id-uuid} is the app id from the manifest and this is just the numerical part of the id so without the ari:cloud:ecosystem::app/ part
  • {environment} is one of the available Forge environments so either DEVELOPMENT, STAGING or PRODUCTION
  • {customField-key} is the key value associated with the jira:customField in the manifest.yml file

Considering that this changes in each environment, the {fieldIdOrKey} reference needs to be set using environment variables which are then referenced in the code.

Hope this helps,
Caterina

Hey @ccurti,

Thanks for detailed answer I’ll try this as soon as possible but I would like to ask something first.

With this plugin I define custom field type, not custom field. Then admins will create custom field themselves. Then, is it not possible to read context configuration of these custom field types in an event? This will a blocker for us.

If possible I would like to have a chat about this because it’s about migration of an data center plugin to cloud.

Thanks!

Hi @masterkeylog,

Ok, I think I might know what is going on here. The problem seems to be because the function update is requested as part of a Product Event. Let me confirm that and update you. In the meantime, I’ve updated my previous reply to reflect the new findings.
Update: I’ve confirmed that only asApp() can be used in a product event function.

I was testing in the same context yesterday and that’s when I could only get asApp to work.

However, the same request works fine and can access any custom fields (not just the ones created by the app) when using asUser outside of a product event function. There is an example in the Forge Currency Exchange sample app.
Would it be possible to move the logic outside the product event function for this app?

Caterina

In the data center app, we utilize both issue edit & create events. We need them because, according to user selection on the custom field type, we update the ticket’s other fields. I don’t know how we can follow or do the changes out of event context.

Maybe we can write these updates to another property and then in a scheduled job we can do updates. I don’t know if these API will work on the scheduled tasks. This is not the way we would like to implement but just trying to find a workaround?