Skip to content

Commit

Permalink
feat(license): add /export endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Kaushlendra Pratap Singh <[email protected]>
  • Loading branch information
Kaushl2208 committed May 20, 2024
1 parent bc69f41 commit f501e39
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 0 deletions.
112 changes: 112 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": {
Expand Down
112 changes: 112 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": {
Expand Down
76 changes: 76 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func Router() *gin.Engine {
{
licenses.GET("", FilterLicense)
licenses.GET(":shortname", GetLicense)
licenses.GET("export", ExportLicenses)
}
search := unAuthorizedv1.Group("/search")
{
Expand Down
39 changes: 39 additions & 0 deletions pkg/api/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/fossology/LicenseDb/pkg/db"
Expand Down Expand Up @@ -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)
}
Loading

0 comments on commit f501e39

Please sign in to comment.