Skip to content

Commit

Permalink
Merge pull request #330 from replicatedhq/cmx-actor
Browse files Browse the repository at this point in the history
Add GitHub/CI info when in CI
  • Loading branch information
marccampbell authored Oct 4, 2023
2 parents 6f5951e + d6df370 commit f29a293
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/platformclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"fmt"
"io"
"net/http"
"os"

"github.com/pkg/errors"
"github.com/replicatedhq/replicated/pkg/version"
)

const apiOrigin = "https://api.replicated.com/vendor"
Expand Down Expand Up @@ -114,6 +116,12 @@ func (c *HTTPClient) DoJSON(method string, path string, successStatus int, reqBo
req.Header.Set("Authorization", c.apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", fmt.Sprintf("Replicated/%s", version.Version()))

if err := addGitHubActionsHeaders(req); err != nil {
return errors.Wrap(err, "add github actions headers")
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
Expand Down Expand Up @@ -149,6 +157,30 @@ func (c *HTTPClient) DoJSON(method string, path string, successStatus int, reqBo
return nil
}

func addGitHubActionsHeaders(req *http.Request) error {
// anyone can set this to false to disable this behavior
if os.Getenv("CI") != "true" {
return nil
}

// the following params are used to link CMX runs back to the workflow
req.Header.Set("X-Replicated-CI", "true")
if os.Getenv("GITHUB_RUN_ID") != "" {
req.Header.Set("X-Replicated-GitHubRunID", os.Getenv("GITHUB_RUN_ID"))
}
if os.Getenv("GITHUB_RUN_NUMBER") != "" {
req.Header.Set("X-Replicated-GitHubRunNumber", os.Getenv("GITHUB_RUN_NUMBER"))
}
if os.Getenv("GITHUB_SERVER_URL") != "" {
req.Header.Set("X-Replicated-GitHubServerURL", os.Getenv("GITHUB_SERVER_URL"))
}
if os.Getenv("GITHUB_REPOSITORY") != "" {
req.Header.Set("X-Replicated-GitHubRepository", os.Getenv("GITHUB_REPOSITORY"))
}

return nil
}

// Minimal, simplified version of DoJSON for GET requests, just returns bytes
func (c *HTTPClient) HTTPGet(path string, successStatus int) ([]byte, error) {

Expand Down

0 comments on commit f29a293

Please sign in to comment.