Skip to content

Commit 28d1dd4

Browse files
code formatted
1 parent 6de8d8f commit 28d1dd4

6 files changed

Lines changed: 53 additions & 30 deletions

File tree

internal/check/helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"strings"
55
"testing"
66

7+
"github.com/step-security/codeowners-validator/internal/check"
78
"github.com/stretchr/testify/assert"
89
"github.com/stretchr/testify/require"
9-
"github.com/step-security/codeowners-validator/internal/check"
1010

1111
"github.com/step-security/codeowners-validator/pkg/codeowners"
1212
)

internal/ctxutil/check_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"context"
55
"testing"
66

7-
"github.com/stretchr/testify/assert"
87
contextutil "github.com/step-security/codeowners-validator/internal/ctxutil"
8+
"github.com/stretchr/testify/assert"
99
)
1010

1111
func TestShouldExit(t *testing.T) {

main.go

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,47 +50,68 @@ func exitOnError(err error) {
5050
}
5151

5252
func validateSubscription() {
53-
upstream := "mszostok/codeowners-validator"
54-
repoPrivate := os.Getenv("REPO_PRIVATE")
55-
56-
// Print StepSecurity banner
57-
fmt.Println("\033[32m" + `
58-
_____ _ _____ _ _
59-
/ ____| | / ____| (_) |
60-
| (___ | |_ ___ _ _| (___ ___ ___ _ _ _ __ _| |_ _ _
61-
\___ \| __/ _ \ '_ \\___ \ / _ \/ __| | | | '__| | __| | | |
62-
____) | || __/ |_) |___) | __/ (__| |_| | | | | |_| |_| |
63-
|_____/ \__\___| .__/_____/ \___|\___|\__,_|_| |_|\__|\__, |
64-
| | __/ |
65-
|_| |___/
66-
` + "\033[0m")
67-
fmt.Println("Maintained by StepSecurity. Learn more at https://stepsecurity.io")
53+
eventPath := os.Getenv("GITHUB_EVENT_PATH")
54+
var repoPrivate *bool
55+
56+
if eventPath != "" {
57+
if eventData, err := os.ReadFile(eventPath); err == nil {
58+
var event struct {
59+
Repository struct {
60+
Private *bool `json:"private"`
61+
} `json:"repository"`
62+
}
63+
if err := json.Unmarshal(eventData, &event); err == nil {
64+
repoPrivate = event.Repository.Private
65+
}
66+
}
67+
}
68+
69+
upstream := "chuhlomin/render-template"
70+
action := os.Getenv("GITHUB_ACTION_REPOSITORY")
71+
docsURL := "https://docs.stepsecurity.io/actions/stepsecurity-maintained-actions"
72+
73+
fmt.Println()
74+
fmt.Println("\x1b[1;36mStepSecurity Maintained Action\x1b[0m")
75+
fmt.Printf("Secure drop-in replacement for %s\n", upstream)
76+
if repoPrivate != nil && !*repoPrivate {
77+
fmt.Println("\x1b[32m\u2713 Free for public repositories\x1b[0m")
78+
}
79+
fmt.Printf("\x1b[36mLearn more:\x1b[0m %s\n", docsURL)
6880
fmt.Println()
6981

70-
if repoPrivate != "true" {
82+
if repoPrivate != nil && !*repoPrivate {
7183
return
7284
}
7385

74-
githubRepo := os.Getenv("GITHUB_REPOSITORY")
75-
url := fmt.Sprintf("https://agent.api.stepsecurity.io/v1/github/%s/actions/maintained-actions-subscription", githubRepo)
86+
serverURL := os.Getenv("GITHUB_SERVER_URL")
87+
if serverURL == "" {
88+
serverURL = "https://github.com"
89+
}
90+
91+
body := map[string]string{"action": action}
92+
if serverURL != "https://github.com" {
93+
body["ghes_server"] = serverURL
94+
}
7695

77-
payload := map[string]string{"upstream": upstream}
78-
body, err := json.Marshal(payload)
96+
jsonBody, err := json.Marshal(body)
7997
if err != nil {
80-
fmt.Println("Info: Unable to validate subscription. Continuing...")
98+
fmt.Println("Timeout or API not reachable. Continuing to next step.")
8199
return
82100
}
83101

102+
apiURL := fmt.Sprintf("https://agent.api.stepsecurity.io/v1/github/%s/actions/maintained-actions-subscription", os.Getenv("GITHUB_REPOSITORY"))
103+
84104
client := &http.Client{Timeout: 3 * time.Second}
85-
resp, err := client.Post(url, "application/json", bytes.NewBuffer(body))
105+
resp, err := client.Post(apiURL, "application/json", bytes.NewBuffer(jsonBody))
86106
if err != nil {
87-
fmt.Println("Info: Unable to validate subscription. Continuing...")
107+
fmt.Println("Timeout or API not reachable. Continuing to next step.")
88108
return
89109
}
90110
defer resp.Body.Close()
91111

92112
if resp.StatusCode == http.StatusForbidden {
93-
fmt.Fprintln(os.Stderr, "::error::This action requires a StepSecurity maintained actions subscription for private repositories. Visit https://stepsecurity.io for more details.")
113+
fmt.Printf("::error::\x1b[1;31mThis action requires a StepSecurity subscription for private repositories.\x1b[0m\n")
114+
fmt.Printf("::error::\x1b[31mLearn how to enable a subscription: %s\x1b[0m\n", docsURL)
94115
os.Exit(1)
95116
}
96117
}

pkg/codeowners/owners_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"testing"
77

88
"github.com/spf13/afero"
9+
"github.com/step-security/codeowners-validator/pkg/codeowners"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
11-
"github.com/step-security/codeowners-validator/pkg/codeowners"
1212
)
1313

1414
const sampleCodeownerFile = `

pkg/url/canonical_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package url_test
33
import (
44
"testing"
55

6-
"github.com/stretchr/testify/assert"
76
"github.com/step-security/codeowners-validator/pkg/url"
7+
"github.com/stretchr/testify/assert"
88
)
99

1010
func TestCanonicalURLPath(t *testing.T) {

tests/integration/integration_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ func TestCheckSuccess(t *testing.T) {
187187
// to the golden file.
188188
//
189189
// To update golden file, run:
190-
// TEST=TestCheckFailures UPDATE_GOLDEN=true make test-integration
190+
//
191+
// TEST=TestCheckFailures UPDATE_GOLDEN=true make test-integration
191192
func TestCheckFailures(t *testing.T) {
192193
type Envs map[string]string
193194
tests := []struct {
@@ -274,7 +275,8 @@ func TestCheckFailures(t *testing.T) {
274275
}
275276

276277
// To update golden file, run:
277-
// TEST=TestOwnerCheckAuthZAndAuthN TOKEN_WITH_NO_SCOPES=<token_with_no_scopes> UPDATE_GOLDEN=true make test-integration
278+
//
279+
// TEST=TestOwnerCheckAuthZAndAuthN TOKEN_WITH_NO_SCOPES=<token_with_no_scopes> UPDATE_GOLDEN=true make test-integration
278280
func TestOwnerCheckAuthZAndAuthN(t *testing.T) {
279281
t.Parallel()
280282

0 commit comments

Comments
 (0)