Skip to content

Commit 66c42b6

Browse files
authored
✨ Support custom CII_Best_Practices_URL via environment variable. (#4882)
* Support custom CII_Best_Practices custom URL via environment variables. Signed-off-by: kash2104 <[email protected]> * Add tests for CII_Best_Practices custom URL. Signed-off-by: kash2104 <[email protected]> --------- Signed-off-by: kash2104 <[email protected]>
1 parent 6197ba3 commit 66c42b6

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

checks/cii_best_practices_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ package checks
1717
import (
1818
"context"
1919
"errors"
20+
"fmt"
21+
"net/http"
22+
"net/http/httptest"
2023
"testing"
2124

2225
"go.uber.org/mock/gomock"
@@ -126,3 +129,61 @@ func TestCIIBestPractices(t *testing.T) {
126129
})
127130
}
128131
}
132+
133+
func TestCIIBestPractices_CustomURL_AllBadges(t *testing.T) {
134+
tests := []struct {
135+
name string
136+
badgeJSON string
137+
expected clients.BadgeLevel
138+
}{
139+
{
140+
"NotFoundBadge",
141+
`[]`,
142+
clients.NotFound,
143+
},
144+
{
145+
"InProgressBadge", `[{"badge_level":"in_progress"}]`,
146+
clients.InProgress,
147+
},
148+
{
149+
"PassingBadge", `[{"badge_level":"passing"}]`,
150+
clients.Passing,
151+
},
152+
{
153+
"SilverBadge", `[{"badge_level":"silver"}]`,
154+
clients.Silver,
155+
},
156+
{
157+
"GoldBadge", `[{"badge_level":"gold"}]`,
158+
clients.Gold,
159+
},
160+
{
161+
"UnknownBadge", `[{"badge_level":"foo"}]`,
162+
clients.Unknown,
163+
},
164+
}
165+
166+
for _, tt := range tests {
167+
t.Run(tt.name, func(t *testing.T) {
168+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
169+
if r.URL.Path != "/projects.json" {
170+
t.Errorf("Expected request path '/projects.json', got: %s", r.URL.Path)
171+
}
172+
fmt.Fprint(w, tt.badgeJSON)
173+
}))
174+
defer server.Close()
175+
176+
t.Setenv("CII_BEST_PRACTICES_URL", server.URL)
177+
178+
client := clients.DefaultCIIBestPracticesClient()
179+
badge, err := client.GetBadgeLevel(t.Context(), "github.com/owner/repo")
180+
if err != nil && tt.expected != clients.Unknown {
181+
t.Fatalf("GetBadgeLevel() returned unexpected error: %v", err)
182+
}
183+
184+
if badge != tt.expected {
185+
t.Errorf("GetBadgeLevel() = %v, want %v", badge, tt.expected)
186+
}
187+
})
188+
}
189+
}

clients/cii_client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package clients
1616

1717
import (
1818
"context"
19+
"os"
1920
)
2021

2122
const (
@@ -31,6 +32,10 @@ const (
3132
Silver
3233
// Gold level for CII Best Practices badge.
3334
Gold
35+
// CII Best Practices default URL.
36+
defaultCIIBestPracticesURL = "https://www.bestpractices.dev"
37+
// CII Best Practices URL environment variable.
38+
envVarCIIBestPracticesURL = "CII_BEST_PRACTICES_URL"
3439
)
3540

3641
// BadgeLevel corresponds to CII-Best-Practices badge levels.
@@ -64,7 +69,13 @@ type CIIBestPracticesClient interface {
6469

6570
// DefaultCIIBestPracticesClient returns http-based implementation of the interface.
6671
func DefaultCIIBestPracticesClient() CIIBestPracticesClient {
67-
return &httpClientCIIBestPractices{}
72+
baseURL := os.Getenv(envVarCIIBestPracticesURL)
73+
if baseURL == "" {
74+
baseURL = defaultCIIBestPracticesURL
75+
}
76+
return &httpClientCIIBestPractices{
77+
baseURL: baseURL,
78+
}
6879
}
6980

7081
// BlobCIIBestPracticesClient returns a blob-based implementation of the interface.

clients/cii_http_client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ var errTooManyRequests = errors.New("failed after exponential backoff")
2828

2929
// httpClientCIIBestPractices implements the CIIBestPracticesClient interface.
3030
// A HTTP client with exponential backoff is used to communicate with the CII Best Practices servers.
31-
type httpClientCIIBestPractices struct{}
31+
type httpClientCIIBestPractices struct {
32+
baseURL string
33+
}
3234

3335
type expBackoffTransport struct {
3436
numRetries uint8
@@ -49,7 +51,7 @@ func (transport *expBackoffTransport) RoundTrip(req *http.Request) (*http.Respon
4951
// GetBadgeLevel implements CIIBestPracticesClient.GetBadgeLevel.
5052
func (client *httpClientCIIBestPractices) GetBadgeLevel(ctx context.Context, uri string) (BadgeLevel, error) {
5153
repoURI := fmt.Sprintf("https://%s", uri)
52-
url := fmt.Sprintf("https://www.bestpractices.dev/projects.json?url=%s", repoURI)
54+
url := fmt.Sprintf("%s/projects.json?url=%s", client.baseURL, repoURI)
5355
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
5456
if err != nil {
5557
return Unknown, fmt.Errorf("error during http.NewRequestWithContext: %w", err)

0 commit comments

Comments
 (0)