From 7ebd069424d5985a3872007082781c9971e4ebbf Mon Sep 17 00:00:00 2001 From: yystopf Date: Tue, 10 Jan 2023 19:05:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E7=89=88=E6=9C=AC=E7=AE=A1=E7=90=86=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/issues/pull.go | 43 +++++++++++++++++++++++++++ routers/init.go | 12 ++++++++ services/pull/check.go | 67 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 models/issues/pull.go create mode 100644 services/pull/check.go diff --git a/models/issues/pull.go b/models/issues/pull.go new file mode 100644 index 0000000..ad79432 --- /dev/null +++ b/models/issues/pull.go @@ -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 +} diff --git a/routers/init.go b/routers/init.go index 29c427c..ee013ac 100644 --- a/routers/init.go +++ b/routers/init.go @@ -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 { diff --git a/services/pull/check.go b/services/pull/check.go new file mode 100644 index 0000000..46e65f6 --- /dev/null +++ b/services/pull/check.go @@ -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 +}