Repository navigation
Expand file tree
/
Copy pathpreview_apps.go
More file actions
97 lines (81 loc) · 3.46 KB
/
Copy pathpreview_apps.go
File metadata and controls
97 lines (81 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"gopkg.in/nullstone-io/go-api-client.v0/response"
"gopkg.in/nullstone-io/go-api-client.v0/types"
)
type PreviewApps struct {
Client *Client
}
func (p PreviewApps) basePath(stackId, envId int64) string {
return fmt.Sprintf("/orgs/%s/stacks/%d/envs/%d/preview_apps", p.Client.Config.OrgName, stackId, envId)
}
func (p PreviewApps) stackPath(stackId int64) string {
return fmt.Sprintf("/orgs/%s/stacks/%d/preview_apps", p.Client.Config.OrgName, stackId)
}
func (p PreviewApps) stackByNamePath(stackName string) string {
return fmt.Sprintf("/orgs/%s/stacks_by_name/%s/preview_apps", p.Client.Config.OrgName, stackName)
}
// List - GET /orgs/{orgName}/stacks/{stackId}/envs/{envId}/preview_apps
func (p PreviewApps) List(ctx context.Context, stackId, envId int64) ([]types.PreviewApp, error) {
res, err := p.Client.Do(ctx, http.MethodGet, p.basePath(stackId, envId), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.PreviewApp](res)
}
type FindPreviewAppsInput struct {
// Repo matches against either the repo full name (e.g. "acme/widgets") or the full repo URL
Repo string
// PullRequest matches against either the pull request number (e.g. GitHub PR #123) or the
// Nullstone pull request id. When nil, preview apps are not filtered by pull request.
PullRequest *int64
}
func (input FindPreviewAppsInput) query() url.Values {
q := url.Values{}
if input.Repo != "" {
q.Set("repo", input.Repo)
}
if input.PullRequest != nil {
q.Set("pull_request", strconv.FormatInt(*input.PullRequest, 10))
}
return q
}
// Find - GET /orgs/{orgName}/stacks/{stackId}/preview_apps
// Searches preview apps across every environment in the stack.
// This is used to locate a preview environment when only the repo and pull request are known.
func (p PreviewApps) Find(ctx context.Context, stackId int64, input FindPreviewAppsInput) ([]types.PreviewApp, error) {
res, err := p.Client.Do(ctx, http.MethodGet, p.stackPath(stackId), input.query(), nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.PreviewApp](res)
}
// FindByStackName - GET /orgs/{orgName}/stacks_by_name/{stackName}/preview_apps
// Same as Find for callers that know the stack by name instead of id (e.g. CI reacting to a PR trigger).
func (p PreviewApps) FindByStackName(ctx context.Context, stackName string, input FindPreviewAppsInput) ([]types.PreviewApp, error) {
res, err := p.Client.Do(ctx, http.MethodGet, p.stackByNamePath(stackName), input.query(), nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.PreviewApp](res)
}
// Replace - PUT /orgs/{orgName}/stacks/{stackId}/envs/{envId}/preview_apps
// This has replace semantics: the env's preview app set becomes exactly previewApps,
// and any app not in the list is removed from the env. In a preview env "enabled"
// means "present in this set", so adding or removing an app is a membership change,
// not a field write. Callers wanting to change one app must List first and send the
// full mutated list back.
func (p PreviewApps) Replace(ctx context.Context, stackId, envId int64, previewApps []types.PreviewApp) ([]types.PreviewApp, error) {
rawPayload, _ := json.Marshal(previewApps)
res, err := p.Client.Do(ctx, http.MethodPut, p.basePath(stackId, envId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.PreviewApp](res)
}