This commit is contained in:
Adam Majer 2024-07-31 17:17:07 +02:00
parent 00ecf8ee85
commit 736a26718d

View File

@ -79,8 +79,84 @@ func processBuildStatus(project, refProject *common.BuildResultList) BuildStatus
return BuildBuilding
}
func processPullNotification(h *common.RequestHandler, notification *models.NotificationSubject) {
func startBuild(h *common.RequestHandler, pr *models.PullRequest, obsClient *common.ObsClient) error {
err := fetchPrGit(h, pr)
if err != nil {
h.LogError("Cannot fetch PR git: %s", pr.URL)
return err
}
// find modified submodules and new submodules -- build them
dir := pr.Head.Sha
headSubmodules := h.GitSubmoduleList(dir, pr.Head.Sha)
baseSubmodules := h.GitSubmoduleList(dir, pr.Base.Sha)
modifiedOrNew := make([]string, 0, 16)
for pkg, headOid := range headSubmodules {
if baseOid, exists := baseSubmodules[pkg]; !exists || baseOid != headOid {
modifiedOrNew = append(modifiedOrNew, pkg)
}
}
h.Log("repo content fetching ...")
buildPrjBytes := h.GitCatFile(dir, pr.Head.Sha, "project.build")
// buildPrjBytes, err := h.GetPullRequestFileContent(pr, "project.build")
if h.HasError() {
h.LogPlainError(h.Error)
/*
_, err := h.AddReviewComment(pr, common.ReviewStateRequestChanges, "Cannot find reference project")
if err != nil {
h.LogPlainError(err)
}
*/
return h.Error
}
buildPrj := strings.TrimSpace(string(buildPrjBytes))
meta, err := obsClient.GetProjectMeta(buildPrj)
if err != nil {
h.Log("error fetching project meta for %s: %v", buildPrj, err)
return err
}
// generate new project with paths pointinig back to original repos
// disable publishing
// TODO: escape things here
meta.Name = getObsProjectAssociatedWithPr(obsClient.HomeProject, pr)
meta.Description = fmt.Sprintf(`Pull request build job: %s%s PR#%d`,
"https://src.opensuse.org", pr.Base.Repo.Name, pr.Index)
urlPkg := make([]string, 0, len(modifiedOrNew))
for _, pkg := range modifiedOrNew {
urlPkg = append(urlPkg, "onlybuild="+url.QueryEscape(pkg))
}
meta.ScmSync = pr.Head.Repo.CloneURL + "?" + strings.Join(urlPkg, "&")
meta.Title = fmt.Sprintf("PR#%d to %s", pr.Index, pr.Base.Name)
meta.PublicFlags = common.Flags{Contents: "<disable/>"}
// set paths to parent project
for idx, r := range meta.Repositories {
meta.Repositories[idx].Paths = []common.RepositoryPathMeta{{
Project: buildPrj,
Repository: r.Name,
}}
}
h.Log("%#v", meta)
err = obsClient.SetProjectMeta(meta)
if err != nil {
h.Error = err
h.LogError("cannot create meta project: %#v", err)
return h.Error
}
return nil
}
func processPullNotification(h *common.RequestHandler, thread *models.NotificationThread) {
rx := regexp.MustCompile(`^https://src\.(?:open)?suse\.(?:org|de)/api/v\d+/repos/(?<org>[a-zA-Z0-9]+)/(?<project>[_a-zA-Z0-9]+)/issues/(?<num>[0-9]+)$`)
notification := thread.Subject
match := rx.FindStringSubmatch(notification.URL)
if match == nil {
log.Panicf("Unexpected format of notification: %s", notification.URL)
@ -141,79 +217,16 @@ func processPullNotification(h *common.RequestHandler, notification *models.Noti
h.Log("processing state...")
switch review.State {
// create build project, if doesn't exist, and add it to pending requests
case common.ReviewStateUnknown, common.ReviewStateRequestReview:
err := fetchPrGit(h, pr)
if err != nil {
h.LogError("Cannot fetch PR git: %s", pr.URL)
if err := startBuild(h, pr, obsClient); err != nil {
return
}
// find modified submodules and new submodules -- build them
dir := pr.Head.Sha
headSubmodules := h.GitSubmoduleList(dir, pr.Head.Sha)
baseSubmodules := h.GitSubmoduleList(dir, pr.Base.Sha)
modifiedOrNew := make([]string, 0, 16)
for pkg, headOid := range headSubmodules {
if baseOid, exists := baseSubmodules[pkg]; !exists || baseOid != headOid {
modifiedOrNew = append(modifiedOrNew, pkg)
}
}
h.Log("repo content fetching ...")
buildPrjBytes := h.GitCatFile(dir, pr.Head.Sha, "project.build")
// buildPrjBytes, err := h.GetPullRequestFileContent(pr, "project.build")
if h.HasError() {
h.LogPlainError(h.Error)
/*
_, err := h.AddReviewComment(pr, common.ReviewStateRequestChanges, "Cannot find reference project")
if err != nil {
h.LogPlainError(err)
}
*/
return
}
buildPrj := strings.TrimSpace(string(buildPrjBytes))
meta, err := obsClient.GetProjectMeta(buildPrj)
if err != nil {
h.Log("error fetching project meta for %s: %v", buildPrj, err)
return
}
// generate new project with paths pointinig back to original repos
// disable publishing
// TODO: escape things here
meta.Name = getObsProjectAssociatedWithPr(obsClient.HomeProject, pr)
meta.Description = fmt.Sprintf(`Pull request build job: %s%s PR#%d`,
"https://src.opensuse.org", pr.Base.Repo.Name, pr.Index)
urlPkg := make([]string, 0, len(modifiedOrNew))
for _, pkg := range modifiedOrNew {
urlPkg = append(urlPkg, "onlybuild="+url.QueryEscape(pkg))
}
meta.ScmSync = pr.Head.Repo.CloneURL + "?" + strings.Join(urlPkg, "&")
meta.Title = fmt.Sprintf("PR#%d to %s", pr.Index, pr.Base.Name)
meta.PublicFlags = common.Flags{Contents: "<disable/>"}
// set paths to parent project
for idx, r := range meta.Repositories {
meta.Repositories[idx].Paths = []common.RepositoryPathMeta{{
Project: buildPrj,
Repository: r.Name,
}}
}
h.Log("%#v", meta)
return
err = obsClient.SetProjectMeta(meta)
if err != nil {
h.LogError("cannot create meta project: %#v", err)
}
// set the review state to pending
msg := "Build is started in https://build.opensuse.org/project/show/" +
getObsProjectAssociatedWithPr(obsClient.HomeProject, pr)
h.AddReviewComment(pr, common.ReviewStatePending, msg)
case common.ReviewStatePending:
err := fetchPrGit(h, pr)
@ -279,10 +292,12 @@ func processPullNotification(h *common.RequestHandler, notification *models.Noti
case common.ReviewStateApproved:
// done, mark notification as read
h.Log("processing request for success build ...")
// h.SetNotificationRead(thread.ID)
case common.ReviewStateRequestChanges:
// build failures, nothing to do here, mark notification as read
h.Log("processing request for failed request changes...")
// h.SetNotificationRead(thread.ID)
}
break
@ -303,7 +318,7 @@ func pollWorkNotifications() {
switch notification.Subject.Type {
case "Pull":
processPullNotification(h, notification.Subject)
processPullNotification(h, notification)
default:
h.SetNotificationRead(notification.ID)
}