-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.go
More file actions
150 lines (128 loc) · 3.2 KB
/
util.go
File metadata and controls
150 lines (128 loc) · 3.2 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
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cs
import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/codesphere-cloud/cs-go/api"
)
type Step struct {
State string
}
type ReplicaStatus struct {
State string `json:"state"`
Steps []Step `json:"steps"`
Replica string `json:"replica"`
Server string `json:"server"`
}
func GetApiUrl() string {
url := os.Getenv("CS_API")
if url != "" {
return url
}
return "https://codesphere.com/api"
}
func GetPipelineStatus(ws int, stage string) (res []ReplicaStatus, err error) {
status, err := Get(fmt.Sprintf("workspaces/%d/pipeline/%s", ws, stage))
if err != nil {
err = fmt.Errorf("failed to get pipeline status: %w", err)
return
}
err = json.Unmarshal(status, &res)
if err != nil {
err = fmt.Errorf("failed to unmarshal pipeline status: %w", err)
return
}
return
}
func Get(path string) (body []byte, err error) {
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", GetApiUrl(), strings.TrimPrefix(path, "/")), http.NoBody)
if err != nil {
err = fmt.Errorf("failed to create request: %w", err)
return
}
err = SetAuthoriziationHeader(req)
if err != nil {
err = fmt.Errorf("failed to set header: %w", err)
return
}
res, err := http.DefaultClient.Do(req)
if err != nil {
err = fmt.Errorf("GET failed: %w", err)
return
}
defer func() { _ = res.Body.Close() }()
body, err = io.ReadAll(res.Body)
return
}
func GetApiToken() (string, error) {
apiToken := os.Getenv("CS_TOKEN")
if apiToken == "" {
return "", errors.New("CS_TOKEN env var required, but not set")
}
return apiToken, nil
}
func SetAuthoriziationHeader(req *http.Request) error {
token, err := GetApiToken()
if err != nil {
return fmt.Errorf("failed to get API token: %w", err)
}
req.Header.Set("Authorization", "Bearer "+token)
return nil
}
func GetRoleName(role int) string {
if role == 1 {
return "Member"
}
return "Admin"
}
// Fetches the team for a given team name.
// If the name is ambigous an error is thrown.
//
// Returns [NotFound] if no plan with the given Id could be found
// Returns [Duplicated] if no plan with the given Id could be found
func TeamIdByName(
client *api.Client,
name string,
) (api.Team, error) {
teams, err := client.ListTeams()
if err != nil {
return api.Team{}, err
}
matchingTeams := []api.Team{}
for _, t := range teams {
if t.Name == name {
matchingTeams = append(matchingTeams, t)
}
}
if len(matchingTeams) == 0 {
return api.Team{}, NewNotFound(fmt.Sprintf("No team with name %s found", name))
}
if len(matchingTeams) > 1 {
return api.Team{}, NewDuplicated(fmt.Sprintf("Multiple teams (%v) with the name %s found.", matchingTeams, name))
}
return matchingTeams[0], nil
}
// Fetches the workspace plan for a given name.
//
// Returns [NotFound] if no plan with the given Id could be found
func PlanByName(
client *api.Client,
name string,
) (api.WorkspacePlan, error) {
plans, err := client.ListWorkspacePlans()
if err != nil {
return api.WorkspacePlan{}, err
}
for _, p := range plans {
if p.Title == name {
return p, nil
}
}
return api.WorkspacePlan{}, NewNotFound(fmt.Sprintf("No team with name %s found", name))
}