Skip to content

Commit abb8db1

Browse files
authored
feat/projects in ui (#1639)
* fix of projects list
1 parent 4d68af8 commit abb8db1

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

backend/utils/allowlist.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ func ExtractCleanRepoName(gitlabURL string) (string, error) {
3636

3737
func IsInRepoAllowList(repoUrl string) bool {
3838
allowList := os.Getenv("DIGGER_REPO_ALLOW_LIST")
39+
if allowList == "" {
40+
return true
41+
}
3942
allowedReposUrls := strings.Split(allowList, ",")
4043
// gitlab.com/diggerhq/test
4144
// https://gitlab.com/diggerhq/test

ee/backend/controllers/gitlab.go

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,84 @@ func (d DiggerEEController) GitlabWebHookHandler(c *gin.Context) {
9393
}
9494
case *gitlab.PushEvent:
9595
log.Printf("Got push event for %v %v", event.Project.URL, event.Ref)
96-
//err := handlePushEvent(gh, event)
97-
//if err != nil {
98-
// log.Printf("handlePushEvent error: %v", err)
99-
// c.String(http.StatusInternalServerError, err.Error())
100-
// return
101-
//}
96+
err := handlePushEvent(d.GitlabProvider, event, organisationId)
97+
if err != nil {
98+
log.Printf("handlePushEvent error: %v", err)
99+
c.String(http.StatusInternalServerError, err.Error())
100+
return
101+
}
102102
default:
103103
log.Printf("Unhandled event, event type %v", reflect.TypeOf(event))
104104
}
105105

106106
c.JSON(200, "ok")
107107
}
108108

109+
func handlePushEvent(gh utils.GitlabProvider, payload *gitlab.PushEvent, organisationId uint) error {
110+
//projectId := payload.Project.ID
111+
repoFullName := payload.Project.PathWithNamespace
112+
repoOwner, repoName, _ := strings.Cut(repoFullName, "/")
113+
cloneURL := payload.Project.GitHTTPURL
114+
webURL := payload.Project.WebURL
115+
ref := payload.Ref
116+
defaultBranch := payload.Project.DefaultBranch
117+
118+
pushBranch := ""
119+
if strings.HasPrefix(ref, "refs/heads/") {
120+
pushBranch = strings.TrimPrefix(ref, "refs/heads/")
121+
} else {
122+
log.Printf("push was not to a branch, ignoring %v", ref)
123+
return nil
124+
}
125+
126+
diggerRepoName := strings.ReplaceAll(repoFullName, "/", "-")
127+
//repo, err := models.DB.GetRepo(organisationId, diggerRepoName)
128+
//if err != nil {
129+
// log.Printf("Error getting Repo: %v", err)
130+
// return fmt.Errorf("error getting github app link")
131+
//}
132+
// create repo if not exists
133+
org, err := models.DB.GetOrganisationById(organisationId)
134+
if err != nil {
135+
log.Printf("Error: could not get organisation: %v", err)
136+
return nil
137+
}
138+
139+
repo, err := models.DB.CreateRepo(diggerRepoName, repoFullName, repoOwner, repoName, webURL, org, "")
140+
if err != nil {
141+
log.Printf("Error: could not create repo: %v", err)
142+
return nil
143+
}
144+
145+
token := os.Getenv("DIGGER_GITLAB_ACCESS_TOKEN")
146+
if token == "" {
147+
log.Printf("could not find gitlab token: %v", err)
148+
return fmt.Errorf("could not find gitlab token")
149+
}
150+
151+
var isMainBranch bool
152+
if strings.HasSuffix(ref, defaultBranch) {
153+
isMainBranch = true
154+
} else {
155+
isMainBranch = false
156+
}
157+
158+
err = utils.CloneGitRepoAndDoAction(cloneURL, pushBranch, token, func(dir string) error {
159+
config, err := dg_configuration.LoadDiggerConfigYaml(dir, true, nil)
160+
if err != nil {
161+
log.Printf("ERROR load digger.yml: %v", err)
162+
return fmt.Errorf("error loading digger.yml %v", err)
163+
}
164+
models.DB.UpdateRepoDiggerConfig(organisationId, *config, repo, isMainBranch)
165+
return nil
166+
})
167+
if err != nil {
168+
return fmt.Errorf("error while cloning repo: %v", err)
169+
}
170+
171+
return nil
172+
}
173+
109174
func GetGitlabRepoUrl(event interface{}) string {
110175
var repoUrl = ""
111176
switch event := event.(type) {

ee/backend/templates/projects.tmpl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@
1616
<th>Name</th>
1717
<th>Repo</th>
1818
<th>Organisation</th>
19-
<th>Status</th>
20-
<th>Details</th>
19+
<th>Commited To Main?</th>
2120
</tr>
2221
</thead>
2322
<tbody>
2423
{{ range .Projects }}
2524
<tr>
2625
<td>{{ .Name }}</td>
27-
<td><a href="/repo/{{.Repo.ID}}/">{{ .Repo.Name }}<a></td>
26+
<td><a href="{{.Repo.RepoUrl}}/">{{ .Repo.RepoFullName }}<a></td>
2827
<td>{{ .Organisation.Name }}</td>
29-
<td>{{ .Status }}</td>
30-
<td><a href="/projects/{{.ID}}/details">Details<a></td>
28+
<td>{{ .IsInMainBranch }}</td>
3129
</tr>
3230
{{ end }}
3331
</tbody>

ee/backend/templates/top.tmpl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
<li class="nav-item"><a class="nav-link" href="/projects"><i class="fas fa-project-diagram"></i><span>Projects</span></a></li>
2929
<li class="nav-item"><a class="nav-link" href="/repos"><i class="fas fa-code-branch"></i><span>Repos</span></a></li>
3030
<li class="nav-item"><a class="nav-link" href="/runs"><i class="fas fa-tasks"></i><span>Runs</span></a></li>
31-
<li class="nav-item"><a class="nav-link active" href="/"><i class="fas fa-user"></i><span>Profile</span></a></li>
3231
</ul>
3332
<div class="text-center d-none d-md-inline"><button class="btn rounded-circle border-0" id="sidebarToggle" type="button"></button></div>
3433
</div>

0 commit comments

Comments
 (0)