Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions apps/container-registry/internal/app/adapter-github.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/kloudlite/api/pkg/errors"
"github.com/kloudlite/api/pkg/logging"

// fn "kloudlite.io/pkg/functions"
"github.com/kloudlite/api/pkg/types"
)
Expand Down Expand Up @@ -175,16 +176,12 @@ func (gh *githubI) GetLatestCommit(ctx context.Context, accToken *entities.Acces
return "", errors.NewE(err)
}

inst, _, err := gh.ghCli.Apps.FindRepositoryInstallation(ctx, owner, repo)
if err != nil {
return "", errors.NewEf(err, "could not fetch repository installation")
}
installationId := *inst.ID
it, _, err := gh.ghCli.Apps.CreateInstallationToken(ctx, installationId, &github.InstallationTokenOptions{})
rc, _, err := gh.ghCliForUser(ctx, accToken.Token).Repositories.GetCommit(ctx, owner, repo, branchName, &github.ListOptions{})
if err != nil {
return "", errors.NewEf(err, "failed to get installation token")
return "", errors.NewEf(err, "could not get latest commit")
}
return it.GetToken(), errors.NewE(err)

return rc.GetSHA(), nil
}

// GetToken implements domain.Github.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func processGitWebhooks(ctx context.Context, d domain.Domain, consumer GitWebhoo
case constants.ProviderGithub:
pullToken, err = d.GithubInstallationToken(ctx, hook.RepoUrl)
if err != nil {
fmt.Println(err)
logger.Warnf("could not get pull token for build, Error: %s", err.Error())
return errors.NewE(err)
}

Expand Down
78 changes: 76 additions & 2 deletions apps/container-registry/internal/domain/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package domain

import (
"context"
"slices"

"github.com/kloudlite/api/apps/container-registry/internal/domain/entities"
fc "github.com/kloudlite/api/apps/container-registry/internal/domain/entities/field-constants"
Expand Down Expand Up @@ -197,7 +198,7 @@ func (d *Impl) DeleteBuild(ctx RegistryContext, buildId repos.ID) error {
ResourceRefs: []string{
iamT.NewResourceRef(ctx.AccountName, iamT.ResourceAccount, ctx.AccountName),
},
Action: string(iamT.UpdateAccount),
Action: string(iamT.GetAccount),
})

if err != nil {
Expand Down Expand Up @@ -235,5 +236,78 @@ func (d *Impl) DeleteBuild(ctx RegistryContext, buildId repos.ID) error {
}

func (d *Impl) TriggerBuild(ctx RegistryContext, buildId repos.ID) error {
panic("implement me")

co, err := d.iamClient.Can(ctx, &iam.CanIn{
UserId: string(ctx.UserId),
ResourceRefs: []string{
iamT.NewResourceRef(ctx.AccountName, iamT.ResourceAccount, ctx.AccountName),
},
Action: string(iamT.GetAccount),
})

if err != nil {
return errors.NewE(err)
}

if !co.Status {
return errors.Newf("unauthorized to trigger build")
}

b, err := d.buildRepo.FindById(ctx, buildId)
if err != nil {
return errors.NewE(err)
}
if b == nil {
return errors.Newf("build not found")
}

var pullToken string
var commitHash string

if !slices.Contains([]string{"github", "gitlab"}, string(b.Source.Provider)) {
return errors.Newf("provider %s not supported", b.Source.Provider)
}

at, err := d.getAccessTokenByUserId(ctx, string(b.Source.Provider), ctx.UserId)
if err != nil {
return errors.NewE(err)
}

switch b.Source.Provider {
case "gitlab":
pullToken, err = d.GitlabPullToken(ctx, ctx.UserId)
if err != nil {
return errors.NewE(err)
}

commitHash, err = d.gitlab.GetLatestCommit(ctx, at, b.Source.Repository, b.Source.Branch)
if err != nil {
return errors.NewE(err)
}

case "github":

pullToken, err = d.GithubInstallationToken(ctx, b.Source.Repository)
if err != nil {
return errors.NewE(err)
}

commitHash, err = d.github.GetLatestCommit(ctx, at, b.Source.Repository, b.Source.Branch)
if err != nil {
return errors.NewE(err)
}
default:
return errors.Newf("provider %s not supported", b.Source.Provider)
}

if err := d.CreateBuildRun(ctx, b, &GitWebhookPayload{
GitProvider: string(b.Source.Provider),
RepoUrl: b.Source.Repository,
GitBranch: b.Source.Branch,
CommitHash: commitHash,
}, pullToken); err != nil {
return errors.NewE(err)
}

return nil
}