Skip to content

Commit

Permalink
SIGINT 1409 workflow simplification for github.host.url
Browse files Browse the repository at this point in the history
Co-authored-by: spurohit <[email protected]>
  • Loading branch information
sadmananik and spurohitsynopsys authored Dec 11, 2023
1 parent 70adfdc commit 45ec794
Show file tree
Hide file tree
Showing 8 changed files with 761 additions and 664 deletions.
1,314 changes: 658 additions & 656 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 @@ -100,6 +100,7 @@ export let EXIT_CODE_MAP = new Map<string, string>([
export const RETRY_DELAY_IN_MILLISECONDS = 15000
export const RETRY_COUNT = 3
export const NON_RETRY_HTTP_CODES = new Set([200, 201, 401, 403, 416])
export const GITHUB_CLOUD_URL = 'https://github.com'

export const GITHUB_ENVIRONMENT_VARIABLES = {
GITHUB_TOKEN: 'GITHUB_TOKEN',
Expand All @@ -109,5 +110,6 @@ export const GITHUB_ENVIRONMENT_VARIABLES = {
GITHUB_REF_NAME: 'GITHUB_REF_NAME',
GITHUB_REPOSITORY_OWNER: 'GITHUB_REPOSITORY_OWNER',
GITHUB_BASE_REF: 'GITHUB_BASE_REF',
GITHUB_EVENT_NAME: 'GITHUB_EVENT_NAME'
GITHUB_EVENT_NAME: 'GITHUB_EVENT_NAME',
GITHUB_SERVER_URL: 'GITHUB_SERVER_URL'
}
1 change: 0 additions & 1 deletion src/synopsys-action/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ export const BLACKDUCK_FIXPR_MAXCOUNT = getInput(constants.BLACKDUCK_FIXPR_MAXCO
export const BLACKDUCK_FIXPR_CREATE_SINGLE_PR = getInput(constants.BLACKDUCK_FIXPR_CREATE_SINGLE_PR_KEY)?.trim() || ''
export const BLACKDUCK_FIXPR_FILTER_SEVERITIES = getInput(constants.BLACKDUCK_FIXPR_FILTER_SEVERITIES_KEY)?.trim() || ''
export const BLACKDUCK_FIXPR_LONG_TERM_GUIDANCE = getInput(constants.BLACKDUCK_FIXPR_USE_UPGRADE_GUIDANCE_KEY)?.trim() || ''
export const GITHUB_HOST_URL = getInput(constants.GITHUB_HOST_URL_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() || ''
7 changes: 4 additions & 3 deletions src/synopsys-action/tools-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,14 +286,15 @@ export class SynopsysToolsParameter {
const githubToken = inputs.GITHUB_TOKEN
const githubRepo = process.env[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[GITHUB_ENVIRONMENT_VARIABLES.GITHUB_HEAD_REF] : process.env[GITHUB_ENVIRONMENT_VARIABLES.GITHUB_REF_NAME]
const githubBranchName = (parseToBoolean(inputs.POLARIS_PRCOMMENT_ENABLED) ? process.env[GITHUB_ENVIRONMENT_VARIABLES.GITHUB_HEAD_REF] : process.env[GITHUB_ENVIRONMENT_VARIABLES.GITHUB_REF_NAME]) || ''
const githubRef = process.env[GITHUB_ENVIRONMENT_VARIABLES.GITHUB_REF]
const githubHostUrl = inputs.GITHUB_HOST_URL ? inputs.GITHUB_HOST_URL : ''
const githubServerUrl = process.env[GITHUB_ENVIRONMENT_VARIABLES.GITHUB_SERVER_URL] || ''
const githubHostUrl = githubServerUrl === constants.GITHUB_CLOUD_URL ? '' : githubServerUrl

// pr number will be part of "refs/pull/<pr_number>/merge"
// if there is manual run without raising pr then GITHUB_REF will return refs/heads/branch_name
const githubPrNumber = githubRef !== undefined ? githubRef.split('/')[2].trim() : ''
const githubRepoOwner = process.env[GITHUB_ENVIRONMENT_VARIABLES.GITHUB_REPOSITORY_OWNER]
const githubRepoOwner = process.env[GITHUB_ENVIRONMENT_VARIABLES.GITHUB_REPOSITORY_OWNER] || ''

if (isNullOrEmptyValue(githubToken)) {
throw new Error('Missing required github token for fix pull request/pull request comments')
Expand Down
1 change: 1 addition & 0 deletions test/contract/coverity.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,5 @@ export function mockBridgeDownloadUrlAndSynopsysBridgePath() {
process.env['GITHUB_REF_NAME'] = 'synopsys-action'
process.env['GITHUB_EVENT_NAME'] = 'pull_request'
process.env['GITHUB_BASE_REF'] = 'current-branch'
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
}
1 change: 0 additions & 1 deletion test/unit/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ test('Enable airgap', async () => {
Object.defineProperty(inputs, 'BLACKDUCK_URL', {value: 'BLACKDUCK_URL'})
Object.defineProperty(inputs, 'BLACKDUCK_API_TOKEN', {value: 'BLACKDUCK_API_TOKEN'})
Object.defineProperty(inputs, 'ENABLE_NETWORK_AIR_GAP', {value: true})
Object.defineProperty(inputs, 'GITHUB_HOST_URL', {value: 'GITHUB_HOST_URL'})

const defaultDir = jest.spyOn(SynopsysBridge.prototype as any, 'getBridgeDefaultPath')
defaultDir.mockImplementation(() => {
Expand Down
95 changes: 94 additions & 1 deletion test/unit/synopsys-action/tools-parameter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as inputs from '../../../src/synopsys-action/inputs'
let tempPath = '/temp'
let polaris_input_file = '/polaris_input.json'
let coverity_input_file = '/coverity_input.json'
let blackduck_input_file = '/bd_input.json'

beforeAll(() => {
createTempDir().then(path => (tempPath = path))
Expand All @@ -21,6 +22,7 @@ beforeEach(() => {
process.env['GITHUB_REF_NAME'] = 'ref-name'
process.env['GITHUB_HEAD_REF'] = 'feature-branch-1'
process.env['GITHUB_BASE_REF'] = 'main'
process.env['GITHUB_SERVER_URL'] = 'https://custom.com'
})

afterAll(() => {
Expand Down Expand Up @@ -112,6 +114,46 @@ test('Test getFormattedCommandForPolaris - prComment', () => {
expect(resp).toContain('--stage polaris')
})

test('Test getFormattedCommandForPolaris - pr comment for enterprise github', () => {
Object.defineProperty(inputs, 'POLARIS_SERVER_URL', {value: 'server_url'})
Object.defineProperty(inputs, 'POLARIS_ACCESS_TOKEN', {value: 'access_token'})
Object.defineProperty(inputs, 'POLARIS_APPLICATION_NAME', {value: 'POLARIS_APPLICATION_NAME'})
Object.defineProperty(inputs, 'POLARIS_PROJECT_NAME', {value: 'POLARIS_PROJECT_NAME'})
Object.defineProperty(inputs, 'POLARIS_ASSESSMENT_TYPES', {value: 'SCA,SAST'})
Object.defineProperty(inputs, 'POLARIS_PRCOMMENT_ENABLED', {value: true})
Object.defineProperty(inputs, 'POLARIS_PRCOMMENT_SEVERITIES', {value: 'CRITICAL,HIGH'})
Object.defineProperty(inputs, 'GITHUB_TOKEN', {value: 'test-token'})
const stp: SynopsysToolsParameter = new SynopsysToolsParameter(tempPath)
const resp = stp.getFormattedCommandForPolaris('synopsys-action')
expect(resp).not.toBeNull()
expect(resp).toContain('--stage polaris')

const jsonString = fs.readFileSync(tempPath.concat(polaris_input_file), 'utf-8')
const jsonData = JSON.parse(jsonString)
expect(jsonData.data.github.host.url).toBe('https://custom.com')
})

test('Test getFormattedCommandForPolaris - pr comment for cloud github', () => {
Object.defineProperty(inputs, 'POLARIS_SERVER_URL', {value: 'server_url'})
Object.defineProperty(inputs, 'POLARIS_ACCESS_TOKEN', {value: 'access_token'})
Object.defineProperty(inputs, 'POLARIS_APPLICATION_NAME', {value: 'POLARIS_APPLICATION_NAME'})
Object.defineProperty(inputs, 'POLARIS_PROJECT_NAME', {value: 'POLARIS_PROJECT_NAME'})
Object.defineProperty(inputs, 'POLARIS_ASSESSMENT_TYPES', {value: 'SCA,SAST'})
Object.defineProperty(inputs, 'POLARIS_PRCOMMENT_ENABLED', {value: true})
Object.defineProperty(inputs, 'POLARIS_PRCOMMENT_SEVERITIES', {value: 'CRITICAL,HIGH'})
Object.defineProperty(inputs, 'GITHUB_TOKEN', {value: 'test-token'})
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
const stp: SynopsysToolsParameter = new SynopsysToolsParameter(tempPath)
const resp = stp.getFormattedCommandForPolaris('synopsys-action')
expect(resp).not.toBeNull()
expect(resp).toContain('--stage polaris')

const jsonString = fs.readFileSync(tempPath.concat(polaris_input_file), 'utf-8')
const jsonData = JSON.parse(jsonString)
expect(jsonData.data.github.host.url).toBe('')
})

process.env['GITHUB_SERVER_URL'] = 'https://custom.com'
test('Test getFormattedCommandForCoverity', () => {
Object.defineProperty(inputs, 'COVERITY_URL', {value: 'COVERITY_URL'})
Object.defineProperty(inputs, 'COVERITY_USER', {value: 'COVERITY_USER'})
Expand Down Expand Up @@ -176,7 +218,6 @@ test('Enable Test getFormattedCommandForCoverity Airgap: SUCCESS', () => {
Object.defineProperty(inputs, 'COVERITY_BRANCH_NAME', {value: 'COVERITY_BRANCH_NAME'})
Object.defineProperty(inputs, 'COVERITY_PRCOMMENT_ENABLED', {value: true})
Object.defineProperty(inputs, 'ENABLE_NETWORK_AIR_GAP', {value: true})
Object.defineProperty(inputs, 'GITHUB_HOST_URL', {value: 'GITHUB_API_URL'})
Object.defineProperty(inputs, 'GITHUB_TOKEN', {value: 'test-token'})

const stp: SynopsysToolsParameter = new SynopsysToolsParameter(tempPath)
Expand Down Expand Up @@ -300,13 +341,18 @@ test('Test getFormattedCommandForCoverity - pr comment', () => {
Object.defineProperty(inputs, 'COVERITY_BRANCH_NAME', {value: 'COVERITY_BRANCH_NAME'})
Object.defineProperty(inputs, 'COVERITY_PRCOMMENT_ENABLED', {value: true})
Object.defineProperty(inputs, 'GITHUB_TOKEN', {value: 'test-token'})
process.env['GITHUB_SERVER_URL'] = 'https://github.com'
let stp: SynopsysToolsParameter = new SynopsysToolsParameter(tempPath)

let resp = stp.getFormattedCommandForCoverity('synopsys-action')

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

const jsonString = fs.readFileSync(tempPath.concat(coverity_input_file), 'utf-8')
const jsonData = JSON.parse(jsonString)
expect(jsonData.data.github.host.url).toBe('')

Object.defineProperty(inputs, 'COVERITY_PRCOMMENT_ENABLED', {value: false})
stp = new SynopsysToolsParameter(tempPath)
resp = stp.getFormattedCommandForCoverity('synopsys-action')
Expand Down Expand Up @@ -359,6 +405,31 @@ test('Test getFormattedCommandForCoverity - pr comment', () => {
Object.defineProperty(inputs, 'COVERITY_STREAM_NAME', {value: null})
})

test('Test getFormattedCommandForCoverity - pr comment for enterprise github', () => {
Object.defineProperty(inputs, 'COVERITY_URL', {value: 'COVERITY_URL'})
Object.defineProperty(inputs, 'COVERITY_USER', {value: 'COVERITY_USER'})
Object.defineProperty(inputs, 'COVERITY_PASSPHRASE', {value: 'COVERITY_PASSPHRASE'})
Object.defineProperty(inputs, 'COVERITY_PROJECT_NAME', {value: 'COVERITY_PROJECT_NAME'})
Object.defineProperty(inputs, 'COVERITY_STREAM_NAME', {value: 'COVERITY_STREAM_NAME'})
Object.defineProperty(inputs, 'COVERITY_INSTALL_DIRECTORY', {value: 'COVERITY_INSTALL_DIRECTORY'})
Object.defineProperty(inputs, 'COVERITY_POLICY_VIEW', {value: 'COVERITY_POLICY_VIEW'})
Object.defineProperty(inputs, 'COVERITY_REPOSITORY_NAME', {value: 'COVERITY_REPOSITORY_NAME'})
Object.defineProperty(inputs, 'COVERITY_BRANCH_NAME', {value: 'COVERITY_BRANCH_NAME'})
Object.defineProperty(inputs, 'COVERITY_PRCOMMENT_ENABLED', {value: true})
Object.defineProperty(inputs, 'GITHUB_TOKEN', {value: 'test-token'})
process.env['GITHUB_SERVER_URL'] = 'https://custom.com'
let stp: SynopsysToolsParameter = new SynopsysToolsParameter(tempPath)

let resp = stp.getFormattedCommandForCoverity('synopsys-action')

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

const jsonString = fs.readFileSync(tempPath.concat(coverity_input_file), 'utf-8')
const jsonData = JSON.parse(jsonString)
expect(jsonData.data.github.host.url).toBe('https://custom.com')
})

test('Test missing data error in getFormattedCommandForCoverity', () => {
const stp: SynopsysToolsParameter = new SynopsysToolsParameter(tempPath)

Expand Down Expand Up @@ -576,6 +647,28 @@ test('Test getFormattedCommandForBlackduck - pr comment test cases', () => {
expect(resp).toContain('--stage blackduck')
})

test('Test getFormattedCommandForBlackduck - pr comment - for enterprise github', () => {
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_PRCOMMENT_ENABLED', {value: true})
Object.defineProperty(inputs, 'GITHUB_TOKEN', {value: 'test-token'})
Object.defineProperty(inputs, 'BLACKDUCK_FIXPR_ENABLED', {value: false})
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.github.host.url).toBe('https://custom.com')
})

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 45ec794

Please sign in to comment.