Distribute some traces to students
Some checks are pending
continuous-integration/drone/push Build is running
Some checks are pending
continuous-integration/drone/push Build is running
This commit is contained in:
parent
018ed9227f
commit
db5658ccc1
4 changed files with 134 additions and 1 deletions
87
works.go
87
works.go
|
|
@ -354,6 +354,12 @@ func declareAPIAdminWorksRoutes(router *gin.RouterGroup) {
|
|||
})
|
||||
}
|
||||
|
||||
type UserTrace struct {
|
||||
Title string `json:"title"`
|
||||
Status string `json:"status"`
|
||||
Logs []*drone.Line `json:"logs"`
|
||||
}
|
||||
|
||||
func declareAPIAuthWorksRoutes(router *gin.RouterGroup) {
|
||||
worksRoutes := router.Group("/works/:wid")
|
||||
worksRoutes.Use(workHandler)
|
||||
|
|
@ -380,6 +386,87 @@ func declareAPIAuthWorksRoutes(router *gin.RouterGroup) {
|
|||
}
|
||||
})
|
||||
|
||||
worksRoutes.GET("/traces", func(c *gin.Context) {
|
||||
u := c.MustGet("LoggedUser").(*User)
|
||||
w := c.MustGet("work").(*Work)
|
||||
|
||||
if !u.IsAdmin && !w.Corrected {
|
||||
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"errmsg": "Permission denied"})
|
||||
} else if g, err := u.GetMyWorkGrade(w); err != nil && errors.Is(err, sql.ErrNoRows) {
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Aucune note n'a été attribuée pour ce travail. Avez-vous rendu ce travail ?"})
|
||||
} else if err != nil {
|
||||
log.Printf("Unable to GetMyWorkGrade(uid=%d;wid=%d): %s", u.Id, w.Id, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during gradation."})
|
||||
} else {
|
||||
repo, err := u.getRepositoryByWork(g.IdWork)
|
||||
if err != nil {
|
||||
log.Printf("Unable to getRepositoryByWork(uid=%d, wid=%d): %s", u.Id, g.IdWork, err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Unable to find a corresponding repository."})
|
||||
return
|
||||
}
|
||||
|
||||
slug := strings.Split(repo.TestsRef, "/")
|
||||
if len(slug) < 3 {
|
||||
return
|
||||
}
|
||||
|
||||
buildn, err := strconv.ParseInt(slug[2], 10, 32)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
client := drone.NewClient(droneEndpoint, droneConfig)
|
||||
build, err := client.Build(slug[0], slug[1], int(buildn))
|
||||
if err != nil {
|
||||
log.Println("Unable to communicate with Drone:", err.Error())
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "Unable to communicate with the gradation service"})
|
||||
return
|
||||
}
|
||||
|
||||
var traces []UserTrace
|
||||
|
||||
for _, stage := range build.Stages {
|
||||
for _, step := range stage.Steps {
|
||||
if step.Name == "TP checks" || strings.HasPrefix(step.Name, "Test ") {
|
||||
result, err := client.Logs(slug[0], slug[1], int(buildn), stage.Number, step.Number)
|
||||
if err != nil {
|
||||
log.Printf("An error occurs when retrieving logs from Drone (%s/%s/%d/%d/%d): %s", slug[0], slug[1], buildn, stage.Number, step.Number, err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
keeptLogs := []*drone.Line{}
|
||||
for nline, line := range result {
|
||||
// Infos about image, skip
|
||||
if nline < 3 {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line.Message, "+ export GRADE") {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line.Message, "+ echo grade:") {
|
||||
line.Message = "+ Your grade for this step is:\r\n"
|
||||
}
|
||||
|
||||
keeptLogs = append(keeptLogs, line)
|
||||
}
|
||||
|
||||
traces = append(traces, UserTrace{
|
||||
Title: step.Name,
|
||||
Status: step.Status,
|
||||
Logs: keeptLogs,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if traces == nil {
|
||||
c.JSON(http.StatusOK, []interface{}{})
|
||||
} else {
|
||||
c.JSON(http.StatusOK, traces)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
declareAPIAuthRepositoriesRoutes(worksRoutes)
|
||||
declareAPIWorkSubmissionsRoutes(worksRoutes)
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue