diff --git a/src/server/db/migrations/012_migrate_labels_comment_config.ts b/src/server/db/migrations/012_migrate_labels_comment_config.ts new file mode 100644 index 00000000..464e83c8 --- /dev/null +++ b/src/server/db/migrations/012_migrate_labels_comment_config.ts @@ -0,0 +1,57 @@ +/** + * Copyright 2025 GoodRx, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Knex } from 'knex'; + +export async function up(knex: Knex): Promise { + const existingLabels = await knex('global_config').where('key', 'labels').first(); + if (!existingLabels) return; + + const config = existingLabels.config; + + if (typeof config.defaultControlComments === 'boolean') { + config.defaultControlComments = { enabled: config.defaultControlComments, overrides: {} }; + } + + if (typeof config.defaultStatusComments === 'boolean') { + config.defaultStatusComments = { enabled: config.defaultStatusComments, overrides: {} }; + } + + await knex('global_config').where('key', 'labels').update({ + config, + updatedAt: knex.fn.now(), + }); +} + +export async function down(knex: Knex): Promise { + const existingLabels = await knex('global_config').where('key', 'labels').first(); + if (!existingLabels) return; + + const config = existingLabels.config; + + if (typeof config.defaultControlComments === 'object' && config.defaultControlComments !== null) { + config.defaultControlComments = config.defaultControlComments.enabled ?? true; + } + + if (typeof config.defaultStatusComments === 'object' && config.defaultStatusComments !== null) { + config.defaultStatusComments = config.defaultStatusComments.enabled ?? true; + } + + await knex('global_config').where('key', 'labels').update({ + config, + updatedAt: knex.fn.now(), + }); +} diff --git a/src/server/lib/__tests__/utils.test.ts b/src/server/lib/__tests__/utils.test.ts index bcc1329c..c2eaea7c 100644 --- a/src/server/lib/__tests__/utils.test.ts +++ b/src/server/lib/__tests__/utils.test.ts @@ -27,6 +27,7 @@ import { getDisabledLabel, getStatusCommentLabel, isDefaultStatusCommentsEnabled, + isControlCommentsEnabled, } from 'server/lib/utils'; import GlobalConfigService from 'server/services/globalConfig'; @@ -47,8 +48,8 @@ jest.mock('server/services/globalConfig', () => { disabled: ['lifecycle-disabled!', 'no-deploy!'], keep: ['lifecycle-keep!'], statusComments: ['lifecycle-status-comments!', 'show-status!'], - defaultStatusComments: true, - defaultControlComments: true, + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: true, overrides: {} }, }), }), }; @@ -378,8 +379,8 @@ describe('hasDeployLabel', () => { disabled: ['lifecycle-disabled!'], keep: ['lifecycle-keep!'], statusComments: ['lifecycle-status-comments!'], - defaultStatusComments: true, - defaultControlComments: true, + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: true, overrides: {} }, }); const result = await hasDeployLabel(['some-label']); expect(result).toBe(false); @@ -458,8 +459,8 @@ describe('getDeployLabel', () => { disabled: ['lifecycle-disabled!'], keep: ['lifecycle-keep!'], statusComments: ['lifecycle-status-comments!'], - defaultStatusComments: true, - defaultControlComments: true, + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: true, overrides: {} }, }); const result = await getDeployLabel(); expect(result).toBe('lifecycle-deploy!'); @@ -495,13 +496,46 @@ describe('isDefaultStatusCommentsEnabled', () => { jest.clearAllMocks(); }); - test('returns defaultStatusComments setting from configuration', async () => { + test('returns global enabled value when no repoFullName is passed', async () => { const result = await isDefaultStatusCommentsEnabled(); expect(result).toBe(true); expect(GlobalConfigService.getInstance().getLabels).toHaveBeenCalled(); }); - test('returns true when defaultStatusComments is missing', async () => { + test('returns global enabled value when repo has no override', async () => { + const result = await isDefaultStatusCommentsEnabled('org/unknown-repo'); + expect(result).toBe(true); + }); + + test('returns repo override when present (global enabled, repo disabled)', async () => { + const mockService = GlobalConfigService.getInstance() as jest.Mocked; + mockService.getLabels.mockResolvedValueOnce({ + deploy: ['lifecycle-deploy!'], + disabled: ['lifecycle-disabled!'], + keep: ['lifecycle-keep!'], + statusComments: ['lifecycle-status-comments!'], + defaultStatusComments: { enabled: true, overrides: { 'org/repo-a': false } }, + defaultControlComments: { enabled: true, overrides: {} }, + }); + const result = await isDefaultStatusCommentsEnabled('org/repo-a'); + expect(result).toBe(false); + }); + + test('returns repo override when present (global disabled, repo enabled)', async () => { + const mockService = GlobalConfigService.getInstance() as jest.Mocked; + mockService.getLabels.mockResolvedValueOnce({ + deploy: ['lifecycle-deploy!'], + disabled: ['lifecycle-disabled!'], + keep: ['lifecycle-keep!'], + statusComments: ['lifecycle-status-comments!'], + defaultStatusComments: { enabled: false, overrides: { 'org/repo-a': true } }, + defaultControlComments: { enabled: true, overrides: {} }, + }); + const result = await isDefaultStatusCommentsEnabled('org/repo-a'); + expect(result).toBe(true); + }); + + test('returns true when enabled is missing (fallback)', async () => { const mockService = GlobalConfigService.getInstance() as jest.Mocked; mockService.getLabels.mockResolvedValueOnce({ deploy: ['lifecycle-deploy!'], @@ -519,44 +553,73 @@ describe('isControlCommentsEnabled', () => { jest.clearAllMocks(); }); - test('returns defaultControlComments setting from configuration', async () => { - const { isControlCommentsEnabled } = await import('../utils'); + test('returns global enabled value when no repoFullName is passed', async () => { const result = await isControlCommentsEnabled(); expect(result).toBe(true); expect(GlobalConfigService.getInstance().getLabels).toHaveBeenCalled(); }); - test('returns true when defaultControlComments is missing', async () => { - const { isControlCommentsEnabled } = await import('../utils'); + test('returns global enabled value when repo has no override', async () => { + const result = await isControlCommentsEnabled('org/unknown-repo'); + expect(result).toBe(true); + }); + + test('returns repo override when present (global enabled, repo disabled)', async () => { const mockService = GlobalConfigService.getInstance() as jest.Mocked; mockService.getLabels.mockResolvedValueOnce({ deploy: ['lifecycle-deploy!'], disabled: ['lifecycle-disabled!'], keep: ['lifecycle-keep!'], statusComments: ['lifecycle-status-comments!'], - defaultStatusComments: true, - } as any); - const result = await isControlCommentsEnabled(); + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: true, overrides: { 'org/repo-a': false } }, + }); + const result = await isControlCommentsEnabled('org/repo-a'); + expect(result).toBe(false); + }); + + test('returns repo override when present (global disabled, repo enabled)', async () => { + const mockService = GlobalConfigService.getInstance() as jest.Mocked; + mockService.getLabels.mockResolvedValueOnce({ + deploy: ['lifecycle-deploy!'], + disabled: ['lifecycle-disabled!'], + keep: ['lifecycle-keep!'], + statusComments: ['lifecycle-status-comments!'], + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: false, overrides: { 'org/repo-b': true } }, + }); + const result = await isControlCommentsEnabled('org/repo-b'); expect(result).toBe(true); }); - test('returns false when defaultControlComments is explicitly false', async () => { - const { isControlCommentsEnabled } = await import('../utils'); + test('returns false when globally disabled with no override for repo', async () => { const mockService = GlobalConfigService.getInstance() as jest.Mocked; mockService.getLabels.mockResolvedValueOnce({ deploy: ['lifecycle-deploy!'], disabled: ['lifecycle-disabled!'], keep: ['lifecycle-keep!'], statusComments: ['lifecycle-status-comments!'], - defaultStatusComments: true, - defaultControlComments: false, + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: false, overrides: {} }, }); - const result = await isControlCommentsEnabled(); + const result = await isControlCommentsEnabled('org/repo-c'); expect(result).toBe(false); }); + test('returns true when enabled is missing (fallback)', async () => { + const mockService = GlobalConfigService.getInstance() as jest.Mocked; + mockService.getLabels.mockResolvedValueOnce({ + deploy: ['lifecycle-deploy!'], + disabled: ['lifecycle-disabled!'], + keep: ['lifecycle-keep!'], + statusComments: ['lifecycle-status-comments!'], + defaultStatusComments: { enabled: true, overrides: {} }, + } as any); + const result = await isControlCommentsEnabled(); + expect(result).toBe(true); + }); + test('returns true when getLabels throws an error', async () => { - const { isControlCommentsEnabled } = await import('../utils'); const mockService = GlobalConfigService.getInstance() as jest.Mocked; mockService.getLabels.mockRejectedValueOnce(new Error('Database error')); const result = await isControlCommentsEnabled(); diff --git a/src/server/lib/utils.ts b/src/server/lib/utils.ts index a4a851ca..6682dde8 100644 --- a/src/server/lib/utils.ts +++ b/src/server/lib/utils.ts @@ -18,6 +18,7 @@ import { execFile } from 'child_process'; import { promisify } from 'util'; import { GithubPullRequestActions, PullRequestStatus, FallbackLabels } from 'shared/constants'; import GlobalConfigService from 'server/services/globalConfig'; +import { CommentToggleConfig } from 'server/services/types/globalConfig'; import { GenerateDeployTagOptions, WaitUntilOptions, EnableKillswitchOptions } from 'server/lib/types'; import { getLogger } from 'server/lib/logger'; @@ -198,11 +199,6 @@ export const isStaging = () => { return ENVIRONMENT === 'staging'; }; -/** - * Check if PR has any deploy labels from configuration - * @param labels Array of PR labels - * @returns Promise True if PR has deploy label - */ export const hasDeployLabel = async (labels: string[]): Promise => { if (!labels || labels.length === 0) return false; const labelsConfig = await GlobalConfigService.getInstance().getLabels(); @@ -210,11 +206,6 @@ export const hasDeployLabel = async (labels: string[]): Promise => { return deployLabels.some((deployLabel) => labels.includes(deployLabel)); }; -/** - * Check if PR has any disabled labels fr m configuration - * @param labels Array of PR labels - * @returns Promise True if PR has disabled label - */ export const hasDisabledLabel = async (labels: string[]): Promise => { if (!labels || labels.length === 0) return false; const labelsConfig = await GlobalConfigService.getInstance().getLabels(); @@ -222,11 +213,6 @@ export const hasDisabledLabel = async (labels: string[]): Promise => { return disabledLabels.some((disabledLabel) => labels.includes(disabledLabel)); }; -/** - * Check if PR has any status comment labels from configuration - * @param labels Array of PR labels - * @returns Promise True if PR has status comment label - */ export const hasStatusCommentLabel = async (labels: string[]): Promise => { if (!labels || labels.length === 0) return false; const labelsConfig = await GlobalConfigService.getInstance().getLabels(); @@ -234,59 +220,43 @@ export const hasStatusCommentLabel = async (labels: string[]): Promise return statusCommentLabels.some((statusLabel) => labels.includes(statusLabel)); }; -/** - * Get the first deploy label from configuration for user-facing messages - * @returns Promise First deploy label from config - */ export const getDeployLabel = async (): Promise => { const labelsConfig = await GlobalConfigService.getInstance().getLabels(); return labelsConfig?.deploy?.[0] || FallbackLabels.DEPLOY; }; -/** - * Get the first disabled label from configuration for user-facing messages - * @returns Promise First disabled label from config - */ export const getDisabledLabel = async (): Promise => { const labelsConfig = await GlobalConfigService.getInstance().getLabels(); return labelsConfig?.disabled?.[0] || FallbackLabels.DISABLED; }; -/** - * Get the first keep label from configuration for user-facing messages - * @returns Promise First keep label from config - */ export const getKeepLabel = async (): Promise => { const labelsConfig = await GlobalConfigService.getInstance().getLabels(); return labelsConfig?.keep?.[0] || FallbackLabels.KEEP; }; -/** - * Get the first status comment label from configuration for user-facing messages - * @returns Promise First status comment label from config - */ export const getStatusCommentLabel = async (): Promise => { const labelsConfig = await GlobalConfigService.getInstance().getLabels(); return labelsConfig?.statusComments?.[0] || FallbackLabels.STATUS_COMMENTS; }; -/** - * Check if status comments should be enabled by default - * @returns Promise True if status comments are enabled by default - */ -export const isDefaultStatusCommentsEnabled = async (): Promise => { +function resolveCommentToggle(config: CommentToggleConfig | undefined, repoFullName?: string): boolean { + if (!config) return true; + if (repoFullName && config.overrides?.[repoFullName] !== undefined) { + return config.overrides[repoFullName]; + } + return config.enabled ?? true; +} + +export const isDefaultStatusCommentsEnabled = async (repoFullName?: string): Promise => { const labelsConfig = await GlobalConfigService.getInstance().getLabels(); - return labelsConfig.defaultStatusComments ?? true; + return resolveCommentToggle(labelsConfig.defaultStatusComments, repoFullName); }; -/** - * Check if control comments should be enabled - * @returns Promise True if control comments are enabled (defaults to true if not configured) - */ -export const isControlCommentsEnabled = async (): Promise => { +export const isControlCommentsEnabled = async (repoFullName?: string): Promise => { try { const labelsConfig = await GlobalConfigService.getInstance().getLabels(); - return labelsConfig.defaultControlComments ?? true; + return resolveCommentToggle(labelsConfig.defaultControlComments, repoFullName); } catch (error) { getLogger().warn(`Config: error retrieving control comments config error=${error}`); return true; diff --git a/src/server/services/__tests__/globalConfig.test.ts b/src/server/services/__tests__/globalConfig.test.ts index 5af4d864..3d68ea4b 100644 --- a/src/server/services/__tests__/globalConfig.test.ts +++ b/src/server/services/__tests__/globalConfig.test.ts @@ -91,8 +91,8 @@ describe('GlobalConfigService', () => { deploy: ['lifecycle-deploy!', 'custom-deploy!'], disabled: ['lifecycle-disabled!', 'no-deploy!'], statusComments: ['lifecycle-status-comments!', 'show-status!'], - defaultStatusComments: true, - defaultControlComments: true, + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: true, overrides: {} }, }; const mockGetAllConfigs = jest.spyOn(service, 'getAllConfigs').mockResolvedValueOnce({ @@ -119,8 +119,8 @@ describe('GlobalConfigService', () => { disabled: ['lifecycle-disabled!'], keep: ['lifecycle-keep!'], statusComments: ['lifecycle-status-comments!'], - defaultStatusComments: true, - defaultControlComments: true, + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: true, overrides: {} }, }); expect(mockGetAllConfigs).toHaveBeenCalled(); @@ -137,8 +137,8 @@ describe('GlobalConfigService', () => { disabled: ['lifecycle-disabled!'], keep: ['lifecycle-keep!'], statusComments: ['lifecycle-status-comments!'], - defaultStatusComments: true, - defaultControlComments: true, + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: true, overrides: {} }, }); expect(mockGetAllConfigs).toHaveBeenCalled(); diff --git a/src/server/services/activityStream.ts b/src/server/services/activityStream.ts index 4172f1ea..4853a900 100644 --- a/src/server/services/activityStream.ts +++ b/src/server/services/activityStream.ts @@ -394,7 +394,7 @@ export default class ActivityStream extends BaseService { const isStatic = build?.isStatic ?? false; const labels = pullRequest?.labels || []; const hasStatusComment = await hasStatusCommentLabel(labels); - const isDefaultStatusEnabled = await isDefaultStatusCommentsEnabled(); + const isDefaultStatusEnabled = await isDefaultStatusCommentsEnabled(fullName); const isShowingStatusComment = isStatic || hasStatusComment || isDefaultStatusEnabled; if (!buildId) { getLogger().error(`Build: id not found repo=${fullName}/${branchName}`); @@ -419,7 +419,7 @@ export default class ActivityStream extends BaseService { } if (updateStatus || updateMissionControl) { - const isControlEnabled = await isControlCommentsEnabled(); + const isControlEnabled = await isControlCommentsEnabled(fullName); if (isControlEnabled) { await this.updateMissionControlComment(build, deploys, pullRequest, repository).catch((error) => { getLogger().warn( diff --git a/src/server/services/globalConfig.ts b/src/server/services/globalConfig.ts index 18c780c7..e860e514 100644 --- a/src/server/services/globalConfig.ts +++ b/src/server/services/globalConfig.ts @@ -132,10 +132,6 @@ export default class GlobalConfigService extends BaseService { return Boolean(features[name]); } - /** - * Retrieves labels configuration from global config with fallback defaults - * @returns Promise The labels configuration - */ async getLabels(): Promise { try { const { labels } = await this.getAllConfigs(); @@ -143,14 +139,13 @@ export default class GlobalConfigService extends BaseService { return labels; } catch (error) { getLogger().error({ error }, 'Config: labels fetch failed using=defaults'); - // Return fallback defaults on error return { deploy: ['lifecycle-deploy!'], disabled: ['lifecycle-disabled!'], keep: ['lifecycle-keep!'], statusComments: ['lifecycle-status-comments!'], - defaultStatusComments: true, - defaultControlComments: true, + defaultStatusComments: { enabled: true, overrides: {} }, + defaultControlComments: { enabled: true, overrides: {} }, }; } } diff --git a/src/server/services/types/globalConfig.ts b/src/server/services/types/globalConfig.ts index 807c0bdb..2f7d7a91 100644 --- a/src/server/services/types/globalConfig.ts +++ b/src/server/services/types/globalConfig.ts @@ -143,13 +143,18 @@ export type ResourceRequirements = { limits?: Record; }; +export type CommentToggleConfig = { + enabled: boolean; + overrides?: Record; +}; + export type LabelsConfig = { deploy: string[]; disabled: string[]; keep: string[]; statusComments: string[]; - defaultStatusComments: boolean; - defaultControlComments: boolean; + defaultStatusComments: CommentToggleConfig; + defaultControlComments: CommentToggleConfig; }; export type TTLCleanupConfig = {