Skip to content

Commit 7385bfa

Browse files
committed
devx(github-action): Add build and linter actions
* Add build action * Add lint action * Lint code * Add CODEOWNERS file
1 parent daf2542 commit 7385bfa

File tree

6 files changed

+101
-23
lines changed

6 files changed

+101
-23
lines changed

.github/workflows/CODEOWNERS

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# For more information about the Codesphere teams see: https://github.com/orgs/codesphere-cloud/teams
2+
#
3+
# add users to groups at: https://github.com/orgs/codesphere-cloud/teams/dev/teams
4+
# new groups should have dev as parent (or else one of the subgroups)
5+
#
6+
# IMPORTANT: only add groups as owners (below); never individual users.
7+
8+
# Global owners.
9+
* @codesphere-cloud/Architects

.github/workflows/build.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Build & Test
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
12+
jobs:
13+
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Set up Go
20+
uses: actions/setup-go@v4
21+
with:
22+
go-version-file: 'go.mod'
23+
24+
- name: Build
25+
run: make build
26+
27+
- name: Test
28+
run: make test

.github/workflows/lint.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Lint
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
8+
permissions:
9+
contents: read
10+
pull-requests: read
11+
12+
jobs:
13+
golangci:
14+
name: lint
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-go@v5
19+
with:
20+
go-version-file: 'go.mod'
21+
- name: golangci-lint
22+
uses: golangci/golangci-lint-action@v7
23+
with:
24+
version: v2.0

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ OPENAPI_DIR = ./pkg/api/openapi_client
44
format:
55
go fmt ./...
66

7-
check:
8-
go vet ./...
7+
lint:
8+
golangci-lint run
99

1010
test:
1111
go test ./...

cmd/log.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ func (logCmd *LogCmd) RunE(_ *cobra.Command, args []string) (err error) {
9494
if *logCmd.scope.workspaceId == 0 {
9595
*logCmd.scope.workspaceId, err = strconv.Atoi(os.Getenv("CS_WORKSPACE_ID"))
9696
if err != nil {
97-
return fmt.Errorf("Failed to read env var: %e", err)
97+
return fmt.Errorf("failer to read env var: %e", err)
9898
}
9999
if *logCmd.scope.workspaceId == 0 {
100-
return errors.New("Workspace ID required, but not provided.")
100+
return errors.New("workspace ID required, but not provided")
101101
}
102102
}
103103

@@ -119,9 +119,9 @@ func (logCmd *LogCmd) RunE(_ *cobra.Command, args []string) (err error) {
119119
return printLogsOfServer(&logCmd.scope)
120120
}
121121

122-
logCmd.printAllLogs()
122+
err = logCmd.printAllLogs()
123123
if err != nil {
124-
return fmt.Errorf("Failed to print logs: %e", err)
124+
return fmt.Errorf("failed to print logs: %e", err)
125125
}
126126

127127
return nil
@@ -132,20 +132,23 @@ func (l *LogCmd) printAllLogs() error {
132132

133133
replicas, err := cs.GetPipelineStatus(*l.scope.workspaceId, "run")
134134
if err != nil {
135-
return fmt.Errorf("Failed to get pipeline status: %e", err)
135+
return fmt.Errorf("failed to get pipeline status: %e", err)
136136
}
137137

138138
var wg sync.WaitGroup
139139
for _, replica := range replicas {
140-
for s, _ := range replica.Steps {
140+
for s := range replica.Steps {
141141
wg.Add(1)
142142
go func() {
143143
defer wg.Done()
144144
scope := l.scope
145145
*scope.step = s
146146
*scope.replica = replica.Replica
147147
prefix := fmt.Sprintf("|%-10s|%s", replica.Server, replica.Replica[len(replica.Replica)-11:])
148-
printLogsOfReplica(prefix, &scope)
148+
err = printLogsOfReplica(prefix, &scope)
149+
if err != nil {
150+
fmt.Printf("Error printling logs: %e\n", err)
151+
}
149152
}()
150153
}
151154
}
@@ -183,21 +186,24 @@ func printLogsOfEndpoint(prefix string, endpoint string) error {
183186

184187
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
185188
if err != nil {
186-
return fmt.Errorf("Failed to construct request: %s", err)
189+
return fmt.Errorf("failed to construct request: %s", err)
187190
}
188191

189192
// Set the Accept header to indicate SSE
190193
req.Header.Set("Accept", "text/event-stream")
191-
cs.SetAuthoriziationHeader(req)
194+
err = cs.SetAuthoriziationHeader(req)
195+
if err != nil {
196+
return fmt.Errorf("failed to set header: %e", err)
197+
}
192198

193199
resp, err := http.DefaultClient.Do(req)
194200
if err != nil {
195-
return fmt.Errorf("Failed to request logs: %s", err)
201+
return fmt.Errorf("failed to request logs: %e", err)
196202
}
197-
defer resp.Body.Close()
203+
defer func() { _ = resp.Body.Close() }()
198204

199205
if resp.StatusCode != http.StatusOK {
200-
return fmt.Errorf("Log server responded with non-ok code: %d", resp.StatusCode)
206+
return fmt.Errorf("log server responded with non-ok code: %d", resp.StatusCode)
201207
}
202208

203209
reader := bufio.NewReader(resp.Body)
@@ -210,7 +216,7 @@ func printLogsOfEndpoint(prefix string, endpoint string) error {
210216
if err == io.EOF {
211217
return nil
212218
}
213-
return fmt.Errorf("Failed to parse log: %s", err)
219+
return fmt.Errorf("failed to parse log: %s", err)
214220
}
215221

216222
line = strings.TrimSpace(line)
@@ -247,9 +253,12 @@ func printLogsOfEndpoint(prefix string, endpoint string) error {
247253
err := json.Unmarshal([]byte(sse.data), &log)
248254
if err != nil {
249255
var errRes ErrResponse
250-
json.Unmarshal([]byte(sse.data), &errRes)
256+
err = json.Unmarshal([]byte(sse.data), &errRes)
257+
if err != nil {
258+
return fmt.Errorf("error reading error json: %e", err)
259+
}
251260
return fmt.Errorf(
252-
"Server responded with error: %d %s: %s",
261+
"server responded with error: %d %s: %s",
253262
errRes.Status, errRes.Title, errRes.Detail,
254263
)
255264
}

pkg/cs/cs.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,36 @@ func GetPipelineStatus(ws int, stage string) (res []ReplicaStatus, err error) {
3333

3434
status, err := Get(fmt.Sprintf("workspaces/%d/pipeline/%s", ws, stage))
3535
if err != nil {
36-
err = fmt.Errorf("Failed to get pipeline status: %e", err)
36+
err = fmt.Errorf("failed to get pipeline status: %e", err)
3737
return
3838
}
3939

40-
json.Unmarshal(status, &res)
40+
err = json.Unmarshal(status, &res)
4141
if err != nil {
42-
err = fmt.Errorf("Failed to unmarshal pipeline status: %e", err)
42+
err = fmt.Errorf("failed to unmarshal pipeline status: %e", err)
4343
return
4444
}
4545
return
4646
}
4747

4848
func Get(path string) (body []byte, err error) {
4949
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", GetApiUrl(), strings.TrimPrefix(path, "/")), http.NoBody)
50-
SetAuthoriziationHeader(req)
50+
if err != nil {
51+
err = fmt.Errorf("failed to create request: %e", err)
52+
return
53+
}
54+
err = SetAuthoriziationHeader(req)
55+
if err != nil {
56+
err = fmt.Errorf("failed to set header: %e", err)
57+
return
58+
}
5159

5260
res, err := http.DefaultClient.Do(req)
5361
if err != nil {
5462
err = fmt.Errorf("GET failed: %e", err)
5563
return
5664
}
57-
defer res.Body.Close()
65+
defer func() { _ = res.Body.Close() }()
5866
body, err = io.ReadAll(res.Body)
5967
return
6068
}
@@ -63,7 +71,7 @@ func SetAuthoriziationHeader(req *http.Request) error {
6371

6472
apiToken := os.Getenv("CS_TOKEN")
6573
if apiToken == "" {
66-
return errors.New("CS_TOKEN env var required, but not set.")
74+
return errors.New("CS_TOKEN env var required, but not set")
6775
}
6876
req.Header.Set("Authorization", "Bearer "+apiToken)
6977
return nil

0 commit comments

Comments
 (0)