|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +jest.mock('#pipeline-utils', () => ({ |
| 11 | + upsertComment: jest.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +import { buildCommentBody, type ImpactEntry } from './notify_api_contract_owners'; |
| 15 | + |
| 16 | +const entry = (overrides: Partial<ImpactEntry> = {}): ImpactEntry => ({ |
| 17 | + path: '/api/spaces/space', |
| 18 | + method: 'GET', |
| 19 | + reason: 'Endpoint removed', |
| 20 | + terraformResource: 'elasticstack_kibana_space', |
| 21 | + owners: ['@elastic/kibana-security'], |
| 22 | + ...overrides, |
| 23 | +}); |
| 24 | + |
| 25 | +describe('buildCommentBody', () => { |
| 26 | + it('renders a markdown table with one entry', () => { |
| 27 | + const body = buildCommentBody([entry()]); |
| 28 | + |
| 29 | + expect(body).toContain('## API Contract Breaking Changes'); |
| 30 | + expect(body).toContain('| `/api/spaces/space` `GET`'); |
| 31 | + expect(body).toContain('elasticstack_kibana_space'); |
| 32 | + expect(body).toContain('@elastic/kibana-security'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('deduplicates owners in the cc line', () => { |
| 36 | + const entries = [ |
| 37 | + entry({ owners: ['@elastic/kibana-security'] }), |
| 38 | + entry({ path: '/api/spaces/space/{id}', owners: ['@elastic/kibana-security'] }), |
| 39 | + ]; |
| 40 | + const body = buildCommentBody(entries); |
| 41 | + |
| 42 | + const ccLine = body.split('\n').find((l) => l.startsWith('cc '))!; |
| 43 | + const mentions = ccLine.replace('cc ', '').trim().split(' '); |
| 44 | + expect(mentions).toEqual(['@elastic/kibana-security']); |
| 45 | + }); |
| 46 | + |
| 47 | + it('aggregates multiple distinct owners', () => { |
| 48 | + const entries = [ |
| 49 | + entry({ owners: ['@elastic/kibana-security'] }), |
| 50 | + entry({ |
| 51 | + path: '/api/fleet/agent_policies', |
| 52 | + method: 'POST', |
| 53 | + terraformResource: 'elasticstack_fleet_agent_policy', |
| 54 | + owners: ['@elastic/fleet'], |
| 55 | + }), |
| 56 | + ]; |
| 57 | + const body = buildCommentBody(entries); |
| 58 | + |
| 59 | + expect(body).toContain('@elastic/kibana-security'); |
| 60 | + expect(body).toContain('@elastic/fleet'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('shows _unknown_ when no owners exist', () => { |
| 64 | + const body = buildCommentBody([entry({ owners: [] })]); |
| 65 | + |
| 66 | + expect(body).toContain('cc _unknown_'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('escapes pipe characters in the reason field', () => { |
| 70 | + const body = buildCommentBody([entry({ reason: 'field|was|removed' })]); |
| 71 | + |
| 72 | + expect(body).toContain('field\\|was\\|removed'); |
| 73 | + expect(body).not.toContain('field|was|removed'); |
| 74 | + }); |
| 75 | + |
| 76 | + it('escapes newlines in the reason field', () => { |
| 77 | + const body = buildCommentBody([entry({ reason: 'line1\nline2' })]); |
| 78 | + |
| 79 | + expect(body).toContain('line1 line2'); |
| 80 | + expect(body).not.toContain('line1\nline2'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('omits method badge when method is undefined', () => { |
| 84 | + const body = buildCommentBody([entry({ method: undefined })]); |
| 85 | + |
| 86 | + expect(body).toContain('| `/api/spaces/space` |'); |
| 87 | + expect(body).not.toMatch(/`GET`|`POST`|`PUT`|`DELETE`/); |
| 88 | + }); |
| 89 | +}); |
0 commit comments