-
Notifications
You must be signed in to change notification settings - Fork 563
/
Copy pathcache.go
94 lines (79 loc) · 2.73 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package controllers
import (
"fmt"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/backend/utils"
dg_configuration "github.com/diggerhq/digger/libs/digger_config"
"github.com/gin-gonic/gin"
"log"
"net/http"
"os"
"path"
"strings"
)
func (d DiggerController) UpdateRepoCache(c *gin.Context) {
type UpdateCacheRequest struct {
RepoFullName string `json:"repo_full_name"`
Branch string `json:"branch"`
OrgId uint `json:"org_id"`
InstallationId int64 `json:"installation_id"`
}
var request UpdateCacheRequest
err := c.BindJSON(&request)
if err != nil {
log.Printf("Error binding JSON: %v", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error binding JSON"})
return
}
repoFullName := request.RepoFullName
installationId := request.InstallationId
link, err := models.DB.GetGithubAppInstallationLink(installationId)
if err != nil {
log.Printf("could not installation link: %v", err)
c.String(500, fmt.Sprintf("coulnt not find installation link %v %v", repoFullName, installationId))
return
}
orgId := link.OrganisationId
log.Printf("the org id is %v", orgId)
repoOwner, repoName, _ := strings.Cut(repoFullName, "/")
repoDiggerName := strings.ReplaceAll(repoFullName, "/", "-")
repo, err := models.DB.GetRepo(orgId, repoDiggerName)
if err != nil {
log.Printf("could not get repo: %v", err)
c.String(500, fmt.Sprintf("coulnt not get repository %v %v", repoFullName, orgId))
return
}
cloneUrl := fmt.Sprintf("https://%v/%v", utils.GetGithubHostname(), repo.RepoFullName)
branch := request.Branch
_, token, err := utils.GetGithubService(d.GithubClientProvider, installationId, repoFullName, repoOwner, repoName)
if err != nil {
log.Printf("could not get github service :%v", err)
c.String(500, fmt.Sprintf("could not get github service %v %v", repoFullName, orgId))
return
}
var diggerYmlStr string
var config *dg_configuration.DiggerConfig
// update the cache here, do it async for immediate response
go func() {
err = utils.CloneGitRepoAndDoAction(cloneUrl, branch, "", *token, "", func(dir string) error {
diggerYmlBytes, err := os.ReadFile(path.Join(dir, "digger.yml"))
diggerYmlStr = string(diggerYmlBytes)
config, _, _, err = dg_configuration.LoadDiggerConfig(dir, true, nil)
if err != nil {
log.Printf("Error loading digger config: %v", err)
return err
}
return nil
})
if err != nil {
log.Printf("could not load digger config :%v", err)
return
}
_, err = models.DB.UpsertRepoCache(orgId, repoFullName, diggerYmlStr, *config)
if err != nil {
log.Printf("could upadate repo cache :%v", err)
return
}
}()
c.String(200, "successfully submitted cache for processing, check backend logs for progress")
}