Skip to content

Commit bdb1a2d

Browse files
committed
feat(external_ref): Make external_ref fields configurable
Signed-off-by: deo002 <[email protected]>
1 parent b56de6e commit bdb1a2d

File tree

15 files changed

+219
-53
lines changed

15 files changed

+219
-53
lines changed

.github/workflows/api-swagger.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ jobs:
2323
check-latest: true
2424
cache: true
2525

26+
- name: Code Generation
27+
run: |
28+
cp external_ref_fields.example.yaml external_ref_fields.yaml && go generate ./...
29+
2630
- name: Install swag
2731
run: |
2832
go install github.com/swaggo/swag/cmd/swag@latest
@@ -33,7 +37,7 @@ jobs:
3337
3438
- name: Check doc diff
3539
run: |
36-
diff swag/docs/docs.go cmd/laas/docs/docs.go > swagger-diff.txt
40+
diff swag/docs/docs.go cmd/laas/docs/docs.go > swagger-diff.txt || true
3741
# Check if file swagger-diff.txt is empty
3842
if [ -s swagger-diff.txt ]
3943
then

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ jobs:
8585
- if: matrix.build-mode == 'manual'
8686
shell: bash
8787
run: |
88-
go build ./cmd/laas
88+
cp external_ref_fields.example.yaml external_ref_fields.yaml && go generate ./... && go build ./cmd/laas
8989
9090
- name: Perform CodeQL Analysis
9191
uses: github/codeql-action/analyze@v3

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
fi
3535
3636
- name: Build
37-
run: go build -v ./...
37+
run: cp external_ref_fields.example.yaml external_ref_fields.yaml && go generate ./... && go build -v ./...
3838

3939
# - name: Test
4040
# run: go test -v ./...

.github/workflows/golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ jobs:
2727
check-latest: true
2828
cache: true
2929

30+
- name: Code Generation
31+
run: cp external_ref_fields.example.yaml external_ref_fields.yaml && go generate ./...
32+
3033
- name: lint
3134
uses: golangci/golangci-lint-action@v3
3235
with:

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ licenseRef.json
2929
# IDE related files
3030
/.idea
3131
/LicenseDb.iml
32+
33+
external_ref_fields.yaml
34+
35+
pkg/models/external_ref_structs.go

Dockerfile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ RUN go mod download
1010
COPY cmd/ cmd/
1111
COPY pkg/ pkg/
1212
COPY docs/ docs/
13+
COPY external_ref_fields.example.yaml external_ref_fields.yaml
1314

1415
RUN wget https://raw.githubusercontent.com/fossology/fossology/master/install/db/licenseRef.json -O licenseRef.json
1516

16-
RUN CGO_ENABLED=0 GOOS=linux go build -a -o laas ./cmd/laas
17+
RUN CGO_ENABLED=0 GOOS=linux go generate ./cmd/laas && go build -a -o laas ./cmd/laas
1718

1819
# Release Stage
1920
FROM alpine:3.20 AS build-release
@@ -22,7 +23,7 @@ WORKDIR /app
2223

2324
COPY entrypoint.sh /app/entrypoint.sh
2425

25-
RUN apk add --no-cache openssl bash \
26+
RUN apk add --no-cache openssl bash libc6-compat \
2627
&& addgroup -S noroot \
2728
&& adduser -S noroot -G noroot
2829

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ git clone https://github.com/fossology/LicenseDb.git
7171
cd LicenseDb
7272
```
7373

74+
- Create the `external_ref_fields.yaml` file in the root directory of the project and change the
75+
values of the extra license json keys as per your requirement.
76+
77+
```bash
78+
cp external_ref_fields.yaml.example external_ref_fields.yaml
79+
vim external_ref_fields.yaml
80+
```
81+
82+
- Generate Go struct for the extra fields listed in the external_ref_fields.yaml.
83+
84+
```bash
85+
go generate ./...
86+
```
87+
7488
- Build the project using following command.
7589

7690
```bash

cmd/laas/gen.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-FileCopyrightText: 2024 Dearsh Oberoi <[email protected]>
2+
// SPDX-FileCopyrightText: 2024 Siemens AG
3+
//
4+
// SPDX-License-Identifier: GPL-2.0-only
5+
6+
package main
7+
8+
//go:generate go run gen_external_ref_schema.go

cmd/laas/gen_external_ref_schema.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// SPDX-FileCopyrightText: 2024 Dearsh Oberoi <[email protected]>
2+
// SPDX-FileCopyrightText: 2024 Siemens AG
3+
//
4+
// SPDX-License-Identifier: GPL-2.0-only
5+
6+
//go:build ignore
7+
8+
package main
9+
10+
import (
11+
"errors"
12+
"fmt"
13+
"log"
14+
"os"
15+
16+
"path/filepath"
17+
18+
"github.com/dave/jennifer/jen"
19+
"gopkg.in/yaml.v3"
20+
)
21+
22+
var (
23+
PATH_EXTERNAL_REF_CONFIG_FILE = filepath.FromSlash("../../external_ref_fields.yaml")
24+
PATH_EXTERNAL_REF_STRUCT_FILE = filepath.FromSlash("../../pkg/models/external_ref_structs.go")
25+
)
26+
27+
// ExternalRefFieldMetadata is the metadata about the extra fields saved as json in the license external_ref column
28+
type ExternalRefFieldMetaData struct {
29+
StructFieldName string `yaml:"struct_field_name"`
30+
Type string `yaml:"type"`
31+
Name string `yaml:"name"`
32+
}
33+
34+
// ExternalRefFields is the list of metadata of all extra fields
35+
type ExternalRefFields struct {
36+
Fields []ExternalRefFieldMetaData `yaml:"fields"`
37+
}
38+
39+
func main() {
40+
externalRefFields := ExternalRefFields{}
41+
42+
fieldsMetadata, err := os.ReadFile(PATH_EXTERNAL_REF_CONFIG_FILE)
43+
if err != nil {
44+
log.Fatalf("Failed to instantiate json schema for external ref in license: %v", err)
45+
}
46+
47+
err = yaml.Unmarshal(fieldsMetadata, &externalRefFields)
48+
if err != nil {
49+
log.Fatalf("Failed to instantiate json schema for external ref in license: %v", err)
50+
}
51+
52+
// REUSE-IgnoreStart
53+
54+
f := jen.NewFile("models")
55+
f.Comment("SPDX-FileCopyrightText: FOSSology Community\nSPDX-License-Identifier: GPL-2.0-only\n\nThis file is autogenerated. Please do not edit it.\n")
56+
57+
// REUSE-IgnoreStart
58+
59+
var fields []jen.Code
60+
61+
for _, f := range externalRefFields.Fields {
62+
field := jen.Id(f.StructFieldName).Op("*")
63+
if f.StructFieldName == "" {
64+
err = errors.New("field struct_field_name is missing in external_ref_fields.yaml")
65+
}
66+
switch f.Type {
67+
case "boolean":
68+
field = field.Bool()
69+
case "string":
70+
field = field.String()
71+
case "int":
72+
field = field.Int64()
73+
default:
74+
err = fmt.Errorf("type %s in external_ref_fields.yaml is not supported", f.Type)
75+
}
76+
if err != nil {
77+
log.Fatalf("Failed to instantiate json schema for external ref in license: %v", err)
78+
return
79+
}
80+
field = field.Tag(map[string]string{"json": fmt.Sprintf("%s,omitempty", f.Name)})
81+
fields = append(fields, field)
82+
}
83+
84+
f.Type().Id("LicenseDBSchemaExtension").Struct(fields...)
85+
f.Save(PATH_EXTERNAL_REF_STRUCT_FILE)
86+
87+
}

cmd/laas/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/joho/godotenv"
1414

15+
_ "github.com/dave/jennifer/jen"
1516
_ "github.com/fossology/LicenseDb/cmd/laas/docs"
1617
"github.com/fossology/LicenseDb/pkg/api"
1718
"github.com/fossology/LicenseDb/pkg/db"

0 commit comments

Comments
 (0)