package main import ( "fmt" "path" "src.opensuse.org/autogits/common" "src.opensuse.org/autogits/common/gitea-generated/models" ) const ( ListenAddr = "[::1]:8001" GitAuthor = "GiteaBot - AutoStaging" PrReview = "pr-review" ) func processPullRequestClosed(h *common.RequestHandler) error { return nil } func processPrjGitPullRequestSync(h *common.RequestHandler) error { // req := h.Data.(*common.PullRequestAction) return nil } func prGitBranchNameForPR(req *common.PullRequestAction) string { return fmt.Sprintf("PR_%s#%d", req.Repository.Name, req.Pull_Request.Number) } func updateOrCreatePRBranch(h *common.RequestHandler, prjGit *models.Repository, commitMsg, branchName string) { req := h.Data.(*common.PullRequestAction) h.GitExec("", "clone", "--depth", "1", prjGit.SSHURL, common.DefaultGitPrj) h.GitExec(common.DefaultGitPrj, "checkout", "-B", branchName, prjGit.DefaultBranch) h.GitExec(common.DefaultGitPrj, "submodule", "update", "--init", "--checkout", "--depth", "1", req.Repository.Name) h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "fetch", "--depth", "1", "origin", req.Pull_Request.Head.Sha) h.GitExec(path.Join(common.DefaultGitPrj, req.Repository.Name), "checkout", req.Pull_Request.Head.Sha) h.GitExec(common.DefaultGitPrj, "commit", "-a", "-m", commitMsg) h.GitExec(common.DefaultGitPrj, "push", "-f", "origin", branchName) } func processPullRequestSync(h *common.RequestHandler) error { req := h.Data.(*common.PullRequestAction) if req.Repository.Name == common.DefaultGitPrj { return processPrjGitPullRequestSync(h) } // need to verify that submodule in the PR for prjgit // is still pointing to the HEAD of the PR prjPr := h.GetAssociatedPrjGitPR(req) h.GitExec("", "clone", "--branch", prjPr.Head.Name, "--depth", "1", prjPr.Head.Repo.SSHURL, common.DefaultGitPrj) commitId, ok := h.GitSubmoduleCommitId(common.DefaultGitPrj, req.Repository.Name, prjPr.Head.Ref) if !ok { return fmt.Errorf("Cannot fetch submodule commit id in prjgit for '%s'", req.Repository.Name) } // nothing changed, still in sync if commitId == req.Pull_Request.Head.Ref { return nil } commitMsg := fmt.Sprintf(`Sync PR Update to %s`, req.Pull_Request.Head.Sha) // we need to update prjgit PR with the new head hash branchName := prGitBranchNameForPR(req) updateOrCreatePRBranch(h, prjPr.Base.Repo, commitMsg, branchName) return h.Error } func processPullRequestOpened(h *common.RequestHandler) error { req := h.Data.(*common.PullRequestAction) // requests against project are not handled here if req.Repository.Name == common.DefaultGitPrj { return nil } // create PrjGit branch for buidling the pull request branchName := prGitBranchNameForPR(req) commitMsg := fmt.Sprintf(`auto-created for %s This commit was autocreated by %s referencing PullRequest: %s/%s#%d`, req.Repository.Owner.Username, req.Repository.Name, GitAuthor, req.Repository.Name, req.Pull_Request.Number) prjGit := h.CreateRepositoryIfNotExist(*req.Repository.Owner, common.DefaultGitPrj) if h.HasError() { return h.Error } updateOrCreatePRBranch(h, prjGit, commitMsg, branchName) PR := h.CreatePullRequest(prjGit, branchName, prjGit.DefaultBranch, fmt.Sprintf("Forwarded PR: %s", req.Repository.Name), fmt.Sprintf(`This is a forwarded pull request by %s referencing the following pull request: `+common.PrPattern, GitAuthor, req.Repository.Owner.Username, req.Repository.Name, req.Pull_Request.Number), ) if h.HasError() { return h.Error } // request build review h.RequestReviews(PR, common.Bot_BuildReview) return h.Error } func processPullRequest(h *common.RequestHandler) error { req := h.Data.(*common.PullRequestAction) switch req.Action { case "opened": return processPullRequestOpened(h) case "synchronized": return processPullRequestSync(h) case "edited": // not need to be handled?? return nil case "closed": return processPullRequestClosed(h) } return fmt.Errorf("Unhandled pull request action: %s", req.Action) } func main() { var defs common.ListenDefinitions defs.Url = PrReview defs.GitAuthor = GitAuthor defs.Handlers = make(map[string]common.RequestProcessor) defs.Handlers[common.RequestType_PR] = processPullRequest defs.Handlers[common.RequestType_PR_sync] = processPullRequest common.RequireGiteaSecretToken() common.RequireObsSecretToken() common.StartServerWithAddress(defs, ListenAddr) }