-
Notifications
You must be signed in to change notification settings - Fork 104
Fix/ghes sarif baseurl #300
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,17 +103,23 @@ export class JobSummary { | |
| core.warning(`Failed populating code scanning sarif: ${error}`); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Uploads the code scanning SARIF content to the code-scanning GitHub API. | ||
| * @param encodedSarif - The final compressed and encoded sarif content. | ||
| * @param token - GitHub token to use for the request. Has to have 'security-events: write' permission. | ||
| * @private | ||
| * Uploads a SARIF (Static Analysis Results Interchange Format) file to GitHub's code scanning API. | ||
| * This method handles the communication with GitHub's REST API to populate the code scanning tab with security analysis results. | ||
| * Supports both GitHub.com and GitHub Enterprise Server installations. | ||
| * @param encodedSarif - The SARIF content that has been compressed using gzip and encoded to base64 format. | ||
| * This encoding is required by GitHub's code-scanning/sarifs API endpoint. | ||
| * @param token - GitHub authentication token with appropriate permissions to upload SARIF files. | ||
| * Must have 'security_events: write' and 'contents: read' permissions. | ||
| * @throws Will throw an error if the HTTP response status is not in the 2xx range or if authentication fails. | ||
| */ | ||
| private static async uploadCodeScanningSarif(encodedSarif: string, token: string) { | ||
| const octokit: Octokit = new Octokit({ auth: token }); | ||
| let response: OctokitResponse<any> | undefined; | ||
| response = await octokit.request('POST /repos/{owner}/{repo}/code-scanning/sarifs', { | ||
| const inputBaseUrl = core.getInput('ghe-base-url', { required: false }) || core.getInput('ghe_base_url', { required: false }) || ''; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why support both conventions ? "-" and "_" ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the sake of compatibility, errors may occur. |
||
|
|
||
| const octokit = inputBaseUrl ? github.getOctokit(token, { baseUrl: inputBaseUrl }) : github.getOctokit(token); | ||
|
|
||
| const response = await octokit.request('POST /repos/{owner}/{repo}/code-scanning/sarifs', { | ||
| owner: github.context.repo.owner, | ||
| repo: github.context.repo.repo, | ||
| commit_sha: github.context.sha, | ||
|
|
@@ -122,7 +128,8 @@ export class JobSummary { | |
| }); | ||
|
|
||
| if (response.status < 200 || response.status >= 300) { | ||
| throw new Error(`Failed to upload SARIF file: ` + JSON.stringify(response)); | ||
| const usedBaseUrl = (octokit as any).request?.endpoint?.DEFAULTS?.baseUrl || 'unknown'; | ||
| throw new Error(`Failed to upload SARIF file (status ${response.status}). baseUrl=${usedBaseUrl}; response=` + JSON.stringify(response)); | ||
| } | ||
|
|
||
| core.info('SARIF file uploaded successfully'); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /** | ||
| * test/job-summary.sarif.baseurl.spec.ts | ||
| * Checking baseUrl selection when loading SARIF on GHES. | ||
| */ | ||
|
|
||
| import * as core from '@actions/core'; | ||
|
|
||
| jest.mock('@actions/github', () => { | ||
| const actual = jest.requireActual('@actions/github'); | ||
| return { | ||
| ...actual, | ||
| getOctokit: jest.fn((token: string, opts?: { baseUrl?: string }) => { | ||
| const usedBaseUrl = (opts && opts.baseUrl) || process.env.__AUTO_BASE_URL__ || 'https://api.github.com'; | ||
|
|
||
| const req = jest.fn(async (_route: string, _params: any) => { | ||
| (global as any).__USED_BASE_URL__ = usedBaseUrl; | ||
| return { status: 201, data: {} }; | ||
| }) as unknown as any; | ||
|
|
||
| req.endpoint = { DEFAULTS: { baseUrl: usedBaseUrl } }; | ||
|
|
||
| return { request: req } as any; | ||
| }), | ||
| context: { | ||
| repo: { owner: 'o', repo: 'r' }, | ||
| sha: 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef', | ||
| ref: 'refs/heads/main', | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| import { JobSummary } from '../src/job-summary'; | ||
|
|
||
| describe('uploadCodeScanningSarif baseUrl selection (GHES)', () => { | ||
| beforeEach(() => { | ||
| jest.resetModules(); | ||
| jest.clearAllMocks(); | ||
|
|
||
| jest.spyOn(core, 'getInput').mockImplementation((_name: string) => ''); | ||
|
|
||
| delete process.env.__AUTO_BASE_URL__; | ||
| delete (global as any).__USED_BASE_URL__; | ||
| }); | ||
|
|
||
| it('Should use explicit input ghe-base-url if given', async () => { | ||
| (core.getInput as jest.Mock).mockImplementation((name: string) => { | ||
| if (name === 'ghe-base-url') return 'https://github.enterprise.local/api/v3'; | ||
| if (name === 'ghe_base_url') return ''; | ||
| return ''; | ||
| }); | ||
|
|
||
| await (JobSummary as any).uploadCodeScanningSarif('eJx4YWJj', 'ghs_token'); | ||
|
|
||
| expect((global as any).__USED_BASE_URL__).toBe('https://github.enterprise.local/api/v3'); | ||
| }); | ||
|
|
||
| it('Should falls back to auto GHES baseUrl via @actions/github if input is not specified', async () => { | ||
| process.env.__AUTO_BASE_URL__ = 'https://ghe.corp.local/api/v3'; | ||
|
|
||
| await (JobSummary as any).uploadCodeScanningSarif('eJx4YWJj', 'ghs_token'); | ||
|
|
||
| expect((global as any).__USED_BASE_URL__).toBe('https://ghe.corp.local/api/v3'); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adjust variable naming
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also variable declaration can be moved closer to usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its lib code and generated by the compiler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right sry