Skip to content

feat: Add github_repository_code_scanning_default_setup resource#3315

Open
oda251 wants to merge 2 commits intointegrations:mainfrom
oda251:feat/code-scanning-default-setup
Open

feat: Add github_repository_code_scanning_default_setup resource#3315
oda251 wants to merge 2 commits intointegrations:mainfrom
oda251:feat/code-scanning-default-setup

Conversation

@oda251
Copy link
Copy Markdown

@oda251 oda251 commented Apr 1, 2026

Resolves #1648
Partially addresses #2043


Before the change?

After the change?

  • New resource github_repository_code_scanning_default_setup manages CodeQL default setup per repository.
  • Handles AcceptedError from the async PATCH API, following the same pattern as resource_github_enterprise_organization and resource_github_actions_hosted_runner.
  • Uses retry.StateChangeConf to poll until the target state is reached.
  • User-configurable timeouts via timeouts block (default 5 min).
  • languages is optional — default setup automatically includes all supported languages when omitted.
resource "github_repository_code_scanning_default_setup" "example" {
  repository  = "my-repo"
  state       = "configured"
  query_suite = "extended"
  languages   = ["javascript-typescript", "python"]
}

Note: The GitHub API also supports threat_model, runner_type, runner_label, and schedule, but go-github v84 does not expose these fields yet. They can be added in a follow-up.

Pull request checklist

  • Schema migrations have been created if needed
  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)

Does this introduce a breaking change?

  • Yes
  • No

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

@github-actions github-actions bot added the Type: Feature New feature or request label Apr 1, 2026
@oda251 oda251 force-pushed the feat/code-scanning-default-setup branch from bc108db to b99d798 Compare April 1, 2026 15:23
Add a new resource to manage code scanning default setup configuration
for GitHub repositories via the REST API.

Closes integrations#2043 (partially)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@oda251 oda251 force-pushed the feat/code-scanning-default-setup branch from b99d798 to f26ff06 Compare April 1, 2026 15:27
Comment on lines +34 to +39
"repository": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The GitHub repository name.",
},
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove ForceNew and add the diffRepo structure used in other similar resources. This allows support for renaming repositories

Comment on lines +120 to +122
if repoName == "" {
repoName = d.Id()
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could this situation arise?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Removed the dead code.

repoName = d.Id()
}

config, _, err := client.CodeScanning.GetDefaultSetupConfiguration(ctx, owner, repoName)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: does this return err or success if the repo has been archived?
If success, then we need to also check if the repo is archived and return an error

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API returns 403 on archived repos, but with a misleading message ("Code scanning is not enabled"). Added an explicit archived check in Create/Update to provide a clear error message, along with a test case. Read does not check (consistent with other resources).

# configured before archiving
$ gh api repos/oda251/actrun-mcp/code-scanning/default-setup
{"state":"configured","languages":[],"query_suite":"default",...}

# after archiving
$ gh api repos/oda251/actrun-mcp/code-scanning/default-setup
403: "Code scanning is not enabled for this repository."

Comment on lines +144 to +146
if !errors.As(err, &acceptedErr) {
return diag.Errorf("error disabling code scanning default setup for %s/%s: %s", owner, repoName, err)
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also not error if the repo is gone.

}
}

log.Printf("[INFO] Code scanning default setup disabled for %s/%s", owner, repoName)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use tflog

Comment on lines +163 to +166
diags := resourceGithubRepositoryCodeScanningDefaultSetupRead(ctx, d, meta)
if diags.HasError() {
return nil, fmt.Errorf("error importing code scanning default setup for %s: %s", repoName, diags[0].Summary)
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't ever call any of the CRUD functions. Read will be automatically called after import, you only need to set any important fields for Read to work

return nil, err
}

return result.(*github.DefaultSetupConfiguration), nil
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this panic if the wait returns nil?

Steps: []resource.TestStep{
{
Config: testAccCodeScanningDefaultSetupConfig(repoName, `state = "configured"`),
Check: resource.ComposeTestCheckFunc(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use ConfigStateChecks

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Also consolidated redundant test cases (9 -> 6) and added repository_id verification in the import test.

repoName := fmt.Sprintf("%srepo-code-scanning-%s", testResourcePrefix, randomID)
config := testAccCodeScanningDefaultSetupConfig(repoName, `state = "not-configured"`)

check := resource.TestCheckResourceAttr(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't use variables for checks

- Replace ForceNew with diffRepository + repository_id for rename support
- Add archived repo check in Create/Update
- Handle 404 in Read (remove from state) and Delete (return nil)
- Use tflog instead of log.Printf
- Simplify Import to only set repository (Read is called automatically)
- Add nil check in waitForCodeScanningState to prevent panic
- Migrate tests to ConfigStateChecks, consolidate redundant cases

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@oda251
Copy link
Copy Markdown
Author

oda251 commented Apr 2, 2026

@deiga
Thank you so much for taking the time to review this PR so thoroughly! Really appreciate the thorough feedback even though I’m a first-time contributor to this repo. All comments have been addressed — please let me know if anything else needs to be changed.

@oda251 oda251 requested a review from deiga April 10, 2026 19:28
@deiga deiga added this to the v6.13.0 Release milestone Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Type: Feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT]: Support Code Scanning default setup

2 participants