Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃尡 Add ProjectPackageClient interface and deps.dev default client #3954

Merged
merged 7 commits into from May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
89 changes: 89 additions & 0 deletions clients/depsdev.go
@@ -0,0 +1,89 @@
// Copyright 2024 OpenSSF Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package clients

import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)

// This interface lets Scorecard look up package manager metadata for a project.
type ProjectPackageClient interface {
GetProjectPackageVersions(ctx context.Context, host, project string) (*ProjectPackageVersions, error)
}

type depsDevClient struct {
client *http.Client
}

type ProjectPackageVersions struct {
// field alignment
//nolint:govet
Versions []struct {
VersionKey struct {
System string `json:"system"`
Name string `json:"name"`
Version string `json:"version"`
} `json:"versionKey"`
SLSAProvenances []struct {
SourceRepository string `json:"sourceRepository"`
Commit string `json:"commit"`
Verified bool `json:"verified"`
} `json:"slsaProvenances"`
RelationType string `json:"relationType"`
RelationProvenance string `json:"relationProvenance"`
} `json:"versions"`
}

func CreateDepsDevClient() ProjectPackageClient {
return depsDevClient{
client: &http.Client{},
}

Check warning on line 57 in clients/depsdev.go

View check run for this annotation

Codecov / codecov/patch

clients/depsdev.go#L54-L57

Added lines #L54 - L57 were not covered by tests
}

func (d depsDevClient) GetProjectPackageVersions(
ctx context.Context, host, project string,
) (*ProjectPackageVersions, error) {
path := fmt.Sprintf("%s/%s", host, project)
query := fmt.Sprintf("https://api.deps.dev/v3/projects/%s:packageversions", url.QueryEscape(path))

req, err := http.NewRequestWithContext(ctx, http.MethodGet, query, nil)
if err != nil {
return nil, fmt.Errorf("http.NewRequestWithContext: %w", err)
}

Check warning on line 69 in clients/depsdev.go

View check run for this annotation

Codecov / codecov/patch

clients/depsdev.go#L62-L69

Added lines #L62 - L69 were not covered by tests

resp, err := d.client.Do(req)
if err != nil {
return nil, fmt.Errorf("deps.dev GetProjectPackageVersions: %w", err)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("resp.Body.Read: %w", err)
}

Check warning on line 80 in clients/depsdev.go

View check run for this annotation

Codecov / codecov/patch

clients/depsdev.go#L71-L80

Added lines #L71 - L80 were not covered by tests

var res ProjectPackageVersions
err = json.Unmarshal(body, &res)
if err != nil {
return nil, fmt.Errorf("json.Unmarshal from deps.dev: %w", err)
}

Check warning on line 86 in clients/depsdev.go

View check run for this annotation

Codecov / codecov/patch

clients/depsdev.go#L82-L86

Added lines #L82 - L86 were not covered by tests

return &res, nil

Check warning on line 88 in clients/depsdev.go

View check run for this annotation

Codecov / codecov/patch

clients/depsdev.go#L88

Added line #L88 was not covered by tests
}
55 changes: 55 additions & 0 deletions e2e/depsdev_test.go
@@ -0,0 +1,55 @@
// Copyright 2024 OpenSSF Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package e2e

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/ossf/scorecard/v5/clients"
)

var _ = Describe("E2E TEST: depsdevclient.GetProjectPackageVersions", func() {
var client clients.ProjectPackageClient

Context("E2E TEST: Confirm ProjectPackageClient works", func() {
It("Should receive a non-empty response from deps.dev for existing projects", func() {
client = clients.CreateDepsDevClient()
versions, err := client.GetProjectPackageVersions(
context.Background(), "github.com", "ossf/scorecard",
)
Expect(err).Should(BeNil())
Expect(len(versions.Versions)).Should(BeNumerically(">", 0))
})
It("Should error from deps.dev for nonexistent projects", func() {
client = clients.CreateDepsDevClient()
versions, err := client.GetProjectPackageVersions(
context.Background(), "github.com", "ossf/scorecard-E2E-TEST-DOES-NOT-EXIST",
)
Expect(err).ShouldNot(BeNil())
Expect(versions).Should(BeNil())
})
It("Should receive a non-empty response from deps.dev for existing projects", func() {
client = clients.CreateDepsDevClient()
versions, err := client.GetProjectPackageVersions(
context.Background(), "gitlab.com", "libtiff/libtiff",
)
Expect(err).Should(BeNil())
Expect(len(versions.Versions)).Should(BeNumerically(">", 0))
})
})
})