Skip to content

Commit 2a8565c

Browse files
authored
fix(gitlab): request project license from the API (#6724)
* fix(gitlab): request project license from the API The GitLab API omits license information from the project payload unless license=true is passed as a query parameter, so the gitlab/license property was always empty. Request it explicitly when fetching a project. Fixes #6589 Signed-off-by: Yash Jain <23dec512@lnmiit.ac.in> * chore: retrigger CI after transient module proxy failure Signed-off-by: Yash Jain <23dec512@lnmiit.ac.in> --------- Signed-off-by: Yash Jain <23dec512@lnmiit.ac.in>
1 parent 6a8e7f5 commit 2a8565c

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

internal/providers/gitlab/properties_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/json"
99
"net/http"
1010
"net/http/httptest"
11+
"net/url"
1112
"testing"
1213

1314
"github.com/stretchr/testify/assert"
@@ -388,6 +389,45 @@ func TestPropertiesToProtoMessage(t *testing.T) {
388389
}
389390
}
390391

392+
func Test_gitlabClient_getGitLabProject_requestsLicense(t *testing.T) {
393+
t.Parallel()
394+
395+
var gotQuery url.Values
396+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
397+
gotQuery = r.URL.Query()
398+
399+
resp := &gitlab.Project{
400+
ID: 1,
401+
Name: "project-1",
402+
Namespace: &gitlab.ProjectNamespace{
403+
Path: "group",
404+
},
405+
License: &gitlab.ProjectLicense{
406+
Key: "mit",
407+
Name: "MIT License",
408+
},
409+
}
410+
411+
w.Header().Set("Content-Type", "application/json")
412+
w.WriteHeader(http.StatusOK)
413+
//nolint:gosec // This is a test
414+
json.NewEncoder(w).Encode(resp)
415+
}))
416+
defer ts.Close()
417+
418+
got, err := newTestGitlabProvider(ts.URL).getPropertiesForRepo(
419+
context.Background(),
420+
properties.NewProperties(map[string]any{
421+
properties.PropertyUpstreamID: "1",
422+
}),
423+
)
424+
assert.NoError(t, err)
425+
426+
assert.Equal(t, "true", gotQuery.Get("license"),
427+
"GitLab omits license information unless license=true is requested")
428+
assert.Equal(t, "MIT License", got.GetProperty(RepoPropertyLicense).GetString())
429+
}
430+
391431
func newTestGitlabProvider(endpoint string) *gitlabClient {
392432
return &gitlabClient{
393433
cred: &credentials.GitLabTokenCredential{},

internal/providers/gitlab/repository_properties.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ func (c *gitlabClient) getGitLabProject(
5454
return nil, fmt.Errorf("failed to join URL path for project using upstream ID: %w", err)
5555
}
5656

57+
// GitLab only includes the project's license in the response when explicitly asked for it
58+
projectURLPath += "?" + url.Values{"license": []string{"true"}}.Encode()
59+
5760
// NOTE: We're not using github.com/xanzy/go-gitlab to do the actual
5861
// request here because of the way they form authentication for requests.
5962
// It would be ideal to use it, so we should consider contributing and making

0 commit comments

Comments
 (0)