New callback to compute grade after tests
This commit is contained in:
parent
91aee60bfb
commit
936a8a80f4
101
repositories.go
101
repositories.go
@ -21,14 +21,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
droneToken = ""
|
droneToken = ""
|
||||||
droneConfig *http.Client
|
droneConfig *http.Client
|
||||||
droneEndpoint string
|
droneEndpoint string
|
||||||
|
testsCallbackToken string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
flag.StringVar(&droneToken, "drone-token", droneToken, "Token for Drone Oauth")
|
flag.StringVar(&droneToken, "drone-token", droneToken, "Token for Drone Oauth")
|
||||||
flag.StringVar(&droneEndpoint, "drone-endpoint", droneEndpoint, "Drone Endpoint")
|
flag.StringVar(&droneEndpoint, "drone-endpoint", droneEndpoint, "Drone Endpoint")
|
||||||
|
flag.StringVar(&testsCallbackToken, "tests-callback-token", testsCallbackToken, "Token of the callback token")
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeDroneOauth() {
|
func initializeDroneOauth() {
|
||||||
@ -326,6 +328,12 @@ type GitLabWebhook struct {
|
|||||||
Repository GitLabRepository
|
Repository GitLabRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TestsWebhook struct {
|
||||||
|
Login string `json:"login"`
|
||||||
|
RepositoryId int `json:"repository_id"`
|
||||||
|
BuildNumber int `json:"build_number"`
|
||||||
|
}
|
||||||
|
|
||||||
func declareCallbacksRoutes(router *gin.RouterGroup) {
|
func declareCallbacksRoutes(router *gin.RouterGroup) {
|
||||||
router.POST("/callbacks/trigger.json", func(c *gin.Context) {
|
router.POST("/callbacks/trigger.json", func(c *gin.Context) {
|
||||||
// Check event type
|
// Check event type
|
||||||
@ -409,6 +417,36 @@ func declareCallbacksRoutes(router *gin.RouterGroup) {
|
|||||||
|
|
||||||
TriggerTagUpdate(c, work, repo, user, &tmp[2], false)
|
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) {
|
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{
|
req, _ := s3.New(s).GetObjectRequest(&s3.GetObjectInput{
|
||||||
Bucket: aws.String(s3_bucket),
|
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)
|
url, err := req.Presign(SharingTime)
|
||||||
@ -541,6 +579,7 @@ func TriggerTests(c *gin.Context, work *Work, repo *Repository, u *User) {
|
|||||||
|
|
||||||
env := map[string]string{
|
env := map[string]string{
|
||||||
"SUBMISSION_URL": repo.URI,
|
"SUBMISSION_URL": repo.URI,
|
||||||
|
"REPO_ID": fmt.Sprintf("%d", repo.Id),
|
||||||
"META_URL": url,
|
"META_URL": url,
|
||||||
"LOGIN": login,
|
"LOGIN": login,
|
||||||
"GROUPS": groups,
|
"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)
|
result, err := client.BuildCreate(slug[0], slug[1], "", branch, env)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Unable to communicate with Drone:", err.Error())
|
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
|
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) {
|
func ClearRepositories() (int64, error) {
|
||||||
if res, err := DBExec("DELETE FROM user_work_repositories"); err != nil {
|
if res, err := DBExec("DELETE FROM user_work_repositories"); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
4
works.go
4
works.go
@ -415,6 +415,10 @@ func (u *User) GetMyWorkGrade(w *Work) (g WorkGrade, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *Work) AddGrade(grade WorkGrade) error {
|
||||||
|
return w.AddGrades([]WorkGrade{grade})
|
||||||
|
}
|
||||||
|
|
||||||
func (w *Work) AddGrades(grades []WorkGrade) error {
|
func (w *Work) AddGrades(grades []WorkGrade) error {
|
||||||
var zerotime time.Time
|
var zerotime time.Time
|
||||||
for i, g := range grades {
|
for i, g := range grades {
|
||||||
|
Reference in New Issue
Block a user