Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QD-10655 fix current branch ref and add cache unit tests #435

Merged
merged 3 commits into from
Jan 13, 2025
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
72 changes: 72 additions & 0 deletions scan/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2021-2025 JetBrains s.r.o.
*
* 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
*
* https://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 {expect} from '@jest/globals'
import {isNeedToUploadCache} from '../src/utils'
import * as github from '@actions/github'
import {ENABLE_USE_CACHE_OPTION_WARNING} from '../src/utils'

const masterBranch = 'master'
describe('isNeedToUploadCache', () => {
it.each([
[masterBranch, true],
['branch', false]
])(
'for branch %s should return %s when cacheDefaultBranchOnly=true, useCache=true',
(currentBranch, expected) => {
initGithubContext(currentBranch)
expect(isNeedToUploadCache(true, true)).toBe(expected)
}
)

it.each([
[true, masterBranch, true],
[true, 'branch', true],
[false, masterBranch, false],
[false, 'branch', false]
])(
'if cacheDefaultBranchOnly=false should return useCache, current branch is %s, useCache=%s, expected %s',
(useCache, currentBranch, expected) => {
initGithubContext(currentBranch)
expect(isNeedToUploadCache(useCache, false)).toBe(expected)
}
)

it.each([masterBranch, 'branch'])(
'should return false when useCache=false and cacheDefaultBranchOnly=true, branch %s',
branch => {
initGithubContext(branch)
expect(isNeedToUploadCache(false, true)).toBe(false)
}
)

it('should warn when useCache=false and cacheDefaultBranchOnly=true', () => {
const core = require('@actions/core')
jest.spyOn(core, 'warning')
initGithubContext(masterBranch)
isNeedToUploadCache(false, true)
expect(core.warning).toBeCalledWith(ENABLE_USE_CACHE_OPTION_WARNING)
})
})

export function initGithubContext(currentBranch: string): void {
Object.defineProperty(github, 'context', {
value: {
ref: `refs/heads/${currentBranch}`,
payload: {repository: {default_branch: masterBranch}}
}
})
}
7 changes: 4 additions & 3 deletions scan/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137552,7 +137552,7 @@ var require_utils10 = __commonJS({
return mod && mod.__esModule ? mod : { "default": mod };
};
Object.defineProperty(exports2, "__esModule", { value: true });
exports2.ANALYSIS_STARTED_REACTION = exports2.ANALYSIS_FINISHED_REACTION = void 0;
exports2.ENABLE_USE_CACHE_OPTION_WARNING = exports2.ANALYSIS_STARTED_REACTION = exports2.ANALYSIS_FINISHED_REACTION = void 0;
exports2.getInputs = getInputs;
exports2.qodana = qodana;
exports2.pushQuickFixes = pushQuickFixes;
Expand Down Expand Up @@ -137582,6 +137582,7 @@ var require_utils10 = __commonJS({
var output_12 = require_output();
exports2.ANALYSIS_FINISHED_REACTION = "+1";
exports2.ANALYSIS_STARTED_REACTION = "eyes";
exports2.ENABLE_USE_CACHE_OPTION_WARNING = 'Turn on "use-cache" option to use "cache-default-branch-only"';
function getInputs() {
const rawArgs = core2.getInput("args");
const argList = rawArgs ? rawArgs.split(",").map((arg) => arg.trim()) : [];
Expand Down Expand Up @@ -137796,10 +137797,10 @@ var require_utils10 = __commonJS({
function isNeedToUploadCache(useCaches, cacheDefaultBranchOnly) {
var _a;
if (!useCaches && cacheDefaultBranchOnly) {
core2.warning('Turn on "use-cache" option to use "cache-default-branch-only"');
core2.warning(exports2.ENABLE_USE_CACHE_OPTION_WARNING);
}
if (useCaches && cacheDefaultBranchOnly) {
const currentBranch = github.context.payload.ref_name;
const currentBranch = github.context.ref;
const defaultBranch = (_a = github.context.payload.repository) === null || _a === void 0 ? void 0 : _a.default_branch;
core2.debug(`Current branch: ${currentBranch} | Default branch: ${defaultBranch}`);
return currentBranch === `refs/heads/${defaultBranch}`;
Expand Down
8 changes: 4 additions & 4 deletions scan/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import {COMMIT_EMAIL, COMMIT_USER, prFixesBody} from './output'

export const ANALYSIS_FINISHED_REACTION = '+1'
export const ANALYSIS_STARTED_REACTION = 'eyes'
export const ENABLE_USE_CACHE_OPTION_WARNING =
'Turn on "use-cache" option to use "cache-default-branch-only"'

type Reaction =
| '+1'
Expand Down Expand Up @@ -356,13 +358,11 @@ export function isNeedToUploadCache(
cacheDefaultBranchOnly: boolean
): boolean {
if (!useCaches && cacheDefaultBranchOnly) {
core.warning(
'Turn on "use-cache" option to use "cache-default-branch-only"'
)
core.warning(ENABLE_USE_CACHE_OPTION_WARNING)
}

if (useCaches && cacheDefaultBranchOnly) {
const currentBranch = github.context.payload.ref_name as string
const currentBranch = github.context.ref
const defaultBranch = github.context.payload.repository
?.default_branch as string
core.debug(
Expand Down
Loading