Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions .github/workflows/go-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,40 @@ on:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.ref_name }}
cancel-in-progress: true

permissions: {}

jobs:
linters:
name: Linters
runs-on: ubuntu-20.04
timeout-minutes: 10

steps:
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y libbtrfs-dev build-essential

- uses: actions/setup-go@v3
with:
go-version: 'stable'
- uses: actions/checkout@v3
- uses: golangci/golangci-lint-action@v3
with:
skip-cache: true
args: --timeout=8m --build-tags testing
- name: Verify go vendor, go modules and gofmt
run: |
sudo apt-get install -y jq
make verify

go-tests:
runs-on: ubuntu-latest

needs: [linters]
steps:
- name: Checkout
uses: actions/checkout@v3
Expand All @@ -24,4 +54,4 @@ jobs:
- run: go version

- name: Run GARM Go Tests
run: make test
run: make go-test
19 changes: 18 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ IMAGE_TAG = garm-build

USER_ID=$(shell ((docker --version | grep -q podman) && echo "0" || id -u))
USER_GROUP=$(shell ((docker --version | grep -q podman) && echo "0" || id -g))
ROOTDIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST))))
GO ?= go


default: build-static

Expand All @@ -15,5 +18,19 @@ build-static:
@echo Binaries are available in $(PWD)/bin

.PHONY: test
test:
test: verify go-test

go-test:
go test -race -mod=vendor -tags testing -v $(TEST_ARGS) -timeout=15m -parallel=4 -count=1 ./...

fmtcheck:
@gofmt -l -s $$(go list ./... | sed 's|garm/||g') | grep ".*\.go"; if [ "$$?" -eq 0 ]; then echo "gofmt check failed; please tun gofmt -w -s"; exit 1;fi

verify-vendor: ## verify if all the go.mod/go.sum files are up-to-date
$(eval TMPDIR := $(shell mktemp -d))
@cp -R ${ROOTDIR} ${TMPDIR}
@(cd ${TMPDIR}/garm && ${GO} mod tidy)
@diff -r -u -q ${ROOTDIR} ${TMPDIR}/garm
@rm -rf ${TMPDIR}

verify: verify-vendor fmtcheck
47 changes: 22 additions & 25 deletions apiserver/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package controllers

import (
"encoding/json"
"io/ioutil"
"io"
"log"
"net/http"

Expand Down Expand Up @@ -79,29 +79,14 @@ func handleError(w http.ResponseWriter, err error) {
apiErr.Details = ""
}

json.NewEncoder(w).Encode(apiErr)
}

func (a *APIController) authenticateHook(body []byte, headers http.Header) error {
// signature := headers.Get("X-Hub-Signature-256")
hookType := headers.Get("X-Github-Hook-Installation-Target-Type")
var workflowJob runnerParams.WorkflowJob
if err := json.Unmarshal(body, &workflowJob); err != nil {
return gErrors.NewBadRequestError("invalid post body: %s", err)
}

switch hookType {
case "repository":
case "organization":
default:
return gErrors.NewBadRequestError("invalid hook type: %s", hookType)
if err := json.NewEncoder(w).Encode(apiErr); err != nil {
log.Printf("failed to encode response: %q", err)
}
return nil
}

func (a *APIController) handleWorkflowJobEvent(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
handleError(w, gErrors.NewBadRequestError("invalid post body: %s", err))
return
Expand Down Expand Up @@ -137,7 +122,9 @@ func (a *APIController) WSHandler(writer http.ResponseWriter, req *http.Request)
ctx := req.Context()
if !auth.IsAdmin(ctx) {
writer.WriteHeader(http.StatusForbidden)
writer.Write([]byte("you need admin level access to view logs"))
if _, err := writer.Write([]byte("you need admin level access to view logs")); err != nil {
log.Printf("failed to encode response: %q", err)
}
return
}

Expand Down Expand Up @@ -177,7 +164,9 @@ func (a *APIController) NotFoundHandler(w http.ResponseWriter, r *http.Request)
}
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(apiErr)
if err := json.NewEncoder(w).Encode(apiErr); err != nil {
log.Printf("failet to write response: %q", err)
}
}

// LoginHandler returns a jwt token
Expand Down Expand Up @@ -206,7 +195,9 @@ func (a *APIController) LoginHandler(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(runnerParams.JWTResponse{Token: tokenString})
if err := json.NewEncoder(w).Encode(runnerParams.JWTResponse{Token: tokenString}); err != nil {
log.Printf("failed to encode response: %q", err)
}
}

func (a *APIController) FirstRunHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -230,7 +221,9 @@ func (a *APIController) FirstRunHandler(w http.ResponseWriter, r *http.Request)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(newUser)
if err := json.NewEncoder(w).Encode(newUser); err != nil {
log.Printf("failed to encode response: %q", err)
}
}

func (a *APIController) ListCredentials(w http.ResponseWriter, r *http.Request) {
Expand All @@ -242,7 +235,9 @@ func (a *APIController) ListCredentials(w http.ResponseWriter, r *http.Request)
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(creds)
if err := json.NewEncoder(w).Encode(creds); err != nil {
log.Printf("failed to encode response: %q", err)
}
}

func (a *APIController) ListProviders(w http.ResponseWriter, r *http.Request) {
Expand All @@ -254,5 +249,7 @@ func (a *APIController) ListProviders(w http.ResponseWriter, r *http.Request) {
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(providers)
if err := json.NewEncoder(w).Encode(providers); err != nil {
log.Printf("failed to encode response: %q", err)
}
}
81 changes: 57 additions & 24 deletions apiserver/controllers/enterprises.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func (a *APIController) CreateEnterpriseHandler(w http.ResponseWriter, r *http.R
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(enterprise)
if err := json.NewEncoder(w).Encode(enterprise); err != nil {
log.Printf("failed to encode response: %q", err)
}
}

func (a *APIController) ListEnterprisesHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -57,7 +59,9 @@ func (a *APIController) ListEnterprisesHandler(w http.ResponseWriter, r *http.Re
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(enterprise)
if err := json.NewEncoder(w).Encode(enterprise); err != nil {
log.Printf("failed to encode response: %q", err)
}
}

func (a *APIController) GetEnterpriseByIDHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -67,10 +71,12 @@ func (a *APIController) GetEnterpriseByIDHandler(w http.ResponseWriter, r *http.
enterpriseID, ok := vars["enterpriseID"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(params.APIErrorResponse{
if err := json.NewEncoder(w).Encode(params.APIErrorResponse{
Error: "Bad Request",
Details: "No enterprise ID specified",
})
}); err != nil {
log.Printf("failed to encode response: %q", err)
}
return
}

Expand All @@ -82,7 +88,9 @@ func (a *APIController) GetEnterpriseByIDHandler(w http.ResponseWriter, r *http.
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(enterprise)
if err := json.NewEncoder(w).Encode(enterprise); err != nil {
log.Printf("failed to encode response: %q", err)
}
}

func (a *APIController) DeleteEnterpriseHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -92,10 +100,12 @@ func (a *APIController) DeleteEnterpriseHandler(w http.ResponseWriter, r *http.R
enterpriseID, ok := vars["enterpriseID"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(params.APIErrorResponse{
if err := json.NewEncoder(w).Encode(params.APIErrorResponse{
Error: "Bad Request",
Details: "No enterprise ID specified",
})
}); err != nil {
log.Printf("failed to encode response: %q", err)
}
return
}

Expand All @@ -117,10 +127,12 @@ func (a *APIController) UpdateEnterpriseHandler(w http.ResponseWriter, r *http.R
enterpriseID, ok := vars["enterpriseID"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(params.APIErrorResponse{
if err := json.NewEncoder(w).Encode(params.APIErrorResponse{
Error: "Bad Request",
Details: "No enterprise ID specified",
})
}); err != nil {
log.Printf("failed to encode response: %q", err)
}
return
}

Expand All @@ -138,7 +150,9 @@ func (a *APIController) UpdateEnterpriseHandler(w http.ResponseWriter, r *http.R
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(enterprise)
if err := json.NewEncoder(w).Encode(enterprise); err != nil {
log.Printf("failed to encode response: %q", err)
}
}

func (a *APIController) CreateEnterprisePoolHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -148,10 +162,12 @@ func (a *APIController) CreateEnterprisePoolHandler(w http.ResponseWriter, r *ht
enterpriseID, ok := vars["enterpriseID"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(params.APIErrorResponse{
if err := json.NewEncoder(w).Encode(params.APIErrorResponse{
Error: "Bad Request",
Details: "No enterprise ID specified",
})
}); err != nil {
log.Printf("failed to encode response: %q", err)
}
return
}

Expand All @@ -170,7 +186,9 @@ func (a *APIController) CreateEnterprisePoolHandler(w http.ResponseWriter, r *ht
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(pool)
if err := json.NewEncoder(w).Encode(pool); err != nil {
log.Printf("failed to encode response: %q", err)
}
}

func (a *APIController) ListEnterprisePoolsHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -179,10 +197,12 @@ func (a *APIController) ListEnterprisePoolsHandler(w http.ResponseWriter, r *htt
enterpriseID, ok := vars["enterpriseID"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(params.APIErrorResponse{
if err := json.NewEncoder(w).Encode(params.APIErrorResponse{
Error: "Bad Request",
Details: "No enterprise ID specified",
})
}); err != nil {
log.Printf("failed to encode response: %q", err)
}
return
}

Expand All @@ -194,7 +214,10 @@ func (a *APIController) ListEnterprisePoolsHandler(w http.ResponseWriter, r *htt
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(pools)
if err := json.NewEncoder(w).Encode(pools); err != nil {
log.Printf("failed to encode response: %q", err)
}

}

func (a *APIController) GetEnterprisePoolHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -204,10 +227,12 @@ func (a *APIController) GetEnterprisePoolHandler(w http.ResponseWriter, r *http.
poolID, poolOk := vars["poolID"]
if !enterpriseOk || !poolOk {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(params.APIErrorResponse{
if err := json.NewEncoder(w).Encode(params.APIErrorResponse{
Error: "Bad Request",
Details: "No enterprise or pool ID specified",
})
}); err != nil {
log.Printf("failed to encode response: %q", err)
}
return
}

Expand All @@ -219,7 +244,9 @@ func (a *APIController) GetEnterprisePoolHandler(w http.ResponseWriter, r *http.
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(pool)
if err := json.NewEncoder(w).Encode(pool); err != nil {
log.Printf("failed to encode response: %q", err)
}
}

func (a *APIController) DeleteEnterprisePoolHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -230,10 +257,12 @@ func (a *APIController) DeleteEnterprisePoolHandler(w http.ResponseWriter, r *ht
poolID, poolOk := vars["poolID"]
if !enterpriseOk || !poolOk {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(params.APIErrorResponse{
if err := json.NewEncoder(w).Encode(params.APIErrorResponse{
Error: "Bad Request",
Details: "No enterprise or pool ID specified",
})
}); err != nil {
log.Printf("failed to encode response: %q", err)
}
return
}

Expand All @@ -256,10 +285,12 @@ func (a *APIController) UpdateEnterprisePoolHandler(w http.ResponseWriter, r *ht
poolID, poolOk := vars["poolID"]
if !enterpriseOk || !poolOk {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(params.APIErrorResponse{
if err := json.NewEncoder(w).Encode(params.APIErrorResponse{
Error: "Bad Request",
Details: "No enterprise or pool ID specified",
})
}); err != nil {
log.Printf("failed to encode response: %q", err)
}
return
}

Expand All @@ -278,5 +309,7 @@ func (a *APIController) UpdateEnterprisePoolHandler(w http.ResponseWriter, r *ht
}

w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(pool)
if err := json.NewEncoder(w).Encode(pool); err != nil {
log.Printf("failed to encode response: %q", err)
}
}
Loading