Skip to content

Commit

Permalink
fix(rf): Remove rf prefix from license fields in json
Browse files Browse the repository at this point in the history
Signed-off-by: deo002 <[email protected]>

refactor(obligations): Make separate database tables for classification and type

chore(deps): bump github.com/golang-jwt/jwt/v4 in the go_modules group

Bumps the go_modules group with 1 update: [github.com/golang-jwt/jwt/v4](https://github.com/golang-jwt/jwt).

Updates `github.com/golang-jwt/jwt/v4` from 4.5.0 to 4.5.1
- [Release notes](https://github.com/golang-jwt/jwt/releases)
- [Changelog](https://github.com/golang-jwt/jwt/blob/main/VERSION_HISTORY.md)
- [Commits](golang-jwt/jwt@v4.5.0...v4.5.1)

---
updated-dependencies:
- dependency-name: github.com/golang-jwt/jwt/v4
  dependency-type: direct:production
  dependency-group: go_modules
...

Signed-off-by: dependabot[bot] <[email protected]>

feat(obligation): category field added in obligation get, post, patch

fix: obligation POST, PATCH fixed for category

fix: resolve merge conflict

feat: added staged changes for category
  • Loading branch information
deo002 authored and Sourav Bhowmik committed Nov 12, 2024
1 parent 1a392a1 commit 83ed38e
Show file tree
Hide file tree
Showing 14 changed files with 1,481 additions and 1,554 deletions.
463 changes: 219 additions & 244 deletions cmd/laas/docs/docs.go

Large diffs are not rendered by default.

463 changes: 219 additions & 244 deletions cmd/laas/docs/swagger.json

Large diffs are not rendered by default.

365 changes: 168 additions & 197 deletions cmd/laas/docs/swagger.yaml

Large diffs are not rendered by default.

25 changes: 22 additions & 3 deletions cmd/laas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"log"

"github.com/joho/godotenv"
"gorm.io/gorm/clause"

_ "github.com/dave/jennifer/jen"
_ "github.com/fossology/LicenseDb/cmd/laas/docs"
"github.com/fossology/LicenseDb/pkg/api"
"github.com/fossology/LicenseDb/pkg/db"
"github.com/fossology/LicenseDb/pkg/models"
"github.com/fossology/LicenseDb/pkg/utils"
)

// declare flags to input the basic requirement of database connection and the path of the data file
Expand Down Expand Up @@ -68,12 +70,29 @@ func main() {
log.Fatalf("Failed to automigrate database: %v", err)
}

if err := db.DB.AutoMigrate(&models.ObligationMap{}); err != nil {
log.Fatalf("Failed to automigrate database: %v", err)
DEFAULT_OBLIGATION_TYPES := []*models.ObligationType{
{Type: "OBLIGATION"},
{Type: "RISK"},
{Type: "RESTRICTION"},
{Type: "RIGHT"},
}
DEFAULT_OBLIGATION_CLASSIFICATIONS := []*models.ObligationClassification{
{Classification: "GREEN", Color: "#00FF00"},
{Classification: "WHITE", Color: "#FFFFFF"},
{Classification: "YELLOW", Color: "#FFDE21"},
{Classification: "RED", Color: "#FF0000"},
}

if err := db.DB.Clauses(clause.OnConflict{DoNothing: true}).Create(DEFAULT_OBLIGATION_TYPES).Error; err != nil {
log.Fatalf("Failed to seed database with default obligation types: %s", err.Error())
}

if err := db.DB.Clauses(clause.OnConflict{DoNothing: true}).Create(DEFAULT_OBLIGATION_CLASSIFICATIONS).Error; err != nil {
log.Fatalf("Failed to seed database with default obligation classifications: %s", err.Error())
}

if *populatedb {
db.Populatedb(*datafile)
utils.Populatedb(*datafile)
}

r := api.Router()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/gin-gonic/gin v1.9.1
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/golang-jwt/jwt/v4 v4.5.1
github.com/joho/godotenv v1.5.1
github.com/stretchr/testify v1.9.0
github.com/swaggo/files v1.0.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-jwt/jwt/v4 v4.5.1 h1:JdqV9zKUdtaa9gdPlywC3aeoEsR681PlKC+4F5gQgeo=
github.com/golang-jwt/jwt/v4 v4.5.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA=
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
Expand Down
44 changes: 10 additions & 34 deletions pkg/api/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ func GetAllAudit(c *gin.Context) {
}

for i := 0; i < len(audits); i++ {
if err := getAuditEntity(c, &audits[i]); err != nil {
if err := utils.GetAuditEntity(c, &audits[i]); err != nil {
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "unable to find audits",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusInternalServerError, er)
return
}
}
Expand Down Expand Up @@ -99,7 +107,7 @@ func GetAudit(c *gin.Context) {
return
}

if err := getAuditEntity(c, &audit); err != nil {
if err := utils.GetAuditEntity(c, &audit); err != nil {
return
}

Expand Down Expand Up @@ -232,35 +240,3 @@ func GetChangeLogbyId(c *gin.Context) {
}
c.JSON(http.StatusOK, res)
}

// getAuditEntity is an utility function to fetch obligation or license associated with an audit
func getAuditEntity(c *gin.Context, audit *models.Audit) error {
if audit.Type == "license" || audit.Type == "License" {
audit.Entity = &models.LicenseDB{}
if err := db.DB.Where(&models.LicenseDB{Id: audit.TypeId}).First(&audit.Entity).Error; err != nil {
er := models.LicenseError{
Status: http.StatusNotFound,
Message: "license corresponding with this audit does not exist",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusNotFound, er)
return err
}
} else if audit.Type == "obligation" || audit.Type == "Obligation" {
audit.Entity = &models.Obligation{}
if err := db.DB.Where(&models.Obligation{Id: audit.TypeId}).First(&audit.Entity).Error; err != nil {
er := models.LicenseError{
Status: http.StatusNotFound,
Message: "obligation corresponding with this audit does not exist",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusNotFound, er)
return err
}
}
return nil
}
22 changes: 14 additions & 8 deletions pkg/api/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ import (
// @Param page query int false "Page number"
// @Param limit query int false "Limit of responses per page"
// @Param externalRef query string false "External reference parameters"
// @Param sort_by query string false "Sort by field" Enums(rf_spdx_id, rf_shortname, rf_fullname) default(rf_shortname)
// @Param order_by query string false "Asc or desc ordering" Enums(asc, desc) default(asc)
// @Param sort_by query string false "Sort by field" Enums(spdx_id, shortname, fullname) default(shortname)
// @Param order_by query string false "Asc or desc ordering" Enums(asc, desc) default(asc)
// @Success 200 {object} models.LicenseResponse "Filtered licenses"
// @Failure 400 {object} models.LicenseError "Invalid value"
// @Security ApiKeyAuth || {}
Expand Down Expand Up @@ -170,8 +170,8 @@ func FilterLicense(c *gin.Context) {
sortBy := c.Query("sort_by")
orderBy := c.Query("order_by")
queryOrderString := ""
if sortBy != "" && (sortBy == "rf_spdx_id" || sortBy == "rf_shortname" || sortBy == "rf_fullname") {
queryOrderString += sortBy
if sortBy != "" && (sortBy == "spdx_id" || sortBy == "shortname" || sortBy == "fullname") {
queryOrderString += "rf_" + sortBy
} else {
queryOrderString += "rf_shortname"
}
Expand Down Expand Up @@ -412,12 +412,12 @@ func UpdateLicense(c *gin.Context) {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "Text is not updatable",
Error: "Field `rf_text_updatable` needs to be true to update the text",
Error: "Field `text_updatable` needs to be true to update the text",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
return errors.New("field `rf_text_updatable` needs to be true to update the text")
return errors.New("field `text_updatable` needs to be true to update the text")
}

// Update flag to indicate the license text was updated.
Expand All @@ -442,7 +442,7 @@ func UpdateLicense(c *gin.Context) {
newLicense := models.LicenseDB(updates)

// Update all other fields except external_ref and rf_shortname
if err := tx.Model(&newLicense).Omit("external_ref", "rf_shortname").Clauses(clause.Returning{}).Where(models.LicenseDB{Id: oldLicense.Id}).Updates(newLicense).Error; err != nil {
if err := tx.Model(&newLicense).Omit("external_ref", "rf_shortname", "Obligations").Clauses(clause.Returning{}).Where(models.LicenseDB{Id: oldLicense.Id}).Updates(newLicense).Error; err != nil {
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "Failed to update license",
Expand Down Expand Up @@ -767,6 +767,8 @@ func SearchInLicense(c *gin.Context) {
return
}

input.Field = "rf_" + input.Field

var license []models.LicenseDB
query := db.DB.Model(&license)

Expand Down Expand Up @@ -911,10 +913,14 @@ func ImportLicenses(c *gin.Context) {
errMessage, importStatus, oldLicense, newLicense := utils.InsertOrUpdateLicenseOnImport(tx, &licenses[i], &externalRefs[i])

if importStatus == utils.IMPORT_FAILED {
erroredLicense := ""
if licenses[i].Shortname != nil {
erroredLicense = *licenses[i].Shortname
}
res.Data = append(res.Data, models.LicenseError{
Status: http.StatusInternalServerError,
Message: errMessage,
Error: *licenses[i].Shortname,
Error: erroredLicense,
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
})
Expand Down
Loading

0 comments on commit 83ed38e

Please sign in to comment.