2024-07-07 21:08:41 +02:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2024-07-09 23:22:42 +02:00
|
|
|
"errors"
|
2024-07-10 17:20:23 +02:00
|
|
|
"fmt"
|
2024-07-07 21:08:41 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-07-10 17:20:23 +02:00
|
|
|
"strings"
|
2024-07-07 21:08:41 +02:00
|
|
|
|
|
|
|
transport "github.com/go-openapi/runtime/client"
|
|
|
|
apiclient "src.opensuse.org/autogits/common/gitea-generated/client"
|
|
|
|
"src.opensuse.org/autogits/common/gitea-generated/client/organization"
|
|
|
|
"src.opensuse.org/autogits/common/gitea-generated/client/repository"
|
|
|
|
"src.opensuse.org/autogits/common/gitea-generated/models"
|
|
|
|
)
|
|
|
|
|
2024-07-11 16:45:49 +02:00
|
|
|
const PrPattern = "PR: %s/%s#%d"
|
|
|
|
|
2024-07-10 17:20:23 +02:00
|
|
|
func (h *RequestHandler) allocateGiteaTransport() (*transport.Runtime, *apiclient.GiteaAPI) {
|
2024-07-09 23:22:42 +02:00
|
|
|
r := transport.New("src.opensuse.org", apiclient.DefaultBasePath, [](string){"https"})
|
2024-07-10 11:25:00 +02:00
|
|
|
r.DefaultAuthentication = transport.BearerToken(giteaToken)
|
2024-07-10 11:18:33 +02:00
|
|
|
r.SetDebug(true)
|
2024-07-10 17:20:23 +02:00
|
|
|
|
|
|
|
return r, apiclient.New(r, nil)
|
2024-07-09 23:22:42 +02:00
|
|
|
}
|
|
|
|
|
2024-07-07 21:08:41 +02:00
|
|
|
func (h *RequestHandler) CreateRepositoryIfNotExist(org Organization, repoName string) *models.Repository {
|
|
|
|
if h.HasError() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-10 17:20:23 +02:00
|
|
|
transport, client := h.allocateGiteaTransport()
|
2024-07-07 21:08:41 +02:00
|
|
|
repo, err := client.Repository.RepoGet(
|
|
|
|
repository.NewRepoGetParams().WithDefaults().WithOwner(org.Username).WithRepo(repoName),
|
2024-07-09 23:22:42 +02:00
|
|
|
transport.DefaultAuthentication)
|
2024-07-07 21:08:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case *repository.RepoGetNotFound:
|
|
|
|
h.Log("repo '%s' does not exist. Trying to create it ....", repoName)
|
|
|
|
|
|
|
|
repo, err := client.Organization.CreateOrgRepo(
|
|
|
|
organization.NewCreateOrgRepoParams().WithDefaults().WithBody(
|
|
|
|
&models.CreateRepoOption{
|
|
|
|
AutoInit: false,
|
|
|
|
Name: &repoName,
|
|
|
|
ObjectFormatName: models.CreateRepoOptionObjectFormatNameSha256,
|
|
|
|
},
|
|
|
|
).WithOrg(org.Username),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case *organization.CreateOrgRepoCreated:
|
|
|
|
h.Log("repo '%s' created, with notification error?", repoName)
|
|
|
|
default:
|
|
|
|
h.Error = err
|
|
|
|
h.LogError("error creating repo '%s' under '%s': %s", repoName, org.Username, err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
h.Log("repo '%s' created", repoName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize repository
|
|
|
|
h.Error = os.Mkdir(filepath.Join(h.GitPath, DefaultGitPrj), 0700)
|
|
|
|
if h.HasError() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
h.GitExec(DefaultGitPrj, "init", "--object-format="+repo.Payload.ObjectFormatName)
|
|
|
|
h.GitExec(DefaultGitPrj, "checkout", "-b", repo.Payload.DefaultBranch)
|
|
|
|
if h.HasError() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
readmeFilename := filepath.Join(h.GitPath, DefaultGitPrj, "README.md")
|
|
|
|
{
|
|
|
|
file, _ := os.Create(readmeFilename)
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
io.WriteString(file, ReadmeBoilerplate)
|
|
|
|
}
|
|
|
|
h.GitExec(DefaultGitPrj, "add", "README.md")
|
|
|
|
h.GitExec(DefaultGitPrj, "commit", "-m", "Automatic devel project creation")
|
|
|
|
h.GitExec(DefaultGitPrj, "remote", "add", "origin", repo.Payload.SSHURL)
|
|
|
|
|
|
|
|
return repo.Payload
|
|
|
|
default:
|
|
|
|
h.Error = err
|
|
|
|
h.LogError("cannot fetch repo data for '%s' / '%s' : %w", org.Username, repoName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.Payload
|
|
|
|
}
|
|
|
|
|
2024-07-10 11:41:34 +02:00
|
|
|
func (h *RequestHandler) CreatePullRequest(repo *models.Repository, srcId, targetId, title, body string) *models.PullRequest {
|
2024-07-09 23:22:42 +02:00
|
|
|
if h.HasError() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-10 17:20:23 +02:00
|
|
|
transport, client := h.allocateGiteaTransport()
|
2024-07-09 23:22:42 +02:00
|
|
|
|
2024-07-10 11:41:34 +02:00
|
|
|
prOptions := models.CreatePullRequestOption{
|
|
|
|
Base: repo.DefaultBranch,
|
|
|
|
Head: srcId,
|
|
|
|
Title: title,
|
|
|
|
Body: body,
|
|
|
|
}
|
2024-07-09 23:22:42 +02:00
|
|
|
|
|
|
|
pr, err := client.Repository.RepoCreatePullRequest(
|
|
|
|
repository.
|
|
|
|
NewRepoCreatePullRequestParams().
|
|
|
|
WithDefaults().
|
|
|
|
WithOwner(repo.Owner.UserName).
|
|
|
|
WithRepo(repo.Name).
|
|
|
|
WithBody(&prOptions),
|
|
|
|
transport.DefaultAuthentication,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.LogError("Cannot create pull request: %v", err)
|
|
|
|
h.Error = err
|
2024-07-10 11:18:33 +02:00
|
|
|
return nil
|
2024-07-09 23:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !pr.IsSuccess() {
|
|
|
|
h.LogError("PR creation failed: %s", pr.Error())
|
|
|
|
h.Error = errors.New(pr.Error())
|
2024-07-10 11:18:33 +02:00
|
|
|
return nil
|
2024-07-09 23:22:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return pr.GetPayload()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RequestHandler) RequestReviews(pr *models.PullRequest, reviewer string) []*models.PullReview {
|
|
|
|
if h.HasError() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-10 17:20:23 +02:00
|
|
|
transport, client := h.allocateGiteaTransport()
|
2024-07-09 23:22:42 +02:00
|
|
|
|
2024-07-10 11:41:34 +02:00
|
|
|
reviewOptions := models.PullReviewRequestOptions{
|
|
|
|
Reviewers: []string{reviewer},
|
|
|
|
}
|
2024-07-09 23:22:42 +02:00
|
|
|
|
|
|
|
review, err := client.Repository.RepoCreatePullReviewRequests(
|
|
|
|
repository.
|
|
|
|
NewRepoCreatePullReviewRequestsParams().
|
|
|
|
WithOwner(pr.Base.Repo.Owner.UserName).
|
|
|
|
WithRepo(pr.Base.Repo.Name).
|
|
|
|
WithIndex(pr.Index).
|
|
|
|
WithBody(&reviewOptions),
|
|
|
|
transport.DefaultAuthentication,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
h.LogError("Cannot create pull request: %v", err)
|
|
|
|
h.Error = err
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !review.IsSuccess() {
|
|
|
|
h.LogError("PR creation failed: %s", review.Error())
|
|
|
|
h.Error = errors.New(review.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return review.GetPayload()
|
|
|
|
}
|
2024-07-10 17:20:23 +02:00
|
|
|
|
2024-07-10 20:41:24 +02:00
|
|
|
func (h *RequestHandler) GetAssociatedPrjGitPR(pr *PullRequestAction) *models.PullRequest {
|
2024-07-10 17:20:23 +02:00
|
|
|
if h.HasError() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
transport, client := h.allocateGiteaTransport()
|
|
|
|
|
|
|
|
var page, maxSize int64;
|
|
|
|
page = 1
|
|
|
|
maxSize = 10000
|
|
|
|
state := "open"
|
|
|
|
prs, err := client.Repository.RepoListPullRequests(
|
|
|
|
repository.
|
|
|
|
NewRepoListPullRequestsParams().
|
|
|
|
WithDefaults().
|
2024-07-10 20:41:24 +02:00
|
|
|
WithOwner(pr.Repository.Owner.Username).
|
2024-07-10 17:20:23 +02:00
|
|
|
WithRepo(DefaultGitPrj).
|
|
|
|
WithState(&state).
|
|
|
|
WithLimit(&maxSize).
|
|
|
|
WithPage(&page),
|
|
|
|
transport.DefaultAuthentication)
|
|
|
|
|
|
|
|
if err != nil {
|
2024-07-10 20:41:24 +02:00
|
|
|
h.Error = fmt.Errorf("cannot fetch PR list for %s / %s : %v", pr.Repository.Owner.Username, pr.Repository.Name, err)
|
2024-07-10 17:20:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !prs.IsSuccess() {
|
2024-07-10 20:41:24 +02:00
|
|
|
h.Error = fmt.Errorf("cannot fetch PR list for %s / %s : %s", pr.Repository.Owner.Username, pr.Repository.Name, prs.Error())
|
2024-07-10 17:20:23 +02:00
|
|
|
}
|
|
|
|
|
2024-07-11 16:45:49 +02:00
|
|
|
prLine := fmt.Sprintf(PrPattern, pr.Repository.Owner.Username, pr.Repository.Name, pr.Number)
|
2024-07-15 20:53:16 +02:00
|
|
|
h.Log("attemping to match line: '%s'", prLine)
|
2024-07-11 16:45:49 +02:00
|
|
|
|
|
|
|
// payload_processing:
|
2024-07-10 17:20:23 +02:00
|
|
|
for _, pr := range prs.Payload {
|
|
|
|
lines := strings.Split(pr.Body, "\n")
|
|
|
|
|
|
|
|
for _, line := range lines {
|
2024-07-11 16:45:49 +02:00
|
|
|
if strings.TrimSpace(line) == prLine {
|
|
|
|
return pr
|
2024-07-10 17:20:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|