新增:合并请求版本管理任务

This commit is contained in:
yystopf 2023-01-10 19:05:35 +08:00
parent a129d256ea
commit 7ebd069424
3 changed files with 122 additions and 0 deletions

43
models/issues/pull.go Normal file
View File

@ -0,0 +1,43 @@
package issues
import (
"context"
"time"
"code.gitea.io/gitea/models/db"
gitea_issues_models "code.gitea.io/gitea/models/issues"
"code.gitlink.org.cn/Gitlink/gitea_hat.git/models/pull"
)
type PullRequestHatStatus int
// Enumerate all the pull request hat status
const (
PullRequestHatStatusConflict PullRequestHatStatus = iota
PullRequestHatStatusChecking
PullRequestHatStatusMergeable
)
type PullRequest struct {
gitea_issues_models.PullRequest
LatestPullRequestVersion *pull.PullRequestVersion
}
func GetPullRequestIDsByIssueUpdatedUnix() ([]int64, error) {
prs := make([]int64, 0, 10)
return prs, db.GetEngine(db.DefaultContext).Table("pull_request").
Where("issue.updated_unix>?", time.Now().Add(-time.Minute*2).Unix()).
Join("INNER", "issue", "issue.id=pull_request.issue_id").
Cols("pull_request.id").
Find(&prs)
}
func (p *PullRequest) LoadLatestPullRequestVersion(ctx context.Context) (err error) {
if p.LatestPullRequestVersion != nil {
return nil
}
p.LatestPullRequestVersion, err = pull.GetPullRequestLastVersionByPullRequest(ctx, &p.PullRequest)
return err
}

View File

@ -13,8 +13,18 @@ import (
"code.gitea.io/gitea/modules/web"
"code.gitlink.org.cn/Gitlink/gitea_hat.git/models/migrations"
api_hat "code.gitlink.org.cn/Gitlink/gitea_hat.git/routers/hat"
hat_pull_service "code.gitlink.org.cn/Gitlink/gitea_hat.git/services/pull"
)
func mustInit(fn func() error) {
err := fn()
if err != nil {
ptr := reflect.ValueOf(fn).Pointer()
fi := runtime.FuncForPC(ptr)
log.Fatal("%s failed: %v", fi.Name(), err)
}
}
func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
err := fn(ctx)
if err != nil {
@ -26,6 +36,8 @@ func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
func GlobalInitInstalled(ctx context.Context) {
mustInitCtx(ctx, InitDBEngine)
mustInit(hat_pull_service.Init)
}
func InitHatRouters(ctx context.Context, e *web.Route) *web.Route {

67
services/pull/check.go Normal file
View File

@ -0,0 +1,67 @@
package pull
import (
"context"
"fmt"
"strconv"
issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/modules/graceful"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/queue"
hat_issues_model "code.gitlink.org.cn/Gitlink/gitea_hat.git/models/issues"
)
var hatPrPatchCheckerQueue queue.UniqueQueue
func AddToTaskQueue(pr *issues_model.PullRequest) {
err := hatPrPatchCheckerQueue.PushFunc(strconv.FormatInt(pr.ID, 10), func() error {
return nil
})
if err != nil && err != queue.ErrAlreadyInQueue {
log.Error("Error adding prID %d to the test pull requests queue: %v", pr.ID, err)
}
}
// InitializePullRequests checks and tests untested patches of pull requests.
func InitializePullRequests(ctx context.Context) {
prs, err := hat_issues_model.GetPullRequestIDsByIssueUpdatedUnix()
if err != nil {
log.Error("Find Checking PRs: %v", err)
return
}
for _, prID := range prs {
select {
case <-ctx.Done():
return
default:
if err := hatPrPatchCheckerQueue.PushFunc(strconv.FormatInt(prID, 10), func() error {
log.Trace("Adding PR ID: %d to the pull requests patch hat checking queue", prID)
return nil
}); err != nil {
log.Error("Error adding prID: %s to the pull requests patch hat checking queue %v", prID, err)
}
}
}
}
func handle(data ...queue.Data) []queue.Data {
for _, datum := range data {
id, _ := strconv.ParseInt(datum.(string), 10, 64)
fmt.Println(id)
}
return nil
}
func Init() error {
hatPrPatchCheckerQueue = queue.CreateUniqueQueue("hat_pr_patch_checker", handle, "")
if hatPrPatchCheckerQueue == nil {
return fmt.Errorf("Unable to create hat_pr_patch_checker Queue")
}
go graceful.GetManager().RunWithShutdownFns(hatPrPatchCheckerQueue.Run)
go graceful.GetManager().RunWithShutdownContext(InitializePullRequests)
return nil
}