Skip to content

Commit 1ddd8e9

Browse files
authored
feat(config): add per-repo comment overrides for labels config (#96)
Convert defaultControlComments and defaultStatusComments from flat booleans to { enabled, overrides } objects, allowing per-repo control over mission control and status comments on PRs.
1 parent f88b620 commit 1ddd8e9

7 files changed

Lines changed: 171 additions & 81 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2025 GoodRx, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import { Knex } from 'knex';
18+
19+
export async function up(knex: Knex): Promise<any> {
20+
const existingLabels = await knex('global_config').where('key', 'labels').first();
21+
if (!existingLabels) return;
22+
23+
const config = existingLabels.config;
24+
25+
if (typeof config.defaultControlComments === 'boolean') {
26+
config.defaultControlComments = { enabled: config.defaultControlComments, overrides: {} };
27+
}
28+
29+
if (typeof config.defaultStatusComments === 'boolean') {
30+
config.defaultStatusComments = { enabled: config.defaultStatusComments, overrides: {} };
31+
}
32+
33+
await knex('global_config').where('key', 'labels').update({
34+
config,
35+
updatedAt: knex.fn.now(),
36+
});
37+
}
38+
39+
export async function down(knex: Knex): Promise<any> {
40+
const existingLabels = await knex('global_config').where('key', 'labels').first();
41+
if (!existingLabels) return;
42+
43+
const config = existingLabels.config;
44+
45+
if (typeof config.defaultControlComments === 'object' && config.defaultControlComments !== null) {
46+
config.defaultControlComments = config.defaultControlComments.enabled ?? true;
47+
}
48+
49+
if (typeof config.defaultStatusComments === 'object' && config.defaultStatusComments !== null) {
50+
config.defaultStatusComments = config.defaultStatusComments.enabled ?? true;
51+
}
52+
53+
await knex('global_config').where('key', 'labels').update({
54+
config,
55+
updatedAt: knex.fn.now(),
56+
});
57+
}

‎src/server/lib/__tests__/utils.test.ts‎

Lines changed: 84 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
getDisabledLabel,
2828
getStatusCommentLabel,
2929
isDefaultStatusCommentsEnabled,
30+
isControlCommentsEnabled,
3031
} from 'server/lib/utils';
3132
import GlobalConfigService from 'server/services/globalConfig';
3233

@@ -47,8 +48,8 @@ jest.mock('server/services/globalConfig', () => {
4748
disabled: ['lifecycle-disabled!', 'no-deploy!'],
4849
keep: ['lifecycle-keep!'],
4950
statusComments: ['lifecycle-status-comments!', 'show-status!'],
50-
defaultStatusComments: true,
51-
defaultControlComments: true,
51+
defaultStatusComments: { enabled: true, overrides: {} },
52+
defaultControlComments: { enabled: true, overrides: {} },
5253
}),
5354
}),
5455
};
@@ -378,8 +379,8 @@ describe('hasDeployLabel', () => {
378379
disabled: ['lifecycle-disabled!'],
379380
keep: ['lifecycle-keep!'],
380381
statusComments: ['lifecycle-status-comments!'],
381-
defaultStatusComments: true,
382-
defaultControlComments: true,
382+
defaultStatusComments: { enabled: true, overrides: {} },
383+
defaultControlComments: { enabled: true, overrides: {} },
383384
});
384385
const result = await hasDeployLabel(['some-label']);
385386
expect(result).toBe(false);
@@ -458,8 +459,8 @@ describe('getDeployLabel', () => {
458459
disabled: ['lifecycle-disabled!'],
459460
keep: ['lifecycle-keep!'],
460461
statusComments: ['lifecycle-status-comments!'],
461-
defaultStatusComments: true,
462-
defaultControlComments: true,
462+
defaultStatusComments: { enabled: true, overrides: {} },
463+
defaultControlComments: { enabled: true, overrides: {} },
463464
});
464465
const result = await getDeployLabel();
465466
expect(result).toBe('lifecycle-deploy!');
@@ -495,13 +496,46 @@ describe('isDefaultStatusCommentsEnabled', () => {
495496
jest.clearAllMocks();
496497
});
497498

498-
test('returns defaultStatusComments setting from configuration', async () => {
499+
test('returns global enabled value when no repoFullName is passed', async () => {
499500
const result = await isDefaultStatusCommentsEnabled();
500501
expect(result).toBe(true);
501502
expect(GlobalConfigService.getInstance().getLabels).toHaveBeenCalled();
502503
});
503504

504-
test('returns true when defaultStatusComments is missing', async () => {
505+
test('returns global enabled value when repo has no override', async () => {
506+
const result = await isDefaultStatusCommentsEnabled('org/unknown-repo');
507+
expect(result).toBe(true);
508+
});
509+
510+
test('returns repo override when present (global enabled, repo disabled)', async () => {
511+
const mockService = GlobalConfigService.getInstance() as jest.Mocked<GlobalConfigService>;
512+
mockService.getLabels.mockResolvedValueOnce({
513+
deploy: ['lifecycle-deploy!'],
514+
disabled: ['lifecycle-disabled!'],
515+
keep: ['lifecycle-keep!'],
516+
statusComments: ['lifecycle-status-comments!'],
517+
defaultStatusComments: { enabled: true, overrides: { 'org/repo-a': false } },
518+
defaultControlComments: { enabled: true, overrides: {} },
519+
});
520+
const result = await isDefaultStatusCommentsEnabled('org/repo-a');
521+
expect(result).toBe(false);
522+
});
523+
524+
test('returns repo override when present (global disabled, repo enabled)', async () => {
525+
const mockService = GlobalConfigService.getInstance() as jest.Mocked<GlobalConfigService>;
526+
mockService.getLabels.mockResolvedValueOnce({
527+
deploy: ['lifecycle-deploy!'],
528+
disabled: ['lifecycle-disabled!'],
529+
keep: ['lifecycle-keep!'],
530+
statusComments: ['lifecycle-status-comments!'],
531+
defaultStatusComments: { enabled: false, overrides: { 'org/repo-a': true } },
532+
defaultControlComments: { enabled: true, overrides: {} },
533+
});
534+
const result = await isDefaultStatusCommentsEnabled('org/repo-a');
535+
expect(result).toBe(true);
536+
});
537+
538+
test('returns true when enabled is missing (fallback)', async () => {
505539
const mockService = GlobalConfigService.getInstance() as jest.Mocked<GlobalConfigService>;
506540
mockService.getLabels.mockResolvedValueOnce({
507541
deploy: ['lifecycle-deploy!'],
@@ -519,44 +553,73 @@ describe('isControlCommentsEnabled', () => {
519553
jest.clearAllMocks();
520554
});
521555

522-
test('returns defaultControlComments setting from configuration', async () => {
523-
const { isControlCommentsEnabled } = await import('../utils');
556+
test('returns global enabled value when no repoFullName is passed', async () => {
524557
const result = await isControlCommentsEnabled();
525558
expect(result).toBe(true);
526559
expect(GlobalConfigService.getInstance().getLabels).toHaveBeenCalled();
527560
});
528561

529-
test('returns true when defaultControlComments is missing', async () => {
530-
const { isControlCommentsEnabled } = await import('../utils');
562+
test('returns global enabled value when repo has no override', async () => {
563+
const result = await isControlCommentsEnabled('org/unknown-repo');
564+
expect(result).toBe(true);
565+
});
566+
567+
test('returns repo override when present (global enabled, repo disabled)', async () => {
531568
const mockService = GlobalConfigService.getInstance() as jest.Mocked<GlobalConfigService>;
532569
mockService.getLabels.mockResolvedValueOnce({
533570
deploy: ['lifecycle-deploy!'],
534571
disabled: ['lifecycle-disabled!'],
535572
keep: ['lifecycle-keep!'],
536573
statusComments: ['lifecycle-status-comments!'],
537-
defaultStatusComments: true,
538-
} as any);
539-
const result = await isControlCommentsEnabled();
574+
defaultStatusComments: { enabled: true, overrides: {} },
575+
defaultControlComments: { enabled: true, overrides: { 'org/repo-a': false } },
576+
});
577+
const result = await isControlCommentsEnabled('org/repo-a');
578+
expect(result).toBe(false);
579+
});
580+
581+
test('returns repo override when present (global disabled, repo enabled)', async () => {
582+
const mockService = GlobalConfigService.getInstance() as jest.Mocked<GlobalConfigService>;
583+
mockService.getLabels.mockResolvedValueOnce({
584+
deploy: ['lifecycle-deploy!'],
585+
disabled: ['lifecycle-disabled!'],
586+
keep: ['lifecycle-keep!'],
587+
statusComments: ['lifecycle-status-comments!'],
588+
defaultStatusComments: { enabled: true, overrides: {} },
589+
defaultControlComments: { enabled: false, overrides: { 'org/repo-b': true } },
590+
});
591+
const result = await isControlCommentsEnabled('org/repo-b');
540592
expect(result).toBe(true);
541593
});
542594

543-
test('returns false when defaultControlComments is explicitly false', async () => {
544-
const { isControlCommentsEnabled } = await import('../utils');
595+
test('returns false when globally disabled with no override for repo', async () => {
545596
const mockService = GlobalConfigService.getInstance() as jest.Mocked<GlobalConfigService>;
546597
mockService.getLabels.mockResolvedValueOnce({
547598
deploy: ['lifecycle-deploy!'],
548599
disabled: ['lifecycle-disabled!'],
549600
keep: ['lifecycle-keep!'],
550601
statusComments: ['lifecycle-status-comments!'],
551-
defaultStatusComments: true,
552-
defaultControlComments: false,
602+
defaultStatusComments: { enabled: true, overrides: {} },
603+
defaultControlComments: { enabled: false, overrides: {} },
553604
});
554-
const result = await isControlCommentsEnabled();
605+
const result = await isControlCommentsEnabled('org/repo-c');
555606
expect(result).toBe(false);
556607
});
557608

609+
test('returns true when enabled is missing (fallback)', async () => {
610+
const mockService = GlobalConfigService.getInstance() as jest.Mocked<GlobalConfigService>;
611+
mockService.getLabels.mockResolvedValueOnce({
612+
deploy: ['lifecycle-deploy!'],
613+
disabled: ['lifecycle-disabled!'],
614+
keep: ['lifecycle-keep!'],
615+
statusComments: ['lifecycle-status-comments!'],
616+
defaultStatusComments: { enabled: true, overrides: {} },
617+
} as any);
618+
const result = await isControlCommentsEnabled();
619+
expect(result).toBe(true);
620+
});
621+
558622
test('returns true when getLabels throws an error', async () => {
559-
const { isControlCommentsEnabled } = await import('../utils');
560623
const mockService = GlobalConfigService.getInstance() as jest.Mocked<GlobalConfigService>;
561624
mockService.getLabels.mockRejectedValueOnce(new Error('Database error'));
562625
const result = await isControlCommentsEnabled();

‎src/server/lib/utils.ts‎

Lines changed: 13 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { execFile } from 'child_process';
1818
import { promisify } from 'util';
1919
import { GithubPullRequestActions, PullRequestStatus, FallbackLabels } from 'shared/constants';
2020
import GlobalConfigService from 'server/services/globalConfig';
21+
import { CommentToggleConfig } from 'server/services/types/globalConfig';
2122
import { GenerateDeployTagOptions, WaitUntilOptions, EnableKillswitchOptions } from 'server/lib/types';
2223

2324
import { getLogger } from 'server/lib/logger';
@@ -198,95 +199,64 @@ export const isStaging = () => {
198199
return ENVIRONMENT === 'staging';
199200
};
200201

201-
/**
202-
* Check if PR has any deploy labels from configuration
203-
* @param labels Array of PR labels
204-
* @returns Promise<boolean> True if PR has deploy label
205-
*/
206202
export const hasDeployLabel = async (labels: string[]): Promise<boolean> => {
207203
if (!labels || labels.length === 0) return false;
208204
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
209205
const deployLabels = labelsConfig.deploy || [];
210206
return deployLabels.some((deployLabel) => labels.includes(deployLabel));
211207
};
212208

213-
/**
214-
* Check if PR has any disabled labels fr m configuration
215-
* @param labels Array of PR labels
216-
* @returns Promise<boolean> True if PR has disabled label
217-
*/
218209
export const hasDisabledLabel = async (labels: string[]): Promise<boolean> => {
219210
if (!labels || labels.length === 0) return false;
220211
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
221212
const disabledLabels = labelsConfig.disabled || [];
222213
return disabledLabels.some((disabledLabel) => labels.includes(disabledLabel));
223214
};
224215

225-
/**
226-
* Check if PR has any status comment labels from configuration
227-
* @param labels Array of PR labels
228-
* @returns Promise<boolean> True if PR has status comment label
229-
*/
230216
export const hasStatusCommentLabel = async (labels: string[]): Promise<boolean> => {
231217
if (!labels || labels.length === 0) return false;
232218
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
233219
const statusCommentLabels = labelsConfig.statusComments || [];
234220
return statusCommentLabels.some((statusLabel) => labels.includes(statusLabel));
235221
};
236222

237-
/**
238-
* Get the first deploy label from configuration for user-facing messages
239-
* @returns Promise<string> First deploy label from config
240-
*/
241223
export const getDeployLabel = async (): Promise<string> => {
242224
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
243225
return labelsConfig?.deploy?.[0] || FallbackLabels.DEPLOY;
244226
};
245227

246-
/**
247-
* Get the first disabled label from configuration for user-facing messages
248-
* @returns Promise<string> First disabled label from config
249-
*/
250228
export const getDisabledLabel = async (): Promise<string> => {
251229
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
252230
return labelsConfig?.disabled?.[0] || FallbackLabels.DISABLED;
253231
};
254232

255-
/**
256-
* Get the first keep label from configuration for user-facing messages
257-
* @returns Promise<string> First keep label from config
258-
*/
259233
export const getKeepLabel = async (): Promise<string> => {
260234
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
261235
return labelsConfig?.keep?.[0] || FallbackLabels.KEEP;
262236
};
263237

264-
/**
265-
* Get the first status comment label from configuration for user-facing messages
266-
* @returns Promise<string> First status comment label from config
267-
*/
268238
export const getStatusCommentLabel = async (): Promise<string> => {
269239
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
270240
return labelsConfig?.statusComments?.[0] || FallbackLabels.STATUS_COMMENTS;
271241
};
272242

273-
/**
274-
* Check if status comments should be enabled by default
275-
* @returns Promise<boolean> True if status comments are enabled by default
276-
*/
277-
export const isDefaultStatusCommentsEnabled = async (): Promise<boolean> => {
243+
function resolveCommentToggle(config: CommentToggleConfig | undefined, repoFullName?: string): boolean {
244+
if (!config) return true;
245+
if (repoFullName && config.overrides?.[repoFullName] !== undefined) {
246+
return config.overrides[repoFullName];
247+
}
248+
return config.enabled ?? true;
249+
}
250+
251+
export const isDefaultStatusCommentsEnabled = async (repoFullName?: string): Promise<boolean> => {
278252
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
279-
return labelsConfig.defaultStatusComments ?? true;
253+
return resolveCommentToggle(labelsConfig.defaultStatusComments, repoFullName);
280254
};
281255

282-
/**
283-
* Check if control comments should be enabled
284-
* @returns Promise<boolean> True if control comments are enabled (defaults to true if not configured)
285-
*/
286-
export const isControlCommentsEnabled = async (): Promise<boolean> => {
256+
export const isControlCommentsEnabled = async (repoFullName?: string): Promise<boolean> => {
287257
try {
288258
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
289-
return labelsConfig.defaultControlComments ?? true;
259+
return resolveCommentToggle(labelsConfig.defaultControlComments, repoFullName);
290260
} catch (error) {
291261
getLogger().warn(`Config: error retrieving control comments config error=${error}`);
292262
return true;

‎src/server/services/__tests__/globalConfig.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ describe('GlobalConfigService', () => {
9191
deploy: ['lifecycle-deploy!', 'custom-deploy!'],
9292
disabled: ['lifecycle-disabled!', 'no-deploy!'],
9393
statusComments: ['lifecycle-status-comments!', 'show-status!'],
94-
defaultStatusComments: true,
95-
defaultControlComments: true,
94+
defaultStatusComments: { enabled: true, overrides: {} },
95+
defaultControlComments: { enabled: true, overrides: {} },
9696
};
9797

9898
const mockGetAllConfigs = jest.spyOn(service, 'getAllConfigs').mockResolvedValueOnce({
@@ -119,8 +119,8 @@ describe('GlobalConfigService', () => {
119119
disabled: ['lifecycle-disabled!'],
120120
keep: ['lifecycle-keep!'],
121121
statusComments: ['lifecycle-status-comments!'],
122-
defaultStatusComments: true,
123-
defaultControlComments: true,
122+
defaultStatusComments: { enabled: true, overrides: {} },
123+
defaultControlComments: { enabled: true, overrides: {} },
124124
});
125125
expect(mockGetAllConfigs).toHaveBeenCalled();
126126

@@ -137,8 +137,8 @@ describe('GlobalConfigService', () => {
137137
disabled: ['lifecycle-disabled!'],
138138
keep: ['lifecycle-keep!'],
139139
statusComments: ['lifecycle-status-comments!'],
140-
defaultStatusComments: true,
141-
defaultControlComments: true,
140+
defaultStatusComments: { enabled: true, overrides: {} },
141+
defaultControlComments: { enabled: true, overrides: {} },
142142
});
143143
expect(mockGetAllConfigs).toHaveBeenCalled();
144144

0 commit comments

Comments
 (0)