Skip to content

Commit

Permalink
Merge pull request #911 from CircleCI-Public/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
JulesFaucherre authored Apr 19, 2023
2 parents 63309c6 + 5b836ba commit cbe4c31
Show file tree
Hide file tree
Showing 25 changed files with 181 additions and 80 deletions.
4 changes: 3 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,9 @@ func FollowProject(config settings.Config, vcs string, owner string, projectName
}
r.Header.Set("Content-Type", "application/json; charset=utf-8")
r.Header.Set("Accept", "application/json; charset=utf-8")
r.Header.Set("Circle-Token", config.Token)
if config.Token != "" {
r.Header.Set("Circle-Token", config.Token)
}

response, err := config.HTTPClient.Do(r)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion api/context_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,9 @@ func (c *ContextRestClient) newHTTPRequest(method, url string, body io.Reader) (
if err != nil {
return nil, err
}
req.Header.Add("circle-token", c.token)
if c.token != "" {
req.Header.Add("circle-token", c.token)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", version.UserAgent())
Expand Down
4 changes: 3 additions & 1 deletion api/info/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ func (c *InfoRESTClient) newHTTPRequest(method, url string, body io.Reader) (*ht
if err != nil {
return nil, err
}
req.Header.Add("circle-token", c.token)
if c.token != "" {
req.Header.Add("circle-token", c.token)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", version.UserAgent())
Expand Down
4 changes: 3 additions & 1 deletion api/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ func NewClient(baseURL string, config *settings.Config) *Client {
// releasing the semaphore after a second ensuring client doesn't make more than cap(sem)/second
time.AfterFunc(time.Second, func() { <-sem })

r.Header.Add("circle-token", config.Token)
if config.Token != "" {
r.Header.Add("circle-token", config.Token)
}
r.Header.Add("Accept", "application/json")
r.Header.Add("Content-Type", "application/json")
r.Header.Add("User-Agent", version.UserAgent())
Expand Down
4 changes: 3 additions & 1 deletion api/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ func (c *Client) NewRequest(method string, u *url.URL, payload interface{}) (req
}

func (c *Client) enrichRequestHeaders(req *http.Request, payload interface{}) {
req.Header.Set("Circle-Token", c.circleToken)
if c.circleToken != "" {
req.Header.Set("Circle-Token", c.circleToken)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", version.UserAgent())
commandStr := header.GetCommandStr()
Expand Down
4 changes: 3 additions & 1 deletion api/schedule_rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,9 @@ func (c *ScheduleRestClient) newHTTPRequest(method, url string, body io.Reader)
if err != nil {
return nil, err
}
req.Header.Add("circle-token", c.token)
if c.token != "" {
req.Header.Add("circle-token", c.token)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", version.UserAgent())
Expand Down
47 changes: 47 additions & 0 deletions cmd/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd

import (
"fmt"
"io"

"github.com/a8m/envsubst"
"github.com/spf13/cobra"
)

func newEnvCmd() *cobra.Command {
var envCmd = &cobra.Command{
Use: "env",
Short: "Manage environment variables",
}
var substCmd = &cobra.Command{
Use: "subst",
Short: "Substitute environment variables in a string",
RunE: substRunE,
}
envCmd.AddCommand(substCmd)
return envCmd
}

// Accepts a string as an argument, or reads from stdin if no argument is provided.
func substRunE(cmd *cobra.Command, args []string) error {
var input string
if len(args) > 0 {
input = args[0]
} else {
// Read from stdin
b, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
}
input = string(b)
}
if input == "" {
return nil
}
output, err := envsubst.String(input)
if err != nil {
return err
}
_, err = fmt.Fprint(cmd.OutOrStdout(), output)
return err
}
97 changes: 97 additions & 0 deletions cmd/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package cmd

import (
"bytes"
"os"
"testing"

"gotest.tools/v3/assert"
)

func TestSubstRunE(t *testing.T) {
// Set environment variables for testing
err := os.Setenv("ENV_NAME", "world")
if err != nil {
t.Fatal(err)
}

testCases := []struct {
name string
input string
output string
}{
{
name: "substitute variables",
input: "Hello $ENV_NAME!",
output: "Hello world!",
},
{
name: "no variables to substitute",
input: "Hello, world!",
output: "Hello, world!",
},
{
name: "empty input",
input: "",
output: "",
},
{
name: "no variables JSON",
input: `{"foo": "bar"}`,
output: `{"foo": "bar"}`,
},
{
name: "substitute variables JSON",
input: `{"foo": "$ENV_NAME"}`,
output: `{"foo": "world"}`,
},
{
name: "no variables key=value",
input: `foo=bar`,
output: `foo=bar`,
},
}

// Run tests for each test case as argument
for _, tc := range testCases {
t.Run("arg: "+tc.name, func(t *testing.T) {
// Set up test command
cmd := newEnvCmd()

// Capture output
outputBuf := bytes.Buffer{}
cmd.SetOut(&outputBuf)

// Run command
cmd.SetArgs([]string{"subst", tc.input})
err := cmd.Execute()

// Check output and error
assert.NilError(t, err)
assert.Equal(t, tc.output, outputBuf.String())
})
}
// Run tests for each test case as stdin
for _, tc := range testCases {
t.Run("stdin: "+tc.name, func(t *testing.T) {
// Set up test command
cmd := newEnvCmd()

// Set up input
inputBuf := bytes.NewBufferString(tc.input)
cmd.SetIn(inputBuf)

// Capture output
outputBuf := bytes.Buffer{}
cmd.SetOut(&outputBuf)

// Run command
cmd.SetArgs([]string{"subst"})
err = cmd.Execute()

// Check output and error
assert.NilError(t, err)
assert.Equal(t, tc.output, outputBuf.String())
})
}
}
14 changes: 7 additions & 7 deletions cmd/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ This group of commands allows the management of polices to be verified against b
return nil
},
Args: cobra.ExactArgs(1),
Example: `policy push ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f`,
Example: `circleci policy push ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f`,
}

cmd.Flags().StringVar(&context, "context", "config", "policy context")
Expand Down Expand Up @@ -127,7 +127,7 @@ This group of commands allows the management of polices to be verified against b
return prettyJSONEncoder(cmd.OutOrStdout()).Encode(diff)
},
Args: cobra.ExactArgs(1),
Example: `policy diff ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f`,
Example: `circleci policy diff ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f`,
}
cmd.Flags().StringVar(&context, "context", "config", "policy context")
cmd.Flags().StringVar(&ownerID, "owner-id", "", "the id of the policy's owner")
Expand Down Expand Up @@ -159,7 +159,7 @@ This group of commands allows the management of polices to be verified against b
return nil
},
Args: cobra.MaximumNArgs(1),
Example: `policy fetch --owner-id 516425b2-e369-421b-838d-920e1f51b0f5`,
Example: `circleci policy fetch --owner-id 516425b2-e369-421b-838d-920e1f51b0f5`,
}

cmd.Flags().StringVar(&context, "context", "config", "policy context")
Expand Down Expand Up @@ -238,7 +238,7 @@ This group of commands allows the management of polices to be verified against b
return nil
},
Args: cobra.MaximumNArgs(1),
Example: `policy logs --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f --after 2022/03/14 --out output.json`,
Example: `circleci policy logs --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f --after 2022/03/14 --out output.json`,
}

cmd.Flags().StringVar(&request.Status, "status", "", "filter decision logs based on their status")
Expand Down Expand Up @@ -313,7 +313,7 @@ This group of commands allows the management of polices to be verified against b
return nil
},
Args: cobra.MaximumNArgs(1),
Example: `policy decide ./policies --input ./.circleci/config.yml`,
Example: `circleci policy decide ./policies --input ./.circleci/config.yml`,
}

cmd.Flags().StringVar(&ownerID, "owner-id", "", "the id of the policy's owner")
Expand Down Expand Up @@ -359,7 +359,7 @@ This group of commands allows the management of polices to be verified against b
return nil
},
Args: cobra.ExactArgs(1),
Example: `policy eval ./policies --input ./.circleci/config.yml`,
Example: `circleci policy eval ./policies --input ./.circleci/config.yml`,
}

cmd.Flags().StringVar(&inputPath, "input", "", "path to input file")
Expand Down Expand Up @@ -406,7 +406,7 @@ This group of commands allows the management of polices to be verified against b
return nil
},
Args: cobra.ExactArgs(0),
Example: `policy settings --enabled=true`,
Example: `circleci policy settings --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f --enabled=true`,
}

cmd.Flags().StringVar(&ownerID, "owner-id", "", "the id of the policy's owner")
Expand Down
13 changes: 0 additions & 13 deletions cmd/policy/testdata/policy/create-expected-usage.txt

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/policy/testdata/policy/decide-expected-usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage:
policy decide [policy_file_or_dir_path] [flags]

Examples:
policy decide ./policies --input ./.circleci/config.yml
circleci policy decide ./policies --input ./.circleci/config.yml

Flags:
--context string policy context for decision (default "config")
Expand Down
11 changes: 0 additions & 11 deletions cmd/policy/testdata/policy/delete-expected-usage.txt

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/policy/testdata/policy/diff-expected-usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage:
policy diff <policy_dir_path> [flags]

Examples:
policy diff ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f
circleci policy diff ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f

Flags:
--context string policy context (default "config")
Expand Down
2 changes: 1 addition & 1 deletion cmd/policy/testdata/policy/eval-expected-usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage:
policy eval <policy_file_or_dir_path> [flags]

Examples:
policy eval ./policies --input ./.circleci/config.yml
circleci policy eval ./policies --input ./.circleci/config.yml

Flags:
--input string path to input file
Expand Down
2 changes: 1 addition & 1 deletion cmd/policy/testdata/policy/fetch-expected-usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage:
policy fetch [policy_name] [flags]

Examples:
policy fetch --owner-id 516425b2-e369-421b-838d-920e1f51b0f5
circleci policy fetch --owner-id 516425b2-e369-421b-838d-920e1f51b0f5

Flags:
--context string policy context (default "config")
Expand Down
11 changes: 0 additions & 11 deletions cmd/policy/testdata/policy/get-expected-usage.txt

This file was deleted.

11 changes: 0 additions & 11 deletions cmd/policy/testdata/policy/list-expected-usage.txt

This file was deleted.

2 changes: 1 addition & 1 deletion cmd/policy/testdata/policy/logs-expected-usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage:
policy logs [decision_id] [flags]

Examples:
policy logs --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f --after 2022/03/14 --out output.json
circleci policy logs --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f --after 2022/03/14 --out output.json

Flags:
--after string filter decision logs triggered AFTER this datetime
Expand Down
2 changes: 1 addition & 1 deletion cmd/policy/testdata/policy/push-expected-usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage:
policy push <policy_dir_path> [flags]

Examples:
policy push ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f
circleci policy push ./policies --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f

Flags:
--context string policy context (default "config")
Expand Down
2 changes: 1 addition & 1 deletion cmd/policy/testdata/policy/settings-expected-usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Usage:
policy settings [flags]

Examples:
policy settings --enabled=true
circleci policy settings --owner-id 462d67f8-b232-4da4-a7de-0c86dd667d3f --enabled=true

Flags:
--context string policy context for decision (default "config")
Expand Down
Loading

0 comments on commit cbe4c31

Please sign in to comment.