Adding a worklog using the Jira REST API

Hi there,
I’m trying to develop a Forge application that can track a Users time, while working on an issue.
The frontend works so far, but I can’t figure out how to actually add the worklog.
I have followed this documentation: https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-worklogs/#api-rest-api-3-issue-issueidorkey-worklog-post, but when I try to execute it, I get a “Connection refused” error, telling me it violates the Content Security Policy.
After reading through the forum for a while it seems like calling ‘@ forge/api’ only works for the backend, so I used ‘@ forge/bridge’ to try to fix it, this time I got a 404 error. According to the documentation:

“Returned if the issue is not found or the user does not have permission to view it.”

I know that the issue exists because:

const res = await requestJira(`/rest/api/3/issue/${key}/worklog`, {
    method: 'GET'
});

works just fine.
I have since tried to put the code in a resolver in the backend, but that just results in me not seeing any error messages while still not working…

Here are parts of the code:
index.jsx (/frontend):

    const setWorklog = async () => {
    const key = context?.extension.issue.key;
    const res = await requestJira(`/rest/api/3/issue/${key}/worklog`, {
      method: 'GET'
    });
    const data = await res.json();
    console.log(data);
    invoke('logWork', {key: key}).then(async (resp) => {
      console.log(resp);
      console.log(`Response: ${resp.status} ${resp.statusText}`);
      console.log(await resp.json());
    });
  };

index.js (/resolvers):

  async function postAPI(payload){
  const resp = await api.asUser().requestJira(route`/rest/api/3/issue/${payload.key}/worklog`, {
    method: 'POST',
    headers: {
      "Content-Type": "application/json",
      "Accept": "application/json"
    },
    body: `{
      "comment": {
        "content": [
          {
            "content": [
              {
                "text": "I did some work here.",
                "type": "text"
              }
            ],
            "type": "paragraph"
          }
        ],
        "type": "doc",
        "version": 1
      },
      "started": "2024-08-06T13:26:47.000+0200",
      "timeSpentSeconds": 240,
      "visibility": {
        "identifier": "276f955c-63d7-42c8-9520-92d01dca0625",
        "type": "group"
      }
    }`
  });
  return resp;
}
setter.define('logWork', ({payload}) => {
  return postAPI(payload);
});

Any help would be greatly appreciated.

Hello @Fjon

When you tested the same request using your API test tool (Postman etc.), using the same user’s credentials, did you get the same result?

@Fjon

I guess you copied the body from the docs, but the visibility section can be removed. I think that group (which is same id as in the docs) will probably be a group that doesn’t exist in your instance.

From docs:

visibility
Visibility
Details about any restrictions in the visibility of the worklog. Optional when creating or updating a worklog.

1 Like

It worked after removing visibility, thank you!