diff --git a/cmd/laas/docs/docs.go b/cmd/laas/docs/docs.go index 0891814..8ecb21d 100644 --- a/cmd/laas/docs/docs.go +++ b/cmd/laas/docs/docs.go @@ -425,6 +425,41 @@ const docTemplate = `{ } } }, + "/licenses/export": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Export all licenses as a json file", + "produces": [ + "application/json" + ], + "tags": [ + "Licenses" + ], + "summary": "Export all licenses as a json file", + "operationId": "ExportLicenses", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/models.ExportLicenseDB" + } + } + }, + "500": { + "description": "Failed to fetch Licenses", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + } + } + } + }, "/licenses/import": { "post": { "security": [ @@ -1516,6 +1551,83 @@ const docTemplate = `{ } } }, + "models.ExportLicenseDB": { + "type": "object", + "properties": { + "external_ref": { + "$ref": "#/definitions/datatypes.JSONType-models_LicenseDBSchemaExtension" + }, + "marydone": { + "type": "boolean" + }, + "rf_FSFfree": { + "type": "boolean" + }, + "rf_Fedora": { + "type": "string" + }, + "rf_GPLv2compatible": { + "type": "boolean" + }, + "rf_GPLv3compatible": { + "type": "boolean" + }, + "rf_OSIapproved": { + "type": "boolean" + }, + "rf_active": { + "type": "boolean" + }, + "rf_add_date": { + "type": "string", + "example": "2023-12-01T18:10:25.00+05:30" + }, + "rf_copyleft": { + "type": "boolean" + }, + "rf_detector_type": { + "type": "integer", + "example": 1 + }, + "rf_flag": { + "type": "integer", + "example": 1 + }, + "rf_fullname": { + "type": "string", + "example": "MIT License" + }, + "rf_notes": { + "type": "string", + "example": "This license has been superseded." + }, + "rf_risk": { + "type": "integer" + }, + "rf_shortname": { + "type": "string", + "example": "MIT" + }, + "rf_source": { + "type": "string" + }, + "rf_spdx_id": { + "type": "string", + "example": "MIT" + }, + "rf_text": { + "type": "string", + "example": "MIT License Text here" + }, + "rf_text_updatable": { + "type": "boolean" + }, + "rf_url": { + "type": "string", + "example": "https://opensource.org/licenses/MIT" + } + } + }, "models.ImportLicensesResponse": { "type": "object", "properties": { diff --git a/cmd/laas/docs/swagger.json b/cmd/laas/docs/swagger.json index e5df2e1..1372cd1 100644 --- a/cmd/laas/docs/swagger.json +++ b/cmd/laas/docs/swagger.json @@ -418,6 +418,41 @@ } } }, + "/licenses/export": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "Export all licenses as a json file", + "produces": [ + "application/json" + ], + "tags": [ + "Licenses" + ], + "summary": "Export all licenses as a json file", + "operationId": "ExportLicenses", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "array", + "items": { + "$ref": "#/definitions/models.ExportLicenseDB" + } + } + }, + "500": { + "description": "Failed to fetch Licenses", + "schema": { + "$ref": "#/definitions/models.LicenseError" + } + } + } + } + }, "/licenses/import": { "post": { "security": [ @@ -1509,6 +1544,83 @@ } } }, + "models.ExportLicenseDB": { + "type": "object", + "properties": { + "external_ref": { + "$ref": "#/definitions/datatypes.JSONType-models_LicenseDBSchemaExtension" + }, + "marydone": { + "type": "boolean" + }, + "rf_FSFfree": { + "type": "boolean" + }, + "rf_Fedora": { + "type": "string" + }, + "rf_GPLv2compatible": { + "type": "boolean" + }, + "rf_GPLv3compatible": { + "type": "boolean" + }, + "rf_OSIapproved": { + "type": "boolean" + }, + "rf_active": { + "type": "boolean" + }, + "rf_add_date": { + "type": "string", + "example": "2023-12-01T18:10:25.00+05:30" + }, + "rf_copyleft": { + "type": "boolean" + }, + "rf_detector_type": { + "type": "integer", + "example": 1 + }, + "rf_flag": { + "type": "integer", + "example": 1 + }, + "rf_fullname": { + "type": "string", + "example": "MIT License" + }, + "rf_notes": { + "type": "string", + "example": "This license has been superseded." + }, + "rf_risk": { + "type": "integer" + }, + "rf_shortname": { + "type": "string", + "example": "MIT" + }, + "rf_source": { + "type": "string" + }, + "rf_spdx_id": { + "type": "string", + "example": "MIT" + }, + "rf_text": { + "type": "string", + "example": "MIT License Text here" + }, + "rf_text_updatable": { + "type": "boolean" + }, + "rf_url": { + "type": "string", + "example": "https://opensource.org/licenses/MIT" + } + } + }, "models.ImportLicensesResponse": { "type": "object", "properties": { diff --git a/cmd/laas/docs/swagger.yaml b/cmd/laas/docs/swagger.yaml index 968805f..f0d4d57 100644 --- a/cmd/laas/docs/swagger.yaml +++ b/cmd/laas/docs/swagger.yaml @@ -62,6 +62,60 @@ definitions: example: 200 type: integer type: object + models.ExportLicenseDB: + properties: + external_ref: + $ref: '#/definitions/datatypes.JSONType-models_LicenseDBSchemaExtension' + marydone: + type: boolean + rf_FSFfree: + type: boolean + rf_Fedora: + type: string + rf_GPLv2compatible: + type: boolean + rf_GPLv3compatible: + type: boolean + rf_OSIapproved: + type: boolean + rf_active: + type: boolean + rf_add_date: + example: "2023-12-01T18:10:25.00+05:30" + type: string + rf_copyleft: + type: boolean + rf_detector_type: + example: 1 + type: integer + rf_flag: + example: 1 + type: integer + rf_fullname: + example: MIT License + type: string + rf_notes: + example: This license has been superseded. + type: string + rf_risk: + type: integer + rf_shortname: + example: MIT + type: string + rf_source: + type: string + rf_spdx_id: + example: MIT + type: string + rf_text: + example: MIT License Text here + type: string + rf_text_updatable: + type: boolean + rf_url: + example: https://opensource.org/licenses/MIT + type: string + type: object models.ImportLicensesResponse: properties: data: @@ -989,6 +1043,28 @@ paths: summary: Update a license tags: - Licenses + /licenses/export: + get: + description: Export all licenses as a json file + operationId: ExportLicenses + produces: + - application/json + responses: + "200": + description: OK + schema: + items: + $ref: '#/definitions/models.ExportLicenseDB' + type: array + "500": + description: Failed to fetch Licenses + schema: + $ref: '#/definitions/models.LicenseError' + security: + - ApiKeyAuth: [] + summary: Export all licenses as a json file + tags: + - Licenses /licenses/import: post: consumes: diff --git a/pkg/api/api.go b/pkg/api/api.go index c756243..d7a4b4e 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -71,6 +71,7 @@ func Router() *gin.Engine { { licenses.GET("", FilterLicense) licenses.GET(":shortname", GetLicense) + licenses.GET("export", ExportLicenses) } search := unAuthorizedv1.Group("/search") { diff --git a/pkg/api/licenses.go b/pkg/api/licenses.go index 52e4bc1..80383b2 100644 --- a/pkg/api/licenses.go +++ b/pkg/api/licenses.go @@ -13,6 +13,7 @@ import ( "net/http" "path/filepath" "strconv" + "strings" "time" "github.com/fossology/LicenseDb/pkg/db" @@ -1135,3 +1136,41 @@ func InsertOrUpdateLicenseOnImport(tx *gorm.DB, newLicenseMap map[string]interfa return message, importStatus, &newLicense, &oldLicense } + +// ExportLicenses gives users all licenses as a json file. +// +// @Summary Export all licenses as a json file +// @Description Export all licenses as a json file +// @Id ExportLicenses +// @Tags Licenses +// @Produce json +// @Success 200 {array} models.ExportLicenseDB +// @Failure 500 {object} models.LicenseError "Failed to fetch Licenses" +// @Security ApiKeyAuth +// @Router /licenses/export [get] +func ExportLicenses(c *gin.Context) { + + var licenses []models.ExportLicenseDB + query := db.DB.Model(&models.LicenseDB{}) + err := query.Find(&licenses).Error + if err != nil { + er := models.LicenseError{ + Status: http.StatusBadRequest, + Message: "Licenses not found", + Error: err.Error(), + Path: c.Request.URL.Path, + Timestamp: time.Now().Format(time.RFC3339), + } + c.JSON(http.StatusBadRequest, er) + return + } + fileName := strings.Map(func(r rune) rune { + if r == '+' || r == ':' { + return '_' + } + return r + }, fmt.Sprintf("license-export-%s.json", time.Now().Format(time.RFC3339))) + + c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName)) + c.JSON(http.StatusOK, &licenses) +} diff --git a/pkg/models/types.go b/pkg/models/types.go index abf506b..6548146 100644 --- a/pkg/models/types.go +++ b/pkg/models/types.go @@ -65,6 +65,30 @@ type LicensePOSTRequestJSONSchema struct { ExternalRef datatypes.JSONType[LicenseDBSchemaExtension] `json:"external_ref" binding:"required"` } +type ExportLicenseDB struct { + Shortname string `json:"rf_shortname" gorm:"unique;not null;column:rf_shortname" example:"MIT"` + Fullname string `json:"rf_fullname" gorm:"column:rf_fullname" example:"MIT License"` + Text string `json:"rf_text" gorm:"column:rf_text" example:"MIT License Text here"` + Url string `json:"rf_url" gorm:"column:rf_url" example:"https://opensource.org/licenses/MIT"` + AddDate time.Time `json:"rf_add_date" gorm:"default:CURRENT_TIMESTAMP;column:rf_add_date" example:"2023-12-01T18:10:25.00+05:30"` + Copyleft bool `json:"rf_copyleft" gorm:"column:rf_copyleft"` + FSFfree bool `json:"rf_FSFfree" gorm:"column:rf_FSFfree"` + OSIapproved bool `json:"rf_OSIapproved" gorm:"column:rf_OSIapproved"` + GPLv2compatible bool `json:"rf_GPLv2compatible" gorm:"column:rf_GPLv2compatible"` + GPLv3compatible bool `json:"rf_GPLv3compatible" gorm:"column:rf_GPLv3compatible"` + Notes string `json:"rf_notes" gorm:"column:rf_notes" example:"This license has been superseded."` + Fedora string `json:"rf_Fedora" gorm:"column:rf_Fedora"` + TextUpdatable bool `json:"rf_text_updatable" gorm:"column:rf_text_updatable"` + DetectorType int64 `json:"rf_detector_type" gorm:"column:rf_detector_type" example:"1"` + Active bool `json:"rf_active" gorm:"column:rf_active"` + Source string `json:"rf_source" gorm:"column:rf_source"` + SpdxId string `json:"rf_spdx_id" gorm:"column:rf_spdx_id" example:"MIT"` + Risk int64 `json:"rf_risk" gorm:"column:rf_risk"` + Flag int64 `json:"rf_flag" gorm:"default:1;column:rf_flag" example:"1"` + Marydone bool `json:"marydone" gorm:"column:marydone"` + ExternalRef datatypes.JSONType[LicenseDBSchemaExtension] `json:"external_ref"` +} + type LicenseJson struct { Shortname string `json:"rf_shortname"` Fullname string `json:"rf_fullname"`