Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions checks/cii_best_practices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package checks
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"go.uber.org/mock/gomock"
Expand Down Expand Up @@ -126,3 +129,61 @@ func TestCIIBestPractices(t *testing.T) {
})
}
}

func TestCIIBestPractices_CustomURL_AllBadges(t *testing.T) {
tests := []struct {
name string
badgeJSON string
expected clients.BadgeLevel
}{
{
"NotFoundBadge",
`[]`,
clients.NotFound,
},
{
"InProgressBadge", `[{"badge_level":"in_progress"}]`,
clients.InProgress,
},
{
"PassingBadge", `[{"badge_level":"passing"}]`,
clients.Passing,
},
{
"SilverBadge", `[{"badge_level":"silver"}]`,
clients.Silver,
},
{
"GoldBadge", `[{"badge_level":"gold"}]`,
clients.Gold,
},
{
"UnknownBadge", `[{"badge_level":"foo"}]`,
clients.Unknown,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/projects.json" {
t.Errorf("Expected request path '/projects.json', got: %s", r.URL.Path)
}
fmt.Fprint(w, tt.badgeJSON)
}))
defer server.Close()

t.Setenv("CII_BEST_PRACTICES_URL", server.URL)

client := clients.DefaultCIIBestPracticesClient()
badge, err := client.GetBadgeLevel(t.Context(), "github.com/owner/repo")
if err != nil && tt.expected != clients.Unknown {
t.Fatalf("GetBadgeLevel() returned unexpected error: %v", err)
}

if badge != tt.expected {
t.Errorf("GetBadgeLevel() = %v, want %v", badge, tt.expected)
}
})
}
}
13 changes: 12 additions & 1 deletion clients/cii_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package clients

import (
"context"
"os"
)

const (
Expand All @@ -31,6 +32,10 @@ const (
Silver
// Gold level for CII Best Practices badge.
Gold
// CII Best Practices default URL.
defaultCIIBestPracticesURL = "https://www.bestpractices.dev"
// CII Best Practices URL environment variable.
envVarCIIBestPracticesURL = "CII_BEST_PRACTICES_URL"
)

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

// DefaultCIIBestPracticesClient returns http-based implementation of the interface.
func DefaultCIIBestPracticesClient() CIIBestPracticesClient {
return &httpClientCIIBestPractices{}
baseURL := os.Getenv(envVarCIIBestPracticesURL)
if baseURL == "" {
baseURL = defaultCIIBestPracticesURL
}
return &httpClientCIIBestPractices{
baseURL: baseURL,
}
}

// BlobCIIBestPracticesClient returns a blob-based implementation of the interface.
Expand Down
6 changes: 4 additions & 2 deletions clients/cii_http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ var errTooManyRequests = errors.New("failed after exponential backoff")

// httpClientCIIBestPractices implements the CIIBestPracticesClient interface.
// A HTTP client with exponential backoff is used to communicate with the CII Best Practices servers.
type httpClientCIIBestPractices struct{}
type httpClientCIIBestPractices struct {
baseURL string
}

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