Repository navigation
feat(code_scanning): manage CodeQL default setup #1131
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
Open
nicomazzarello
wants to merge
4
commits into
github-community-projects:main-enterprise
Choose a base branch
from
nicomazzarello:nicomazzarello/code-scanning-default-setup
base: main-enterprise
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
8963eee
feat(code_scanning): manage CodeQL default setup
nicomazzarello 093d9e0
fix(code_scanning): replace inherited languages and name NOP results …
nicomazzarello 91261af
fix(code_scanning): report cleared fields as deletions
nicomazzarello dcbd5bd
fix(code_scanning): keep before values for modified fields in NOP rep…
nicomazzarello File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| const ErrorStash = require('./errorStash') | ||
| const NopCommand = require('../nopcommand') | ||
|
|
||
| // The config section name. NOP results carry it as their plugin, since the dry-run | ||
| // drift filter looks up the section by the lowercased plugin name. | ||
| const SECTION = 'code_scanning' | ||
| const DEFAULT_SETUP_FIELDS = ['state', 'query_suite', 'threat_model', 'languages', 'runner_type', 'runner_label'] | ||
|
|
||
| module.exports = class CodeScanning extends ErrorStash { | ||
| /** | ||
| * The `default_setup.languages` of the highest-priority layer that declares them. | ||
| * The generic deep merge concatenates arrays, but a lower layer's languages should be | ||
| * replaced, so a repo can narrow or clear the list it inherits. | ||
| * | ||
| * @param {Array<object|undefined>} sections `code_scanning` sections, lowest priority first | ||
| * @returns {string[]|undefined} | ||
| */ | ||
| static declaredLanguages (sections) { | ||
| let languages | ||
| for (const section of sections) { | ||
| if (Array.isArray(section?.default_setup?.languages)) { | ||
| languages = section.default_setup.languages | ||
| } | ||
| } | ||
| return languages | ||
| } | ||
|
|
||
| constructor (nop, github, repo, settings, log, errors) { | ||
| super(errors) | ||
| this.github = github | ||
| this.repo = repo | ||
| this.settings = settings | ||
| this.log = log | ||
| this.nop = nop | ||
| } | ||
|
|
||
| /** | ||
| * Reconcile the repository's CodeQL default setup with `code_scanning.default_setup`. | ||
| * | ||
| * @see {@link https://docs.github.com/en/rest/code-scanning/code-scanning#update-a-code-scanning-default-setup-configuration} | ||
| */ | ||
| async sync () { | ||
| this.hasChanges = false | ||
| const desired = this.settings?.default_setup | ||
| if (!desired) { | ||
| return [] | ||
| } | ||
|
|
||
| let current | ||
| try { | ||
| const { data } = await this.github.rest.codeScanning.getDefaultSetup({ owner: this.repo.owner, repo: this.repo.repo }) | ||
| current = data | ||
| } catch (e) { | ||
| return this.handleError('Error getting code scanning default setup', e) | ||
| } | ||
|
|
||
| const { modifications, deletions } = this.changes(current, desired) | ||
| if (Object.keys(modifications).length === 0 && Object.keys(deletions).length === 0) { | ||
| this.log.debug(`Code scanning default setup for ${this.repo.owner}/${this.repo.repo} already matches the config`) | ||
| return [] | ||
| } | ||
| this.hasChanges = true | ||
|
|
||
| const params = { owner: this.repo.owner, repo: this.repo.repo, ...this.declaredFields(desired) } | ||
| if (this.nop) { | ||
| return [new NopCommand( | ||
| SECTION, | ||
| this.repo, | ||
| this.github.rest.codeScanning.updateDefaultSetup.endpoint(params), | ||
| { msg: 'Change found', additions: {}, modifications, deletions } | ||
| )] | ||
| } | ||
|
|
||
| this.log.info(`Updating code scanning default setup for ${this.repo.owner}/${this.repo.repo}`) | ||
| try { | ||
| await this.github.rest.codeScanning.updateDefaultSetup(params) | ||
| } catch (e) { | ||
| return this.handleError('Error updating code scanning default setup', e) | ||
| } | ||
| return [] | ||
| } | ||
|
|
||
| /** | ||
| * The declared fields that differ from GitHub. With `state: not-configured` only the state | ||
| * is compared, since GitHub keeps no other settings for a repository without default setup. | ||
| * | ||
| * As in the Repository plugin, the NOP report reads `deletions` as the before values and | ||
| * `modifications` as the after values. Every changed field keeps GitHub's value in | ||
| * `deletions`. A field cleared to `[]` or `null` stays out of `modifications`, where the | ||
| * report would treat its empty value as no change, and so shows as deleted. | ||
| */ | ||
| changes (current, desired) { | ||
| const fields = desired.state === 'not-configured' ? ['state'] : DEFAULT_SETUP_FIELDS | ||
| const modifications = {} | ||
| const deletions = {} | ||
| for (const field of fields) { | ||
| if (desired[field] === undefined) continue | ||
| const same = field === 'languages' | ||
| ? this.sameLanguages(current[field], desired[field]) | ||
| : current[field] === desired[field] | ||
| if (same) continue | ||
| deletions[field] = current[field] | ||
| const cleared = desired[field] === null || (Array.isArray(desired[field]) && desired[field].length === 0) | ||
| if (!cleared) { | ||
| modifications[field] = desired[field] | ||
| } | ||
| } | ||
| return { modifications, deletions } | ||
| } | ||
|
|
||
| sameLanguages (current = [], desired = []) { | ||
| const currentSet = new Set(current) | ||
| const desiredSet = new Set(desired) | ||
| return currentSet.size === desiredSet.size && [...desiredSet].every(lang => currentSet.has(lang)) | ||
| } | ||
|
|
||
| declaredFields (desired) { | ||
| const fields = desired.state === 'not-configured' ? ['state'] : DEFAULT_SETUP_FIELDS | ||
| return Object.fromEntries(fields.filter(field => desired[field] !== undefined).map(field => [field, desired[field]])) | ||
| } | ||
|
|
||
| handleError (msg, e) { | ||
| const message = `${msg} for ${this.repo.owner}/${this.repo.repo}: ${e.status ? `${e.status} ` : ''}${e.message}` | ||
| if (this.nop) { | ||
| // As in Diffable: in a dry run the repo may not exist yet. | ||
| if (e.status === 404) return [] | ||
| return [new NopCommand(SECTION, this.repo, null, message, 'ERROR')] | ||
| } | ||
| this.logError(message) | ||
| return [] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.