Skip to content

Commit c8cb10b

Browse files
k-avyGMishx
authored andcommitted
feat: added audit and change history endpoints.
- /api/audit: to get all the audit logs - /api/audit/:audit_id: to get the audit by its id - /api/audit/:audit_id/changes: to get all the change log of a particular audit - /api/audit/:audit_id/changes/:id: to get change of a particular change log Signed-off-by: Kavya Shukla <[email protected]>
1 parent 570e8ab commit c8cb10b

File tree

5 files changed

+357
-10
lines changed

5 files changed

+357
-10
lines changed

cmd/laas/main.go

+8
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ func main() {
4343
log.Fatalf("Failed to automigrate database: %v", err)
4444
}
4545

46+
if err := db.DB.AutoMigrate(&models.Audit{}); err != nil {
47+
log.Fatalf("Failed to automigrate database: %v", err)
48+
}
49+
50+
if err := db.DB.AutoMigrate(&models.ChangeLog{}); err != nil {
51+
log.Fatalf("Failed to automigrate database: %v", err)
52+
}
4653
db.Populatedb(*populatedb, *datafile)
4754

4855
r := api.Router()
56+
4957
r.Run()
5058
}

pkg/api/api.go

+313-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package api
66
import (
77
"fmt"
88
"net/http"
9+
"strconv"
910
"time"
1011

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

40+
authorized.GET("/api/audit", GetAllAudit)
41+
authorized.GET("/api/audit/:audit_id", GetAudit)
42+
authorized.GET("/api/audit/:audit_id/changes", GetChangeLog)
43+
authorized.GET("/api/audit/:audit_id/changes/:id", GetChangeLogbyId)
44+
3945
return r
4046
}
4147

@@ -168,6 +174,10 @@ func CreateLicense(c *gin.Context) {
168174
func UpdateLicense(c *gin.Context) {
169175
var update models.LicenseDB
170176
var license models.LicenseDB
177+
var oldlicense models.LicenseDB
178+
179+
username := c.GetString("username")
180+
171181
shortname := c.Param("shortname")
172182
if err := db.DB.Where("shortname = ?", shortname).First(&license).Error; err != nil {
173183
er := models.LicenseError{
@@ -180,6 +190,7 @@ func UpdateLicense(c *gin.Context) {
180190
c.JSON(http.StatusBadRequest, er)
181191
return
182192
}
193+
oldlicense = license
183194
if err := c.ShouldBindJSON(&update); err != nil {
184195
er := models.LicenseError{
185196
Status: http.StatusBadRequest,
@@ -202,14 +213,202 @@ func UpdateLicense(c *gin.Context) {
202213
c.JSON(http.StatusInternalServerError, er)
203214
return
204215
}
216+
205217
res := models.LicenseResponse{
206218
Data: []models.LicenseDB{license},
207219
Status: http.StatusOK,
208220
Meta: models.PaginationMeta{
209221
ResourceCount: 1,
210222
},
211223
}
224+
audit := models.Audit{
225+
Username: username,
226+
Shortname: shortname,
227+
Timestamp: time.Now().Format(time.RFC3339),
228+
}
212229

230+
db.DB.Create(&audit)
231+
232+
if oldlicense.Shortname != license.Shortname {
233+
change := models.ChangeLog{
234+
AuditId: audit.Id,
235+
Field: "shortname",
236+
OldValue: oldlicense.Shortname,
237+
UpdatedValue: license.Shortname,
238+
}
239+
db.DB.Create(&change)
240+
}
241+
if oldlicense.Fullname != license.Fullname {
242+
change := models.ChangeLog{
243+
AuditId: audit.Id,
244+
Field: "fullname",
245+
OldValue: oldlicense.Fullname,
246+
UpdatedValue: license.Fullname,
247+
}
248+
db.DB.Create(&change)
249+
}
250+
if oldlicense.Url != license.Url {
251+
change := models.ChangeLog{
252+
AuditId: audit.Id,
253+
Field: "Url",
254+
OldValue: oldlicense.Url,
255+
UpdatedValue: license.Url,
256+
}
257+
db.DB.Create(&change)
258+
}
259+
if oldlicense.AddDate != license.AddDate {
260+
change := models.ChangeLog{
261+
AuditId: audit.Id,
262+
Field: "Adddate",
263+
OldValue: oldlicense.AddDate,
264+
UpdatedValue: license.AddDate,
265+
}
266+
db.DB.Create(&change)
267+
}
268+
if oldlicense.Active != license.Active {
269+
change := models.ChangeLog{
270+
AuditId: audit.Id,
271+
Field: "Active",
272+
OldValue: oldlicense.Active,
273+
UpdatedValue: license.Active,
274+
}
275+
db.DB.Create(&change)
276+
}
277+
if oldlicense.Copyleft != license.Copyleft {
278+
change := models.ChangeLog{
279+
AuditId: audit.Id,
280+
Field: "Copyleft",
281+
OldValue: oldlicense.Copyleft,
282+
UpdatedValue: license.Copyleft,
283+
}
284+
db.DB.Create(&change)
285+
}
286+
if oldlicense.FSFfree != license.FSFfree {
287+
change := models.ChangeLog{
288+
AuditId: audit.Id,
289+
Field: "FSFfree",
290+
OldValue: oldlicense.FSFfree,
291+
UpdatedValue: license.FSFfree,
292+
}
293+
db.DB.Create(&change)
294+
}
295+
if oldlicense.GPLv2compatible != license.GPLv2compatible {
296+
change := models.ChangeLog{
297+
AuditId: audit.Id,
298+
Field: "GPLv2compatible",
299+
OldValue: oldlicense.GPLv2compatible,
300+
UpdatedValue: license.GPLv2compatible,
301+
}
302+
db.DB.Create(&change)
303+
}
304+
if oldlicense.GPLv3compatible != license.GPLv3compatible {
305+
change := models.ChangeLog{
306+
AuditId: audit.Id,
307+
Field: "GPLv3compatible",
308+
OldValue: oldlicense.GPLv3compatible,
309+
UpdatedValue: license.GPLv3compatible,
310+
}
311+
db.DB.Create(&change)
312+
}
313+
if oldlicense.OSIapproved != license.OSIapproved {
314+
change := models.ChangeLog{
315+
AuditId: audit.Id,
316+
Field: "OSIapproved",
317+
OldValue: oldlicense.Shortname,
318+
UpdatedValue: license.Shortname,
319+
}
320+
db.DB.Create(&change)
321+
}
322+
if oldlicense.Text != license.Text {
323+
change := models.ChangeLog{
324+
AuditId: audit.Id,
325+
Field: "Text",
326+
OldValue: oldlicense.Text,
327+
UpdatedValue: license.Text,
328+
}
329+
db.DB.Create(&change)
330+
}
331+
if oldlicense.TextUpdatable != license.TextUpdatable {
332+
change := models.ChangeLog{
333+
AuditId: audit.Id,
334+
Field: "TextUpdatable",
335+
OldValue: oldlicense.TextUpdatable,
336+
UpdatedValue: license.TextUpdatable,
337+
}
338+
db.DB.Create(&change)
339+
}
340+
if oldlicense.Fedora != license.Fedora {
341+
change := models.ChangeLog{
342+
AuditId: audit.Id,
343+
Field: "Fedora",
344+
OldValue: oldlicense.Fedora,
345+
UpdatedValue: license.Fedora,
346+
}
347+
db.DB.Create(&change)
348+
}
349+
if oldlicense.Flag != license.Flag {
350+
change := models.ChangeLog{
351+
AuditId: audit.Id,
352+
Field: "Flag",
353+
OldValue: oldlicense.Shortname,
354+
UpdatedValue: license.Shortname,
355+
}
356+
db.DB.Create(&change)
357+
}
358+
if oldlicense.Notes != license.Notes {
359+
change := models.ChangeLog{
360+
AuditId: audit.Id,
361+
Field: "Notes",
362+
OldValue: oldlicense.Notes,
363+
UpdatedValue: license.Notes,
364+
}
365+
db.DB.Create(&change)
366+
}
367+
if oldlicense.DetectorType != license.DetectorType {
368+
change := models.ChangeLog{
369+
AuditId: audit.Id,
370+
Field: "DetectorType",
371+
OldValue: oldlicense.DetectorType,
372+
UpdatedValue: license.DetectorType,
373+
}
374+
db.DB.Create(&change)
375+
}
376+
if oldlicense.Source != license.Source {
377+
change := models.ChangeLog{
378+
AuditId: audit.Id,
379+
Field: "Source",
380+
OldValue: oldlicense.Source,
381+
UpdatedValue: license.Source,
382+
}
383+
db.DB.Create(&change)
384+
}
385+
if oldlicense.SpdxId != license.SpdxId {
386+
change := models.ChangeLog{
387+
AuditId: audit.Id,
388+
Field: "SpdxId",
389+
OldValue: oldlicense.SpdxId,
390+
UpdatedValue: license.SpdxId,
391+
}
392+
db.DB.Create(&change)
393+
}
394+
if oldlicense.Risk != license.Risk {
395+
change := models.ChangeLog{
396+
AuditId: audit.Id,
397+
Field: "Risk",
398+
OldValue: oldlicense.Risk,
399+
UpdatedValue: license.Risk,
400+
}
401+
db.DB.Create(&change)
402+
}
403+
if oldlicense.Marydone != license.Marydone {
404+
change := models.ChangeLog{
405+
AuditId: audit.Id,
406+
Field: "Marydone",
407+
OldValue: oldlicense.Marydone,
408+
UpdatedValue: license.Marydone,
409+
}
410+
db.DB.Create(&change)
411+
}
213412
c.JSON(http.StatusOK, res)
214413

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

312-
if input.SearchType == "fuzzy" {
511+
if input.Search == "fuzzy" {
313512
query = query.Where(fmt.Sprintf("%s ILIKE ?", input.Field), fmt.Sprintf("%%%s%%", input.SearchTerm))
314-
} else if input.SearchType == "" || input.SearchType == "full_text_search" {
513+
} else if input.Search == "" || input.Search == "full_text_search" {
315514
query = query.Where(input.Field+" @@ plainto_tsquery(?)", input.SearchTerm)
316515

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

339538
}
539+
540+
func GetAllAudit(c *gin.Context) {
541+
var audit []models.Audit
542+
543+
if err := db.DB.Find(&audit).Error; err != nil {
544+
er := models.LicenseError{
545+
Status: http.StatusBadRequest,
546+
Message: "Change log not found",
547+
Error: err.Error(),
548+
Path: c.Request.URL.Path,
549+
Timestamp: time.Now().Format(time.RFC3339),
550+
}
551+
c.JSON(http.StatusBadRequest, er)
552+
return
553+
}
554+
res := models.AuditResponse{
555+
Data: audit,
556+
Status: http.StatusOK,
557+
Meta: models.PaginationMeta{
558+
ResourceCount: len(audit),
559+
},
560+
}
561+
562+
c.JSON(http.StatusOK, res)
563+
}
564+
565+
func GetAudit(c *gin.Context) {
566+
var chngelog models.Audit
567+
id := c.Param("audit_id")
568+
569+
if err := db.DB.Where("id = ?", id).First(&chngelog).Error; err != nil {
570+
er := models.LicenseError{
571+
Status: http.StatusBadRequest,
572+
Message: "no change log with such id exists",
573+
Error: err.Error(),
574+
Path: c.Request.URL.Path,
575+
Timestamp: time.Now().Format(time.RFC3339),
576+
}
577+
c.JSON(http.StatusBadRequest, er)
578+
}
579+
res := models.AuditResponse{
580+
Data: []models.Audit{chngelog},
581+
Status: http.StatusOK,
582+
Meta: models.PaginationMeta{
583+
ResourceCount: 1,
584+
},
585+
}
586+
587+
c.JSON(http.StatusOK, res)
588+
}
589+
590+
func GetChangeLog(c *gin.Context) {
591+
var changelog []models.ChangeLog
592+
id := c.Param("audit_id")
593+
594+
if err := db.DB.Where("audit_id = ?", id).Find(&changelog).Error; err != nil {
595+
er := models.LicenseError{
596+
Status: http.StatusBadRequest,
597+
Message: "no change log with such id exists",
598+
Error: err.Error(),
599+
Path: c.Request.URL.Path,
600+
Timestamp: time.Now().Format(time.RFC3339),
601+
}
602+
c.JSON(http.StatusBadRequest, er)
603+
}
604+
605+
res := models.ChangeLogResponse{
606+
Data: changelog,
607+
Status: http.StatusOK,
608+
Meta: models.PaginationMeta{
609+
ResourceCount: 1,
610+
},
611+
}
612+
613+
c.JSON(http.StatusOK, res)
614+
}
615+
616+
func GetChangeLogbyId(c *gin.Context) {
617+
var changelog models.ChangeLog
618+
auditid := c.Param("audit_id")
619+
id := c.Param("id")
620+
621+
if err := db.DB.Where("id = ?", id).Find(&changelog).Error; err != nil {
622+
er := models.LicenseError{
623+
Status: http.StatusBadRequest,
624+
Message: "no change history with such id exists",
625+
Error: err.Error(),
626+
Path: c.Request.URL.Path,
627+
Timestamp: time.Now().Format(time.RFC3339),
628+
}
629+
c.JSON(http.StatusBadRequest, er)
630+
}
631+
audit_id, _ := strconv.Atoi(auditid)
632+
if changelog.AuditId != audit_id {
633+
er := models.LicenseError{
634+
Status: http.StatusBadRequest,
635+
Message: "no change history with such id and audit id exists",
636+
Error: "Invalid change history for the requested audit id",
637+
Path: c.Request.URL.Path,
638+
Timestamp: time.Now().Format(time.RFC3339),
639+
}
640+
c.JSON(http.StatusBadRequest, er)
641+
}
642+
res := models.ChangeLogResponse{
643+
Data: []models.ChangeLog{changelog},
644+
Status: http.StatusOK,
645+
Meta: models.PaginationMeta{
646+
ResourceCount: 1,
647+
},
648+
}
649+
c.JSON(http.StatusOK, res)
650+
}

0 commit comments

Comments
 (0)