Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: created audit and added change history. #14

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*.dll
*.so
*.dylib
laas
/laas

# Test binary, built with `go test -c`
*.test
Expand Down
8 changes: 8 additions & 0 deletions cmd/laas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,16 @@ func main() {
log.Fatalf("Failed to automigrate database: %v", err)
}

if err := db.DB.AutoMigrate(&models.Audit{}); err != nil {
log.Fatalf("Failed to automigrate database: %v", err)
}

if err := db.DB.AutoMigrate(&models.ChangeLog{}); err != nil {
log.Fatalf("Failed to automigrate database: %v", err)
}
db.Populatedb(*populatedb, *datafile)

r := api.Router()

r.Run()
}
315 changes: 313 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package api
import (
"fmt"
"net/http"
"strconv"
"time"

"github.com/fossology/LicenseDb/pkg/auth"
Expand Down Expand Up @@ -36,6 +37,11 @@ func Router() *gin.Engine {
authorized.GET("/api/users", auth.GetAllUser)
authorized.GET("/api/users/:id", auth.GetUser)

authorized.GET("/api/audit", GetAllAudit)
authorized.GET("/api/audit/:audit_id", GetAudit)
authorized.GET("/api/audit/:audit_id/changes", GetChangeLog)
authorized.GET("/api/audit/:audit_id/changes/:id", GetChangeLogbyId)

return r
}

Expand Down Expand Up @@ -168,6 +174,10 @@ func CreateLicense(c *gin.Context) {
func UpdateLicense(c *gin.Context) {
var update models.LicenseDB
var license models.LicenseDB
var oldlicense models.LicenseDB

username := c.GetString("username")

shortname := c.Param("shortname")
if err := db.DB.Where("shortname = ?", shortname).First(&license).Error; err != nil {
er := models.LicenseError{
Expand All @@ -180,6 +190,7 @@ func UpdateLicense(c *gin.Context) {
c.JSON(http.StatusBadRequest, er)
return
}
oldlicense = license
if err := c.ShouldBindJSON(&update); err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Expand All @@ -202,14 +213,202 @@ func UpdateLicense(c *gin.Context) {
c.JSON(http.StatusInternalServerError, er)
return
}

res := models.LicenseResponse{
Data: []models.LicenseDB{license},
Status: http.StatusOK,
Meta: models.PaginationMeta{
ResourceCount: 1,
},
}
audit := models.Audit{
Username: username,
Shortname: shortname,
Timestamp: time.Now().Format(time.RFC3339),
}

db.DB.Create(&audit)

if oldlicense.Shortname != license.Shortname {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "shortname",
OldValue: oldlicense.Shortname,
UpdatedValue: license.Shortname,
}
db.DB.Create(&change)
}
if oldlicense.Fullname != license.Fullname {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "fullname",
OldValue: oldlicense.Fullname,
UpdatedValue: license.Fullname,
}
db.DB.Create(&change)
}
if oldlicense.Url != license.Url {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Url",
OldValue: oldlicense.Url,
UpdatedValue: license.Url,
}
db.DB.Create(&change)
}
if oldlicense.AddDate != license.AddDate {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Adddate",
OldValue: oldlicense.AddDate,
UpdatedValue: license.AddDate,
}
db.DB.Create(&change)
}
if oldlicense.Active != license.Active {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Active",
OldValue: oldlicense.Active,
UpdatedValue: license.Active,
}
db.DB.Create(&change)
}
if oldlicense.Copyleft != license.Copyleft {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Copyleft",
OldValue: oldlicense.Copyleft,
UpdatedValue: license.Copyleft,
}
db.DB.Create(&change)
}
if oldlicense.FSFfree != license.FSFfree {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "FSFfree",
OldValue: oldlicense.FSFfree,
UpdatedValue: license.FSFfree,
}
db.DB.Create(&change)
}
if oldlicense.GPLv2compatible != license.GPLv2compatible {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "GPLv2compatible",
OldValue: oldlicense.GPLv2compatible,
UpdatedValue: license.GPLv2compatible,
}
db.DB.Create(&change)
}
if oldlicense.GPLv3compatible != license.GPLv3compatible {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "GPLv3compatible",
OldValue: oldlicense.GPLv3compatible,
UpdatedValue: license.GPLv3compatible,
}
db.DB.Create(&change)
}
if oldlicense.OSIapproved != license.OSIapproved {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "OSIapproved",
OldValue: oldlicense.Shortname,
UpdatedValue: license.Shortname,
}
db.DB.Create(&change)
}
if oldlicense.Text != license.Text {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Text",
OldValue: oldlicense.Text,
UpdatedValue: license.Text,
}
db.DB.Create(&change)
}
if oldlicense.TextUpdatable != license.TextUpdatable {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "TextUpdatable",
OldValue: oldlicense.TextUpdatable,
UpdatedValue: license.TextUpdatable,
}
db.DB.Create(&change)
}
if oldlicense.Fedora != license.Fedora {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Fedora",
OldValue: oldlicense.Fedora,
UpdatedValue: license.Fedora,
}
db.DB.Create(&change)
}
if oldlicense.Flag != license.Flag {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Flag",
OldValue: oldlicense.Shortname,
UpdatedValue: license.Shortname,
}
db.DB.Create(&change)
}
if oldlicense.Notes != license.Notes {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Notes",
OldValue: oldlicense.Notes,
UpdatedValue: license.Notes,
}
db.DB.Create(&change)
}
if oldlicense.DetectorType != license.DetectorType {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "DetectorType",
OldValue: oldlicense.DetectorType,
UpdatedValue: license.DetectorType,
}
db.DB.Create(&change)
}
if oldlicense.Source != license.Source {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Source",
OldValue: oldlicense.Source,
UpdatedValue: license.Source,
}
db.DB.Create(&change)
}
if oldlicense.SpdxId != license.SpdxId {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "SpdxId",
OldValue: oldlicense.SpdxId,
UpdatedValue: license.SpdxId,
}
db.DB.Create(&change)
}
if oldlicense.Risk != license.Risk {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Risk",
OldValue: oldlicense.Risk,
UpdatedValue: license.Risk,
}
db.DB.Create(&change)
}
if oldlicense.Marydone != license.Marydone {
change := models.ChangeLog{
AuditId: audit.Id,
Field: "Marydone",
OldValue: oldlicense.Marydone,
UpdatedValue: license.Marydone,
}
db.DB.Create(&change)
}
c.JSON(http.StatusOK, res)

}
Expand Down Expand Up @@ -309,9 +508,9 @@ func SearchInLicense(c *gin.Context) {
var license []models.LicenseDB
query := db.DB.Model(&license)

if input.SearchType == "fuzzy" {
if input.Search == "fuzzy" {
query = query.Where(fmt.Sprintf("%s ILIKE ?", input.Field), fmt.Sprintf("%%%s%%", input.SearchTerm))
} else if input.SearchType == "" || input.SearchType == "full_text_search" {
} else if input.Search == "" || input.Search == "full_text_search" {
query = query.Where(input.Field+" @@ plainto_tsquery(?)", input.SearchTerm)

} else {
Expand All @@ -337,3 +536,115 @@ func SearchInLicense(c *gin.Context) {
c.JSON(http.StatusOK, res)

}

func GetAllAudit(c *gin.Context) {
var audit []models.Audit

if err := db.DB.Find(&audit).Error; err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "Change log not found",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
return
}
res := models.AuditResponse{
Data: audit,
Status: http.StatusOK,
Meta: models.PaginationMeta{
ResourceCount: len(audit),
},
}

c.JSON(http.StatusOK, res)
}

func GetAudit(c *gin.Context) {
var chngelog models.Audit
id := c.Param("audit_id")

if err := db.DB.Where("id = ?", id).First(&chngelog).Error; err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "no change log with such id exists",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
}
res := models.AuditResponse{
Data: []models.Audit{chngelog},
Status: http.StatusOK,
Meta: models.PaginationMeta{
ResourceCount: 1,
},
}

c.JSON(http.StatusOK, res)
}

func GetChangeLog(c *gin.Context) {
var changelog []models.ChangeLog
id := c.Param("audit_id")

if err := db.DB.Where("audit_id = ?", id).Find(&changelog).Error; err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "no change log with such id exists",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
}

res := models.ChangeLogResponse{
Data: changelog,
Status: http.StatusOK,
Meta: models.PaginationMeta{
ResourceCount: 1,
},
}

c.JSON(http.StatusOK, res)
}

func GetChangeLogbyId(c *gin.Context) {
var changelog models.ChangeLog
auditid := c.Param("audit_id")
id := c.Param("id")

if err := db.DB.Where("id = ?", id).Find(&changelog).Error; err != nil {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "no change history with such id exists",
Error: err.Error(),
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
}
audit_id, _ := strconv.Atoi(auditid)
if changelog.AuditId != audit_id {
er := models.LicenseError{
Status: http.StatusBadRequest,
Message: "no change history with such id and audit id exists",
Error: "Invalid change history for the requested audit id",
Path: c.Request.URL.Path,
Timestamp: time.Now().Format(time.RFC3339),
}
c.JSON(http.StatusBadRequest, er)
}
res := models.ChangeLogResponse{
Data: []models.ChangeLog{changelog},
Status: http.StatusOK,
Meta: models.PaginationMeta{
ResourceCount: 1,
},
}
c.JSON(http.StatusOK, res)
}
Loading