Skip to content

Commit 903c12c

Browse files
authored
repo details page (#1888)
* add endpoints for repo details
1 parent 889539f commit 903c12c

File tree

6 files changed

+202
-2
lines changed

6 files changed

+202
-2
lines changed

backend/bootstrap/main.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,12 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController
220220

221221
reposApiGroup := apiGroup.Group("/repos")
222222
reposApiGroup.GET("/", controllers.ListReposApi)
223+
reposApiGroup.GET("/:repo_id/jobs", controllers.GetJobsForRepoApi)
223224

224225
githubApiGroup := apiGroup.Group("/github")
225226
githubApiGroup.POST("/link", controllers.LinkGithubInstallationToOrgApi)
226227
}
227228

228-
fronteggWebhookProcessor.POST("/create-org-from-frontegg", controllers.CreateFronteggOrgFromWebhook)
229-
230229
return r
231230
}
232231

backend/controllers/dashboard.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package controllers
2+
3+
import (
4+
"errors"
5+
"github.com/diggerhq/digger/backend/middleware"
6+
"github.com/diggerhq/digger/backend/models"
7+
"github.com/gin-gonic/gin"
8+
"gorm.io/gorm"
9+
"log"
10+
"net/http"
11+
)
12+
13+
func GetDashboardStatusApi(c *gin.Context) {
14+
organisationId := c.GetString(middleware.ORGANISATION_ID_KEY)
15+
organisationSource := c.GetString(middleware.ORGANISATION_SOURCE_KEY)
16+
17+
var org models.Organisation
18+
err := models.DB.GormDB.Where("external_id = ? AND external_source = ?", organisationId, organisationSource).First(&org).Error
19+
if err != nil {
20+
if errors.Is(err, gorm.ErrRecordNotFound) {
21+
c.String(http.StatusNotFound, "Could not find organisation: "+organisationId)
22+
} else {
23+
log.Printf("could not fetch organisation: %v err: %v", organisationId, err)
24+
c.String(http.StatusNotFound, "Could not fetch organisation: "+organisationId)
25+
}
26+
return
27+
}
28+
29+
response := make(map[string]interface{})
30+
31+
c.JSON(http.StatusOK, response)
32+
}

backend/controllers/jobs.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
}

backend/models/queries.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package models
2+
3+
import "time"
4+
5+
type JobQueryResult struct {
6+
ID uint `gorm:"column:id"`
7+
CreatedAt time.Time `gorm:"column:created_at"`
8+
UpdatedAt time.Time `gorm:"column:updated_at"`
9+
DeletedAt *time.Time `gorm:"column:deleted_at"`
10+
DiggerJobID string `gorm:"column:digger_job_id"`
11+
Status string `gorm:"column:status"`
12+
WorkflowRunURL string `gorm:"column:workflow_run_url"`
13+
WorkflowFile string `gorm:"column:workflow_file"`
14+
TerraformOutput string `gorm:"column:terraform_output"`
15+
PRNumber int `gorm:"column:pr_number"`
16+
RepoFullName string `gorm:"column:repo_full_name"`
17+
BranchName string `gorm:"column:branch_name"`
18+
}

backend/models/storage.go

+21
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,27 @@ func (db *Database) GetDiggerJobsForBatch(batchId uuid.UUID) ([]DiggerJob, error
924924
return jobs, nil
925925
}
926926

927+
func (db *Database) GetJobsByRepoName(orgId uint, repoFullName string) ([]JobQueryResult, error) {
928+
var results []JobQueryResult
929+
930+
query := `
931+
SELECT
932+
j.id, j.created_at, j.updated_at, j.deleted_at,
933+
j.digger_job_id, j.status, j.workflow_run_url,
934+
j.workflow_file, j.terraform_output, db.pr_number, db.repo_full_name, db.branch_name
935+
FROM digger_jobs j, digger_batches db, organisations o, github_app_installation_links l
936+
WHERE o.id = l.organisation_id
937+
AND l.github_installation_id = db.github_installation_id
938+
AND db.id = j.batch_id
939+
AND o.id = ?
940+
AND db.repo_full_name = ?
941+
ORDER BY j.created_at
942+
`
943+
944+
err := db.GormDB.Raw(query, orgId, repoFullName).Scan(&results).Error
945+
return results, err
946+
}
947+
927948
func (db *Database) GetDiggerJobsForBatchWithStatus(batchId uuid.UUID, status []scheduler.DiggerJobStatus) ([]DiggerJob, error) {
928949
jobs := make([]DiggerJob, 0)
929950

backend/models/storage_dashboard.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package models
2+
3+
import (
4+
"github.com/diggerhq/digger/libs/scheduler"
5+
"time"
6+
)
7+
8+
func (db *Database) GetRepoCount(orgID uint) (int64, error) {
9+
var count int64
10+
11+
thirtyDaysAgo := time.Now().AddDate(0, 0, -30)
12+
13+
result := db.GormDB.Model(&Repo{}).
14+
Where("organisation_id = ? AND created_at >= ?", orgID, thirtyDaysAgo).
15+
Count(&count)
16+
17+
if result.Error != nil {
18+
return 0, result.Error
19+
}
20+
21+
return count, nil
22+
}
23+
24+
func (db *Database) GetJobsCountThisMonth(orgID uint) (int64, error) {
25+
var count int64
26+
27+
thirtyDaysAgo := time.Now().AddDate(0, 0, -30)
28+
29+
result := db.GormDB.Model(DiggerJob{}).
30+
Joins("JOIN digger_batches ON digger_jobs.batch_id = digger_batches.id").
31+
Joins("JOIN github_app_installation_links ON digger_batches.github_installation_id = github_app_installation_links.github_installation_id").
32+
Joins("JOIN organisations ON github_app_installation_links.organisation_id = organisations.id").
33+
Where("digger_jobs.created_at >= ? AND organisations.id = ?", thirtyDaysAgo, orgID).
34+
Count(&count)
35+
36+
if result.Error != nil {
37+
return 0, result.Error
38+
}
39+
40+
return count, nil
41+
}
42+
43+
func (db *Database) GetSuccessfulJobsCountThisMonth(orgID uint) (int64, error) {
44+
var count int64
45+
46+
thirtyDaysAgo := time.Now().AddDate(0, 0, -30)
47+
48+
result := db.GormDB.Model(DiggerJob{}).
49+
Joins("JOIN digger_batches ON digger_jobs.batch_id = digger_batches.id").
50+
Joins("JOIN github_app_installation_links ON digger_batches.github_installation_id = github_app_installation_links.github_installation_id").
51+
Joins("JOIN organisations ON github_app_installation_links.organisation_id = organisations.id").
52+
Where("digger_jobs.created_at >= ? AND organisations.id = ?", thirtyDaysAgo, orgID).
53+
Where("digger_jobs.status = ?", scheduler.DiggerJobSucceeded).
54+
Count(&count)
55+
56+
if result.Error != nil {
57+
return 0, result.Error
58+
}
59+
60+
return count, nil
61+
}

0 commit comments

Comments
 (0)