|
1 | | -import { synchronizeIssueLabels } from '../../src/labels.js'; |
| 1 | +import { synchronizeIssueLabels, ensureLabelsExist } from '../../src/labels.js'; |
2 | 2 |
|
3 | 3 | function createMockOctokit() { |
4 | 4 | return { |
@@ -114,3 +114,76 @@ describe('synchronizeIssueLabels', () => { |
114 | 114 | ).rejects.toThrow('API down'); |
115 | 115 | }); |
116 | 116 | }); |
| 117 | + |
| 118 | +describe('ensureLabelsExist', () => { |
| 119 | + let octokit; |
| 120 | + beforeEach(() => { octokit = createMockOctokit(); }); |
| 121 | + |
| 122 | + it('creates missing labels with DEPENDABOT_LABEL_DEFAULTS', async () => { |
| 123 | + octokit.paginate.mockResolvedValue([]); |
| 124 | + |
| 125 | + await ensureLabelsExist(octokit, 'owner', 'repo', ['dependencies']); |
| 126 | + |
| 127 | + expect(octokit.request).toHaveBeenCalledWith( |
| 128 | + 'POST /repos/{owner}/{repo}/labels', |
| 129 | + expect.objectContaining({ name: 'dependencies', color: '0366d6', description: 'Dependency updates' }) |
| 130 | + ); |
| 131 | + }); |
| 132 | + |
| 133 | + it('uses fallback defaults for unknown labels', async () => { |
| 134 | + octokit.paginate.mockResolvedValue([]); |
| 135 | + |
| 136 | + await ensureLabelsExist(octokit, 'owner', 'repo', ['custom-label']); |
| 137 | + |
| 138 | + expect(octokit.request).toHaveBeenCalledWith( |
| 139 | + 'POST /repos/{owner}/{repo}/labels', |
| 140 | + expect.objectContaining({ name: 'custom-label', color: 'ededed', description: 'Automated label' }) |
| 141 | + ); |
| 142 | + }); |
| 143 | + |
| 144 | + it('skips labels that already exist', async () => { |
| 145 | + octokit.paginate.mockResolvedValue([{ name: 'dependencies', color: '0366d6', description: 'Deps' }]); |
| 146 | + |
| 147 | + await ensureLabelsExist(octokit, 'owner', 'repo', ['dependencies']); |
| 148 | + |
| 149 | + expect(octokit.request).not.toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + |
| 152 | + it('never updates or deletes existing labels', async () => { |
| 153 | + octokit.paginate.mockResolvedValue([ |
| 154 | + { name: 'dependencies', color: 'ffffff', description: 'Old' }, |
| 155 | + { name: 'extra', color: 'aaa', description: 'Extra' } |
| 156 | + ]); |
| 157 | + |
| 158 | + await ensureLabelsExist(octokit, 'owner', 'repo', ['dependencies']); |
| 159 | + |
| 160 | + expect(octokit.request).not.toHaveBeenCalledWith( |
| 161 | + 'PATCH /repos/{owner}/{repo}/labels/{name}', |
| 162 | + expect.anything() |
| 163 | + ); |
| 164 | + expect(octokit.request).not.toHaveBeenCalledWith( |
| 165 | + 'DELETE /repos/{owner}/{repo}/labels/{name}', |
| 166 | + expect.anything() |
| 167 | + ); |
| 168 | + }); |
| 169 | + |
| 170 | + it('creates only missing labels from a mixed set', async () => { |
| 171 | + octokit.paginate.mockResolvedValue([{ name: 'dependencies', color: '0366d6', description: 'Deps' }]); |
| 172 | + |
| 173 | + await ensureLabelsExist(octokit, 'owner', 'repo', ['dependencies', 'automation']); |
| 174 | + |
| 175 | + expect(octokit.request).toHaveBeenCalledTimes(1); |
| 176 | + expect(octokit.request).toHaveBeenCalledWith( |
| 177 | + 'POST /repos/{owner}/{repo}/labels', |
| 178 | + expect.objectContaining({ name: 'automation', color: '0e8a16' }) |
| 179 | + ); |
| 180 | + }); |
| 181 | + |
| 182 | + it('handles empty label list', async () => { |
| 183 | + octokit.paginate.mockResolvedValue([{ name: 'existing' }]); |
| 184 | + |
| 185 | + await ensureLabelsExist(octokit, 'owner', 'repo', []); |
| 186 | + |
| 187 | + expect(octokit.request).not.toHaveBeenCalled(); |
| 188 | + }); |
| 189 | +}); |
0 commit comments