Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add parent-child relationship support for createWorkItem #35

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

# Run prettier
npm run format

# Run eslint
npm run lint
69 changes: 69 additions & 0 deletions src/features/work-items/create-work-item/feature.spec.int.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,73 @@ describe('createWorkItem integration', () => {
expect(result.fields['System.Tags']).toContain('Automated');
}
});

test('should create a child work item with parent-child relationship', async () => {
// Skip if no connection is available
if (shouldSkipIntegrationTest()) {
return;
}

// This connection must be available if we didn't skip
if (!connection) {
throw new Error(
'Connection should be available when test is not skipped',
);
}

// For a true integration test, use a real project
const projectName =
process.env.AZURE_DEVOPS_DEFAULT_PROJECT || 'DefaultProject';

// First, create a parent work item (User Story)
const parentTitle = `Parent Story ${new Date().toISOString()}`;
const parentOptions: CreateWorkItemOptions = {
title: parentTitle,
description: 'This is a parent user story',
};

const parentResult = await createWorkItem(
connection,
projectName,
'User Story', // Assuming User Story type exists
parentOptions,
);

expect(parentResult).toBeDefined();
expect(parentResult.id).toBeDefined();
const parentId = parentResult.id;

// Now create a child work item (Task) with a link to the parent
const childTitle = `Child Task ${new Date().toISOString()}`;
const childOptions: CreateWorkItemOptions = {
title: childTitle,
description: 'This is a child task of a user story',
parentId: parentId, // Reference to parent work item
};

const childResult = await createWorkItem(
connection,
projectName,
'Task',
childOptions,
);

// Assert the child work item was created
expect(childResult).toBeDefined();
expect(childResult.id).toBeDefined();

// Now verify the parent-child relationship
// We would need to fetch the relations, but for now we'll just assert
// that the response indicates a relationship was created
expect(childResult.relations).toBeDefined();

// Check that at least one relation exists that points to our parent
const parentRelation = childResult.relations?.find(
(relation) =>
relation.rel === 'System.LinkTypes.Hierarchy-Reverse' &&
relation.url &&
relation.url.includes(`/${parentId}`),
);
expect(parentRelation).toBeDefined();
});
});
12 changes: 12 additions & 0 deletions src/features/work-items/create-work-item/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ export async function createWorkItem(
});
}

// 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)) {
Expand Down
4 changes: 4 additions & 0 deletions src/features/work-items/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export const CreateWorkItemSchema = z.object({
.optional()
.describe('The iteration path for the work item'),
priority: z.number().optional().describe('The priority of the work item'),
parentId: z
.number()
.optional()
.describe('The ID of the parent work item to create a relationship with'),
additionalFields: z
.record(z.string(), z.any())
.optional()
Expand Down
1 change: 1 addition & 0 deletions src/features/work-items/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface CreateWorkItemOptions {
areaPath?: string;
iterationPath?: string;
priority?: number;
parentId?: number;
additionalFields?: Record<string, string | number | boolean | null>;
}

Expand Down
1 change: 1 addition & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export function createAzureDevOpsServer(config: AzureDevOpsConfig): Server {
areaPath: args.areaPath,
iterationPath: args.iterationPath,
priority: args.priority,
parentId: args.parentId,
additionalFields: args.additionalFields,
},
);
Expand Down