|
| 1 | +package controllers |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "github.com/diggerhq/digger/backend/middleware" |
| 6 | + "github.com/diggerhq/digger/backend/models" |
| 7 | + orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler" |
| 8 | + "github.com/gin-gonic/gin" |
| 9 | + "gorm.io/gorm" |
| 10 | + "log" |
| 11 | + "net/http" |
| 12 | + "strconv" |
| 13 | +) |
| 14 | + |
| 15 | +func GetJobsForRepoApi(c *gin.Context) { |
| 16 | + organisationId := c.GetString(middleware.ORGANISATION_ID_KEY) |
| 17 | + organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY) |
| 18 | + repoId := c.Param("repo_id") |
| 19 | + |
| 20 | + if repoId == "" { |
| 21 | + log.Printf("missing parameter: repo_full_name") |
| 22 | + c.String(http.StatusBadRequest, "missing parameter: repo_full_name") |
| 23 | + return |
| 24 | + } |
| 25 | + |
| 26 | + var org models.Organisation |
| 27 | + err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error |
| 28 | + if err != nil { |
| 29 | + if errors.Is(err, gorm.ErrRecordNotFound) { |
| 30 | + log.Printf("could not find organisation: %v err: %v", organisationId, err) |
| 31 | + c.String(http.StatusNotFound, "Could not find organisation: "+organisationId) |
| 32 | + } else { |
| 33 | + log.Printf("could not fetch organisation: %v err: %v", organisationId, err) |
| 34 | + c.String(http.StatusNotFound, "Could not fetch organisation: "+organisationId) |
| 35 | + } |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + repo, err := models.DB.GetRepoById(org.ID, repoId) |
| 40 | + if err != nil { |
| 41 | + log.Printf("could not fetch repo details %v", err) |
| 42 | + c.String(http.StatusInternalServerError, "Unknown error occurred while fetching jobs from database") |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + jobsRes, err := models.DB.GetJobsByRepoName(org.ID, repo.RepoFullName) |
| 47 | + if err != nil { |
| 48 | + log.Printf("could not fetch job details") |
| 49 | + c.String(http.StatusInternalServerError, "Unknown error occurred while fetching jobs from database") |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + // update the values of "status" accordingly |
| 54 | + for i, j := range jobsRes { |
| 55 | + statusInt, err := strconv.Atoi(j.Status) |
| 56 | + if err != nil { |
| 57 | + log.Printf("could not convert status to string: job id: %v status: %v", j.ID, j.Status) |
| 58 | + continue |
| 59 | + } |
| 60 | + statusI := orchestrator_scheduler.DiggerJobStatus(statusInt) |
| 61 | + jobsRes[i].Status = statusI.ToString() |
| 62 | + } |
| 63 | + |
| 64 | + response := make(map[string]interface{}) |
| 65 | + response["repo"] = repo |
| 66 | + response["jobs"] = jobsRes |
| 67 | + |
| 68 | + c.JSON(http.StatusOK, response) |
| 69 | +} |
0 commit comments