Skip to content

Commit

Permalink
Merge pull request #21 from grafana/20-output-change-detection
Browse files Browse the repository at this point in the history
output change detection in GitHub action mode
  • Loading branch information
szkiba committed Aug 12, 2024
2 parents 216410c + b87630a commit c6f6c0c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**

Expand Down
73 changes: 73 additions & 0 deletions cmd/k6registry/action.go
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions cmd/k6registry/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions releases/v0.1.6.md
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit c6f6c0c

Please sign in to comment.