2019-12-13 12:18:28 +09:00
|
|
|
import * as fs from 'fs'
|
|
|
|
|
import * as path from 'path'
|
2025-05-07 11:18:01 +00:00
|
|
|
import * as core from '../__fixtures__/core'
|
2025-05-03 12:32:25 +00:00
|
|
|
import { Audit } from '../src/audit'
|
|
|
|
|
import { run } from '../src/main'
|
2021-10-03 09:26:16 +09:00
|
|
|
import * as issue from '../src/issue'
|
2019-12-13 12:18:28 +09:00
|
|
|
import * as pr from '../src/pr'
|
2025-05-03 12:32:25 +00:00
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
|
import { dirname } from 'path'
|
2025-05-03 02:15:44 +00:00
|
|
|
|
|
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
|
|
|
const __dirname = dirname(__filename)
|
2019-12-13 12:18:28 +09:00
|
|
|
|
2025-05-07 11:18:01 +00:00
|
|
|
// Mocks should be declared before the module being tested is imported.
|
|
|
|
|
vi.mock('@actions/core', () => core)
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mock('../src/audit')
|
|
|
|
|
vi.mock('../src/issue')
|
|
|
|
|
vi.mock('../src/pr')
|
|
|
|
|
vi.mock('@octokit/rest', () => {
|
2025-05-01 13:16:23 +00:00
|
|
|
return {
|
2025-05-02 14:03:34 +00:00
|
|
|
Octokit: vi.fn().mockImplementation(() => {
|
2025-05-01 13:16:23 +00:00
|
|
|
return {
|
|
|
|
|
issues: {
|
2025-05-02 14:03:34 +00:00
|
|
|
listForRepo: vi.fn(),
|
|
|
|
|
createComment: vi.fn(),
|
|
|
|
|
create: vi.fn()
|
2025-05-01 13:16:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
2019-12-13 12:18:28 +09:00
|
|
|
|
2021-10-03 09:26:16 +09:00
|
|
|
describe('run: pr', () => {
|
2019-12-13 12:18:28 +09:00
|
|
|
beforeEach(() => {
|
|
|
|
|
// initialize mock
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mocked(Audit).mockClear()
|
|
|
|
|
vi.mocked(pr).createComment.mockClear()
|
2019-12-14 21:08:54 +09:00
|
|
|
|
2020-03-21 07:08:53 +09:00
|
|
|
process.env.INPUT_AUDIT_LEVEL = 'low'
|
2020-11-12 12:33:56 +02:00
|
|
|
process.env.INPUT_PRODUCTION_FLAG = 'false'
|
2020-12-12 13:56:05 +02:00
|
|
|
process.env.INPUT_JSON_FLAG = 'false'
|
2019-12-14 21:08:54 +09:00
|
|
|
process.env.INPUT_GITHUB_CONTEXT =
|
|
|
|
|
'{ "event_name": "pull_request", "event": { "number": 100} }'
|
|
|
|
|
process.env.INPUT_GITHUB_TOKEN = '***'
|
|
|
|
|
process.env.GITHUB_REPOSITORY = 'alice/example'
|
2021-10-03 09:26:16 +09:00
|
|
|
process.env.INPUT_CREATE_PR_COMMENTS = 'true'
|
2019-12-13 12:18:28 +09:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('does not call pr.createComment if vulnerabilities are not found', () => {
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mocked(Audit).mockImplementation((): any => {
|
2019-12-13 12:18:28 +09:00
|
|
|
return {
|
|
|
|
|
stdout: fs.readFileSync(
|
|
|
|
|
path.join(__dirname, 'testdata/audit/success.txt')
|
|
|
|
|
),
|
|
|
|
|
status: 0,
|
2025-05-07 11:58:37 +00:00
|
|
|
run: (): Promise<void> => {
|
2020-03-19 17:46:36 +09:00
|
|
|
return Promise.resolve(void 0)
|
2019-12-13 12:18:28 +09:00
|
|
|
},
|
|
|
|
|
foundVulnerability: (): boolean => {
|
2019-12-14 21:08:54 +09:00
|
|
|
return false
|
|
|
|
|
},
|
|
|
|
|
strippedStdout: (): string => {
|
|
|
|
|
return path.join(__dirname, 'testdata/audit/success.txt')
|
2019-12-13 12:18:28 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mocked(pr).createComment.mockResolvedValue()
|
2019-12-13 12:18:28 +09:00
|
|
|
|
2019-12-14 21:08:54 +09:00
|
|
|
expect(run).not.toThrowError()
|
2019-12-13 12:18:28 +09:00
|
|
|
expect(pr.createComment).not.toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('calls pr.createComment if vulnerabilities are found in PR', () => {
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mocked(Audit).mockImplementation((): any => {
|
2019-12-13 12:18:28 +09:00
|
|
|
return {
|
|
|
|
|
stdout: fs.readFileSync(
|
|
|
|
|
path.join(__dirname, 'testdata/audit/error.txt')
|
|
|
|
|
),
|
|
|
|
|
status: 1,
|
2025-05-07 11:58:37 +00:00
|
|
|
run: (): Promise<void> => {
|
2020-03-19 17:46:36 +09:00
|
|
|
return Promise.resolve(void 0)
|
2019-12-13 12:18:28 +09:00
|
|
|
},
|
|
|
|
|
foundVulnerability: (): boolean => {
|
|
|
|
|
return true
|
|
|
|
|
},
|
|
|
|
|
strippedStdout: (): string => {
|
|
|
|
|
return path.join(__dirname, 'testdata/audit/error.txt')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mocked(pr).createComment.mockResolvedValue()
|
2019-12-13 12:18:28 +09:00
|
|
|
|
2019-12-14 21:08:54 +09:00
|
|
|
expect(run).not.toThrowError()
|
2019-12-13 12:18:28 +09:00
|
|
|
expect(pr.createComment).toHaveBeenCalled()
|
|
|
|
|
})
|
2021-10-03 09:26:16 +09:00
|
|
|
|
|
|
|
|
test('does not call pr.createComment if create_pr_comments is set to false', () => {
|
|
|
|
|
process.env.INPUT_CREATE_PR_COMMENTS = 'false'
|
|
|
|
|
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mocked(Audit).mockImplementation((): any => {
|
2021-10-03 09:26:16 +09:00
|
|
|
return {
|
|
|
|
|
stdout: fs.readFileSync(
|
|
|
|
|
path.join(__dirname, 'testdata/audit/error.txt')
|
|
|
|
|
),
|
|
|
|
|
status: 1,
|
2025-05-07 11:58:37 +00:00
|
|
|
run: (): Promise<void> => {
|
2021-10-03 09:26:16 +09:00
|
|
|
return Promise.resolve(void 0)
|
|
|
|
|
},
|
|
|
|
|
foundVulnerability: (): boolean => {
|
|
|
|
|
return true
|
|
|
|
|
},
|
|
|
|
|
strippedStdout: (): string => {
|
|
|
|
|
return path.join(__dirname, 'testdata/audit/error.txt')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(run).not.toThrowError()
|
|
|
|
|
expect(pr.createComment).not.toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('run: issue', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
// initialize mock
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mocked(Audit).mockClear()
|
|
|
|
|
vi.mocked(issue).getExistingIssueNumber.mockClear()
|
2021-10-03 09:26:16 +09:00
|
|
|
|
|
|
|
|
process.env.INPUT_AUDIT_LEVEL = 'low'
|
|
|
|
|
process.env.INPUT_PRODUCTION_FLAG = 'false'
|
|
|
|
|
process.env.INPUT_JSON_FLAG = 'false'
|
|
|
|
|
process.env.INPUT_GITHUB_CONTEXT = '{ "event_name": "push" }'
|
|
|
|
|
process.env.INPUT_GITHUB_TOKEN = '***'
|
|
|
|
|
process.env.GITHUB_REPOSITORY = 'alice/example'
|
|
|
|
|
process.env.INPUT_CREATE_ISSUES = 'true'
|
|
|
|
|
process.env.INPUT_DEDUPE_ISSUES = 'true'
|
|
|
|
|
})
|
|
|
|
|
|
2021-10-09 12:22:18 +09:00
|
|
|
test('does not call octokit.rest.issues.create if create_issues is set to false', () => {
|
2021-10-03 09:26:16 +09:00
|
|
|
process.env.INPUT_CREATE_ISSUES = 'false'
|
|
|
|
|
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mocked(Audit).mockImplementation((): any => {
|
2021-10-03 09:26:16 +09:00
|
|
|
return {
|
|
|
|
|
stdout: fs.readFileSync(
|
|
|
|
|
path.join(__dirname, 'testdata/audit/error.txt')
|
|
|
|
|
),
|
|
|
|
|
status: 1,
|
2025-05-07 11:58:37 +00:00
|
|
|
run: (): Promise<void> => {
|
2021-10-03 09:26:16 +09:00
|
|
|
return Promise.resolve(void 0)
|
|
|
|
|
},
|
|
|
|
|
foundVulnerability: (): boolean => {
|
|
|
|
|
return true
|
|
|
|
|
},
|
|
|
|
|
strippedStdout: (): string => {
|
|
|
|
|
return path.join(__dirname, 'testdata/audit/error.txt')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
2025-05-02 14:03:34 +00:00
|
|
|
vi.mocked(issue).getExistingIssueNumber.mockResolvedValue(null)
|
2021-10-03 09:26:16 +09:00
|
|
|
|
|
|
|
|
expect(run).not.toThrowError()
|
|
|
|
|
expect(issue.getExistingIssueNumber).not.toHaveBeenCalled()
|
|
|
|
|
})
|
2019-12-09 08:51:14 +09:00
|
|
|
})
|