Loading
feat(api): support custom placeholder values
Description
Adds -V, --placeholder <name>=<value> to glab api, so you can define your own :name placeholders alongside the built-in ones (:fullpath, :branch, and so on).
Why: values like branch names often contain /, which has to be percent-encoded in an API path. Until now you had to encode them yourself before interpolating. Custom placeholders reuse the same encoding the built-in placeholders got in d914d050:
- In the endpoint, the value is percent-encoded (
-V target=feat/xturnsbranches/:targetintobranches/feat%2Fx). - In
--fieldvalues, the value is sent unchanged, matching the built-ins.--raw-fieldvalues are left alone, as they are for built-ins.
How it works:
validateparses each flag into a name/value map and returns aFlagErrorfor a missing=, a malformed name (must start with a letter, then letters, digits,_or-, and not end with-), a repeated name, or a name that clashes with a built-in placeholder (point 2 of the issue).- When custom placeholders are defined, one combined regexp is built per invocation from the built-in and custom names, sorted longest first, so
:repo-nameis not swallowed by:repo. Everything expands in a single pass, so a custom value containing:repois not expanded again. Without--placeholder, the existing package-level regexp is used unchanged. LongandExampleare updated, anddocs/source/api/_index.mdis regenerated withmake gen-docs.
Example:
glab api projects/:fullpath/repository/branches/:target --placeholder target=feature/loginRelated Issues
Closes #8303 (closed)
How has this been tested?
- New table tests drive the command end to end (
runAPIArgvRecordingandrunAPIArgvGuarded):Test_apiRun_customPlaceholders: encoding in the endpoint, raw value in--field, untouched--raw-field, several placeholders, a name extending a built-in one, no re-expansion of values, undefined names sent verbatim, and an empty value.Test_apiRun_customPlaceholderErrors: missing=, empty, digit-first, trailing-hyphen and slash names, clash with a built-in, and a duplicate name. All of them fail before any request is sent.
go test ./internal/commands/api/... ./internal/commands/... ./cmd/glab/...(api package plus its reverse deps, includingskills/bundled)golangci-lint run ./internal/commands/api/...(0 issues)go run ./scripts/comment-overlap(no findings in the touched files)make gen-docs(the tree is clean afterwards)