Skip to content

Commit

Permalink
Merge pull request #49 from grafana/48-fix-filedirectory-permissions-…
Browse files Browse the repository at this point in the history
…in-github-action-mode

fix: cache permission fixed in GH action mode
  • Loading branch information
szkiba authored Aug 30, 2024
2 parents 66af04c + ef998b6 commit 1797d69
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
27 changes: 27 additions & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"encoding/json"
"io/fs"
"os"
"path/filepath"

"github.com/adrg/xdg"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -45,6 +47,9 @@ func New() (*cobra.Command, error) {

return run(cmd.Context(), args, opts)
},
PostRunE: func(_ *cobra.Command, _ []string) error {
return ghActionFixPerm(xdg.CacheHome)
},
}

ctx, err := newContext(context.TODO(), root.Root().Name())
Expand Down Expand Up @@ -150,6 +155,28 @@ func run(ctx context.Context, args []string, opts *options) (result error) {
return nil
}

func ghActionFixPerm(dir string) error {
if os.Getenv("GITHUB_ACTIONS") != "true" { //nolint:forbidigo
return nil
}

return filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

var mode fs.FileMode

if info.IsDir() {
mode = permDir
} else {
mode = permFile
}

return os.Chmod(path, mode) //nolint:forbidigo
})
}

const (
permFile fs.FileMode = 0o644
permDir fs.FileMode = 0o755
Expand Down
26 changes: 2 additions & 24 deletions cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,6 @@ func saveCompliance(ctx context.Context, module string, comp *k6lint.Compliance)
return os.WriteFile(filename, data, permFile)
}

func fixWorkdirPerm(dir string) error {
return filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

var mode fs.FileMode

if info.IsDir() {
mode = permDir
} else {
mode = permFile
}

return os.Chmod(path, mode) //nolint:forbidigo
})
}

//nolint:forbidigo
func updateWorkdir(ctx context.Context, dir string, cloneURL string) error {
_, err := os.Stat(dir)
Expand All @@ -92,11 +74,7 @@ func updateWorkdir(ctx context.Context, dir string, cloneURL string) error {

if notfound {
_, err = git.PlainCloneContext(ctx, dir, false, &git.CloneOptions{URL: cloneURL})
if err != nil {
return err
}

return fixWorkdirPerm(dir)
return err
}

repo, err := git.PlainOpen(dir)
Expand All @@ -114,7 +92,7 @@ func updateWorkdir(ctx context.Context, dir string, cloneURL string) error {
return err
}

return fixWorkdirPerm(dir)
return nil
}

func checkCompliance(ctx context.Context, module string, cloneURL string, tstamp float64) (*k6lint.Compliance, error) {
Expand Down
9 changes: 9 additions & 0 deletions releases/v0.1.17.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
k6registry `v0.1.17` is here 🎉!

This is an internal maintenance release.

**Fix file/directory permissions**

In GitHub Action mode, the permissions of the cache (XDG_CACHE_HOME) files have been corrected.

In the case of files, now the permission set to `0o644`, in the case of a direcotry to `0o755`.

0 comments on commit 1797d69

Please sign in to comment.