-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathfeature.ts
122 lines (108 loc) · 2.91 KB
/
feature.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { WebApi } from 'azure-devops-node-api';
import { AzureDevOpsError } from '../../../shared/errors';
import { CreateWorkItemOptions, WorkItem } from '../types';
/**
* Create a work item
*
* @param connection The Azure DevOps WebApi connection
* @param projectId The ID or name of the project
* @param workItemType The type of work item to create (e.g., "Task", "Bug", "User Story")
* @param options Options for creating the work item
* @returns The created work item
*/
export async function createWorkItem(
connection: WebApi,
projectId: string,
workItemType: string,
options: CreateWorkItemOptions,
): Promise<WorkItem> {
try {
if (!options.title) {
throw new Error('Title is required');
}
const witApi = await connection.getWorkItemTrackingApi();
// Create the JSON patch document
const document = [];
// Add required fields
document.push({
op: 'add',
path: '/fields/System.Title',
value: options.title,
});
// Add optional fields if provided
if (options.description) {
document.push({
op: 'add',
path: '/fields/System.Description',
value: options.description,
});
}
if (options.assignedTo) {
document.push({
op: 'add',
path: '/fields/System.AssignedTo',
value: options.assignedTo,
});
}
if (options.areaPath) {
document.push({
op: 'add',
path: '/fields/System.AreaPath',
value: options.areaPath,
});
}
if (options.iterationPath) {
document.push({
op: 'add',
path: '/fields/System.IterationPath',
value: options.iterationPath,
});
}
if (options.priority !== undefined) {
document.push({
op: 'add',
path: '/fields/Microsoft.VSTS.Common.Priority',
value: options.priority,
});
}
// Add parent relationship if parentId is provided
if (options.parentId) {
document.push({
op: 'add',
path: '/relations/-',
value: {
rel: 'System.LinkTypes.Hierarchy-Reverse',
url: `${connection.serverUrl}/_apis/wit/workItems/${options.parentId}`,
},
});
}
// Add any additional fields
if (options.additionalFields) {
for (const [key, value] of Object.entries(options.additionalFields)) {
document.push({
op: 'add',
path: `/fields/${key}`,
value: value,
});
}
}
// Create the work item
const workItem = await witApi.createWorkItem(
null,
document,
projectId,
workItemType,
);
if (!workItem) {
throw new Error('Failed to create work item');
}
return workItem;
} catch (error) {
if (error instanceof AzureDevOpsError) {
throw error;
}
throw new Error(
`Failed to create work item: ${error instanceof Error ? error.message : String(error)}`,
);
}
}