Cookbook¶
The snippets below use the restFetch helper from Calling the REST API from a view and a context obtained from fetchContext(). Resource names and attribute ids follow the Polarion REST API v1 and were exercised against a 2612 server for reading work items (including created and the assignee relationship), Lucene queries, linked work items with include=workItem, PATCH of attributes and POST of comments. For the remaining recipes the OpenAPI definition of your server (https://<host>/polarion/rest/v1/definition) is the authority for field names and optional parameters.
Read one work item with chosen fields¶
const { data } = await restFetch<WorkItemResponse>(
context,
`/projects/${context.projectId}/workitems/${context.workItemId}` +
`?fields[workitems]=id,title,type,status,priority,severity,description,created,updated`,
);
data.attributes.title;
data.attributes.description?.value; // HTML
Read the assignee and author (relationships)¶
const { data, included } = await restFetch<WorkItemResponse>(
context,
`/projects/${p}/workitems/${id}?fields[workitems]=id,title,assignee,author&include=assignee,author&fields[users]=id,name,email`,
);
const assigneeIds = data.relationships?.assignee?.data?.map((u) => u.id) ?? []; // ["admin"]
const users = new Map((included ?? []).map((u) => [u.id, u.attributes]));
Query work items¶
const params = new URLSearchParams({
query: 'type:requirement AND status:open AND NOT HAS_VALUE:assignee',
sort: '-updated',
'fields[workitems]': 'id,title,status,type,updated',
'page[size]': '100',
'page[number]': '1',
});
const list = await restFetch<WorkItemListResponse>(context, `/projects/${p}/workitems?${params}`);
list.data; // array of resources
list.meta?.totalCount; // total across pages
The query syntax is the Lucene syntax of the Work Items table. A set of ids: id:(TEST-1 TEST-2 TEST-3).
Page through a large result¶
async function allWorkItems(context: PolarionContext, p: string, query: string) {
const out: WorkItemResource[] = [];
for (let page = 1; ; page++) {
const params = new URLSearchParams({ query, 'fields[workitems]': 'id,title', 'page[size]': '100', 'page[number]': String(page) });
const r = await restFetch<WorkItemListResponse>(context, `/projects/${p}/workitems?${params}`);
out.push(...r.data);
// Stop on the reported total, not on a short page: a server may cap page[size] below 100.
const total = r.meta?.totalCount;
if (r.data.length === 0 || (total !== undefined && out.length >= total)) return out;
}
}
The loop ends when all items reported by meta.totalCount are collected, or when a page comes back empty. It does not rely on the requested page size, because the server can serve fewer items per page than requested.
For thousands of items, move the aggregation into an endpoint.
Update attributes¶
await restFetch(context, `/projects/${p}/workitems/${id}`, {
method: 'PATCH',
body: JSON.stringify({
data: { type: 'workitems', id: `${p}/${id}`, attributes: { title: 'New title', priority: '90.0' } },
}),
});
Enumeration fields take the option id (priority: '90.0', severity: 'must_have'), not the label.
Change the status¶
await restFetch(context, `/projects/${p}/workitems/${id}`, {
method: 'PATCH',
body: JSON.stringify({ data: { type: 'workitems', id: `${p}/${id}`, attributes: { status: 'inprogress' } } }),
});
The transition must be allowed by the workflow for the current status and the user's role; otherwise the API answers 400 or 403 with the reason in errors[].detail. Newer servers also expose explicit workflow actions (see …/workitems/{id}/actions in the definition).
Set the assignee¶
await restFetch(context, `/projects/${p}/workitems/${id}`, {
method: 'PATCH',
body: JSON.stringify({
data: {
type: 'workitems',
id: `${p}/${id}`,
relationships: { assignee: { data: [{ type: 'users', id: 'jdoe' }] } },
},
}),
});
Create a work item¶
const created = await restFetch<WorkItemListResponse>(context, `/projects/${p}/workitems`, {
method: 'POST',
body: JSON.stringify({
data: [
{ type: 'workitems', attributes: { type: 'task', title: 'Follow-up', description: { type: 'text/html', value: '<p>Created by the app.</p>' } } },
],
}),
});
created.data[0].id; // "test/TEST-600"
Link two work items¶
await restFetch(context, `/projects/${p}/workitems/${id}/linkedworkitems`, {
method: 'POST',
body: JSON.stringify({
data: [
{
type: 'linkedworkitems',
attributes: { role: 'relates_to' },
relationships: { workItem: { data: { type: 'workitems', id: `${p}/TEST-513` } } },
},
],
}),
});
Role ids come from the project's link role enumeration (relates_to, implements, verifies, …).
Read links with the linked items' titles¶
const r = await restFetch<LinkedWorkItemsResponse>(
context,
`/projects/${p}/workitems/${id}/linkedworkitems?fields[linkedworkitems]=role,suspect,workItem&include=workItem&fields[workitems]=id,title,status`,
);
const byId = new Map((r.included ?? []).map((wi) => [wi.id, wi]));
r.data.map((link) => ({ role: link.attributes.role, item: byId.get(link.relationships.workItem.data.id) }));
Add a comment¶
await restFetch(context, `/projects/${p}/workitems/${id}/comments`, {
method: 'POST',
body: JSON.stringify({
data: [{ type: 'workitem_comments', attributes: { text: { type: 'text/html', value: '<p>Reviewed by the app.</p>' } } }],
}),
});
Escape user input before putting it into HTML.
Read a Document and its work items¶
const doc = await restFetch<DocumentResponse>(
context,
`/projects/${p}/spaces/${context.spaceId}/documents/${context.documentId}?fields[documents]=title,type,status,updated`,
);
const parts = await restFetch<DocumentPartsResponse>(
context,
`/projects/${p}/spaces/${context.spaceId}/documents/${context.documentId}/parts?include=workItem&fields[workitems]=id,title,type`,
);
Test runs and records¶
const runs = await restFetch<TestRunListResponse>(context, `/projects/${p}/testruns?fields[testruns]=id,title,status,finishedOn&page[size]=50`);
const records = await restFetch<TestRecordListResponse>(
context,
`/projects/${p}/testruns/${runId}/testrecords?include=testCase&fields[testrecords]=result,executed&fields[workitems]=id,title`,
);
Enumerations (for select boxes)¶
const statuses = await restFetch<EnumerationResponse>(context, `/projects/${p}/enumerations/~/status/~`);
// options: statuses.data.attributes.options → [{ id, name, color, … }]
The exact path depends on the enumeration's context (work item type specific enumerations have their own path); check the definition.
Users¶
const me = await restFetch<UserResponse>(context, `/users/${userId}?fields[users]=id,name,email`);
Views do not know the current user's id directly; an endpoint can return it from the security service, or compare relationships.author of an item the user just created.
Error body¶
All errors share one shape:
{ "errors": [ { "status": "400", "title": "Bad Request", "detail": "…", "source": { "pointer": "$.data.attributes.status" } } ] }
Show detail to the user; it usually names the field or rule that failed.