diff --git a/README.md b/README.md index 04549e4..89d7e06 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,9 @@ lint | no | `false` | enable built-in linter compact| no | `false` | compact instead of pretty-printed output raw | no | `false` | output raw strings, not JSON texts yaml | no | `false` | output YAML instead of JSON +ref | no | | reference output URL for change detection + +In GitHub action mode, the change can be indicated by comparing the output to a reference output. The reference output URL can be passed in the `ref` action parameter. The `changed` output variable will be `true` or `false` depending on whether the output has changed or not compared to the reference output. **Example usage** diff --git a/cmd/k6registry/action.go b/cmd/k6registry/action.go new file mode 100644 index 0000000..39a45ff --- /dev/null +++ b/cmd/k6registry/action.go @@ -0,0 +1,73 @@ +package main + +import ( + "bytes" + "context" + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "time" +) + +//nolint:forbidigo +func emitOutput() error { + out := getenv("INPUT_OUT", "") + if len(out) == 0 { + return nil + } + + ref := getenv("INPUT_REF", "") + if len(ref) == 0 { + return nil + } + + ghOutput := getenv("GITHUB_OUTPUT", "") + if len(ghOutput) == 0 { + return nil + } + + file, err := os.Create(filepath.Clean(ghOutput)) + if err != nil { + return err + } + + _, err = fmt.Fprintf(file, "changed=%t\n", isChanged(ref, out)) + if err != nil { + return err + } + + return file.Close() +} + +//nolint:forbidigo +func isChanged(refURL string, localFile string) bool { + client := &http.Client{Timeout: httpTimeout} + + req, err := http.NewRequestWithContext(context.TODO(), http.MethodGet, refURL, nil) + if err != nil { + return true + } + + resp, err := client.Do(req) + if err != nil { + return true + } + + defer resp.Body.Close() //nolint:errcheck + + refData, err := io.ReadAll(resp.Body) + if err != nil { + return true + } + + localData, err := os.ReadFile(filepath.Clean(localFile)) + if err != nil { + return true + } + + return !bytes.Equal(refData, localData) +} + +const httpTimeout = 10 * time.Second diff --git a/cmd/k6registry/main.go b/cmd/k6registry/main.go index 72242da..b0bbe00 100644 --- a/cmd/k6registry/main.go +++ b/cmd/k6registry/main.go @@ -34,6 +34,12 @@ func runCmd(cmd *cobra.Command) { if err := cmd.Execute(); err != nil { log.Fatal(formatError(err)) } + + if isGitHubAction() { + if err := emitOutput(); err != nil { + log.Fatal(formatError(err)) + } + } } //nolint:forbidigo diff --git a/releases/v0.1.6.md b/releases/v0.1.6.md new file mode 100644 index 0000000..193b2fc --- /dev/null +++ b/releases/v0.1.6.md @@ -0,0 +1,5 @@ +k6registry `v0.1.6` is here 🎉! + +This is an internal maintenance release. + +The only change is that in GitHub action mode, the change can be indicated by comparing the output to a reference output. The reference output URL can be passed in the `ref` action parameter. The output variable `changed` will be `true` or `false` depending on whether the output has changed or not compared to the reference output.