-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploys.go
109 lines (98 loc) · 3.59 KB
/
deploys.go
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
98
99
100
101
102
103
104
105
106
107
108
109
package api
import (
"context"
"encoding/json"
"fmt"
"gopkg.in/nullstone-io/go-api-client.v0/response"
"gopkg.in/nullstone-io/go-api-client.v0/types"
"io"
"net/http"
"net/url"
"time"
)
const (
AutomationToolCircleCI = "circleci"
AutomationToolGithubActions = "github-actions"
AutomationToolGitlab = "gitlab"
AutomationToolBitbucket = "bitbucket"
AutomationToolJenkins = "jenkins"
AutomationToolTravis = "travis"
AutomationToolAzurePipelines = "azure-pipeline"
AutomationToolAppveyor = "appveyor"
AutomationToolTeamCity = "team-city"
AutomationToolCodeship = "codeship"
AutomationToolSemaphore = "semaphore"
)
type Deploys struct {
Client *Client
}
// DeployCreatePayload is the payload for creating a deploy
//
// If `FromSource` is specified, we deploy from the latest commit on the configured branch for this environment
// Otherwise, version is required
// commitSha and reference are optional and get populated on the deploy no matter whether we are deploying
// fromSource or by version
type DeployCreatePayload struct {
FromSource bool `json:"fromSource"`
CommitSha string `json:"commitSha"`
Version string `json:"version"`
Reference string `json:"reference"`
AutomationTool string `json:"automationTool"`
}
// DeployCreateResult contains the result of Deploys Create
// The result can be one of types.Deploy or types.IntentWorkflow
type DeployCreateResult struct {
Deploy *types.Deploy
IntentWorkflow *types.IntentWorkflow
}
func (d Deploys) basePath(stackId, appId, envId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/apps/%d/envs/%d/deploys", d.Client.Config.OrgName, stackId, appId, envId)
}
func (d Deploys) path(stackId, appId, envId, deployId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/apps/%d/envs/%d/deploys/%d", d.Client.Config.OrgName, stackId, appId, envId, deployId)
}
func (d Deploys) Create(ctx context.Context, stackId, appId, envId int64, payload DeployCreatePayload) (*DeployCreateResult, error) {
rawPayload, _ := json.Marshal(payload)
res, err := d.Client.Do(ctx, http.MethodPost, d.basePath(stackId, appId, envId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
if err := response.Verify(res); err != nil {
if response.IsNotFoundError(err) {
return nil, nil
}
return nil, err
}
defer res.Body.Close()
result := &DeployCreateResult{}
if raw, err := io.ReadAll(res.Body); err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
} else {
// Try to parse into IntentWorkflow; if it doesn't match, parse into Deploy
if err := json.Unmarshal(raw, &result.IntentWorkflow); err != nil || result.IntentWorkflow.Intent == "" {
result.IntentWorkflow = nil
if err := json.Unmarshal(raw, &result.Deploy); err != nil {
return result, fmt.Errorf("unknown response body: %w", err)
}
}
}
return result, nil
}
func (d Deploys) Get(ctx context.Context, stackId, appId, envId, deployId int64) (*types.Deploy, error) {
res, err := d.Client.Do(ctx, http.MethodGet, d.path(stackId, appId, envId, deployId), nil, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Deploy](res)
}
func (d Deploys) GetLatest(ctx context.Context, stackId, appId, envId int64, since *time.Time) (*types.Deploy, error) {
query := url.Values{}
if since != nil {
query["since"] = []string{since.Format(time.RFC3339)}
}
res, err := d.Client.Do(ctx, http.MethodGet, d.basePath(stackId, appId, envId)+"/latest", query, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Deploy](res)
}