Skip to content

Commit da8555e

Browse files
authored
Merge pull request #6 from scanoss/kbdetails
KB Details
2 parents def351d + 666adcb commit da8555e

8 files changed

Lines changed: 249 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Upcoming changes...
1111

12+
## [1.2.2] - 2023-08-24
13+
### Added
14+
- Added support for `/api/kb/details` endpoint
15+
1216
## [1.2.1] - 2023-08-09
1317
### Added
1418
- Added option to enable/disable HPSM processing (`HPSMEnabled`)
@@ -61,3 +65,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6165
[1.1.0]: https://github.com/scanoss/api.go/compare/v1.0.0...v1.1.0
6266
[1.2.0]: https://github.com/scanoss/api.go/compare/v1.1.0...v1.2.0
6367
[1.2.1]: https://github.com/scanoss/api.go/compare/v1.2.0...v1.2.1
68+
[1.2.2]: https://github.com/scanoss/api.go/compare/v1.2.1...v1.2.2

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module scanoss.com/go-api
33
go 1.19
44

55
require (
6+
github.com/go-co-op/gocron v1.31.0
67
github.com/golobby/config/v3 v3.4.2
78
github.com/google/uuid v1.3.0
89
github.com/gorilla/mux v1.8.0
@@ -20,8 +21,9 @@ require (
2021
github.com/golobby/env/v2 v2.2.4 // indirect
2122
github.com/phuslu/iploc v1.0.20230201 // indirect
2223
github.com/pmezard/go-difflib v1.0.0 // indirect
24+
github.com/robfig/cron/v3 v3.0.1 // indirect
2325
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce // indirect
24-
go.uber.org/atomic v1.7.0 // indirect
26+
go.uber.org/atomic v1.9.0 // indirect
2527
go.uber.org/multierr v1.6.0 // indirect
2628
gopkg.in/yaml.v3 v3.0.1 // indirect
2729
)

go.sum

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
55
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
77
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8+
github.com/go-co-op/gocron v1.31.0 h1:8VaWk7ARDpsVYFP8SmjvHrBZQkcPQ7HyAzF7acG57yE=
9+
github.com/go-co-op/gocron v1.31.0/go.mod h1:39f6KNSGVOU1LO/ZOoZfcSxwlsJDQOKSu8erN0SH48Y=
810
github.com/golobby/cast v1.3.3 h1:s2Lawb9RMz7YyYf8IrfMQY4IFmA1R/lgfmj97Vc6fig=
911
github.com/golobby/cast v1.3.3/go.mod h1:0oDO5IT84HTXcbLDf1YXuk0xtg/cRDrxhbpWKxwtJCY=
1012
github.com/golobby/config/v3 v3.4.2 h1:oIOSo24mC0A8f93ZTL24NDNw0hZ3Tbb34wc1ckn2CsA=
@@ -19,7 +21,9 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
1921
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
2022
github.com/jpillora/ipfilter v1.2.9 h1:vjjcI1JpxZ6HvIj1MZfomhrfzXW/67QNdE449ZZfon8=
2123
github.com/jpillora/ipfilter v1.2.9/go.mod h1:QUYQLXQU0myCdxZVbYBZ5+An/qtSB2m1OBRiwqTa9pk=
24+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
2225
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
26+
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
2327
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
2428
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
2529
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
@@ -32,6 +36,10 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK
3236
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
3337
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3438
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
39+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
40+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
41+
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
42+
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
3543
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
3644
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
3745
github.com/scanoss/zap-logging-helper v0.2.0 h1:r1HM/lalRZkkZU/3RQoIQJmZtzWsoAQr6Eqy/FRxWR8=
@@ -43,20 +51,24 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
4351
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
4452
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
4553
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
54+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
4655
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
4756
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
4857
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce h1:fb190+cK2Xz/dvi9Hv8eCYJYvIGUTN2/KLq1pT6CjEc=
4958
github.com/tomasen/realip v0.0.0-20180522021738-f0c99a92ddce/go.mod h1:o8v6yHRoik09Xen7gje4m9ERNah1d1PPsVq1VEx9vE4=
50-
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
5159
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
60+
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
61+
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
5262
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
5363
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
5464
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
5565
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
5666
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
5767
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
68+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5869
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
5970
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
71+
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
6072
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6173
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
6274
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pkg/protocol/rest/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func RunServer(config *myconfig.ServerConfig) error {
5454
zlog.S.Warnf("Scanning engine test failed. Scan requests are likely to fail.")
5555
zlog.S.Warnf("Please make sure that %v is accessible", config.Scanning.ScanBinary)
5656
}
57+
apiService.SetupKBDetailsCron()
5758
// Set up the endpoint routing
5859
router := mux.NewRouter().StrictSlash(true)
5960
router.HandleFunc("/", service.WelcomeMsg).Methods(http.MethodGet)
@@ -62,6 +63,7 @@ func RunServer(config *myconfig.ServerConfig) error {
6263
router.HandleFunc("/api/health-check", service.HealthCheck).Methods(http.MethodGet)
6364
router.HandleFunc("/api/metrics/{type}", service.MetricsHandler).Methods(http.MethodGet)
6465
router.HandleFunc("/api/file_contents/{md5}", apiService.FileContents).Methods(http.MethodGet)
66+
router.HandleFunc("/api/kb/details", apiService.KBDetails).Methods(http.MethodGet)
6567
router.HandleFunc("/api/license/obligations/{license}", apiService.LicenseDetails).Methods(http.MethodGet)
6668
router.HandleFunc("/api/scan/direct", apiService.ScanDirect).Methods(http.MethodPost)
6769
router.HandleFunc("/api/sbom/attribution", apiService.SbomAttribution).Methods(http.MethodPost)

pkg/service/kb_details.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (C) 2018-2023 SCANOSS.COM
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 2 of the License, or
8+
* (at your option) any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
*/
16+
17+
package service
18+
19+
import (
20+
"context"
21+
"encoding/json"
22+
"fmt"
23+
"net/http"
24+
"time"
25+
26+
"github.com/go-co-op/gocron"
27+
zlog "github.com/scanoss/zap-logging-helper/pkg/logger"
28+
)
29+
30+
// Structure for parsing KB & Engine version from scan response.
31+
type matchStructure []struct {
32+
Server struct {
33+
Hostname string `json:"hostname"`
34+
KbVersion struct {
35+
Daily string `json:"daily"`
36+
Monthly string `json:"monthly"`
37+
} `json:"kb_version"`
38+
Version string `json:"version"`
39+
} `json:"server"`
40+
}
41+
42+
var kbDetails string // KB Details JSON string
43+
44+
// SetupKBDetailsCron sets up a background cron to update the KB version once an hour.
45+
func (s APIService) SetupKBDetailsCron() {
46+
scheduler := gocron.NewScheduler(time.UTC)
47+
_, err := scheduler.Every(60).Minutes().Do(s.loadKBDetails)
48+
if err != nil {
49+
zlog.S.Warnf("Problem setting up KB details cron: %v", err)
50+
return
51+
}
52+
scheduler.StartAsync()
53+
}
54+
55+
// KBDetails retrieves the KB details and send back to the requester.
56+
func (s APIService) KBDetails(w http.ResponseWriter, r *http.Request) {
57+
reqID := getReqID(r)
58+
w.Header().Set(ResponseIDKey, reqID)
59+
zs := sugaredLogger(context.WithValue(r.Context(), RequestContextKey{}, reqID)) // Setup logger with context
60+
zs.Infof("%v request from %v", r.URL.Path, r.RemoteAddr)
61+
w.Header().Set(ContentTypeKey, ApplicationJSON)
62+
w.WriteHeader(http.StatusOK)
63+
printResponse(w, fmt.Sprintf("%s\n", kbDetails), zlog.S, true)
64+
}
65+
66+
// loadKBDetails attempts to scan a file to load the latest KB details from the server.
67+
func (s APIService) loadKBDetails() {
68+
zs := sugaredLogger(context.TODO()) // Setup logger without context
69+
zs.Debugf("Loading latest KB details...")
70+
if len(kbDetails) == 0 {
71+
kbDetails = fmt.Sprintf(`{"kb_version": { "monthly": "%v", "daily": "%v"}}`, "unknown", "unknown")
72+
}
73+
// Load a random (hopefully non-existent) file match to extract the KB version details
74+
result, err := s.scanWfp("file=7c53a2de7dfeaa20d057db98468d6670,2321,path/to/dummy/file.txt", "", "", "", zs)
75+
if err != nil {
76+
zs.Warnf("Failed to detect KB version from eninge: %v", err)
77+
return
78+
}
79+
if len(result) > 0 {
80+
if !json.Valid([]byte(result)) {
81+
zs.Warnf("Invalid JSON response from engine for KB version: %v", result)
82+
return
83+
}
84+
resDataAny := map[string]interface{}{}
85+
err = json.Unmarshal([]byte(result), &resDataAny) // parse the response JSON into an interface map
86+
if err != nil {
87+
zs.Warnf("Failed to parse KB version from eninge response: %v - %v", result, err)
88+
return
89+
}
90+
if s.config.App.Trace {
91+
zs.Debugf("KB details JSON: %v", resDataAny)
92+
}
93+
var ms matchStructure
94+
// Go through the list of file results and extract one set of KB details
95+
for _, key := range resDataAny {
96+
data, err := json.Marshal(key) // convert the given interface to JSON
97+
if err != nil {
98+
zs.Warnf("Failed to convert KB version map to json: %v - %v", key, err)
99+
return
100+
}
101+
err = json.Unmarshal(data, &ms)
102+
if err != nil {
103+
zs.Warnf("Failed to parse KB version from eninge result: %v - %v", data, err)
104+
return
105+
}
106+
}
107+
if len(ms) > 0 {
108+
kbDetails = fmt.Sprintf(`{"kb_version": { "monthly": "%v", "daily": "%v"}}`, ms[0].Server.KbVersion.Monthly, ms[0].Server.KbVersion.Daily)
109+
}
110+
}
111+
}

pkg/service/kb_details_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (C) 2018-2023 SCANOSS.COM
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 2 of the License, or
8+
* (at your option) any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
*/
16+
17+
package service
18+
19+
import (
20+
"fmt"
21+
"io"
22+
"net/http"
23+
"net/http/httptest"
24+
"strings"
25+
"testing"
26+
"time"
27+
28+
zlog "github.com/scanoss/zap-logging-helper/pkg/logger"
29+
"github.com/stretchr/testify/assert"
30+
)
31+
32+
func TestKBDetails(t *testing.T) {
33+
err := zlog.NewSugaredDevLogger()
34+
if err != nil {
35+
t.Fatalf("an error '%s' was not expected when opening a sugared logger", err)
36+
}
37+
defer zlog.SyncZap()
38+
39+
myConfig := setupConfig(t)
40+
myConfig.App.Trace = true
41+
apiService := NewAPIService(myConfig)
42+
apiService.SetupKBDetailsCron()
43+
time.Sleep(time.Duration(5) * time.Second) // Sleep a little to allow the KB details to be loaded
44+
req := httptest.NewRequest(http.MethodGet, "http://localhost/", nil)
45+
w := httptest.NewRecorder()
46+
apiService.KBDetails(w, req)
47+
48+
resp := w.Result()
49+
body, err := io.ReadAll(resp.Body)
50+
if err != nil {
51+
t.Fatalf("an error was not expected when reading from request: %v", err)
52+
}
53+
bodyStr := string(body)
54+
assert.Equal(t, http.StatusOK, resp.StatusCode)
55+
expected := `{"kb_version": { "monthly": "23.07", "daily": "23.08.09"}}`
56+
assert.Equal(t, expected, strings.TrimSpace(bodyStr))
57+
fmt.Println("Status: ", resp.StatusCode)
58+
fmt.Println("Type: ", resp.Header.Get("Content-Type"))
59+
fmt.Println("Body: ", bodyStr)
60+
61+
// Test the version loading to fail
62+
myConfig.Scanning.ScanBinary = "../path/to/does-not-exist.sh"
63+
apiService.loadKBDetails()
64+
}

test-support/scanoss.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fi
5555
if [ "$1" == "-w" ] || [ "$2" == "-w" ] || [ "$3" == "-w" ] || [ "$4" == "-w" ] || [ "$5" == "-w" ] || [ "$6" == "-w" ] || [ "$7" == "-w" ] || [ "$8" == "-w" ]; then
5656
for i in "$@"; do :; done
5757
scf=$i
58-
echo " {\"$scf\":[{\"id\": \"none\"}]} "
58+
echo " {\"$scf\":[{\"id\": \"none\", \"server\": { \"kb_version\": {\"daily\": \"23.08.09\", \"monthly\": \"23.07\"}, \"version\": \"5.2.7\"}}]} "
5959
exit 0
6060
fi
6161

tests/kb_details_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (C) 2018-2023 SCANOSS.COM
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 2 of the License, or
8+
* (at your option) any later version.
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
*/
16+
17+
package tests
18+
19+
import (
20+
"fmt"
21+
"github.com/stretchr/testify/suite"
22+
"io"
23+
"net/http"
24+
"testing"
25+
)
26+
27+
type E2EKBDetailsSuite struct {
28+
suite.Suite
29+
}
30+
31+
func TestE2EKBDetailsSuite(t *testing.T) {
32+
suite.Run(t, new(E2EKBDetailsSuite))
33+
}
34+
35+
func (s *E2EKBDetailsSuite) TestHappyKBDetails() {
36+
c := http.Client{}
37+
resp, err := c.Get(fmt.Sprintf("%v/api/kb/details", hostPort))
38+
if err != nil {
39+
s.Failf("an error was not expected when sending request.", "error: %v", err)
40+
}
41+
s.Equal(http.StatusOK, resp.StatusCode)
42+
body, err := io.ReadAll(resp.Body)
43+
if err != nil {
44+
s.Failf("an error was not expected when reading response body.", "error: %v", err)
45+
}
46+
bodyStr := string(body)
47+
fmt.Println("Status: ", resp.StatusCode)
48+
fmt.Println("Type: ", resp.Header.Get("Content-Type"))
49+
fmt.Println("Body: ", bodyStr)
50+
}

0 commit comments

Comments
 (0)