-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathutils.ts
More file actions
120 lines (108 loc) · 3.6 KB
/
Copy pathutils.ts
File metadata and controls
120 lines (108 loc) · 3.6 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
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
import type { AzureDevOpsComment, AzureDevOpsWorkItem } from '@/tools/azure_devops/types'
/** States for Azure DevOps Basic process work items (Issue, Task, Epic). */
export const AZURE_DEVOPS_BASIC_WORK_ITEM_STATES = ['To Do', 'Doing', 'Done'] as const
/** Work item types for Azure DevOps Basic process. */
export const AZURE_DEVOPS_BASIC_WORK_ITEM_TYPES = ['Issue', 'Task', 'Epic'] as const
export type AzureDevOpsJsonPatchOp = {
op: string
path: string
value: string | number
}
/**
* Appends a JSON-Patch op for a single work item field when the value is non-empty.
* Skips silently on undefined/empty-string. Numbers are validated; strings are
* passed through.
*/
export function appendFieldPatchOp(
ops: AzureDevOpsJsonPatchOp[],
refName: string,
value: string | number | undefined,
patchOp: 'add' | 'replace',
kind: 'number' | 'string'
): void {
if (value === undefined || value === '') return
if (kind === 'number') {
const numeric = Number(value)
if (Number.isNaN(numeric)) return
ops.push({ op: patchOp, path: `/fields/${refName}`, value: numeric })
return
}
ops.push({ op: patchOp, path: `/fields/${refName}`, value: String(value) })
}
/**
* Appends a Microsoft.VSTS.Scheduling.Effort patch when effort is a valid number.
* Field availability depends on work item type and process template (Issue in Basic).
*/
export function appendEffortPatchOp(
ops: AzureDevOpsJsonPatchOp[],
effort: number | string | undefined,
patchOp: 'add' | 'replace'
): void {
appendFieldPatchOp(ops, 'Microsoft.VSTS.Scheduling.Effort', effort, patchOp, 'number')
}
export function mapWorkItem(raw: AzureDevOpsRawWorkItem): AzureDevOpsWorkItem {
const fields = raw.fields ?? {}
return {
id: raw.id,
title: (fields['System.Title'] as string | undefined) ?? '',
state: (fields['System.State'] as string | undefined) ?? '',
workItemType: (fields['System.WorkItemType'] as string | undefined) ?? '',
assignedTo:
(fields['System.AssignedTo'] as { displayName?: string } | undefined)?.displayName ?? null,
areaPath: (fields['System.AreaPath'] as string | undefined) ?? '',
url: raw.url,
}
}
export function formatWorkItem(w: AzureDevOpsWorkItem): string {
return [
`ID: ${w.id} [${w.workItemType}] ${w.title}`,
` State: ${w.state}`,
` Assigned To: ${w.assignedTo ?? 'Unassigned'}`,
` Area: ${w.areaPath}`,
].join('\n')
}
export interface AzureDevOpsRawWorkItem {
id: number
url: string
fields: Record<string, unknown>
}
export function mapComment(raw: AzureDevOpsRawComment): AzureDevOpsComment {
return {
workItemId: raw.workItemId,
commentId: raw.commentId ?? raw.id,
version: raw.version,
text: raw.text,
renderedText: raw.renderedText,
createdBy: raw.createdBy?.displayName ?? null,
createdDate: raw.createdDate,
modifiedBy: raw.modifiedBy?.displayName ?? null,
modifiedDate: raw.modifiedDate,
isDeleted: raw.isDeleted ?? false,
url: raw.url,
}
}
export function formatComment(comment: AzureDevOpsComment): string {
return [
`Comment #${comment.commentId} on work item #${comment.workItemId}`,
` Author: ${comment.createdBy ?? 'Unknown'}`,
` Created: ${comment.createdDate}`,
` Text: ${comment.text}`,
].join('\n')
}
interface AzureDevOpsIdentityRef {
displayName?: string
}
export interface AzureDevOpsRawComment {
id: number
commentId?: number
workItemId: number
version: number
text: string
renderedText?: string
createdBy?: AzureDevOpsIdentityRef
createdDate: string
modifiedBy?: AzureDevOpsIdentityRef
modifiedDate: string
isDeleted?: boolean
url: string
}