mirror of
https://github.com/harness/drone.git
synced 2025-05-21 19:39:59 +08:00
feat: [PIPE-22454]: add PR metadata in get/list PR API response (#2912)
* add PR metadata in PR API response
This commit is contained in:
parent
0ca7e83e34
commit
0db33abeb1
@ -32,6 +32,7 @@ func (c *Controller) Find(
|
|||||||
session *auth.Session,
|
session *auth.Session,
|
||||||
repoRef string,
|
repoRef string,
|
||||||
pullreqNum int64,
|
pullreqNum int64,
|
||||||
|
options types.PullReqMetadataOptions,
|
||||||
) (*types.PullReq, error) {
|
) (*types.PullReq, error) {
|
||||||
if pullreqNum <= 0 {
|
if pullreqNum <= 0 {
|
||||||
return nil, usererror.BadRequest("A valid pull request number must be provided.")
|
return nil, usererror.BadRequest("A valid pull request number must be provided.")
|
||||||
@ -52,6 +53,10 @@ func (c *Controller) Find(
|
|||||||
return nil, fmt.Errorf("failed to backfill labels assigned to pull request: %w", err)
|
return nil, fmt.Errorf("failed to backfill labels assigned to pull request: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := c.pullreqListService.BackfillMetadataForPullReq(ctx, repo, pr, options); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to backfill pull request metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
if err := c.pullreqListService.BackfillStats(ctx, pr); err != nil {
|
if err := c.pullreqListService.BackfillStats(ctx, pr); err != nil {
|
||||||
log.Ctx(ctx).Warn().Err(err).Msg("failed to backfill PR stats")
|
log.Ctx(ctx).Warn().Err(err).Msg("failed to backfill PR stats")
|
||||||
}
|
}
|
||||||
@ -59,7 +64,7 @@ func (c *Controller) Find(
|
|||||||
return pr, nil
|
return pr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find returns a pull request from the provided repository.
|
// FindByBranches returns a pull request from the provided branch pair.
|
||||||
func (c *Controller) FindByBranches(
|
func (c *Controller) FindByBranches(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
session *auth.Session,
|
session *auth.Session,
|
||||||
@ -67,6 +72,7 @@ func (c *Controller) FindByBranches(
|
|||||||
sourceRepoRef,
|
sourceRepoRef,
|
||||||
sourceBranch,
|
sourceBranch,
|
||||||
targetBranch string,
|
targetBranch string,
|
||||||
|
options types.PullReqMetadataOptions,
|
||||||
) (*types.PullReq, error) {
|
) (*types.PullReq, error) {
|
||||||
if sourceBranch == "" || targetBranch == "" {
|
if sourceBranch == "" || targetBranch == "" {
|
||||||
return nil, usererror.BadRequest("A valid source/target branch must be provided.")
|
return nil, usererror.BadRequest("A valid source/target branch must be provided.")
|
||||||
@ -103,5 +109,9 @@ func (c *Controller) FindByBranches(
|
|||||||
return nil, usererror.ErrNotFound
|
return nil, usererror.ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := c.pullreqListService.BackfillMetadataForPullReq(ctx, targetRepo, prs[0], options); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to backfill pull request metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return prs[0], nil
|
return prs[0], nil
|
||||||
}
|
}
|
||||||
|
@ -81,6 +81,10 @@ func (c *Controller) List(
|
|||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := c.pullreqListService.BackfillMetadataForRepo(ctx, repo, list, filter.PullReqMetadataOptions); err != nil {
|
||||||
|
return nil, 0, fmt.Errorf("failed to backfill metadata for pull requests: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
for _, pr := range list {
|
for _, pr := range list {
|
||||||
if err := c.pullreqListService.BackfillStats(ctx, pr); err != nil {
|
if err := c.pullreqListService.BackfillStats(ctx, pr); err != nil {
|
||||||
log.Ctx(ctx).Warn().Err(err).Msg("failed to backfill PR stats")
|
log.Ctx(ctx).Warn().Err(err).Msg("failed to backfill PR stats")
|
||||||
|
@ -40,7 +40,13 @@ func HandleFind(pullreqCtrl *pullreq.Controller) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pr, err := pullreqCtrl.Find(ctx, session, repoRef, pullreqNumber)
|
options, err := request.ParsePullReqMetadataOptions(r)
|
||||||
|
if err != nil {
|
||||||
|
render.TranslatedUserError(ctx, w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pr, err := pullreqCtrl.Find(ctx, session, repoRef, pullreqNumber, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
render.TranslatedUserError(ctx, w, err)
|
render.TranslatedUserError(ctx, w, err)
|
||||||
return
|
return
|
||||||
@ -50,7 +56,7 @@ func HandleFind(pullreqCtrl *pullreq.Controller) http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleFind returns a http.HandlerFunc that finds a pull request.
|
// HandleFindByBranches returns a http.HandlerFunc that finds a pull request from the provided branch pair.
|
||||||
func HandleFindByBranches(pullreqCtrl *pullreq.Controller) http.HandlerFunc {
|
func HandleFindByBranches(pullreqCtrl *pullreq.Controller) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx := r.Context()
|
ctx := r.Context()
|
||||||
@ -76,7 +82,13 @@ func HandleFindByBranches(pullreqCtrl *pullreq.Controller) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pr, err := pullreqCtrl.FindByBranches(ctx, session, repoRef, sourceRepoRef, sourceBranch, targetBranch)
|
options, err := request.ParsePullReqMetadataOptions(r)
|
||||||
|
if err != nil {
|
||||||
|
render.TranslatedUserError(ctx, w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
pr, err := pullreqCtrl.FindByBranches(ctx, session, repoRef, sourceRepoRef, sourceBranch, targetBranch, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
render.TranslatedUserError(ctx, w, err)
|
render.TranslatedUserError(ctx, w, err)
|
||||||
return
|
return
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/harness/gitness/app/api/controller/pullreq"
|
"github.com/harness/gitness/app/api/controller/pullreq"
|
||||||
"github.com/harness/gitness/app/api/render"
|
"github.com/harness/gitness/app/api/render"
|
||||||
"github.com/harness/gitness/app/api/request"
|
"github.com/harness/gitness/app/api/request"
|
||||||
|
"github.com/harness/gitness/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HandleMetadata returns a http.HandlerFunc that returns PR metadata.
|
// HandleMetadata returns a http.HandlerFunc that returns PR metadata.
|
||||||
@ -40,7 +41,7 @@ func HandleMetadata(pullreqCtrl *pullreq.Controller) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pr, err := pullreqCtrl.Find(ctx, session, repoRef, pullreqNumber)
|
pr, err := pullreqCtrl.Find(ctx, session, repoRef, pullreqNumber, types.PullReqMetadataOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
render.TranslatedUserError(ctx, w, err)
|
render.TranslatedUserError(ctx, w, err)
|
||||||
return
|
return
|
||||||
|
@ -504,7 +504,8 @@ func pullReqOperations(reflector *openapi3.Reflector) {
|
|||||||
QueryParameterPage, QueryParameterLimit,
|
QueryParameterPage, QueryParameterLimit,
|
||||||
QueryParameterLabelID, QueryParameterValueID,
|
QueryParameterLabelID, QueryParameterValueID,
|
||||||
queryParameterAuthorID, queryParameterCommenterID, queryParameterMentionedID,
|
queryParameterAuthorID, queryParameterCommenterID, queryParameterMentionedID,
|
||||||
queryParameterReviewerID, queryParameterReviewDecision)
|
queryParameterReviewerID, queryParameterReviewDecision,
|
||||||
|
queryParameterIncludeChecks, queryParameterIncludeRules)
|
||||||
_ = reflector.SetRequest(&listPullReq, new(listPullReqRequest), http.MethodGet)
|
_ = reflector.SetRequest(&listPullReq, new(listPullReqRequest), http.MethodGet)
|
||||||
_ = reflector.SetJSONResponse(&listPullReq, new([]types.PullReq), http.StatusOK)
|
_ = reflector.SetJSONResponse(&listPullReq, new([]types.PullReq), http.StatusOK)
|
||||||
_ = reflector.SetJSONResponse(&listPullReq, new(usererror.Error), http.StatusBadRequest)
|
_ = reflector.SetJSONResponse(&listPullReq, new(usererror.Error), http.StatusBadRequest)
|
||||||
@ -516,6 +517,7 @@ func pullReqOperations(reflector *openapi3.Reflector) {
|
|||||||
getPullReq := openapi3.Operation{}
|
getPullReq := openapi3.Operation{}
|
||||||
getPullReq.WithTags("pullreq")
|
getPullReq.WithTags("pullreq")
|
||||||
getPullReq.WithMapOfAnything(map[string]interface{}{"operationId": "getPullReq"})
|
getPullReq.WithMapOfAnything(map[string]interface{}{"operationId": "getPullReq"})
|
||||||
|
getPullReq.WithParameters(queryParameterIncludeChecks, queryParameterIncludeRules)
|
||||||
_ = reflector.SetRequest(&getPullReq, new(getPullReqRequest), http.MethodGet)
|
_ = reflector.SetRequest(&getPullReq, new(getPullReqRequest), http.MethodGet)
|
||||||
_ = reflector.SetJSONResponse(&getPullReq, new(types.PullReq), http.StatusOK)
|
_ = reflector.SetJSONResponse(&getPullReq, new(types.PullReq), http.StatusOK)
|
||||||
_ = reflector.SetJSONResponse(&getPullReq, new(usererror.Error), http.StatusBadRequest)
|
_ = reflector.SetJSONResponse(&getPullReq, new(usererror.Error), http.StatusBadRequest)
|
||||||
@ -527,7 +529,8 @@ func pullReqOperations(reflector *openapi3.Reflector) {
|
|||||||
getPullReqByBranches := openapi3.Operation{}
|
getPullReqByBranches := openapi3.Operation{}
|
||||||
getPullReqByBranches.WithTags("pullreq")
|
getPullReqByBranches.WithTags("pullreq")
|
||||||
getPullReqByBranches.WithMapOfAnything(map[string]interface{}{"operationId": "getPullReqByBranches"})
|
getPullReqByBranches.WithMapOfAnything(map[string]interface{}{"operationId": "getPullReqByBranches"})
|
||||||
getPullReqByBranches.WithParameters(queryParameterSourceRepoRefPullRequest)
|
getPullReqByBranches.WithParameters(queryParameterSourceRepoRefPullRequest,
|
||||||
|
queryParameterIncludeChecks, queryParameterIncludeRules)
|
||||||
_ = reflector.SetRequest(&getPullReqByBranches, new(getPullReqByBranchesRequest), http.MethodGet)
|
_ = reflector.SetRequest(&getPullReqByBranches, new(getPullReqByBranchesRequest), http.MethodGet)
|
||||||
_ = reflector.SetJSONResponse(&getPullReqByBranches, new(types.PullReq), http.StatusOK)
|
_ = reflector.SetJSONResponse(&getPullReqByBranches, new(types.PullReq), http.StatusOK)
|
||||||
_ = reflector.SetJSONResponse(&getPullReqByBranches, new(usererror.Error), http.StatusBadRequest)
|
_ = reflector.SetJSONResponse(&getPullReqByBranches, new(usererror.Error), http.StatusBadRequest)
|
||||||
|
@ -600,9 +600,10 @@ func spaceOperations(reflector *openapi3.Reflector) {
|
|||||||
QueryParameterLimit,
|
QueryParameterLimit,
|
||||||
QueryParameterLabelID, QueryParameterValueID,
|
QueryParameterLabelID, QueryParameterValueID,
|
||||||
queryParameterAuthorID, queryParameterCommenterID, queryParameterMentionedID,
|
queryParameterAuthorID, queryParameterCommenterID, queryParameterMentionedID,
|
||||||
queryParameterReviewerID, queryParameterReviewDecision)
|
queryParameterReviewerID, queryParameterReviewDecision,
|
||||||
|
queryParameterIncludeChecks, queryParameterIncludeRules)
|
||||||
_ = reflector.SetRequest(&listPullReq, new(listPullReqRequest), http.MethodGet)
|
_ = reflector.SetRequest(&listPullReq, new(listPullReqRequest), http.MethodGet)
|
||||||
_ = reflector.SetJSONResponse(&listPullReq, new([]types.PullReq), http.StatusOK)
|
_ = reflector.SetJSONResponse(&listPullReq, new([]types.PullReqRepo), http.StatusOK)
|
||||||
_ = reflector.SetJSONResponse(&listPullReq, new(usererror.Error), http.StatusBadRequest)
|
_ = reflector.SetJSONResponse(&listPullReq, new(usererror.Error), http.StatusBadRequest)
|
||||||
_ = reflector.SetJSONResponse(&listPullReq, new(usererror.Error), http.StatusInternalServerError)
|
_ = reflector.SetJSONResponse(&listPullReq, new(usererror.Error), http.StatusInternalServerError)
|
||||||
_ = reflector.SetJSONResponse(&listPullReq, new(usererror.Error), http.StatusUnauthorized)
|
_ = reflector.SetJSONResponse(&listPullReq, new(usererror.Error), http.StatusUnauthorized)
|
||||||
|
@ -109,6 +109,23 @@ func parseReviewDecisions(r *http.Request) []enum.PullReqReviewDecision {
|
|||||||
return reviewDecisions
|
return reviewDecisions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ParsePullReqMetadataOptions(r *http.Request) (types.PullReqMetadataOptions, error) {
|
||||||
|
includeChecks, err := GetIncludeChecksFromQueryOrDefault(r, false)
|
||||||
|
if err != nil {
|
||||||
|
return types.PullReqMetadataOptions{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
includeRules, err := GetIncludeRulesFromQueryOrDefault(r, false)
|
||||||
|
if err != nil {
|
||||||
|
return types.PullReqMetadataOptions{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return types.PullReqMetadataOptions{
|
||||||
|
IncludeChecks: includeChecks,
|
||||||
|
IncludeRules: includeRules,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
// ParsePullReqFilter extracts the pull request query parameter from the url.
|
// ParsePullReqFilter extracts the pull request query parameter from the url.
|
||||||
func ParsePullReqFilter(r *http.Request) (*types.PullReqFilter, error) {
|
func ParsePullReqFilter(r *http.Request) (*types.PullReqFilter, error) {
|
||||||
createdBy, err := QueryParamListAsPositiveInt64(r, QueryParamCreatedBy)
|
createdBy, err := QueryParamListAsPositiveInt64(r, QueryParamCreatedBy)
|
||||||
@ -170,6 +187,11 @@ func ParsePullReqFilter(r *http.Request) (*types.PullReqFilter, error) {
|
|||||||
return nil, fmt.Errorf("encountered error parsing mentioned ID filter: %w", err)
|
return nil, fmt.Errorf("encountered error parsing mentioned ID filter: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
metadataOptions, err := ParsePullReqMetadataOptions(r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return &types.PullReqFilter{
|
return &types.PullReqFilter{
|
||||||
Page: ParsePage(r),
|
Page: ParsePage(r),
|
||||||
Size: ParseLimit(r),
|
Size: ParseLimit(r),
|
||||||
@ -192,6 +214,7 @@ func ParsePullReqFilter(r *http.Request) (*types.PullReqFilter, error) {
|
|||||||
CreatedFilter: createdFilter,
|
CreatedFilter: createdFilter,
|
||||||
UpdatedFilter: updatedFilter,
|
UpdatedFilter: updatedFilter,
|
||||||
EditedFilter: editedFilter,
|
EditedFilter: editedFilter,
|
||||||
|
PullReqMetadataOptions: metadataOptions,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/harness/gitness/app/auth"
|
"github.com/harness/gitness/app/auth"
|
||||||
"github.com/harness/gitness/app/auth/authz"
|
"github.com/harness/gitness/app/auth/authz"
|
||||||
"github.com/harness/gitness/app/services/label"
|
"github.com/harness/gitness/app/services/label"
|
||||||
|
"github.com/harness/gitness/app/services/protection"
|
||||||
"github.com/harness/gitness/app/store"
|
"github.com/harness/gitness/app/store"
|
||||||
"github.com/harness/gitness/errors"
|
"github.com/harness/gitness/errors"
|
||||||
"github.com/harness/gitness/git"
|
"github.com/harness/gitness/git"
|
||||||
@ -30,6 +31,7 @@ import (
|
|||||||
"github.com/harness/gitness/types"
|
"github.com/harness/gitness/types"
|
||||||
"github.com/harness/gitness/types/enum"
|
"github.com/harness/gitness/types/enum"
|
||||||
|
|
||||||
|
"github.com/gotidy/ptr"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -41,7 +43,9 @@ type ListService struct {
|
|||||||
repoStore store.RepoStore
|
repoStore store.RepoStore
|
||||||
repoGitInfoCache store.RepoGitInfoCache
|
repoGitInfoCache store.RepoGitInfoCache
|
||||||
pullreqStore store.PullReqStore
|
pullreqStore store.PullReqStore
|
||||||
|
checkStore store.CheckStore
|
||||||
labelSvc *label.Service
|
labelSvc *label.Service
|
||||||
|
protectionManager *protection.Manager
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewListService(
|
func NewListService(
|
||||||
@ -52,7 +56,9 @@ func NewListService(
|
|||||||
repoStore store.RepoStore,
|
repoStore store.RepoStore,
|
||||||
repoGitInfoCache store.RepoGitInfoCache,
|
repoGitInfoCache store.RepoGitInfoCache,
|
||||||
pullreqStore store.PullReqStore,
|
pullreqStore store.PullReqStore,
|
||||||
|
checkStore store.CheckStore,
|
||||||
labelSvc *label.Service,
|
labelSvc *label.Service,
|
||||||
|
protectionManager *protection.Manager,
|
||||||
) *ListService {
|
) *ListService {
|
||||||
return &ListService{
|
return &ListService{
|
||||||
tx: tx,
|
tx: tx,
|
||||||
@ -62,7 +68,9 @@ func NewListService(
|
|||||||
repoStore: repoStore,
|
repoStore: repoStore,
|
||||||
repoGitInfoCache: repoGitInfoCache,
|
repoGitInfoCache: repoGitInfoCache,
|
||||||
pullreqStore: pullreqStore,
|
pullreqStore: pullreqStore,
|
||||||
|
checkStore: checkStore,
|
||||||
labelSvc: labelSvc,
|
labelSvc: labelSvc,
|
||||||
|
protectionManager: protectionManager,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -166,6 +174,10 @@ func (c *ListService) ListForSpace(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := c.BackfillMetadata(ctx, response, filter.PullReqMetadataOptions); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to backfill metadata: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,3 +240,188 @@ func (c *ListService) BackfillStats(ctx context.Context, pr *types.PullReq) erro
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BackfillChecks collects the check metadata for the provided list of pull requests.
|
||||||
|
func (c *ListService) BackfillChecks(
|
||||||
|
ctx context.Context,
|
||||||
|
list []types.PullReqRepo,
|
||||||
|
) error {
|
||||||
|
// prepare list of commit SHAs per repository
|
||||||
|
|
||||||
|
repoCommitSHAs := make(map[int64][]string)
|
||||||
|
for _, entry := range list {
|
||||||
|
repoID := entry.Repository.ID
|
||||||
|
commitSHAs := repoCommitSHAs[repoID]
|
||||||
|
repoCommitSHAs[repoID] = append(commitSHAs, entry.PullRequest.SourceSHA)
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch checks for every repository
|
||||||
|
|
||||||
|
type repoSHA struct {
|
||||||
|
repoID int64
|
||||||
|
sha string
|
||||||
|
}
|
||||||
|
|
||||||
|
repoCheckSummaryMap := make(map[repoSHA]types.CheckCountSummary)
|
||||||
|
for repoID, commitSHAs := range repoCommitSHAs {
|
||||||
|
commitCheckSummaryMap, err := c.checkStore.ResultSummary(ctx, repoID, commitSHAs)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("fail to fetch check summary for commits: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for commitSHA, checkSummary := range commitCheckSummaryMap {
|
||||||
|
repoCheckSummaryMap[repoSHA{repoID: repoID, sha: commitSHA.String()}] = checkSummary
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// backfill the list with check count summary
|
||||||
|
|
||||||
|
for _, entry := range list {
|
||||||
|
entry.PullRequest.CheckSummary =
|
||||||
|
ptr.Of(repoCheckSummaryMap[repoSHA{repoID: entry.Repository.ID, sha: entry.PullRequest.SourceSHA}])
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// BackfillRules collects the rule metadata for the provided list of pull requests.
|
||||||
|
func (c *ListService) BackfillRules(
|
||||||
|
ctx context.Context,
|
||||||
|
list []types.PullReqRepo,
|
||||||
|
) error {
|
||||||
|
// prepare list of branch names per repository
|
||||||
|
|
||||||
|
repoBranchNames := make(map[int64][]string)
|
||||||
|
repoDefaultBranch := make(map[int64]string)
|
||||||
|
for _, entry := range list {
|
||||||
|
repoID := entry.Repository.ID
|
||||||
|
branchNames := repoBranchNames[repoID]
|
||||||
|
repoBranchNames[repoID] = append(branchNames, entry.PullRequest.TargetBranch)
|
||||||
|
repoDefaultBranch[repoID] = entry.Repository.DefaultBranch
|
||||||
|
}
|
||||||
|
|
||||||
|
// fetch checks for every repository
|
||||||
|
|
||||||
|
type repoBranchName struct {
|
||||||
|
repoID int64
|
||||||
|
branchName string
|
||||||
|
}
|
||||||
|
|
||||||
|
repoBranchNameMap := make(map[repoBranchName][]types.RuleInfo)
|
||||||
|
for repoID, branchNames := range repoBranchNames {
|
||||||
|
repoProtection, err := c.protectionManager.ForRepository(ctx, repoID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("fail to fetch protection rules for repository: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, branchName := range branchNames {
|
||||||
|
branchRuleInfos, err := protection.GetRuleInfos(
|
||||||
|
repoProtection,
|
||||||
|
repoDefaultBranch[repoID],
|
||||||
|
branchName,
|
||||||
|
protection.RuleInfoFilterStatusActive,
|
||||||
|
protection.RuleInfoFilterTypeBranch)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("fail to get rule infos for branch %s: %w", branchName, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repoBranchNameMap[repoBranchName{repoID: repoID, branchName: branchName}] = branchRuleInfos
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// backfill the list with check count summary
|
||||||
|
|
||||||
|
for _, entry := range list {
|
||||||
|
key := repoBranchName{repoID: entry.Repository.ID, branchName: entry.PullRequest.TargetBranch}
|
||||||
|
entry.PullRequest.Rules = repoBranchNameMap[key]
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ListService) BackfillMetadata(
|
||||||
|
ctx context.Context,
|
||||||
|
list []types.PullReqRepo,
|
||||||
|
options types.PullReqMetadataOptions,
|
||||||
|
) error {
|
||||||
|
if options.IsAllFalse() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.IncludeChecks {
|
||||||
|
if err := c.BackfillChecks(ctx, list); err != nil {
|
||||||
|
return fmt.Errorf("failed to backfill checks")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.IncludeRules {
|
||||||
|
if err := c.BackfillRules(ctx, list); err != nil {
|
||||||
|
return fmt.Errorf("failed to backfill rules")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ListService) BackfillMetadataForRepo(
|
||||||
|
ctx context.Context,
|
||||||
|
repo *types.Repository,
|
||||||
|
list []*types.PullReq,
|
||||||
|
options types.PullReqMetadataOptions,
|
||||||
|
) error {
|
||||||
|
if options.IsAllFalse() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
listPullReqRepo := make([]types.PullReqRepo, len(list))
|
||||||
|
for i, pr := range list {
|
||||||
|
listPullReqRepo[i] = types.PullReqRepo{
|
||||||
|
PullRequest: pr,
|
||||||
|
Repository: repo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.IncludeChecks {
|
||||||
|
if err := c.BackfillChecks(ctx, listPullReqRepo); err != nil {
|
||||||
|
return fmt.Errorf("failed to backfill checks")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.IncludeRules {
|
||||||
|
if err := c.BackfillRules(ctx, listPullReqRepo); err != nil {
|
||||||
|
return fmt.Errorf("failed to backfill rules")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *ListService) BackfillMetadataForPullReq(
|
||||||
|
ctx context.Context,
|
||||||
|
repo *types.Repository,
|
||||||
|
pr *types.PullReq,
|
||||||
|
options types.PullReqMetadataOptions,
|
||||||
|
) error {
|
||||||
|
if options.IsAllFalse() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
list := []types.PullReqRepo{{
|
||||||
|
PullRequest: pr,
|
||||||
|
Repository: repo,
|
||||||
|
}}
|
||||||
|
|
||||||
|
if options.IncludeChecks {
|
||||||
|
if err := c.BackfillChecks(ctx, list); err != nil {
|
||||||
|
return fmt.Errorf("failed to backfill checks")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if options.IncludeRules {
|
||||||
|
if err := c.BackfillRules(ctx, list); err != nil {
|
||||||
|
return fmt.Errorf("failed to backfill rules")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -22,6 +22,7 @@ import (
|
|||||||
pullreqevents "github.com/harness/gitness/app/events/pullreq"
|
pullreqevents "github.com/harness/gitness/app/events/pullreq"
|
||||||
"github.com/harness/gitness/app/services/codecomments"
|
"github.com/harness/gitness/app/services/codecomments"
|
||||||
"github.com/harness/gitness/app/services/label"
|
"github.com/harness/gitness/app/services/label"
|
||||||
|
"github.com/harness/gitness/app/services/protection"
|
||||||
"github.com/harness/gitness/app/sse"
|
"github.com/harness/gitness/app/sse"
|
||||||
"github.com/harness/gitness/app/store"
|
"github.com/harness/gitness/app/store"
|
||||||
"github.com/harness/gitness/app/url"
|
"github.com/harness/gitness/app/url"
|
||||||
@ -85,7 +86,9 @@ func ProvideListService(
|
|||||||
repoStore store.RepoStore,
|
repoStore store.RepoStore,
|
||||||
repoGitInfoCache store.RepoGitInfoCache,
|
repoGitInfoCache store.RepoGitInfoCache,
|
||||||
pullreqStore store.PullReqStore,
|
pullreqStore store.PullReqStore,
|
||||||
|
checkStore store.CheckStore,
|
||||||
labelSvc *label.Service,
|
labelSvc *label.Service,
|
||||||
|
protectionManager *protection.Manager,
|
||||||
) *ListService {
|
) *ListService {
|
||||||
return NewListService(
|
return NewListService(
|
||||||
tx,
|
tx,
|
||||||
@ -95,6 +98,8 @@ func ProvideListService(
|
|||||||
repoStore,
|
repoStore,
|
||||||
repoGitInfoCache,
|
repoGitInfoCache,
|
||||||
pullreqStore,
|
pullreqStore,
|
||||||
|
checkStore,
|
||||||
labelSvc,
|
labelSvc,
|
||||||
|
protectionManager,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -275,7 +275,7 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
|
|||||||
connectorStore := database.ProvideConnectorStore(db, secretStore)
|
connectorStore := database.ProvideConnectorStore(db, secretStore)
|
||||||
repoGitInfoView := database.ProvideRepoGitInfoView(db)
|
repoGitInfoView := database.ProvideRepoGitInfoView(db)
|
||||||
repoGitInfoCache := cache.ProvideRepoGitInfoCache(repoGitInfoView)
|
repoGitInfoCache := cache.ProvideRepoGitInfoCache(repoGitInfoView)
|
||||||
listService := pullreq.ProvideListService(transactor, gitInterface, authorizer, spaceStore, repoStore, repoGitInfoCache, pullReqStore, labelService)
|
listService := pullreq.ProvideListService(transactor, gitInterface, authorizer, spaceStore, repoStore, repoGitInfoCache, pullReqStore, checkStore, labelService, protectionManager)
|
||||||
exporterRepository, err := exporter.ProvideSpaceExporter(provider, gitInterface, repoStore, jobScheduler, executor, encrypter, streamer)
|
exporterRepository, err := exporter.ProvideSpaceExporter(provider, gitInterface, repoStore, jobScheduler, executor, encrypter, streamer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -65,6 +65,8 @@ type PullReq struct {
|
|||||||
Stats PullReqStats `json:"stats"`
|
Stats PullReqStats `json:"stats"`
|
||||||
|
|
||||||
Labels []*LabelPullReqAssignmentInfo `json:"labels,omitempty"`
|
Labels []*LabelPullReqAssignmentInfo `json:"labels,omitempty"`
|
||||||
|
CheckSummary *CheckCountSummary `json:"check_summary,omitempty"`
|
||||||
|
Rules []RuleInfo `json:"rules,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (pr *PullReq) UpdateMergeOutcome(method enum.MergeMethod, conflictFiles []string) {
|
func (pr *PullReq) UpdateMergeOutcome(method enum.MergeMethod, conflictFiles []string) {
|
||||||
@ -167,12 +169,22 @@ type PullReqFilter struct {
|
|||||||
CreatedFilter
|
CreatedFilter
|
||||||
UpdatedFilter
|
UpdatedFilter
|
||||||
EditedFilter
|
EditedFilter
|
||||||
|
PullReqMetadataOptions
|
||||||
|
|
||||||
// internal use only
|
// internal use only
|
||||||
SpaceIDs []int64
|
SpaceIDs []int64
|
||||||
RepoIDBlacklist []int64
|
RepoIDBlacklist []int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PullReqMetadataOptions struct {
|
||||||
|
IncludeChecks bool `json:"include_checks"`
|
||||||
|
IncludeRules bool `json:"include_rules"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (options PullReqMetadataOptions) IsAllFalse() bool {
|
||||||
|
return !options.IncludeChecks && !options.IncludeRules
|
||||||
|
}
|
||||||
|
|
||||||
// PullReqReview holds pull request review.
|
// PullReqReview holds pull request review.
|
||||||
type PullReqReview struct {
|
type PullReqReview struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
|
Loading…
Reference in New Issue
Block a user