Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/server/db/migrations/012_migrate_labels_comment_config.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
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<any> {
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(),
});
}
105 changes: 84 additions & 21 deletions src/server/lib/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getDisabledLabel,
getStatusCommentLabel,
isDefaultStatusCommentsEnabled,
isControlCommentsEnabled,
} from 'server/lib/utils';
import GlobalConfigService from 'server/services/globalConfig';

Expand All @@ -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: {} },
}),
}),
};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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!');
Expand Down Expand Up @@ -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<GlobalConfigService>;
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<GlobalConfigService>;
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<GlobalConfigService>;
mockService.getLabels.mockResolvedValueOnce({
deploy: ['lifecycle-deploy!'],
Expand All @@ -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<GlobalConfigService>;
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<GlobalConfigService>;
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<GlobalConfigService>;
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<GlobalConfigService>;
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<GlobalConfigService>;
mockService.getLabels.mockRejectedValueOnce(new Error('Database error'));
const result = await isControlCommentsEnabled();
Expand Down
56 changes: 13 additions & 43 deletions src/server/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -198,95 +199,64 @@ export const isStaging = () => {
return ENVIRONMENT === 'staging';
};

/**
* Check if PR has any deploy labels from configuration
* @param labels Array of PR labels
* @returns Promise<boolean> True if PR has deploy label
*/
export const hasDeployLabel = async (labels: string[]): Promise<boolean> => {
if (!labels || labels.length === 0) return false;
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
const deployLabels = labelsConfig.deploy || [];
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<boolean> True if PR has disabled label
*/
export const hasDisabledLabel = async (labels: string[]): Promise<boolean> => {
if (!labels || labels.length === 0) return false;
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
const disabledLabels = labelsConfig.disabled || [];
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<boolean> True if PR has status comment label
*/
export const hasStatusCommentLabel = async (labels: string[]): Promise<boolean> => {
if (!labels || labels.length === 0) return false;
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
const statusCommentLabels = labelsConfig.statusComments || [];
return statusCommentLabels.some((statusLabel) => labels.includes(statusLabel));
};

/**
* Get the first deploy label from configuration for user-facing messages
* @returns Promise<string> First deploy label from config
*/
export const getDeployLabel = async (): Promise<string> => {
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<string> First disabled label from config
*/
export const getDisabledLabel = async (): Promise<string> => {
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<string> First keep label from config
*/
export const getKeepLabel = async (): Promise<string> => {
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<string> First status comment label from config
*/
export const getStatusCommentLabel = async (): Promise<string> => {
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
return labelsConfig?.statusComments?.[0] || FallbackLabels.STATUS_COMMENTS;
};

/**
* Check if status comments should be enabled by default
* @returns Promise<boolean> True if status comments are enabled by default
*/
export const isDefaultStatusCommentsEnabled = async (): Promise<boolean> => {
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<boolean> => {
const labelsConfig = await GlobalConfigService.getInstance().getLabels();
return labelsConfig.defaultStatusComments ?? true;
return resolveCommentToggle(labelsConfig.defaultStatusComments, repoFullName);
};

/**
* Check if control comments should be enabled
* @returns Promise<boolean> True if control comments are enabled (defaults to true if not configured)
*/
export const isControlCommentsEnabled = async (): Promise<boolean> => {
export const isControlCommentsEnabled = async (repoFullName?: string): Promise<boolean> => {
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;
Expand Down
12 changes: 6 additions & 6 deletions src/server/services/__tests__/globalConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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();

Expand All @@ -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();

Expand Down
Loading