Skip to content

Commit

Permalink
feat/projects in ui (#1639)
Browse files Browse the repository at this point in the history
* fix of projects list
  • Loading branch information
motatoes authored Jul 27, 2024
1 parent 4d68af8 commit abb8db1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 12 deletions.
3 changes: 3 additions & 0 deletions backend/utils/allowlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func ExtractCleanRepoName(gitlabURL string) (string, error) {

func IsInRepoAllowList(repoUrl string) bool {
allowList := os.Getenv("DIGGER_REPO_ALLOW_LIST")
if allowList == "" {
return true
}
allowedReposUrls := strings.Split(allowList, ",")
// gitlab.com/diggerhq/test
// https://gitlab.com/diggerhq/test
Expand Down
77 changes: 71 additions & 6 deletions ee/backend/controllers/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,19 +93,84 @@ func (d DiggerEEController) GitlabWebHookHandler(c *gin.Context) {
}
case *gitlab.PushEvent:
log.Printf("Got push event for %v %v", event.Project.URL, event.Ref)
//err := handlePushEvent(gh, event)
//if err != nil {
// log.Printf("handlePushEvent error: %v", err)
// c.String(http.StatusInternalServerError, err.Error())
// return
//}
err := handlePushEvent(d.GitlabProvider, event, organisationId)
if err != nil {
log.Printf("handlePushEvent error: %v", err)
c.String(http.StatusInternalServerError, err.Error())
return
}
default:
log.Printf("Unhandled event, event type %v", reflect.TypeOf(event))
}

c.JSON(200, "ok")
}

func handlePushEvent(gh utils.GitlabProvider, payload *gitlab.PushEvent, organisationId uint) error {
//projectId := payload.Project.ID
repoFullName := payload.Project.PathWithNamespace
repoOwner, repoName, _ := strings.Cut(repoFullName, "/")
cloneURL := payload.Project.GitHTTPURL
webURL := payload.Project.WebURL
ref := payload.Ref
defaultBranch := payload.Project.DefaultBranch

pushBranch := ""
if strings.HasPrefix(ref, "refs/heads/") {
pushBranch = strings.TrimPrefix(ref, "refs/heads/")
} else {
log.Printf("push was not to a branch, ignoring %v", ref)
return nil
}

diggerRepoName := strings.ReplaceAll(repoFullName, "/", "-")
//repo, err := models.DB.GetRepo(organisationId, diggerRepoName)
//if err != nil {
// log.Printf("Error getting Repo: %v", err)
// return fmt.Errorf("error getting github app link")
//}
// create repo if not exists
org, err := models.DB.GetOrganisationById(organisationId)
if err != nil {
log.Printf("Error: could not get organisation: %v", err)
return nil
}

repo, err := models.DB.CreateRepo(diggerRepoName, repoFullName, repoOwner, repoName, webURL, org, "")
if err != nil {
log.Printf("Error: could not create repo: %v", err)
return nil
}

token := os.Getenv("DIGGER_GITLAB_ACCESS_TOKEN")
if token == "" {
log.Printf("could not find gitlab token: %v", err)
return fmt.Errorf("could not find gitlab token")
}

var isMainBranch bool
if strings.HasSuffix(ref, defaultBranch) {
isMainBranch = true
} else {
isMainBranch = false
}

err = utils.CloneGitRepoAndDoAction(cloneURL, pushBranch, token, func(dir string) error {
config, err := dg_configuration.LoadDiggerConfigYaml(dir, true, nil)
if err != nil {
log.Printf("ERROR load digger.yml: %v", err)
return fmt.Errorf("error loading digger.yml %v", err)
}
models.DB.UpdateRepoDiggerConfig(organisationId, *config, repo, isMainBranch)
return nil
})
if err != nil {
return fmt.Errorf("error while cloning repo: %v", err)
}

return nil
}

func GetGitlabRepoUrl(event interface{}) string {
var repoUrl = ""
switch event := event.(type) {
Expand Down
8 changes: 3 additions & 5 deletions ee/backend/templates/projects.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@
<th>Name</th>
<th>Repo</th>
<th>Organisation</th>
<th>Status</th>
<th>Details</th>
<th>Commited To Main?</th>
</tr>
</thead>
<tbody>
{{ range .Projects }}
<tr>
<td>{{ .Name }}</td>
<td><a href="/repo/{{.Repo.ID}}/">{{ .Repo.Name }}<a></td>
<td><a href="{{.Repo.RepoUrl}}/">{{ .Repo.RepoFullName }}<a></td>
<td>{{ .Organisation.Name }}</td>
<td>{{ .Status }}</td>
<td><a href="/projects/{{.ID}}/details">Details<a></td>
<td>{{ .IsInMainBranch }}</td>
</tr>
{{ end }}
</tbody>
Expand Down
1 change: 0 additions & 1 deletion ee/backend/templates/top.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
<li class="nav-item"><a class="nav-link" href="/projects"><i class="fas fa-project-diagram"></i><span>Projects</span></a></li>
<li class="nav-item"><a class="nav-link" href="/repos"><i class="fas fa-code-branch"></i><span>Repos</span></a></li>
<li class="nav-item"><a class="nav-link" href="/runs"><i class="fas fa-tasks"></i><span>Runs</span></a></li>
<li class="nav-item"><a class="nav-link active" href="/"><i class="fas fa-user"></i><span>Profile</span></a></li>
</ul>
<div class="text-center d-none d-md-inline"><button class="btn rounded-circle border-0" id="sidebarToggle" type="button"></button></div>
</div>
Expand Down

0 comments on commit abb8db1

Please sign in to comment.