This commit is contained in:
Kasper Juul Hermansen 2022-09-18 00:10:30 +02:00
parent eef25c4c7a
commit 88430eb879
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
4 changed files with 98 additions and 17 deletions

View File

@ -122,8 +122,6 @@ func (pr *ProcessRepos) processRepo(ctx context.Context, repoUrl string, action
return err
}
//pr.gitea.CreatePr(ctx, )
pr.logger.Debug("processing done", zap.String("path", area.Path), zap.String("repoUrl", repoUrl))
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 {
_, err := pr.git.Add(ctx, area, repo)
wt, err := pr.git.Add(ctx, area, repo)
if err != nil {
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)
}
dryrun := true
dryrun := false
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)
if err != nil {
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, "/")
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
}

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"sync"
"time"
"code.gitea.io/sdk/gitea"
"go.uber.org/zap"
@ -71,17 +70,47 @@ func (g *Gitea) CreatePr(
return err
}
client.CreatePullRequest(organization, repository, gitea.CreatePullRequestOption{
prs, _, err := client.ListRepoPullRequests(organization, repository, gitea.ListPullRequestsOptions{
ListOptions: gitea.ListOptions{
Page: 0,
PageSize: 30,
},
State: gitea.StateOpen,
Sort: "recentupdate",
Milestone: 0,
})
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,
Body: "",
Assignee: "",
Assignees: []string{},
Milestone: 0,
Labels: []int64{},
Deadline: &time.Time{},
})
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
}
@ -92,8 +121,9 @@ func (g *Gitea) getOrCreateClient(ctx context.Context, server string) (*gitea.Cl
client, ok := g.giteaClients[server]
if !ok || client == nil {
c, err := gitea.NewClient(server)
c.SetBasicAuth("kjuulh", "c0bd801cc9a7f2ed559ea45d603afc92f5443f19")
if err != nil {
return nil, nil
return nil, err
}
g.giteaClients[server] = c
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}
}
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) {
g.logger.Debug(
"cloning repository",

View File

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