Trying @forge/api I do not get a result (supposedly there is error 500 on api endpoint)

I am trying to add a work log to some issue with my app. I managed to write this function and pass proper parameter to it but I am not getting any answer from the API endpoint.

  postworklogData = async (worklog) => {
    const context = useProductContext();
    // @ts-ignore
    const issueKey = context.platformContext.issueKey;
    worklog["started"]+="T09:00:00.000+0000";
    worklog["comment"] = { "version": 1, "type": "doc", "content": [{ "type": "paragraph", "content": [{ "type": "text", "text": worklog.comment }] }] };
    const options = {
      method: 'POST',
      //headers: { 'content-type': 'application/json' },
      body: JSON.stringify(worklog)
    };
    console.log(options.body);
    console.log(`/rest/api/3/issue/${issueKey}/worklog`);
    const response = await api
      .asUser()
      .requestJira(`/rest/api/3/issue/${issueKey}/worklog`);
    const worklogData = await response.json();
    return worklogData;
  }

The console logs these proper values for body and for API endpoint

{"Additional Properties":"801","timeSpent":"4h","started":"2020-11-18T09:00:00.000+0000","comment":{"version":1,"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"test 2"}]}]}}

and

/rest/api/3/issue/RVI-3/worklog

Now, I copy these values and using Postman I can create the work log entry. But the forge app is lost, there is no response from the API endpoint (suppose there is error 500). I tried to log the response.status but there is no value.

The app never reports any error on frontend, the execution just stops with the API call.

Hey @RichardVencu,

Would you mind showing me how this function postworklogData is being called? Do you see any logs at all from the function?

Thanks. First, the above outputs are made from inside the function (i.e. options.body that is valid in Postman, and the api endpoint url). I guess it does not matter how I call the function since the post body is OK. But let me try to publish the project and then post a link here.

here it is, I started from the countries example since it has the proper Jira API examples and I modified several things to convert it to my needs.

https://github.com/rvencu/forge-worklog

Hi @RichardVencu,

I think the way you are calling refresh() is the problem here. In the example below I’ve added a handleSubmit function that sets the state, and then calls refresh. Currently, you cannot have async function calls in the top level function unless you use a hook.

const [submitted, setSubmitted] = useAction(
    (_, formData) => formData,
    console.log("submit ran"),
    undefined
  );

const handleSubmit = async (formData) => {
  setSubmitted(formData);
  await refresh();
}

return (
  <Fragment>
    <Text>Utilizati acest form pentru pontaj IBC/Focality</Text>
    <Form onSubmit={handleSubmit} name="wlform">
      <TextField name="timeSpent" label="Duration" placeholder="4h 5m" />
      <DatePicker name="started" label="Work Date" description="Enter proper work date" defaultValue={getCurrentDate()} />
      <TextArea label="Description" name="comment" />
      <Select label="Work Type" name="Additional Properties">
        {options.map(option => <Option {...option} defaultSelected={wlData.matchesWlType("801", option)} />)}
      </Select>
    </Form>
    {worklogData ? null : new worklogAdmin().render()}
  </Fragment>
);

Yes. Problem solved, thanks a lot.

Since I was able to save the worklog in Jira issue, next challenge is to save the custom data related to the worklog.
Naturally I would save it with

  postworklogDataJira = async (worklog) => {
    const context = useProductContext();
    // @ts-ignore
    const issueKey = context.platformContext.issueKey;
    worklog["started"] += "T09:00:00.000+0000";
    worklog["comment"] = { "version": 1, "type": "doc", "content": [{ "type": "paragraph", "content": [{ "type": "text", "text": worklog.comment }] }] };
    const options = {
      method: 'POST',
      body: JSON.stringify(worklog)
    };
    const response = await api
      .asUser()
      .requestJira(`/rest/api/3/issue/${issueKey}/worklog`, options);
    const worklogData = await response.json();
    const options1 = {
      method: 'PUT',
      body: JSON.stringify(worklog)
    };
    const response1 = await api
      .asUser()
      .requestJira(`/rest/api/3/issue/${issueKey}/worklog/${worklogData.id}/properties/worktype`, options1);
    const worklogData1 = await response1.json();
    console.log(worklogData1);
    return worklogData;
  }

but apparently cannot because it either says the body is not a valid JSON or this when I send a valid JSON

There was an error invoking the function - Unexpected end of JSON input

Next I thought I just use storage api / properties api to keep a key-value pair in form of

properties.onJiraIssue(worklogData.issueId).set('wltype_' + worklogData.id, submitted["wltype"]);

But despite my efforts the storage api and properties api does not seem to work like in the provided examples. I keep getting this error:

There was an error invoking the function - Cannot read property 'onJiraIssue' of undefined

similar error appears for storage with ‘get’ property

I feel out of luck here…

OK, I was able to save. The problem was the line where I tried to read the response1. it arrives in the form of string “1” not as a json, hence the error.

This is anyway against the REST API documentation that specifies the return should be a json. https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-worklog-properties/#api-rest-api-3-issue-issueidorkey-worklog-worklogid-properties-propertykey-put

Hey Richard. I am running into similar issues and trying to do a similar app as you. I tried to look at your github, but the link says not found. Is there anyway you could post the github link to your app again?