feature/gitea-integration #10

Merged
kjuulh merged 10 commits from feature/gitea-integration into v0.1 2022-09-18 00:10:44 +02:00
4 changed files with 98 additions and 17 deletions
Showing only changes of commit 88430eb879 - Show all commits

View File

@ -122,8 +122,6 @@ func (pr *ProcessRepos) processRepo(ctx context.Context, repoUrl string, action
return err return err
} }
//pr.gitea.CreatePr(ctx, )
pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl)) pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
return nil return nil
} }
@ -165,7 +163,7 @@ func (pr *ProcessRepos) clone(ctx context.Context, area *storage.Area, repoUrl s
} }
func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *providers.GitRepo, repoUrl string) error { func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *providers.GitRepo, repoUrl string) error {
_, err := pr.git.Add(ctx, area, repo) wt, err := pr.git.Add(ctx, area, repo)
if err != nil { if err != nil {
return fmt.Errorf("could not add file: %w", err) return fmt.Errorf("could not add file: %w", err)
} }
@ -175,8 +173,18 @@ func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *pr
return fmt.Errorf("could not get diff: %w", err) return fmt.Errorf("could not get diff: %w", err)
} }
dryrun := true dryrun := false
if !dryrun { if !dryrun {
status, err := wt.Status()
if err != nil {
return err
}
if status.IsClean() {
pr.logger.Info("Returning early, as no modifications are detected")
return nil
}
err = pr.git.Push(ctx, repo) err = pr.git.Push(ctx, repo)
if err != nil { if err != nil {
return fmt.Errorf("could not push to repo: %w", err) return fmt.Errorf("could not push to repo: %w", err)
@ -195,7 +203,22 @@ func (pr *ProcessRepos) commit(ctx context.Context, area *storage.Area, repo *pr
path := strings.Split(url.Path, "/") path := strings.Split(url.Path, "/")
pr.logger.Debug("path string", zap.Strings("paths", path), zap.String("HEAD", head)) pr.logger.Debug("path string", zap.Strings("paths", path), zap.String("HEAD", head))
pr.gitea.CreatePr(ctx, url.Host, path[0], repoUrl, head, "", "kraken-apply") org := path[0]
repoName := path[1]
semanticName, _, ok := strings.Cut(repoName, ".")
if !ok {
semanticName = repoName
}
originHead, err := pr.git.GetOriginHEADForRepo(ctx, repo)
if err != nil {
return err
}
err = pr.gitea.CreatePr(ctx, fmt.Sprintf("%s://%s", "https", url.Host), org, semanticName, head, originHead, "kraken-apply")
if err != nil {
return err
}
} }
return nil return nil
} }

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"sync" "sync"
"time"
"code.gitea.io/sdk/gitea" "code.gitea.io/sdk/gitea"
"go.uber.org/zap" "go.uber.org/zap"
@ -71,17 +70,47 @@ func (g *Gitea) CreatePr(
return err return err
} }
client.CreatePullRequest(organization, repository, gitea.CreatePullRequestOption{ prs, _, err := client.ListRepoPullRequests(organization, repository, gitea.ListPullRequestsOptions{
Head: head, ListOptions: gitea.ListOptions{
Base: base, Page: 0,
Title: actionName, PageSize: 30,
Body: "", },
Assignee: "", State: gitea.StateOpen,
Assignees: []string{}, Sort: "recentupdate",
Milestone: 0, Milestone: 0,
Labels: []int64{},
Deadline: &time.Time{},
}) })
if err != nil {
return fmt.Errorf(
"could not list repos, needed because we need to check for conflicts. Original error: %w",
err,
)
}
for _, pr := range prs {
if pr.Head.Name == head {
g.logger.Info(
"returning early from creating pull-request, as it already exists.",
zap.String("repository", repository),
zap.String("pull-request", pr.URL),
)
return nil
}
}
pr, _, err := client.CreatePullRequest(organization, repository, gitea.CreatePullRequestOption{
Head: head,
Base: base,
Title: actionName,
})
if err != nil {
return err
}
g.logger.Debug(
"Created pr",
zap.String("repository", repository),
zap.String("branch", head),
zap.String("pull-request", pr.URL),
)
return nil return nil
} }
@ -92,8 +121,9 @@ func (g *Gitea) getOrCreateClient(ctx context.Context, server string) (*gitea.Cl
client, ok := g.giteaClients[server] client, ok := g.giteaClients[server]
if !ok || client == nil { if !ok || client == nil {
c, err := gitea.NewClient(server) c, err := gitea.NewClient(server)
c.SetBasicAuth("kjuulh", "c0bd801cc9a7f2ed559ea45d603afc92f5443f19")
if err != nil { if err != nil {
return nil, nil return nil, err
} }
g.giteaClients[server] = c g.giteaClients[server] = c
return c, nil return c, nil

View File

@ -62,6 +62,34 @@ func NewGit(logger *zap.Logger, gitConfig *GitConfig, openPGP *signer.OpenPGP) *
return &Git{logger: logger, gitConfig: gitConfig, openPGP: openPGP} return &Git{logger: logger, gitConfig: gitConfig, openPGP: openPGP}
} }
func (g *Git) GetOriginHEADForRepo(ctx context.Context, gitRepo *GitRepo) (string, error) {
remote, err := gitRepo.repo.Remote("origin")
if err != nil {
return "", err
}
auth, err := g.GetAuth()
if err != nil {
return "", err
}
refs, err := remote.ListContext(ctx, &git.ListOptions{
Auth: auth,
})
if err != nil {
return "", err
}
headRef := ""
for _, ref := range refs {
if !ref.Name().IsBranch() {
headRef = ref.Target().Short()
}
}
return headRef, nil
}
func (g *Git) CloneBranch(ctx context.Context, storageArea *storage.Area, repoUrl string, branch string) (*GitRepo, error) { func (g *Git) CloneBranch(ctx context.Context, storageArea *storage.Area, repoUrl string, branch string) (*GitRepo, error) {
g.logger.Debug( g.logger.Debug(
"cloning repository", "cloning repository",

View File

@ -22,7 +22,7 @@
- [x] Allow instantiation of actions, kraken template repo etc. - [x] Allow instantiation of actions, kraken template repo etc.
- [ ] Implement docker action - [ ] Implement docker action
- [ ] Providing query results - [ ] Providing query results
- [ ] Create pr for gitea provider - [x] Create pr for gitea provider
- [ ] Think about some sort of isolation - [ ] Think about some sort of isolation
- [ ] Create CLI to trigger action - [ ] Create CLI to trigger action
- [ ] Setup pool of runners - [ ] Setup pool of runners