diff --git a/bots-common/gitea_utils.go b/bots-common/gitea_utils.go index ebdca9b..9f3980c 100644 --- a/bots-common/gitea_utils.go +++ b/bots-common/gitea_utils.go @@ -34,6 +34,7 @@ import ( apiclient "src.opensuse.org/autogits/common/gitea-generated/client" "src.opensuse.org/autogits/common/gitea-generated/client/notification" "src.opensuse.org/autogits/common/gitea-generated/client/organization" + "src.opensuse.org/autogits/common/gitea-generated/client/issue" "src.opensuse.org/autogits/common/gitea-generated/client/repository" "src.opensuse.org/autogits/common/gitea-generated/client/user" "src.opensuse.org/autogits/common/gitea-generated/models" @@ -63,6 +64,10 @@ const ( ReviewStateUnknown models.ReviewStateType = "" ) +type GiteaComment interface { + AddComment(pr *models.PullRequest, comment string) (error) +} + type GiteaMaintainershipReader interface { FetchMaintainershipFile(org, prjGit, branch string) ([]byte, string, error) FetchMaintainershipDirFile(org, prjGit, branch, pkg string) ([]byte, string, error) @@ -100,6 +105,7 @@ type GiteaRepoFetcher interface { } type Gitea interface { + GiteaComment GiteaRepoFetcher GiteaReviewRequester GiteaReviewer @@ -459,8 +465,15 @@ func (gitea *GiteaTransport) AddReviewComment(pr *models.PullRequest, state mode transport.DefaultAuthentication, ) */ + if err != nil { + return nil, err + } - /* c, err := client.Issue.IssueCreateComment( + return c.Payload, nil +} + +func (gitea *GiteaTransport) AddComment(pr *models.PullRequest, comment string) (error) { + _, err := gitea.client.Issue.IssueCreateComment( issue.NewIssueCreateCommentParams(). WithDefaults(). WithOwner(pr.Base.Repo.Owner.UserName). @@ -469,13 +482,13 @@ func (gitea *GiteaTransport) AddReviewComment(pr *models.PullRequest, state mode WithBody(&models.CreateIssueCommentOption{ Body: &comment, }), - transport.DefaultAuthentication) - */ + gitea.transport.DefaultAuthentication) + if err != nil { - return nil, err + return err } - return c.Payload, nil + return nil } func (gitea *GiteaTransport) GetRepositoryFileContent(org, repo, hash, path string) ([]byte, string, error) { diff --git a/obs-staging-bot/main.go b/obs-staging-bot/main.go index 03d9002..faa616f 100644 --- a/obs-staging-bot/main.go +++ b/obs-staging-bot/main.go @@ -93,6 +93,14 @@ func getObsProjectAssociatedWithPr(baseProject string, pr *models.PullRequest) s func processBuildStatusUpdate() { } +type RequestModification int + +const ( + RequestModificationNoChange = 1 + RequestModificationProjectCreated = 2 + RequestModificationSourceChanged = 3 +) + type BuildStatusSummary int const ( @@ -317,13 +325,13 @@ func generateObsPrjMeta(git common.Git, gitea common.Gitea, pr *models.PullReque return meta, nil } -func startOrUpdateBuild(git common.Git, gitea common.Gitea, pr *models.PullRequest, obsClient *common.ObsClient) error { +func startOrUpdateBuild(git common.Git, gitea common.Gitea, pr *models.PullRequest, obsClient *common.ObsClient) (RequestModification, error) { log.Println("fetching OBS project Meta") obsPrProject := getObsProjectAssociatedWithPr(obsClient.HomeProject, pr) meta, err := obsClient.GetProjectMeta(obsPrProject) if err != nil { log.Println("error fetching project meta for", obsPrProject, ":", err) - return err + return RequestModificationNoChange, err } if meta != nil { @@ -334,28 +342,31 @@ func startOrUpdateBuild(git common.Git, gitea common.Gitea, pr *models.PullReque } else { if path.Fragment == pr.Head.Sha { // build in progress - return nil + return RequestModificationNoChange, nil } // build needs update log.Println("Detected Head update... regenerating build...") meta = nil } } + var state RequestModification + state = RequestModificationSourceChanged if meta == nil { // new build meta, err = generateObsPrjMeta(git, gitea, pr, obsClient) if err != nil { - return err + return RequestModificationNoChange, err } + state = RequestModificationProjectCreated } err = obsClient.SetProjectMeta(meta) if err != nil { log.Println("cannot create meta project:", err) - return err + return RequestModificationNoChange, err } - return nil + return state, nil } func processPullNotification(gitea common.Gitea, thread *models.NotificationThread) { @@ -454,25 +465,31 @@ func processPullNotification(gitea common.Gitea, thread *models.NotificationThre } log.Println("processing state...") + log.Println("processing state... %d", review.State) + switch review.State { // create build project, if doesn't exist, and add it to pending requests - case common.ReviewStateUnknown, common.ReviewStateRequestReview: - if err := startOrUpdateBuild(git, gitea, pr, obsClient); err != nil { + case common.ReviewStateUnknown: + // what to do? + + + case common.ReviewStatePending, common.ReviewStateRequestReview: + change, err := startOrUpdateBuild(git, gitea, pr, obsClient) + if err != nil { return } + if change != RequestModificationNoChange { + msg := "Changed source updated for build" + if change == RequestModificationProjectCreated { + msg = "Build is started in https://" + obsWebHost + "/project/show/" + + getObsProjectAssociatedWithPr(obsClient.HomeProject, pr) + } + gitea.AddComment(pr, msg) + } - msg := "Build is started in https://" + obsWebHost + "/project/show/" + - getObsProjectAssociatedWithPr(obsClient.HomeProject, pr) - gitea.AddReviewComment(pr, common.ReviewStatePending, msg) - - case common.ReviewStatePending: - if err := startOrUpdateBuild(git, gitea, pr, obsClient); err != nil { - return - } - - err := fetchPrGit(git, pr) + err = fetchPrGit(git, pr) if err != nil { log.Println("Cannot fetch PR git:", pr.URL) return