-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathcreateWorkItem.ts
More file actions
67 lines (60 loc) · 1.98 KB
/
createWorkItem.ts
File metadata and controls
67 lines (60 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import axios from "axios";
import { getConnection } from "./shared/auth.js";
const API_VERSION = "v65.0";
export interface CreateWorkItemParams {
usernameOrAlias: string;
projectId: string;
subject: string;
description: string;
}
export interface CreateWorkItemResult {
success: boolean;
workItemId?: string;
workItemName?: string;
subject?: string;
error?: string;
}
/**
* Creates a new DevOps Center Work Item in the specified project.
* API: POST /services/data/v65.0/connect/devops/projects/<ProjectID>/workitem
* Body: { subject: string, description: string }
*/
export async function createWorkItem(params: CreateWorkItemParams): Promise<CreateWorkItemResult> {
const { usernameOrAlias, projectId, subject, description } = params;
const connection = await getConnection(usernameOrAlias);
const accessToken = connection.accessToken;
const instanceUrl = connection.instanceUrl;
if (!accessToken || !instanceUrl) {
return {
success: false,
error: "Missing access token or instance URL.",
};
}
const url = `${instanceUrl}/services/data/${API_VERSION}/connect/devops/projects/${projectId}/workitem`;
const headers = {
Authorization: `Bearer ${accessToken}`,
"Content-Type": "application/json",
};
const body = { subject, description };
try {
const response = await axios.post(url, body, { headers });
const data = response.data ?? {};
return {
success: true,
workItemId: data.id ?? data.Id,
workItemName: data.name ?? data.Name,
subject: data.subject ?? data.Subject ?? subject,
};
} catch (error: any) {
const data = error.response?.data;
const message =
(typeof data === "object" && (data?.message ?? data?.error ?? data?.errorDescription)) ??
error.message ??
"Unknown error";
const details = Array.isArray(data?.body) ? data.body.join("; ") : undefined;
return {
success: false,
error: details ? `${message}: ${details}` : String(message),
};
}
}