Refactor gradation + add way to give point for a succeeded task

This commit is contained in:
nemunaire 2023-03-05 17:54:51 +01:00
parent a78de73671
commit 8b8f3947f8
2 changed files with 75 additions and 62 deletions

View File

@ -1,8 +1,11 @@
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/drone/drone-go/drone"
"github.com/gin-gonic/gin"
@ -30,3 +33,74 @@ func declareAPIAdminGradationRoutes(router *gin.RouterGroup) {
c.JSON(http.StatusOK, result)
})
}
type TestsWebhook struct {
Login string `json:"login"`
RepositoryId int `json:"repository_id"`
BuildNumber int `json:"build_number"`
Steps map[string]float64 `json:"steps,omitempty"`
}
func (r *Repository) fetchRepoTests(build_number int, steps map[string]float64) 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 {
if g, ok := steps[fmt.Sprintf("%d", step.Number)]; ok {
log.Printf("Step %q (%d) in status %q", step.Name, step.Number, step.Status)
// Give the point if it succeed
if step.Status == "success" {
grade += g
}
continue
}
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
}
if len(logs) < 1 {
continue
}
line := logs[len(logs)-1]
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
}

View File

@ -334,12 +334,6 @@ 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
@ -446,7 +440,7 @@ func declareCallbacksRoutes(router *gin.RouterGroup) {
return
}
err = repo.fetchRepoTests(hook.BuildNumber)
err = repo.fetchRepoTests(hook.BuildNumber, hook.Steps)
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."})
@ -716,61 +710,6 @@ 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
}
if len(logs) < 1 {
continue
}
line := logs[len(logs)-1]
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