Repository navigation
feat(targeting): manage rules and guard activation #181
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
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9e298b9
feat(targeting): manage rules and guard activation
joshdholtz d59f790
fix(targeting): show rule impact before activation
joshdholtz f3a8a56
fix(targeting): show resulting audience on updates
joshdholtz ea3fb0b
fix(targeting): show rule details in human output
joshdholtz cd2b531
feat(targeting): expose rule config fields in schema
joshdholtz 56c7ac8
test(targeting): exercise approved rule reorder
joshdholtz ca76e4a
fix(targeting): make activation previews readable
joshdholtz d0c02d6
fix(targeting): validate audience transitions and document nested fields
joshdholtz a36968a
docs(targeting): explain audience transitions in command schema
joshdholtz cd533b7
fix(targeting): require force to delete live rules
joshdholtz 86c6fdc
docs(targeting): explain scheduled offering rules
joshdholtz 18c26be
test(targeting): snapshot scheduled rule preview
joshdholtz 16ee1e3
chore(targeting): integrate experiment review fixes
joshdholtz d02f717
fix(targeting): expose ordering contract and verify scheduled updates
joshdholtz ff46780
fix(targeting): support checkpoint rule updates
joshdholtz 0c94120
fix(targeting): preserve preview state and document null values
joshdholtz 1c7ba3e
chore(targeting): integrate lifecycle review followups
joshdholtz 7846ec1
chore(targeting): integrate condition schema fix
joshdholtz 85548b0
fix(targeting): include every page in rule pickers
joshdholtz 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,91 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/url" | ||
| "strconv" | ||
| ) | ||
|
|
||
| type TargetingRulesService struct{ c *Client } | ||
|
|
||
| type TargetingRule struct { | ||
| Object string `json:"object"` | ||
| ID string `json:"id"` | ||
| RuleType string `json:"rule_type"` | ||
| State string `json:"state"` | ||
| DisplayName string `json:"display_name"` | ||
| OfferingID string `json:"offering_id,omitempty"` | ||
| AudienceID *string `json:"audience_id,omitempty"` | ||
| Conditions []any `json:"conditions,omitempty"` | ||
| Schedule any `json:"schedule,omitempty"` | ||
| Placements any `json:"placements,omitempty"` | ||
| FlowID string `json:"flow_id,omitempty"` | ||
| Checkpoints []any `json:"checkpoints,omitempty"` | ||
| } | ||
|
|
||
| type ListTargetingRulesOptions struct { | ||
| State string | ||
| Limit int | ||
| StartingAfter string | ||
| } | ||
|
|
||
| func (s *TargetingRulesService) List(ctx context.Context, projectID string, opts ListTargetingRulesOptions) (*Page[TargetingRule], error) { | ||
| q := url.Values{} | ||
| if opts.State != "" { | ||
| q.Set("state", opts.State) | ||
| } | ||
| if opts.Limit > 0 { | ||
| q.Set("limit", strconv.Itoa(opts.Limit)) | ||
| } | ||
| if opts.StartingAfter != "" { | ||
| q.Set("starting_after", opts.StartingAfter) | ||
| } | ||
| path := pathTargetingRules(projectID) | ||
| if len(q) > 0 { | ||
| path += "?" + q.Encode() | ||
| } | ||
| var out Page[TargetingRule] | ||
| err := s.c.do(ctx, http.MethodGet, path, nil, &out) | ||
| return &out, err | ||
| } | ||
|
|
||
| func (s *TargetingRulesService) Get(ctx context.Context, projectID, id string) (*TargetingRule, error) { | ||
| var out TargetingRule | ||
| err := s.c.do(ctx, http.MethodGet, pathTargetingRule(projectID, id), nil, &out) | ||
| return &out, err | ||
| } | ||
|
|
||
| type TargetingRuleCreate struct { | ||
| RuleType string `json:"rule_type,omitempty"` | ||
| Position *int `json:"position,omitempty"` | ||
| ID string `json:"id,omitempty"` | ||
| State string `json:"state,omitempty"` | ||
| DisplayName string `json:"display_name"` | ||
| OfferingID string `json:"offering_id,omitempty"` | ||
| AudienceID string `json:"audience_id,omitempty"` | ||
| Conditions json.RawMessage `json:"conditions,omitempty"` | ||
| Schedule json.RawMessage `json:"schedule,omitempty"` | ||
| Placements json.RawMessage `json:"placements,omitempty"` | ||
| FlowID string `json:"flow_id,omitempty"` | ||
| Checkpoints json.RawMessage `json:"checkpoints,omitempty"` | ||
| } | ||
|
|
||
| type TargetingRuleUpdate map[string]json.RawMessage | ||
|
|
||
| func (s *TargetingRulesService) Create(ctx context.Context, projectID string, body TargetingRuleCreate) (*TargetingRule, error) { | ||
| var out TargetingRule | ||
| err := s.c.do(ctx, http.MethodPost, pathTargetingRules(projectID), body, &out) | ||
| return &out, err | ||
| } | ||
|
|
||
| func (s *TargetingRulesService) Update(ctx context.Context, projectID, id string, body TargetingRuleUpdate) (*TargetingRule, error) { | ||
| var out TargetingRule | ||
| err := s.c.do(ctx, http.MethodPost, pathTargetingRule(projectID, id), body, &out) | ||
| return &out, err | ||
| } | ||
|
|
||
| func (s *TargetingRulesService) Delete(ctx context.Context, projectID, id string) error { | ||
| return s.c.do(ctx, http.MethodDelete, pathTargetingRule(projectID, id), nil, nil) | ||
| } | ||
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,67 @@ | ||
| package api_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/revenuecat/cli/internal/api" | ||
| ) | ||
|
|
||
| func TestTargetingRuleRoutes(t *testing.T) { | ||
| requests := []string{} | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| requests = append(requests, r.Method+" "+r.URL.RequestURI()) | ||
| w.Header().Set("Content-Type", "application/json") | ||
| if r.Method == http.MethodPost && r.URL.Path == "/projects/proj/targeting_rules" { | ||
| var body api.TargetingRuleCreate | ||
| if err := json.NewDecoder(r.Body).Decode(&body); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if body.DisplayName != "US paywall" || body.OfferingID != "ofrng_us" || body.State != "inactive" { | ||
| t.Fatalf("unexpected create body: %+v", body) | ||
| } | ||
| } | ||
| if r.Method == http.MethodDelete { | ||
| fmt.Fprint(w, `{"object":"deleted_object","id":"trle1"}`) | ||
| return | ||
| } | ||
| if r.URL.Path == "/projects/proj/targeting_rules" && r.Method == http.MethodGet { | ||
| fmt.Fprint(w, `{"object":"list","items":[{"object":"targeting_rule","id":"trle1","rule_type":"legacy","state":"inactive","display_name":"US paywall","offering_id":"ofrng_us"}],"next_page":null,"url":"/projects/proj/targeting_rules"}`) | ||
| return | ||
| } | ||
| fmt.Fprint(w, `{"object":"targeting_rule","id":"trle1","rule_type":"legacy","state":"inactive","display_name":"US paywall","offering_id":"ofrng_us"}`) | ||
| })) | ||
| t.Cleanup(srv.Close) | ||
| client := api.NewClient(api.Options{APIKey: "sk_test", BaseURL: srv.URL}) | ||
| ctx := context.Background() | ||
| page, err := client.TargetingRules.List(ctx, "proj", api.ListTargetingRulesOptions{State: "inactive", Limit: 5, StartingAfter: "trle0"}) | ||
| if err != nil || len(page.Items) != 1 || page.Items[0].OfferingID != "ofrng_us" { | ||
| t.Fatalf("page=%+v err=%v", page, err) | ||
| } | ||
| if _, err := client.TargetingRules.Get(ctx, "proj", "trle1"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := client.TargetingRules.Create(ctx, "proj", api.TargetingRuleCreate{DisplayName: "US paywall", OfferingID: "ofrng_us", State: "inactive"}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if _, err := client.TargetingRules.Update(ctx, "proj", "trle1", api.TargetingRuleUpdate{"state": json.RawMessage(`"active"`)}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := client.TargetingRules.Delete(ctx, "proj", "trle1"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| want := []string{ | ||
| "GET /projects/proj/targeting_rules?limit=5&starting_after=trle0&state=inactive", | ||
| "GET /projects/proj/targeting_rules/trle1", | ||
| "POST /projects/proj/targeting_rules", | ||
| "POST /projects/proj/targeting_rules/trle1", | ||
| "DELETE /projects/proj/targeting_rules/trle1", | ||
| } | ||
| if fmt.Sprint(requests) != fmt.Sprint(want) { | ||
| t.Fatalf("requests = %v, want %v", requests, want) | ||
| } | ||
| } |
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.
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.