Skip to content

Commit

Permalink
merge conflicts resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
maksudur-rahman-maruf committed Aug 30, 2024
2 parents d168cae + b91ebf8 commit 54439cc
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 12 deletions.
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ inputs:
blackduck_config_path:
description: 'Black Duck config file path (.properties/.yml)'
required: false
blackduck_policy_badges_create:
description: 'To enable creation of badges on the GitHub repository'
required: false
blackduck_policy_badges_maxCount:
description: 'To limit number of badges to be displayed on the GitHub repository'
required: false
srm_url:
description: 'SRM Url'
required: false
Expand Down
50 changes: 41 additions & 9 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/application-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export const BLACKDUCK_WAITFORSCAN_KEY = 'blackduck_waitForScan'
export const BLACKDUCK_SEARCH_DEPTH_KEY = 'blackduck_search_depth'
export const BLACKDUCK_CONFIG_PATH_KEY = 'blackduck_config_path'
export const BLACKDUCK_ARGS_KEY = 'blackduck_args'
export const BLACKDUCK_POLICY_BADGES_CREATE_KEY = 'blackduck_policy_badges_create'
export const BLACKDUCK_POLICY_BADGES_MAX_COUNT_KEY = 'blackduck_policy_badges_maxCount'

export const GITHUB_HOST_URL_KEY = 'github_host_url'
export const GITHUB_TOKEN_KEY = 'github_token'
Expand Down Expand Up @@ -175,4 +177,4 @@ export const POLARIS_PR_COMMENT_LOG_INFO_FOR_NON_PR_SCANS = 'Polaris PR Comment
export const COVERITY_PR_COMMENT_LOG_INFO_FOR_NON_PR_SCANS = 'Coverity PR Comment is ignored for non pull request scan'
export const BLACKDUCK_PR_COMMENT_LOG_INFO_FOR_NON_PR_SCANS = 'Black Duck PR Comment is ignored for non pull request scan'
export const BLACKDUCK_FIXPR_LOG_INFO_FOR_PR_SCANS = 'Black Duck Fix PR is ignored for pull request scan'
export const MISSING_GITHUB_TOKEN_FOR_FIX_PR_AND_PR_COMMENT = 'Missing required github token for fix pull request/pull request comments'
export const MISSING_GITHUB_TOKEN_FOR_FIX_PR_AND_PR_COMMENT = 'Missing required github token for fix pull request/pull request comments/Github Badges'
10 changes: 10 additions & 0 deletions src/synopsys-action/input-data/blackduck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export interface BlackduckData extends BlackDuckArbitrary, AsyncMode {
automation?: AutomationData
fixpr?: BlackDuckFixPrData
reports?: Reports
policy?: Policy
}

export interface Policy {
badges?: Badges
}

export interface Badges {
create?: boolean
maxCount?: number
}

export interface BlackDuckArbitrary {
Expand Down
2 changes: 2 additions & 0 deletions src/synopsys-action/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ export const BLACKDUCK_WAITFORSCAN = getInput(constants.BLACKDUCK_WAITFORSCAN_KE
export const BLACKDUCK_SEARCH_DEPTH = getInput(constants.BLACKDUCK_SEARCH_DEPTH_KEY)?.trim() || ''
export const BLACKDUCK_CONFIG_PATH = getInput(constants.BLACKDUCK_CONFIG_PATH_KEY)?.trim() || ''
export const BLACKDUCK_ARGS = getInput(constants.BLACKDUCK_ARGS_KEY)?.trim() || ''
export const BLACKDUCK_POLICY_BADGES_CREATE = getInput(constants.BLACKDUCK_POLICY_BADGES_CREATE_KEY)?.trim() === 'true' || false
export const BLACKDUCK_POLICY_BADGES_MAX_COUNT = getInput(constants.BLACKDUCK_POLICY_BADGES_MAX_COUNT_KEY)?.trim() || ''
export const GITHUB_TOKEN = getInput(constants.GITHUB_TOKEN_KEY)?.trim() || ''
export const INCLUDE_DIAGNOSTICS = getInput(constants.INCLUDE_DIAGNOSTICS_KEY)?.trim() || ''
export const DIAGNOSTICS_RETENTION_DAYS = getInput(constants.DIAGNOSTICS_RETENTION_DAYS_KEY)?.trim() || ''
33 changes: 32 additions & 1 deletion src/synopsys-action/tools-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,21 @@ export class SynopsysToolsParameter {
}
}

if (inputs.BLACKDUCK_POLICY_BADGES_CREATE) {
blackduckData.data.blackduck.policy = {
badges: {
create: true,
...(Number.isInteger(parseInt(inputs.BLACKDUCK_POLICY_BADGES_MAX_COUNT)) && {
maxCount: parseInt(inputs.BLACKDUCK_POLICY_BADGES_MAX_COUNT)
})
}
}
// Additional null check has been added to support avoid duplicate call to getGithubRepoInfo() when fix pr is enabled
if (blackduckData.data.github == null) {
blackduckData.data.github = this.getGithubRepoInfo()
}
}

if (isBoolean(inputs.ENABLE_NETWORK_AIR_GAP)) {
blackduckData.data.network = {airGap: parseToBoolean(inputs.ENABLE_NETWORK_AIR_GAP)}
}
Expand Down Expand Up @@ -557,7 +572,7 @@ export class SynopsysToolsParameter {
const githubToken = inputs.GITHUB_TOKEN
const githubRepo = process.env[constants.GITHUB_ENVIRONMENT_VARIABLES.GITHUB_REPOSITORY]
const githubRepoName = githubRepo !== undefined ? githubRepo.substring(githubRepo.indexOf('/') + 1, githubRepo.length).trim() : ''
const githubBranchName = (parseToBoolean(inputs.POLARIS_PRCOMMENT_ENABLED) ? process.env[constants.GITHUB_ENVIRONMENT_VARIABLES.GITHUB_HEAD_REF] : process.env[constants.GITHUB_ENVIRONMENT_VARIABLES.GITHUB_REF_NAME]) || ''
const githubBranchName = this.getGithubBranchName()
const githubRef = process.env[constants.GITHUB_ENVIRONMENT_VARIABLES.GITHUB_REF]
const githubServerUrl = process.env[constants.GITHUB_ENVIRONMENT_VARIABLES.GITHUB_SERVER_URL] || ''
const githubHostUrl = githubServerUrl === constants.GITHUB_CLOUD_URL ? '' : githubServerUrl
Expand All @@ -584,6 +599,22 @@ export class SynopsysToolsParameter {
return undefined
}

private getGithubBranchName(): string {
let branchName = ''
if (parseToBoolean(inputs.POLARIS_PRCOMMENT_ENABLED)) {
// Only polaris use case
branchName = process.env[constants.GITHUB_ENVIRONMENT_VARIABLES.GITHUB_HEAD_REF] || ''
} else {
// For pull requests, non-pull requests and manual trigger events
if (process.env[constants.GITHUB_ENVIRONMENT_VARIABLES.GITHUB_HEAD_REF] !== '') {
branchName = process.env[constants.GITHUB_ENVIRONMENT_VARIABLES.GITHUB_HEAD_REF] || ''
} else {
branchName = process.env[constants.GITHUB_ENVIRONMENT_VARIABLES.GITHUB_REF_NAME] || ''
}
}
return branchName
}

private setGithubData(githubToken: string, githubRepoName: string, githubRepoOwner: string, githubBranchName: string, githubPrNumber: string, githubHostUrl: string): GithubData {
const isPrEvent = isPullRequestEvent()
const githubData: GithubData = {
Expand Down
46 changes: 46 additions & 0 deletions test/unit/synopsys-action/tools-parameter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ beforeEach(() => {
process.env['GITHUB_SERVER_URL'] = 'https://custom.com'
Object.defineProperty(inputs, 'SRM_PROJECT_NAME', {value: null})
Object.defineProperty(inputs, 'SRM_PROJECT_ID', {value: null})
Object.defineProperty(inputs, 'BLACKDUCK_POLICY_BADGES_CREATE', {value: null})
Object.defineProperty(inputs, 'BLACKDUCK_POLICY_BADGES_MAX_COUNT', {value: null})
})

afterAll(() => {
Expand Down Expand Up @@ -694,6 +696,50 @@ test('Test getFormattedCommandForBlackduck - pr comment - for enterprise github'
expect(jsonData.data.github.host.url).toBe('https://custom.com')
})

test('Test getFormattedCommandForBlackduck - badges', () => {
Object.defineProperty(inputs, 'BLACKDUCK_URL', {value: 'BLACKDUCK_URL'})
Object.defineProperty(inputs, 'BLACKDUCK_API_TOKEN', {value: 'BLACKDUCK_API_TOKEN'})
Object.defineProperty(inputs, 'BLACKDUCK_INSTALL_DIRECTORY', {value: 'BLACKDUCK_INSTALL_DIRECTORY'})
Object.defineProperty(inputs, 'BLACKDUCK_SCAN_FULL', {value: 'TRUE'})
Object.defineProperty(inputs, 'BLACKDUCK_SCAN_FAILURE_SEVERITIES', {value: 'BLOCKER, CRITICAL, MAJOR'})
Object.defineProperty(inputs, 'BLACKDUCK_POLICY_BADGES_CREATE', {value: true})
Object.defineProperty(inputs, 'BLACKDUCK_POLICY_BADGES_MAX_COUNT', {value: 5})
process.env['GITHUB_SERVER_URL'] = 'https://custom.com'
let stp: SynopsysToolsParameter = new SynopsysToolsParameter(tempPath)

let resp = stp.getFormattedCommandForBlackduck()

expect(resp).not.toBeNull()
expect(resp).toContain('--stage blackduck')

const jsonString = fs.readFileSync(tempPath.concat(blackduck_input_file), 'utf-8')
const jsonData = JSON.parse(jsonString)
expect(jsonData.data.blackduck.policy.badges.create).toBe(true)
expect(jsonData.data.blackduck.policy.badges.maxCount).toBe(5)
})

test('Test getFormattedCommandForBlackduck - badges failure (empty github token)', () => {
Object.defineProperty(inputs, 'GITHUB_TOKEN', {value: ''})
Object.defineProperty(inputs, 'BLACKDUCK_URL', {value: 'BLACKDUCK_URL'})
Object.defineProperty(inputs, 'BLACKDUCK_API_TOKEN', {value: 'BLACKDUCK_API_TOKEN'})
Object.defineProperty(inputs, 'BLACKDUCK_INSTALL_DIRECTORY', {value: 'BLACKDUCK_INSTALL_DIRECTORY'})
Object.defineProperty(inputs, 'BLACKDUCK_SCAN_FULL', {value: 'TRUE'})
Object.defineProperty(inputs, 'BLACKDUCK_SCAN_FAILURE_SEVERITIES', {value: 'BLOCKER, CRITICAL, MAJOR'})
Object.defineProperty(inputs, 'BLACKDUCK_POLICY_BADGES_CREATE', {value: true})
Object.defineProperty(inputs, 'BLACKDUCK_POLICY_BADGES_MAX_COUNT', {value: 5})

process.env['GITHUB_SERVER_URL'] = 'https://custom.com'
let stp: SynopsysToolsParameter = new SynopsysToolsParameter(tempPath)

try {
stp.getFormattedCommandForBlackduck()
} catch (error: any) {
expect(error).toBeInstanceOf(Error)
expect(error.message).toContain('Missing required github token for fix pull request/pull request comments/Github Badges')
}
Object.defineProperty(inputs, 'GITHUB_TOKEN', {value: 'token'})
})

test('Test missing data error in getFormattedCommandForBlackduck', () => {
Object.defineProperty(inputs, 'BLACKDUCK_INSTALL_DIRECTORY', {value: 'BLACKDUCK_INSTALL_DIRECTORY'})
Object.defineProperty(inputs, 'BLACKDUCK_SCAN_FULL', {value: 'TRUE'})
Expand Down

0 comments on commit 54439cc

Please sign in to comment.