Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore CI actions to lints and checks #18

Merged
merged 9 commits into from
Mar 15, 2024
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
29 changes: 29 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
labels:
- "dependencies"
- "go"
schedule:
interval: "daily"
groups:
tracing:
patterns:
- "go.opentelemetry.io/*"
golangx:
patterns:
- "golang.org/x/*"

- package-ecosystem: "github-actions"
directory: "/"
labels:
- "dependencies"
- "actions"
schedule:
interval: "daily"
groups:
artifacts:
patterns:
- "action/upload-artifact"
- "action/download-artifact"
67 changes: 67 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: Checks

on:
workflow_dispatch:
pull_request:
push:
branches: [master]

permissions:
contents: read
pull-requests: read

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

jobs:
precommit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
check-latest: true
- uses: pre-commit/action@v3.0.1
with:
extra_args: --all-files --hook-stage=manual

lint:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
# check-latest: true
- uses: golangci/golangci-lint-action@v4
with:
version: v1.54
working-directory: .
skip-pkg-cache: true

test:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest-m, macos-latest-xl, windows-latest-l ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
check-latest: true
- name: Get go version
id: go-version
run: echo "name=version::$(go env GOVERSION)" >> $GITHUB_OUTPUT
- name: go mod download
run: go mod download
- name: go mod verify
run: go mod verify
- name: generate command strings
run: go generate ./... && git diff --exit-code
- name: Run tests
run: go test ./...
7 changes: 3 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,17 @@ linters-settings:
gomodguard:
allowed:
modules:
- github.com/cenkalti/backoff/v4
- github.com/Khan/genqlient
- github.com/google/go-querystring
- github.com/PuerkitoBio/rehttp
- github.com/superfly/graphql
- github.com/superfly/macaroon
- github.com/superfly/macaroon/flyio
- github.com/superfly/macaroon/tp
- go.opentelemetry.io/otel
- go.opentelemetry.io/otel/attribute
- go.opentelemetry.io/otel/codes
- go.opentelemetry.io/otel/trace
domains:
- golang.org
- go.opentelemetry.io

blocked:
modules:
Expand Down
24 changes: 24 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/tekwizely/pre-commit-golang
rev: v1.0.0-rc.1
hooks:
- id: go-mod-tidy

# NOTE: This pre-commit hook is ignored when running on Github Workflow
# because goalngci-lint github action is much more useful than the pre-commit action.
# The trick is to run github action only for "manual" hook stage
- repo: https://github.com/golangci/golangci-lint
rev: v1.54.2
hooks:
- id: golangci-lint
stages: [commit]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ it can be used by any project that wants to automate its [Fly.io] deployment.

## Development

If you are making changes in another project and need to test `fly-go` changes
If you are making changes in another project and need to test `fly-go` changes
locally, you can enable a [Go workspace][]. For example, if you have a directory
structure like this:

Expand Down
2 changes: 1 addition & 1 deletion flaps/actions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package flaps

//go:generate go run golang.org/x/tools/cmd/stringer -type=flapsAction
//go:generate go run golang.org/x/tools/cmd/stringer@latest -type=flapsAction

// flapsAction is used to record actions in traces' attributes.
type flapsAction int
Expand Down
34 changes: 15 additions & 19 deletions flaps/flaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"net/url"
"os"
"regexp"
"slices"
"strings"
"time"

"github.com/azazeal/pause"
"github.com/jpillora/backoff"
"github.com/cenkalti/backoff/v4"
fly "github.com/superfly/fly-go"
"github.com/superfly/fly-go/internal/tracing"
"github.com/superfly/fly-go/tokens"
Expand Down Expand Up @@ -165,31 +165,27 @@ func (f *Client) CreateApp(ctx context.Context, name string, org string) (err er
}

func (f *Client) WaitForApp(ctx context.Context, name string) error {
bo := &backoff.Backoff{
Min: 100 * time.Millisecond,
Max: 500 * time.Millisecond,
Jitter: true,
}

ctx = contextWithAction(ctx, machineGet)

waiting:
for {
bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 100 * time.Millisecond
bo.MaxInterval = 500 * time.Millisecond
bo.MaxElapsedTime = 0 // no stop
bo.RandomizationFactor = 0.5
bo.Multiplier = 2

var op backoff.Operation = func() error {
err := f._sendRequest(ctx, http.MethodGet, "/apps/"+url.PathEscape(name), nil, nil, nil)
if err == nil {
return nil
}

if ferr, ok := err.(*FlapsError); ok {
switch ferr.ResponseStatusCode {
case 404, 401:
pause.For(ctx, bo.Duration())
continue waiting
}
if ferr, ok := err.(*FlapsError); ok && slices.Contains([]int{404, 401}, ferr.ResponseStatusCode) {
return err
}

return err
return backoff.Permanent(err)
}

return backoff.Retry(op, bo)
}

var snakeCasePattern = regexp.MustCompile("[A-Z]")
Expand Down
6 changes: 3 additions & 3 deletions flaps/flaps_machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/google/go-querystring/query"
"github.com/samber/lo"
fly "github.com/superfly/fly-go"
)

Expand Down Expand Up @@ -233,8 +233,8 @@ func (f *Client) ListActive(ctx context.Context) ([]*fly.Machine, error) {
return nil, fmt.Errorf("failed to list active VMs: %w", err)
}

machines = lo.Filter(machines, func(m *fly.Machine, _ int) bool {
return !m.IsReleaseCommandMachine() && !m.IsFlyAppsConsole() && m.IsActive()
machines = slices.DeleteFunc(machines, func(m *fly.Machine) bool {
return m.IsReleaseCommandMachine() || m.IsFlyAppsConsole() || !m.IsActive()
})

return machines, nil
Expand Down
20 changes: 16 additions & 4 deletions flaps/flaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@ package flaps

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSnakeCase(t *testing.T) {
assert.Equal(t, "foo_bar", snakeCase("fooBar"))
assert.Equal(t, "app_create", snakeCase(appCreate.String()))
type testcase struct {
name string
in string
want string
}

cases := []testcase{
{name: "case1", in: "fooBar", want: "foo_bar"},
{name: "case2", in: appCreate.String(), want: "app_create"},
}
for _, tc := range cases {
got := snakeCase(tc.in)
if got != tc.want {
t.Errorf("%s, got '%v', want '%v'", tc.name, got, tc.want)
}
}
}
10 changes: 6 additions & 4 deletions flaps/flaps_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"net/http"
"slices"

"github.com/samber/lo"
fly "github.com/superfly/fly-go"
)

Expand Down Expand Up @@ -35,9 +34,12 @@ func (f *Client) GetVolumes(ctx context.Context) ([]fly.Volume, error) {
if err != nil {
return nil, err
}
return lo.Filter(volumes, func(v fly.Volume, _ int) bool {
return !slices.Contains(destroyedVolumeStates, v.State)
}), nil

volumes = slices.DeleteFunc(volumes, func(v fly.Volume) bool {
return slices.Contains(destroyedVolumeStates, v.State)
})

return volumes, nil
}

func (f *Client) CreateVolume(ctx context.Context, req fly.CreateVolumeRequest) (*fly.Volume, error) {
Expand Down
14 changes: 2 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
module github.com/superfly/fly-go

go 1.20
go 1.21

require (
github.com/Khan/genqlient v0.6.0
github.com/PuerkitoBio/rehttp v1.3.0
github.com/azazeal/pause v1.3.0
github.com/jpillora/backoff v1.0.0
github.com/stretchr/testify v1.8.4
github.com/superfly/graphql v0.2.4
github.com/superfly/macaroon v0.2.10
go.opentelemetry.io/otel v1.23.1
Expand All @@ -16,13 +13,7 @@ require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
require github.com/felixge/httpsnoop v1.0.4 // indirect

require (
github.com/cenkalti/backoff/v4 v4.2.1
Expand All @@ -32,7 +23,6 @@ require (
github.com/google/uuid v1.4.0 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/samber/lo v1.39.0
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/vektah/gqlparser/v2 v2.5.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
Expand Down
Loading
Loading