# Add CodeQL Scanning to a Cobra CLI

Yes. An agent adds a CodeQL workflow for Go to your Cobra CLI repository, pinned to majors Dependabot already tracks, validates it, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-codeql-scanning/cobra/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads what the repository already runs before adding a scanner, so the new workflow fits beside the old ones. In a repository like spf13/cobra-cli, `go.mod` declares `go 1.15` with `github.com/spf13/cobra` v1.6.1 and `github.com/spf13/viper` v1.12.0, `main.go` calls `cmd.Execute()`, and `cmd/` holds `root.go`, `add.go` and `init.go` plus golden-file tests that compare generated `cmd/*.go` output against `cmd/testdata/*.golden`. `.github/workflows/test.yml` has three jobs: `golangci-lint` on Go 1.17 through `golangci/golangci-lint-action@v2`, `test-unix` on Ubuntu and macOS across Go 1.14 to 1.17 running `make test build` with `richgo` and `gox`, and `test-win` under MSYS2. `.github/dependabot.yml` already updates both `gomod` and `github-actions` weekly, and the `Makefile` builds with `gox` for linux and darwin on amd64. There is no CodeQL, no `security-events` permission and no scheduled scan anywhere.

From that the agent writes `.github/workflows/codeql.yml`. It triggers on `push` to `main`, on `pull_request`, and on a weekly `schedule`, with a `permissions` block of `security-events: write`, `contents: read` and `actions: read` and nothing else. One job on `ubuntu-latest` checks out with `actions/checkout@v7`, sets up a toolchain with `actions/setup-go@v7` and `go-version: stable`, then runs `github/codeql-action/init@v4` with `languages: go` and `build-mode: autobuild`, and `github/codeql-action/analyze@v4` with `category: "/language:go"`. `autobuild` is the mode CodeQL's own README recommends for compiled languages with ordinary build steps, and a `go build ./...` is as ordinary as it gets; the `category` keeps these results separate if a second analysis is ever added. The go toolchain step is deliberate: `go 1.15` in `go.mod` is a floor, and the extractor needs a current compiler on the runner, which `stable` provides.

Pinning is by major tag rather than by commit SHA, and the pull request says why: `dependabot.yml` already watches `github-actions` with an open pull request limit of 99, so `v4` and `v7` are updated the same way `actions/checkout@v3.1.0` in `test.yml` is today, and a SHA pin would need the same Dependabot entry to stay fresh. The agent does not touch `test.yml`, the `.golangci.yml` file with its older linter list, or the Makefile; CodeQL is a separate check and should fail or pass on its own.

Validation happens in the sandbox. The agent installs `actionlint` and runs it on the new file to catch a wrong key or an unknown event before the workflow ever runs on GitHub, then runs `go vet ./...` and `go test ./...` so the pull request shows the repository still builds with the toolchain the new job will use. CodeQL itself needs GitHub's runners and the code scanning upload, so the first real result appears on the pull request's own checks once the workflow runs there; the description tells the reviewer to look for the `CodeQL` check and for the Security tab after merge, and to make sure the repository does not also have code scanning's default setup enabled, since GitHub refuses an advanced-setup upload when it is.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add CodeQL scanning to this Cobra CLI repository. Read go.mod, main.go, cmd/root.go, cmd/add.go, cmd/init.go, cmd/golden_test.go, Makefile, .golangci.yml, .github/workflows/test.yml and .github/dependabot.yml first. Write .github/workflows/codeql.yml that runs on push to main, on pull_request and on a weekly schedule, with permissions limited to security-events write, contents read and actions read, one job on ubuntu-latest that uses actions/checkout@v7, actions/setup-go@v7 with go-version stable, github/codeql-action/init@v4 with languages go and build-mode autobuild, and github/codeql-action/analyze@v4 with category /language:go. Pin by major tag and explain in the pull request that dependabot.yml already tracks github-actions. Do not change test.yml, .golangci.yml or the Makefile. Install actionlint and run it on the new workflow, run go vet ./... and go test ./..., and open a pull request that pastes the actionlint and test output and tells the reviewer where the first CodeQL result will appear and to check that default setup for code scanning is not enabled on the repository.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds one file, `.github/workflows/codeql.yml`. The description lists the triggers, the three permissions, the four actions with their major tags, the reason `autobuild` and a `stable` Go toolchain were chosen over the `go 1.15` floor in `go.mod`, and the `actionlint`, `go vet` and `go test` output.

The run's record holds the `actionlint` install and run, the vet and the test run with the golden-file cases. Nothing else in the repository changes, and the existing `Test` workflow keeps its three jobs as they were.

Before merging, a reviewer checks the repository's code security settings on GitHub: if default setup is on, it is turned off before this workflow runs, or the workflow's upload is refused. Once merged, the weekly schedule and every pull request produce results under the Security tab, and Dependabot's existing `github-actions` entry keeps the four action tags current.

## Run it on every new Go repository

An organisation with many Go CLIs starts one run per repository from its own server: one `POST /o/{org}/sessions` each, with that repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started rather than a second pull request. Each run reads that repository's own workflows and `dependabot.yml`, so a repository that pins by SHA gets SHA pins and a repository without Dependabot gets a `github-actions` entry added beside the workflow.

Afterwards, an automation on the Project with a `github` trigger on `workflow_run.failure` and the prompt "if the failed workflow is CodeQL, read its log, say whether autobuild failed or the upload was refused, and propose the fix as a comment" turns a red scan into a diagnosis rather than a notification. The trigger adds the event after the prompt; only work by an owner, member or collaborator fires it, never a fork's.

## Why run it on Rush?

A CodeQL workflow is twenty lines, and the mistakes are in the details nobody reads twice: a missing `security-events` permission, a build mode that cannot compile the module, a toolchain older than the extractor needs, pins that nothing keeps current, and a default-setup conflict that refuses the upload after merge. The agent reads the repository's existing workflows and Dependabot config and writes the file to match them, then lints it before anyone waits on a runner.

In its own sandbox the agent runs `actionlint`, `go vet` and the test suite, and the run keeps that output for the pull request. For an organisation with many Go repositories, each gets its own run and its own pull request, and the pattern is consistent without a template being pasted blindly into each.

## Frequently asked questions

### Why pin by major tag and not by commit SHA?

Because `dependabot.yml` already tracks `github-actions` weekly, so `v4` and `v7` move the same way the existing `actions/checkout@v3.1.0` does. A SHA pin is stricter but goes stale without the same Dependabot entry, and the pull request explains the trade so a team with a SHA policy can switch the four lines.

### Why does the workflow set up Go when autobuild compiles the code?

The CodeQL Go extractor runs the build with whatever toolchain the runner has, and `go 1.15` in `go.mod` is a minimum, not a request for that version. `actions/setup-go@v7` with `go-version: stable` gives a current compiler that builds the module and that the extractor supports.

### Can the agent run CodeQL itself before opening the PR?

Not usefully. The analysis and the upload to code scanning happen on GitHub's side, so the agent validates the workflow with `actionlint` and proves the module builds and tests with the same toolchain. The first scan result appears as the `CodeQL` check on the pull request itself, since the workflow triggers on `pull_request`.

### Will this conflict with the existing lint and test workflow?

No. `test.yml` is untouched and CodeQL is a separate workflow with its own job and permissions. If the repository already has code scanning's default setup turned on, GitHub refuses uploads from an advanced-setup workflow, which is why the description asks the reviewer to check that setting first.

## More Cobra jobs

- [Add Test Coverage Reporting to a Cobra CLI's CI](https://getrush.ai/run/add-coverage-to-ci/cobra/)
- [Add Dependabot to a Cobra CLI](https://getrush.ai/run/add-dependabot/cobra/)
- [Add a --dry-run Flag to a Cobra CLI](https://getrush.ai/run/add-dry-run-flag/cobra/)

## The same job on other stacks

- [Add CodeQL Scanning to a Repository with GitHub Actions](https://getrush.ai/run/add-codeql-scanning/github-actions/)
- [Add CodeQL Scanning to a Go CLI](https://getrush.ai/run/add-codeql-scanning/go-cli/)
- [Add CodeQL Scanning to a Gradle Project](https://getrush.ai/run/add-codeql-scanning/gradle/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
