summaryrefslogtreecommitdiff
blob: 7725c88b239c517469a276289c58d05b964e5a76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package cvetool

import (
	"glsamaker/pkg/app/handler/authentication"
	"glsamaker/pkg/app/handler/authentication/utils"
	"glsamaker/pkg/database/connection"
	"glsamaker/pkg/logger"
	"glsamaker/pkg/models/bugzilla"
	"glsamaker/pkg/models/cve"
	"net/http"
)

// Show renders a template to show the landing page of the application
func AssignBug(w http.ResponseWriter, r *http.Request) {

	user := utils.GetAuthenticatedUser(r)

	if !user.Permissions.CVETool.AssignBug {
		authentication.AccessDenied(w, r)
		return
	}

	cveId, bugId, err := getBugAssignParams(r)

	// TODO validate bug using bugzilla api before continue

	cveItem := &cve.DefCveItem{Id: cveId}
	err = connection.DB.Select(cveItem)

	if err != nil {
		w.Write([]byte("err"))
		return
	}

	cveItem.State = "Assigned"

	logger.Info.Println("bugId")
	logger.Info.Println(bugId)

	//assign bug
	newBugs := bugzilla.GetBugsByIds([]string{bugId})

	for _, newBug := range newBugs {
		_, err = connection.DB.Model(&newBug).OnConflict("(id) DO UPDATE").Insert()

		if err != nil {
			logger.Info.Println("Error creating bug")
			logger.Info.Println(err)
		}

		cveToBug := &cve.DefCveItemToBug{
			DefCveItemId: cveId,
			BugId:        newBug.Id,
		}

		connection.DB.Model(cveToBug).Insert()

	}

	// TODO MIGRATION
	//cveItem.Bugs = append(cveItem.Bugs, bugId)

	_, err = connection.DB.Model(cveItem).Column("bugs").WherePK().Update()
	_, err = connection.DB.Model(cveItem).Column("state").WherePK().Update()

	if err != nil {
		logger.Info.Println("Err")
		logger.Info.Println(err)
		w.Write([]byte("err"))
		return
	}

	w.Write([]byte("ok"))

}

func getBugAssignParams(r *http.Request) (string, string, error) {
	err := r.ParseForm()
	if err != nil {
		return "", "", err
	}
	cveid := r.Form.Get("cveid")
	bugid := r.Form.Get("bugid")
	return cveid, bugid, err
}