forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.go
More file actions
251 lines (215 loc) · 5.49 KB
/
Copy pathproject.go
File metadata and controls
251 lines (215 loc) · 5.49 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package gateway
import (
"context"
"fmt"
gql "github.com/machinebox/graphql"
"github.com/pkg/browser"
configs "github.com/railwayapp/cli/configs"
"github.com/railwayapp/cli/entity"
"github.com/railwayapp/cli/errors"
)
// GetProject returns the project associated with the projectId, as well as
// it's environments, plugins, etc
func (g *Gateway) GetProject(ctx context.Context, projectId string) (*entity.Project, error) {
gqlReq := gql.NewRequest(`
query ($projectId: ID!) {
projectById(projectId: $projectId) {
id,
name,
plugins {
id,
name,
},
environments {
id,
name
},
}
}
`)
gqlReq.Var("projectId", projectId)
err := g.authorize(ctx, gqlReq.Header)
if err != nil {
return nil, err
}
var resp struct {
Project *entity.Project `json:"projectById"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, errors.ProjectConfigNotFound
}
return resp.Project, nil
}
func (g *Gateway) CreateProject(ctx context.Context, req *entity.CreateProjectRequest) (*entity.Project, error) {
gqlReq := gql.NewRequest(`
mutation($name: String) {
createProject(name: $name) {
id,
name
environments {
id
name
}
}
}
`)
err := g.authorize(ctx, gqlReq.Header)
if err != nil {
return nil, err
}
gqlReq.Var("name", req.Name)
var resp struct {
Project *entity.Project `json:"createProject"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, errors.ProjectCreateFailed
}
return resp.Project, nil
}
func (g *Gateway) CreateProjectFromTemplate(ctx context.Context, req *entity.CreateProjectFromTemplateRequest) (*entity.CreateProjectFromTemplateResult, error) {
gqlReq := gql.NewRequest(`
mutation($name: String!, $owner: String!, $template: String!, $isPrivate: Boolean, $plugins: [String!], $variables: Json) {
createProjectFromTemplate(name: $name, owner: $owner, template: $template, isPrivate: $isPrivate, plugins: $plugins, variables: $variables) {
projectId
workflowId
}
}
`)
err := g.authorize(ctx, gqlReq.Header)
if err != nil {
return nil, err
}
gqlReq.Var("name", req.Name)
gqlReq.Var("owner", req.Owner)
gqlReq.Var("template", req.Template)
gqlReq.Var("isPrivate", req.IsPrivate)
gqlReq.Var("plugins", req.Plugins)
gqlReq.Var("variables", req.Variables)
var resp struct {
Result *entity.CreateProjectFromTemplateResult `json:"createProjectFromTemplate"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, errors.ProjectCreateFromTemplateFailed
}
return resp.Result, nil
}
func (g *Gateway) UpdateProject(ctx context.Context, req *entity.UpdateProjectRequest) (*entity.Project, error) {
gqlReq := gql.NewRequest(`
mutation($projectId: ID!) {
updateProject(projectId: $projectId) {
id,
name
}
}
`)
err := g.authorize(ctx, gqlReq.Header)
if err != nil {
return nil, err
}
gqlReq.Var("projectId", req.Id)
var resp struct {
Project *entity.Project `json:"createProject"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, err
}
return resp.Project, nil
}
func (g *Gateway) DeleteProject(ctx context.Context, projectId string) error {
gqlReq := gql.NewRequest(`
mutation($projectId: ID!) {
deleteProject(projectId: $projectId)
}
`)
err := g.authorize(ctx, gqlReq.Header)
if err != nil {
return err
}
gqlReq.Var("projectId", projectId)
var resp struct {
Deleted bool `json:"deleteProject"`
}
return g.gqlClient.Run(ctx, gqlReq, &resp)
}
// GetProjects returns all projects associated with the user, as well as
// their environments associated with those projects, error otherwise
// Performs a dual join
func (g *Gateway) GetProjects(ctx context.Context) ([]*entity.Project, error) {
projectFrag := `
id,
name,
plugins {
id,
name,
},
environments {
id,
name
},
`
gqlReq := gql.NewRequest(fmt.Sprintf(`
query {
me {
name
projects {
%s
}
teams {
name
projects {
%s
}
}
}
}
`, projectFrag, projectFrag))
// TODO build this into the GQL client
err := g.authorize(ctx, gqlReq.Header)
if err != nil {
return nil, err
}
var resp struct {
Me struct {
Name *string `json:"name"`
Projects []*entity.Project `json:"projects"`
Teams []*struct {
Name string `json:"name"`
Projects []*entity.Project `json:"projects"`
} `json:"teams"`
} `json:"me"`
}
if err := g.gqlClient.Run(ctx, gqlReq, &resp); err != nil {
return nil, errors.ProblemFetchingProjects
}
projects := resp.Me.Projects
for _, project := range resp.Me.Projects {
name := "Me"
if resp.Me.Name != nil {
name = *resp.Me.Name
}
project.Team = &name
}
for _, team := range resp.Me.Teams {
for _, project := range team.Projects {
project.Team = &team.Name
}
projects = append(projects, team.Projects...)
}
return projects, nil
}
func GetRailwayUrl() string {
url := "https://railway.app"
if configs.IsDevMode() {
url = "http://localhost:3000"
}
return url
}
func (g *Gateway) OpenProjectInBrowser(projectID string, environmentID string) error {
return browser.OpenURL(fmt.Sprintf("%s/project/%s?environmentId=%s", GetRailwayUrl(), projectID, environmentID))
}
func (g *Gateway) OpenProjectDeploymentsInBrowser(projectID string) error {
return browser.OpenURL(g.GetProjectDeploymentsURL(projectID))
}
func (g *Gateway) GetProjectDeploymentsURL(projectID string) string {
return fmt.Sprintf("%s/project/%s/deployments?open=true", GetRailwayUrl(), projectID)
}