Skip to content

Commit

Permalink
Merge pull request #72 from siemens/license_preview
Browse files Browse the repository at this point in the history
feat(license_preview): Created an endpoint to fetch all license shortnames

Reviewed-by: opensource@avinal.space, mishra.gaurav@siemens.com
Tested-by: mishra.gaurav@siemens.com
  • Loading branch information
GMishx authored Jun 28, 2024
2 parents 7940f0a + bcab679 commit f6f5616
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 1 deletion.
64 changes: 64 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,51 @@ const docTemplate = `{
}
}
},
"/licenses/preview": {
"get": {
"description": "Get shortnames of all active licenses from the service",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Licenses"
],
"summary": "Get shortnames of all active licenses",
"operationId": "GetAllLicensePreviews",
"parameters": [
{
"type": "boolean",
"description": "Active license only",
"name": "active",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.LicensePreviewResponse"
}
},
"400": {
"description": "Invalid active value",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
},
"500": {
"description": "Unable to fetch licenses",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
},
"/licenses/{shortname}": {
"get": {
"description": "Get a single license by its shortname",
Expand Down Expand Up @@ -2020,6 +2065,25 @@ const docTemplate = `{
}
}
},
"models.LicensePreviewResponse": {
"type": "object",
"properties": {
"shortnames": {
"type": "array",
"items": {
"type": "string"
},
"example": [
"GPL-2.0-only",
"GPL-2.0-or-later"
]
},
"status": {
"type": "integer",
"example": 200
}
}
},
"models.LicenseResponse": {
"type": "object",
"properties": {
Expand Down
64 changes: 64 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,51 @@
}
}
},
"/licenses/preview": {
"get": {
"description": "Get shortnames of all active licenses from the service",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Licenses"
],
"summary": "Get shortnames of all active licenses",
"operationId": "GetAllLicensePreviews",
"parameters": [
{
"type": "boolean",
"description": "Active license only",
"name": "active",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/models.LicensePreviewResponse"
}
},
"400": {
"description": "Invalid active value",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
},
"500": {
"description": "Unable to fetch licenses",
"schema": {
"$ref": "#/definitions/models.LicenseError"
}
}
}
}
},
"/licenses/{shortname}": {
"get": {
"description": "Get a single license by its shortname",
Expand Down Expand Up @@ -2013,6 +2058,25 @@
}
}
},
"models.LicensePreviewResponse": {
"type": "object",
"properties": {
"shortnames": {
"type": "array",
"items": {
"type": "string"
},
"example": [
"GPL-2.0-only",
"GPL-2.0-or-later"
]
},
"status": {
"type": "integer",
"example": 200
}
}
},
"models.LicenseResponse": {
"type": "object",
"properties": {
Expand Down
43 changes: 43 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,19 @@ definitions:
- rf_text_updatable
- rf_url
type: object
models.LicensePreviewResponse:
properties:
shortnames:
example:
- GPL-2.0-only
- GPL-2.0-or-later
items:
type: string
type: array
status:
example: 200
type: integer
type: object
models.LicenseResponse:
properties:
data:
Expand Down Expand Up @@ -1135,6 +1148,36 @@ paths:
summary: Import licenses by uploading a json file
tags:
- Licenses
/licenses/preview:
get:
consumes:
- application/json
description: Get shortnames of all active licenses from the service
operationId: GetAllLicensePreviews
parameters:
- description: Active license only
in: query
name: active
required: true
type: boolean
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/models.LicensePreviewResponse'
"400":
description: Invalid active value
schema:
$ref: '#/definitions/models.LicenseError'
"500":
description: Unable to fetch licenses
schema:
$ref: '#/definitions/models.LicenseError'
summary: Get shortnames of all active licenses
tags:
- Licenses
/login:
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 @@ -72,6 +72,7 @@ func Router() *gin.Engine {
licenses.GET("", FilterLicense)
licenses.GET(":shortname", GetLicense)
licenses.GET("export", ExportLicenses)
licenses.GET("/preview", GetAllLicensePreviews)
}
search := unAuthorizedv1.Group("/search")
{
Expand Down
59 changes: 58 additions & 1 deletion pkg/api/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ func SearchInLicense(c *gin.Context) {
// @Tags Licenses
// @Accept multipart/form-data
// @Produce json
// @Param file formData file true "licenses json file list"
// @Param file formData file true "licenses json file list"
// @Success 200 {object} models.ImportLicensesResponse{data=[]models.LicenseImportStatus}
// @Failure 400 {object} models.LicenseError "input file must be present"
// @Failure 500 {object} models.LicenseError "Internal server error"
Expand Down Expand Up @@ -1152,3 +1152,60 @@ func ExportLicenses(c *gin.Context) {
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
c.JSON(http.StatusOK, &licenses)
}

// GetAllLicensePreviews retrieves a list of shortnames of all licenses
//
// @Summary Get shortnames of all active licenses
// @Description Get shortnames of all active licenses from the service
// @Id GetAllLicensePreviews
// @Tags Licenses
// @Accept json
// @Produce json
// @Param active query bool true "Active license only"
// @Success 200 {object} models.LicensePreviewResponse
// @Failure 400 {object} models.LicenseError "Invalid active value"
// @Failure 500 {object} models.LicenseError "Unable to fetch licenses"
// @Router /licenses/preview [get]
func GetAllLicensePreviews(c *gin.Context) {
var licenses []models.LicenseDB
active := c.Query("active")
if active == "" {
active = "true"
}
var parsedActive bool
parsedActive, err := strconv.ParseBool(active)
if err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "Invalid active value",
Error: fmt.Sprintf("Parsing failed for value '%s'", active),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
return
}
query := db.DB.Model(&models.LicenseDB{})
query.Where("rf_active = ?", parsedActive)

if err = query.Find(&licenses).Error; err != nil {
er := models.LicenseError{
Status: http.StatusInternalServerError,
Message: "Unable to fetch licenses",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusInternalServerError, er)
return
}

var res models.LicensePreviewResponse
for _, lic := range licenses {
res.Shortnames = append(res.Shortnames, lic.Shortname)
}

res.Status = http.StatusOK

c.JSON(http.StatusOK, res)
}
6 changes: 6 additions & 0 deletions pkg/models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ type LicensePATCHRequestJSONSchema struct {
Marydone OptionalData[bool] `json:"marydone" swaggertype:"boolean"`
}

// LicensePreviewResponse gets us the list of all license shortnames
type LicensePreviewResponse struct {
Status int `json:"status" example:"200"`
Shortnames []string `json:"shortnames" example:"GPL-2.0-only,GPL-2.0-or-later"`
}

// UpdateExternalRefsJSONPayload struct represents the external ref key value
// pairs for update
type UpdateExternalRefsJSONPayload struct {
Expand Down

0 comments on commit f6f5616

Please sign in to comment.