Skip to content

fix(global-rule): drop --id from create, derive id from plugin name#45

Merged
shreemaan-abhishek merged 4 commits into
masterfrom
fix/global-rule-create-drop-id
May 26, 2026
Merged

fix(global-rule): drop --id from create, derive id from plugin name#45
shreemaan-abhishek merged 4 commits into
masterfrom
fix/global-rule-create-drop-id

Conversation

@shreemaan-abhishek
Copy link
Copy Markdown
Contributor

@shreemaan-abhishek shreemaan-abhishek commented May 26, 2026

Summary

a7 global-rule create --id <X> --plugins-json '{"cors":...}' ignored
--id and returned a resource at id = "cors". API7 EE models each
global rule 1:1 with a plugin and uses the plugin key as the rule's
id, so --id was a flag the backend would always silently override.

This PR drops the misleading flag and tightens the file path:

  • Remove --id flag, Options.ID, the required-flag check, and the
    ID: field from the request body.
  • Reject --file payloads that contain an id field with an explicit
    error pointing users at a7 global-rule update for edits.
  • Require one of --file or --plugins-json.
  • Remove the dead PUT branch in the --file path; create always POSTs
    (update already handles edits via PUT).
  • Update docs/user-guide/global-rule.md to reflect id-from-plugin
    behavior and document the new --plugins-json flag.

Fixes #37.

Test plan

  • TestGlobalRule_CreateWithoutID — POST with --plugins-json '{"cors":{}}' returns id "cors".
  • TestGlobalRule_CreateFromFileRejectsID — file containing id fails before any HTTP call.
  • TestGlobalRule_CreateRequiresPayload — neither flag supplied returns a clear error.
  • TestGlobalRule_CreateMissingGatewayGroup — no gateway group returns the existing guard.
  • go vet ./... clean.
  • go test ./... clean (full suite green).

make check failed on pre-existing tooling issues unrelated to this
change (golangci-lint's bundled type checker is out of date for Go
1.25 and reports false positives on yaml in files this PR does not
touch). gofmt is clean on the changed files.

Summary by CodeRabbit

  • New Features

    • Added a --plugins-json option to create global rules from a plugins JSON map as an alternative to file-based creation.
  • Changes

    • Rule ID is now derived from the single plugin name; explicit --id removed and payloads containing id are rejected.
    • Create requires exactly one of file or --plugins-json (file takes precedence); default output for get/update changed.
  • Documentation

    • Docs and examples updated to show payloads without id and the new CLI usage.
  • Tests

    • Unit and end-to-end tests added/updated to cover the new behaviors.

Review Change Stack

A global rule's id is set by the backend to the (single) plugin key,
so any user-supplied id was silently ignored.

- Remove the --id flag and its required-flag check.
- Reject --file payloads that include an "id" field, pointing users
  at "a7 global-rule update" for edits.
- Require one of --file or --plugins-json.
- Remove the dead PUT branch in the --file path; create always POSTs.
- Update the user guide to reflect id-from-plugin behavior.

Tests cover create-without-id, file-with-id rejection, and the
missing-payload / missing-gateway-group guards.

Fixes #37
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f0b143ed-83fa-48c2-a1b3-2cdaee1b93db

📥 Commits

Reviewing files that changed from the base of the PR and between b497f7a and af23e56.

📒 Files selected for processing (2)
  • docs/user-guide/global-rule.md
  • pkg/cmd/global-rule/create/create.go

📝 Walkthrough

Walkthrough

This PR removes the --id flag from global-rule create, requires exactly one of --file or --plugins-json, rejects id in payloads, derives the created rule ID from the single plugin key, and updates tests and docs accordingly.

Changes

Global Rule ID Derivation from Plugin Name

Layer / File(s) Summary
Command contract and options
pkg/cmd/global-rule/create/create.go
Options struct drops ID; --id flag removed; command help updated to state ID is derived from the plugin name.
Create command logic and validation
pkg/cmd/global-rule/create/create.go
actionRun requires one of --file or --plugins-json; file payloads containing id are rejected and always POSTed; --plugins-json is unmarshaled into Plugins and sent as api.GlobalRule.
Unit tests for create command
pkg/cmd/global-rule/create/create_test.go
Added tests exercising creation without ID (ID derived from plugin), rejection of id in file payloads, requirement for a payload, and gateway group validation; tests use HTTP mock registry and mock config.
Docs, skills examples, and e2e updates
docs/user-guide/global-rule.md, skills/*/SKILL.md, test/e2e/global_rule_test.go
Updated docs and SKILL examples to remove explicit id in create payloads and document single-plugin rule semantics; adjusted get/update output defaults and e2e tests to submit plugins-only JSON.

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
E2e Test Quality Review ⚠️ Warning Missing critical test scenarios: valid file create, both-flags precedence, invalid JSON, file I/O errors, API errors. E2E only tests --file path, not --plugins-json. Add unit tests for valid file, both flags, invalid JSON, file/API errors. Add E2E test for --plugins-json to ensure complete coverage of all code paths.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: removing the --id flag and deriving the rule ID from the plugin name, which is the core objective of the PR.
Linked Issues check ✅ Passed All requirements from issue #37 are met: --id flag removed [#37], id field rejected in --file payloads [#37], one of --file or --plugins-json required [#37], dead PUT branch removed [#37], and required tests added [#37].
Out of Scope Changes check ✅ Passed All changes are in scope: documentation updates reflect the implementation changes, test files verify the new behavior, and no unrelated modifications were introduced.
Security Check ✅ Passed No critical security vulnerabilities detected. PR correctly validates gateway group, enforces id derivation, and rejects create payloads with explicit id fields.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/global-rule-create-drop-id

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/user-guide/global-rule.md`:
- Around line 53-55: The docs table incorrectly states that the `--output` flag
defaults to `yaml` for the `create` command; update the `--output` row in
global-rule.md so the default value shows `json` to match the actual `create`
command behavior (change the table cell for `--output` from `yaml` to `json` and
ensure the `create` command docs referencing `--output` are consistent).

In `@pkg/cmd/global-rule/create/create_test.go`:
- Line 1: The file create_test.go (package create) is missing the required
Apache 2.0 license header; prepend the standard Apache 2.0 header comment block
to the top of the file (above the "package create" declaration) so all Go
sources follow the repository's licensing guideline for **/*.go files.

In `@pkg/cmd/global-rule/create/create.go`:
- Around line 61-63: The current validation only checks that one of opts.File or
opts.PluginsJSON is provided but allows both; update the validation in create.go
(the block using opts.File and opts.PluginsJSON) to return an error when both
are set (e.g., "only one of --file or --plugins-json may be provided") and keep
the existing check for neither being set; locate the check referencing opts.File
and opts.PluginsJSON and replace it with a two-step validation: first error if
both are non-empty, then error if both are empty.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d758835e-7990-4ea9-9f2f-0dea7d16966d

📥 Commits

Reviewing files that changed from the base of the PR and between 3fbbb89 and c98f74e.

📒 Files selected for processing (3)
  • docs/user-guide/global-rule.md
  • pkg/cmd/global-rule/create/create.go
  • pkg/cmd/global-rule/create/create_test.go

Comment thread docs/user-guide/global-rule.md Outdated
Comment thread pkg/cmd/global-rule/create/create_test.go
Comment thread pkg/cmd/global-rule/create/create.go
The create handler now rejects payloads containing an "id" field;
update the e2e fixtures and skill examples that were still setting one.
The resulting id is still asserted to match the plugin name in the
verification steps.
- get: default is `table`, not `yaml`.
- create / update: default is `json`, not `yaml`.
- create: document that `--file` takes precedence when both `--file` and
  `--plugins-json` are passed.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR removes the misleading --id flag from a7 global-rule create and aligns CLI/documentation/tests with API7 EE’s model where a global rule’s id is derived from the (single) plugin name.

Changes:

  • Dropped --id/Options.ID from global-rule create; create now always POSTs and rejects file payloads that contain an id.
  • Added --plugins-json support for global-rule create and enforced that one of --file or --plugins-json must be provided.
  • Updated docs and skill/e2e examples to remove id from create payloads; added unit tests for the new behaviors.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/e2e/global_rule_test.go Updates e2e fixtures/examples to create global rules without an explicit id.
skills/a7-recipe-multi-tenant/SKILL.md Removes id from a global-rule create example JSON payload.
skills/a7-persona-operator/SKILL.md Removes id from global-rule create example payloads.
pkg/cmd/global-rule/create/create.go Removes --id, adds --plugins-json, enforces payload requirements, rejects id in file payloads, and always POSTs.
pkg/cmd/global-rule/create/create_test.go Adds unit tests covering create-with-plugins-json, file payload id rejection, payload requirement, and missing gateway group.
docs/user-guide/global-rule.md Documents id-from-plugin behavior, new --plugins-json flag, and updates examples/flag defaults.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/cmd/global-rule/create/create.go
@shreemaan-abhishek shreemaan-abhishek merged commit 0395913 into master May 26, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

global-rule: drop or warn on --id (API7 EE forces id = plugin name)

2 participants