New callback to compute grade after tests

This commit is contained in:
nemunaire 2023-03-05 03:57:58 +01:00
parent 91aee60bfb
commit 936a8a80f4
2 changed files with 100 additions and 5 deletions

View File

@ -21,14 +21,16 @@ import (
)
var (
droneToken = ""
droneConfig *http.Client
droneEndpoint string
droneToken = ""
droneConfig *http.Client
droneEndpoint string
testsCallbackToken string
)
func init() {
flag.StringVar(&droneToken, "drone-token", droneToken, "Token for Drone Oauth")
flag.StringVar(&droneEndpoint, "drone-endpoint", droneEndpoint, "Drone Endpoint")
flag.StringVar(&testsCallbackToken, "tests-callback-token", testsCallbackToken, "Token of the callback token")
}
func initializeDroneOauth() {
@ -326,6 +328,12 @@ type GitLabWebhook struct {
Repository GitLabRepository
}
type TestsWebhook struct {
Login string `json:"login"`
RepositoryId int `json:"repository_id"`
BuildNumber int `json:"build_number"`
}
func declareCallbacksRoutes(router *gin.RouterGroup) {
router.POST("/callbacks/trigger.json", func(c *gin.Context) {
// Check event type
@ -409,6 +417,36 @@ func declareCallbacksRoutes(router *gin.RouterGroup) {
TriggerTagUpdate(c, work, repo, user, &tmp[2], false)
})
router.POST("/callbacks/tests.json", func(c *gin.Context) {
// Check auth token
if c.GetHeader("X-Authorization") != testsCallbackToken {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"errmsg": "Authorization token is invalid"})
return
}
// Get form data
hook := TestsWebhook{}
if err := c.ShouldBindJSON(&hook); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
return
}
// Retrieve corresponding repository
repo, err := getRepository(hook.RepositoryId)
if err != nil {
log.Printf("Unable to getRepository(%d): %s", hook.RepositoryId, err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to retrieve repository."})
return
}
err = repo.fetchRepoTests(hook.BuildNumber)
if err != nil {
log.Printf("Unable to fetchRepoTests(%d): %s", hook.RepositoryId, err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to fetch tests results."})
return
}
})
}
func repositoryHandler(c *gin.Context) {
@ -529,7 +567,7 @@ func TriggerTests(c *gin.Context, work *Work, repo *Repository, u *User) {
req, _ := s3.New(s).GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(s3_bucket),
Key: aws.String(filepath.Join(fmt.Sprintf("%d", work.Id), fmt.Sprintf("rendu-%s.tar.xz", u.Login))),
Key: aws.String(filepath.Join(fmt.Sprintf("%d", work.Id), fmt.Sprintf("rendu-%s.tar.xz", login))),
})
url, err := req.Presign(SharingTime)
@ -541,6 +579,7 @@ func TriggerTests(c *gin.Context, work *Work, repo *Repository, u *User) {
env := map[string]string{
"SUBMISSION_URL": repo.URI,
"REPO_ID": fmt.Sprintf("%d", repo.Id),
"META_URL": url,
"LOGIN": login,
"GROUPS": groups,
@ -551,7 +590,7 @@ func TriggerTests(c *gin.Context, work *Work, repo *Repository, u *User) {
result, err := client.BuildCreate(slug[0], slug[1], "", branch, env)
if err != nil {
log.Println("Unable to communicate with Drone:", err.Error())
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to communication with the extraction service."})
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to communication with the gradation service."})
return
}
@ -665,6 +704,58 @@ func (r Repository) Delete() (int64, error) {
}
}
func (r *Repository) fetchRepoTests(build_number int) error {
tmp := strings.Split(r.TestsRef, "/")
if len(tmp) < 3 {
return fmt.Errorf("This repository tests reference is not filled properly.")
}
work, err := getWork(r.IdWork)
if err != nil {
return fmt.Errorf("Unable to retrieve the related work: %w", err)
}
client := drone.NewClient(droneEndpoint, droneConfig)
result, err := client.Build(tmp[0], tmp[1], build_number)
if err != nil {
return fmt.Errorf("Unable to find the referenced build (%d): %w", build_number, err)
}
if result.Finished > 0 {
return fmt.Errorf("The test phase is not finished")
}
var grade float64
for _, stage := range result.Stages {
for _, step := range stage.Steps {
logs, err := client.Logs(tmp[0], tmp[1], build_number, stage.Number, step.Number)
if err != nil {
log.Printf("Unable to retrieve build logs %s/%s/%d/%d/%d: %s", tmp[0], tmp[1], build_number, stage.Number, step.Number, err.Error())
continue
}
for _, line := range logs {
if strings.HasPrefix(line.Message, "grade:") {
g, err := strconv.ParseFloat(strings.TrimSpace(strings.TrimPrefix(line.Message, "grade:")), 64)
if err == nil {
grade += g
} else {
log.Println("Unable to parse grade:", err.Error())
}
}
}
}
}
work.AddGrade(WorkGrade{
IdUser: r.IdUser,
IdWork: work.Id,
Grade: grade,
})
return nil
}
func ClearRepositories() (int64, error) {
if res, err := DBExec("DELETE FROM user_work_repositories"); err != nil {
return 0, err

View File

@ -415,6 +415,10 @@ func (u *User) GetMyWorkGrade(w *Work) (g WorkGrade, err error) {
return
}
func (w *Work) AddGrade(grade WorkGrade) error {
return w.AddGrades([]WorkGrade{grade})
}
func (w *Work) AddGrades(grades []WorkGrade) error {
var zerotime time.Time
for i, g := range grades {