diff --git a/scripts/demo/demo-data.js b/scripts/demo/demo-data.js index 7fbf78c99..d1c8ca6bf 100644 --- a/scripts/demo/demo-data.js +++ b/scripts/demo/demo-data.js @@ -208,6 +208,11 @@ export const workspaces = [ name: 'Marketing', key: 'MKTG', description: 'Marketing campaigns and content management' + }, + { + name: 'Storefront', + key: 'STOR', + description: 'Online storefront, checkout, and payments' } ]; @@ -247,6 +252,13 @@ export const projects = [ name: 'Q1 2025 Campaign', description: 'First quarter marketing campaign and content', active: true + }, + { + workspaceKey: 'STOR', + customerName: 'Acme Corporation', + name: 'Acme Storefront', + description: 'Checkout, payments, and order management for the Acme online store', + active: true } ]; @@ -1111,6 +1123,49 @@ export const workItems = { iterationName: 'v2.0 Release', // Global release iteration due_date: '2025-02-01' } + ], + // A single inspectable bug-fix story for the public sandbox (WW-133). + // A customer report (the Story) contains the engineering Bug, which carries + // the linked test result and the sample-repository evidence. + 'STOR': [ + { + title: 'Checkout reliability', + description: 'Keep the Acme Storefront checkout correct under retries, timeouts, and duplicate submissions.', + item_type: 'Epic', + status_name: 'In Progress', + priority: 'High', + project: 'Acme Storefront', + custom_fields: { 'Story Points': 21 }, + children: [ + { + title: 'Charged full price after a payment retry', + description: 'A customer reported that an order is charged the full price when the payment is retried after a transient gateway failure. The SAVE20 coupon is applied to the first attempt but lost on the retry.', + item_type: 'Story', + status_name: 'Open', + priority: 'Critical', + project: 'Acme Storefront', + custom_fields: { + 'Request Type': 'Bug Report', + 'Customer Tier': 'Enterprise', + 'Severity': 'Critical' + }, + children: [ + { + title: 'Payment retry drops the applied discount', + description: 'chargeWithRetry recomputes the amount from the cart on every attempt, so the coupon is lost after the first failure. Keep the quoted amount across attempts. The fix and its regression test are in the linked pull request.', + item_type: 'Bug', + status_name: 'Done', + priority: 'Critical', + project: 'Acme Storefront', + custom_fields: { + 'Story Points': 3, + 'Severity': 'Critical' + } + } + ] + } + ] + } ] }; @@ -2662,3 +2717,93 @@ export const personalTasks = [ // No due date } ]; + +// Sample bug-fix story for the public sandbox (WW-133). +// +// The STOR workspace and its work items above are seeded for every demo. This +// block drives the extra evidence that only the sandbox needs: a Windshift test +// case and recorded run linked to the bug, a customer-visible update on the +// request, and (when a sample repository is supplied) an item link to the +// public fix. The repository is fictional and unrelated to Windshift core. +export const sampleStory = { + workspaceKey: 'STOR', + requestTitle: 'Charged full price after a payment retry', + bugTitle: 'Payment retry drops the applied discount', + testFolder: { + name: 'Checkout', + description: 'Checkout and payment regression tests' + }, + testCases: [ + { + folderPath: 'Checkout', + title: 'Retry after a transient payment failure keeps the discount', + preconditions: 'A cart with a 20% coupon (SAVE20) and a payment gateway that fails the first charge with a transient error.', + steps: [ + { + action: 'Start checkout with coupon SAVE20 and a gateway that fails the first charge', + data: '', + expected: 'The first attempt is rejected and the charge is retried' + }, + { + action: 'Inspect the amount charged on the successful retry', + data: '', + expected: 'The retry charges the discounted total (1600 cents), not the original subtotal (2000 cents)' + } + ] + } + ], + testPlan: { + name: 'Checkout Regression', + description: 'Regression coverage for the Acme Storefront checkout' + }, + testRunTemplate: { + name: 'Checkout regression', + description: 'Run the checkout regression suite' + }, + // Two executions tell the before/after story: the bug reproduced, then the + // fix was verified. Results are recorded against the single test case. + testRuns: [ + { + status: 'failed', + actualResult: 'Retry charged 2000 cents instead of the discounted 1600 cents' + }, + { + status: 'passed', + actualResult: 'Retry charged the discounted 1600 cents; matches the merged fix' + } + ], + requestComment: 'Thanks for the report. We reproduced this on a retried charge and shipped a fix. The regression test "Retry after a transient payment failure keeps the discount" now covers the retry path.', + bugComment: 'Root cause: chargeWithRetry recomputed the amount from the cart on every attempt, dropping the coupon. Fixed by keeping the quoted amount across attempts.', + scm: { + provider: { + slug: 'sample-checkout', + name: 'GitHub (sample-checkout)', + provider_type: 'github', + auth_method: 'pat' + }, + repository: { + externalId: 'Windshiftapp/sample-checkout', + name: 'sample-checkout', + url: 'https://github.com/Windshiftapp/sample-checkout', + defaultBranch: 'main' + }, + links: [ + { + linkType: 'pull_request', + externalId: '1', + url: 'https://github.com/Windshiftapp/sample-checkout/pull/1', + title: 'fix: keep the coupon applied when a payment is retried', + state: 'merged', + authorName: 'Stefan Ernst' + }, + { + linkType: 'commit', + externalId: '95cd7dc61a38489026bfa63a8396f79ea05b811d', + url: 'https://github.com/Windshiftapp/sample-checkout/commit/95cd7dc61a38489026bfa63a8396f79ea05b811d', + title: 'fix: keep the coupon applied when a payment is retried (#1)', + state: 'merged', + authorName: 'Stefan Ernst' + } + ] + } +}; diff --git a/scripts/demo/generate-demo.js b/scripts/demo/generate-demo.js index c4fb44cfb..add76839a 100755 --- a/scripts/demo/generate-demo.js +++ b/scripts/demo/generate-demo.js @@ -63,7 +63,8 @@ import { assetCategories, assetTypeFields, assets, - personalTasks + personalTasks, + sampleStory } from './demo-data.js'; // Date utility functions for relative date generation @@ -130,7 +131,9 @@ function parseArgs() { challenge: false, scale: false, adminUser: 'admin', - adminPassword: 'admin' + adminPassword: 'admin', + sampleRepo: null, + throttleMs: 0 }; for (let i = 0; i < args.length; i++) { @@ -175,6 +178,16 @@ function parseArgs() { case '--admin-password': options.adminPassword = args[++i]; break; + case '--sample-repo': + // Seed the sandbox bug-fix story's SCM evidence against this public + // sample repository. Omitted for partner demos. + options.sampleRepo = args[++i]; + break; + case '--throttle-ms': + // Delay between API requests. Keep a host-local seed inside the v2 + // rate limiter and the operator request budget. + options.throttleMs = parseInt(args[++i], 10) || 0; + break; case '--help': console.log(` ${colors.bright}${APP_NAME} Demo Content Generator${colors.reset} @@ -196,6 +209,9 @@ ${colors.cyan}Options:${colors.reset} --scale Generate large-scale dataset (22 workspaces, 10,000+ items) --admin-user Admin username for login (default: admin) --admin-password Admin password for login (default: admin) + --sample-repo Seed the sample bug-fix story's SCM evidence for this + public repository (sandbox only) + --throttle-ms Delay between API requests (default: 0) --help Show this help message ${colors.cyan}Examples:${colors.reset} @@ -256,8 +272,21 @@ function logInfo(message) { log(`${colors.blue}ℹ${colors.reset} ${message}`, colors.dim); } +// Delay between API requests. A seed that runs from the demo host is fast +// enough to trip the v2 rate limiter (100 req/s, burst 200) and, on the +// sandbox, the operator request budget (600/min). A small delay keeps the +// whole seed inside both without weakening either limit. 0 disables it. +let requestThrottleMs = 0; + +function sleep(ms) { + return new Promise((resolve) => setTimeout(resolve, ms)); +} + // HTTP request helper -function makeRequest(method, url, data = null, headers = {}) { +async function makeRequest(method, url, data = null, headers = {}) { + if (requestThrottleMs > 0) { + await sleep(requestThrottleMs); + } return new Promise((resolve, reject) => { const urlObj = new URL(url); const isHttps = urlObj.protocol === 'https:'; @@ -1285,8 +1314,14 @@ async function createWorkItem(baseURL, token, item, workspaceId, workspaceKey, i iterationId = iterationMap[localKey] || iterationMap[item.iterationName] || null; } - // Determine item type based on depth and characteristics - const itemTypeId = determineItemType(item, depth, itemTypes); + // Prefer an explicit item_type (by name) so a dataset can place a Bug, + // Story, or Epic precisely; otherwise fall back to the depth heuristic. + let itemTypeId; + if (item.item_type && itemTypes[item.item_type]) { + itemTypeId = itemTypes[item.item_type]; + } else { + itemTypeId = determineItemType(item, depth, itemTypes); + } const itemData = { workspace_id: workspaceId, @@ -1707,6 +1742,228 @@ async function createTestCaseLinks(baseURL, token, itemMap, testCaseMap, linkTyp return createdCount; } +// --- Sample bug-fix story (WW-133) ----------------------------------------- +// +// The STOR workspace and its work items are seeded by the generic pass above. +// This adds the evidence an evaluator follows: a regression test linked to the +// bug, a recorded run, a customer-visible update, and (sandbox only) an item +// link to the public fix. No GitHub credentials are used; the link is written +// through the normal item SCM-link API as trusted provisioning. + +// executeSampleTestRun executes a template, records one outcome per result, +// and ends the run. +async function executeSampleTestRun(baseURL, token, workspaceId, templateId, run) { + const testRun = await v2(baseURL, token, 'POST', `/api/v2/workspaces/${workspaceId}/test-run-templates/${templateId}/execute`, {}); + const results = await v2(baseURL, token, 'GET', `/api/v2/workspaces/${workspaceId}/test-runs/${testRun.id}/results`); + for (const result of Array.isArray(results) ? results : []) { + await v2Patch(baseURL, token, `/api/v2/workspaces/${workspaceId}/test-runs/${testRun.id}/results/${result.id}`, { + status: run.status, + actual_result: run.actualResult + }); + } + await v2(baseURL, token, 'POST', `/api/v2/workspaces/${workspaceId}/test-runs/${testRun.id}/end`, {}); + logSuccess(`Recorded ${run.status} run (${Array.isArray(results) ? results.length : 0} result(s))`); +} + +// createItemComment adds one comment. Private comments are internal notes; +// public comments are the customer-visible updates. +async function createItemComment(baseURL, token, itemId, content, isPrivate) { + try { + await v2(baseURL, token, 'POST', `/api/v2/items/${itemId}/comments`, { content, is_private: isPrivate }); + } catch (error) { + logError(`Failed to comment on item ${itemId}: ${error.message}`); + } +} + +// seedSampleScmLink writes the SCM provider, workspace connection, repository, +// and item links that the GitHub integration would normally create. It never +// calls GitHub and stores no credential. +async function seedSampleScmLink(baseURL, token, repoURL, workspaceId, bugId) { + const { provider, repository, links } = sampleStory.scm; + + const providersResp = await makeAuthRequest(baseURL, 'GET', '/api/admin/scm-providers', null, token); + const providers = Array.isArray(providersResp.data) ? providersResp.data : []; + let providerId = (providers.find(p => p.slug === provider.slug) || {}).id; + + if (!providerId) { + const created = await makeAuthRequest(baseURL, 'POST', '/api/admin/scm-providers', { + slug: provider.slug, + name: provider.name, + provider_type: provider.provider_type, + auth_method: provider.auth_method, + enabled: true, + is_default: false, + workspace_restriction_mode: 'unrestricted' + }, token); + if (created.status < 200 || created.status >= 300) { + throw new Error(`provider create failed: ${created.status} - ${JSON.stringify(created.data)?.slice(0, 200)}`); + } + providerId = created.data.id; + logSuccess(`Created SCM provider: ${provider.name}`); + } + + const connectionsResp = await makeAuthRequest(baseURL, 'GET', `/api/workspaces/${workspaceId}/scm-connections`, null, token); + const connections = Array.isArray(connectionsResp.data) ? connectionsResp.data : []; + let connectionId = (connections.find(c => c.scm_provider_id === providerId) || {}).id; + + if (!connectionId) { + const created = await makeAuthRequest(baseURL, 'POST', `/api/workspaces/${workspaceId}/scm-connections`, { + scm_provider_id: providerId + }, token); + if (created.status < 200 || created.status >= 300) { + throw new Error(`connection create failed: ${created.status}`); + } + connectionId = created.data.id; + logSuccess('Created workspace SCM connection'); + } + + const reposResp = await makeAuthRequest(baseURL, 'GET', `/api/workspaces/${workspaceId}/scm-connections/${connectionId}/repositories`, null, token); + const repos = Array.isArray(reposResp.data) ? reposResp.data : []; + let repoId = (repos.find(r => r.repository_external_id === repository.externalId) || {}).id; + + if (!repoId) { + const created = await makeAuthRequest(baseURL, 'POST', `/api/workspaces/${workspaceId}/scm-connections/${connectionId}/repositories`, { + repository_external_id: repository.externalId, + repository_name: repository.name, + repository_url: repoURL, + default_branch: repository.defaultBranch + }, token); + if (created.status < 200 || created.status >= 300) { + throw new Error(`repository link failed: ${created.status}`); + } + repoId = created.data.id; + logSuccess(`Linked repository: ${repository.name}`); + } + + for (const link of links) { + const created = await makeAuthRequest(baseURL, 'POST', `/api/items/${bugId}/scm-links`, { + workspace_repository_id: repoId, + link_type: link.linkType, + external_id: link.externalId, + external_url: link.url, + title: link.title, + state: link.state, + author_name: link.authorName + }, token); + if (created.status < 200 || created.status >= 300) { + throw new Error(`SCM link create failed for ${link.linkType} ${link.externalId}: ${created.status}`); + } + } + logSuccess(`Linked the bug to ${repoURL}`); +} + +// verifySampleStory re-reads the seeded evidence over the API. It runs on the +// sandbox seed so a nightly reset fails loudly if any link is missing. +async function verifySampleStory(baseURL, token, { requestId, bugId, testCaseId }) { + const unwrap = (response) => { + const body = response.data; + return body && typeof body === 'object' && 'data' in body ? body.data : body; + }; + const problems = []; + + const scmLinks = unwrap(await makeAuthRequest(baseURL, 'GET', `/api/items/${bugId}/scm-links`, null, token)) || []; + if (!scmLinks.some(link => link.link_type === 'pull_request' && link.external_url)) { + problems.push('no pull_request SCM link on the bug'); + } + + const linkBody = unwrap(await makeAuthRequest(baseURL, 'GET', `/api/v2/items/${bugId}/links`, null, token)) || {}; + const links = [...(linkBody.outgoing || []), ...(linkBody.incoming || [])]; + if (!links.some(link => link.source_type === 'test_case' && link.source_id === testCaseId)) { + problems.push('the regression test is not linked to the bug'); + } + + const commentBody = unwrap(await makeAuthRequest(baseURL, 'GET', `/api/v2/items/${requestId}/comments`, null, token)) || {}; + const comments = commentBody.comments || []; + if (!comments.some(comment => comment.is_private === false)) { + problems.push('no customer-visible comment on the request'); + } + + if (problems.length > 0) { + throw new Error(`sample story verification failed: ${problems.join('; ')}`); + } + logSuccess('Verified the sample story links'); +} + +async function seedSampleStory(baseURL, token, options, { workspaceMap, itemMap, linkTypeMap }) { + const workspaceId = workspaceMap[sampleStory.workspaceKey]; + if (!workspaceId) { + logError(`Sample story workspace ${sampleStory.workspaceKey} not found - skipping sample story`); + return; + } + + const requestId = itemMap[`${sampleStory.workspaceKey}:${sampleStory.requestTitle}`]; + const bugId = itemMap[`${sampleStory.workspaceKey}:${sampleStory.bugTitle}`]; + if (!requestId || !bugId) { + logError('Sample story work items not found - skipping sample story'); + return; + } + + logSection('Seeding Sample Bug-Fix Story (WW-133)'); + + // Regression test case, in its own folder in the storefront workspace. + const folderMap = await createTestFolders(baseURL, token, workspaceId, [sampleStory.testFolder]); + const testCaseMap = await createTestCases(baseURL, token, workspaceId, folderMap, {}, sampleStory.testCases); + const testCaseId = testCaseMap[`${sampleStory.testCases[0].folderPath}:${sampleStory.testCases[0].title}`]; + if (!testCaseId) { + logError('Sample story test case was not created'); + return; + } + + // Test plan, template, and the before/after executions. + let planId = null; + try { + const plan = await v2(baseURL, token, 'POST', `/api/v2/workspaces/${workspaceId}/test-plans`, { + name: sampleStory.testPlan.name, + description: sampleStory.testPlan.description + }); + planId = plan.id; + await v2(baseURL, token, 'POST', `/api/v2/workspaces/${workspaceId}/test-plans/${planId}/test-cases`, { test_case_id: testCaseId }); + logSuccess(`Created test plan: ${sampleStory.testPlan.name}`); + + const template = await v2(baseURL, token, 'POST', `/api/v2/workspaces/${workspaceId}/test-run-templates`, { + plan_id: planId, + name: sampleStory.testRunTemplate.name, + description: sampleStory.testRunTemplate.description + }); + logSuccess(`Created test run template: ${sampleStory.testRunTemplate.name}`); + + for (const run of sampleStory.testRuns) { + await executeSampleTestRun(baseURL, token, workspaceId, template.id, run); + } + } catch (error) { + logError(`Failed to seed sample story test runs: ${error.message}`); + } + + // Traceability: the regression test covers the bug. + const testsLinkTypeId = linkTypeMap['Tests']; + if (testsLinkTypeId) { + try { + await v2(baseURL, token, 'POST', '/api/v2/links', { + link_type_id: testsLinkTypeId, + source_type: 'test_case', + source_id: testCaseId, + target_type: 'item', + target_id: bugId + }); + logSuccess('Linked the regression test to the bug'); + } catch (error) { + logError(`Failed to link the regression test: ${error.message}`); + } + } + + // Customer-visible update on the request, internal note on the bug. + await createItemComment(baseURL, token, requestId, sampleStory.requestComment, false); + await createItemComment(baseURL, token, bugId, sampleStory.bugComment, true); + + // Sandbox only: link the bug to the public fix. Partner demos omit the flag. + // A sandbox seed that cannot write the evidence must fail the nightly reset, + // so this path verifies and rethrows instead of logging and continuing. + if (options.sampleRepo) { + await seedSampleScmLink(baseURL, token, options.sampleRepo, workspaceId, bugId); + await verifySampleStory(baseURL, token, { requestId, bugId, testCaseId }); + } +} + // Create asset management sets async function createAssetSets(baseURL, token) { logSection('Creating Asset Management Sets'); @@ -2044,6 +2301,7 @@ async function createComments(baseURL, token, itemMap, userMap, scaleModule) { // Main execution async function main() { const options = parseArgs(); + requestThrottleMs = options.throttleMs || 0; log(`${colors.bright}${colors.magenta} ╔═══════════════════════════════════════════════════════╗ @@ -2190,6 +2448,10 @@ ${colors.reset}`); linkCount = await createTestCaseLinks(options.baseURL, token, itemMap, testCaseMap, linkTypeMap); } + // Sample bug-fix story (WW-133): regression test, recorded run, and + // customer-visible update; the SCM link is seeded only with --sample-repo. + await seedSampleStory(options.baseURL, token, options, { workspaceMap, itemMap, linkTypeMap }); + // Asset Management const assetSetMap = await createAssetSets(options.baseURL, token); const assetTypeMap = await createAssetTypes(options.baseURL, token, assetSetMap); @@ -2302,6 +2564,7 @@ export { createWorkItems, createComments, createPersonalTasks, + seedSampleStory, createAssetSets, createAssetTypes, createAssetCategories,