Skip to content

Commit e14fdbd

Browse files
committed
WIP: change PR flow to AGit
To allow to dissociate from parent branch need to change head_branch = "user/topic", flow = 1, head_repo_id = base_repo_id +----+------+--------+------------------+---------------+----------------+-------------------------+----------+-------+--------------+--------------+-----------------+-------------+--------------------------------------------------------------+-----------------------+------------+------------------+-----------+-------------+------+ | id | type | status | conflicted_files | commits_ahead | commits_behind | changed_protected_files | issue_id | index | head_repo_id | base_repo_id | head_branch | base_branch | merge_base | allow_maintainer_edit | has_merged | merged_commit_id | merger_id | merged_unix | flow | +----+------+--------+------------------+---------------+----------------+-------------------------+----------+-------+--------------+--------------+-----------------+-------------+--------------------------------------------------------------+-----------------------+------------+------------------+-----------+-------------+------+ | 2 | 0 | 2 | null | 1 | 0 | null | 2 | 2 | 3 | 1 | master | master | 21ef3959cb995307e97c5c76f7677ab13a3ea88a911999d69f2ca3033b70 | 0 | 0 | | 0 | 1752264162 | 0 | | | | | | | | | | | | | | | 28ab | | | | | | | +----+------+--------+------------------+---------------+----------------+-------------------------+----------+-------+--------------+--------------+-----------------+-------------+--------------------------------------------------------------+-----------------------+------------+------------------+-----------+-------------+------+ | 3 | 0 | 2 | null | 1 | 0 | null | 3 | 3 | 1 | 1 | test/topic_here | master | 21ef3959cb995307e97c5c76f7677ab13a3ea88a911999d69f2ca3033b70 | 0 | 0 | | 0 | 1752424365 | 1 | | | | | | | | | | | | | | | 28ab | | | | | | | +----+------+--------+------------------+---------------+----------------+-------------------------+----------+-------+--------------+--------------+-----------------+-------------+--------------------------------------------------------------+-----------------------+------------+------------------+-----------+-------------+------+ Related to: #33883
1 parent 39f145a commit e14fdbd

File tree

13 files changed

+206
-0
lines changed

13 files changed

+206
-0
lines changed

models/issues/comment.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ const (
115115
CommentTypeUnpin // 37 unpin Issue/PullRequest
116116

117117
CommentTypeChangeTimeEstimate // 38 Change time estimate
118+
CommentTypeChangePRFlowType // 39 Change pull request's flow type
118119
)
119120

120121
var commentStrings = []string{
@@ -157,6 +158,7 @@ var commentStrings = []string{
157158
"pin",
158159
"unpin",
159160
"change_time_estimate",
161+
"change_flow_type",
160162
}
161163

162164
func (t CommentType) String() string {

models/issues/pull.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ const (
113113
PullRequestFlowAGit
114114
)
115115

116+
var PullRequestFlowMap map[PullRequestFlow]string = map[PullRequestFlow]string{
117+
PullRequestFlowGithub: "github",
118+
PullRequestFlowAGit: "agit",
119+
}
120+
var PullRequestFlowTypeUnknown error = errors.New("Unknown pull request type")
121+
122+
func PullRequestFlowFromString(strtype string) (PullRequestFlow, error) {
123+
for k, v := range PullRequestFlowMap {
124+
if v == strtype {
125+
return k, nil
126+
}
127+
}
128+
129+
return 0, PullRequestFlowTypeUnknown
130+
}
131+
132+
func PullRequestFlowTypeToString(flow PullRequestFlow) string {
133+
v, ok := PullRequestFlowMap[flow]
134+
if ok {
135+
return v
136+
}
137+
return ""
138+
}
139+
116140
// PullRequest represents relation between pull request and repositories.
117141
type PullRequest struct {
118142
ID int64 `xorm:"pk autoincr"`
@@ -462,6 +486,18 @@ func (pr *PullRequest) IsFromFork() bool {
462486
return pr.HeadRepoID != pr.BaseRepoID
463487
}
464488

489+
func (pr *PullRequest) ConvertToAgitPullRequest(ctx context.Context, doer *user_model.User) (err error) {
490+
if pr.IsAgitFlow() {
491+
return nil
492+
}
493+
494+
pr.Flow = PullRequestFlowAGit
495+
pr.HeadRepoID = pr.BaseRepoID
496+
pr.HeadBranch = doer.LowerName + "/" + pr.HeadBranch
497+
498+
return pr.UpdateColsIfNotMerged(ctx, "flow", "head_repo_id", "head_branch")
499+
}
500+
465501
// NewPullRequest creates new pull request with labels for repository.
466502
func NewPullRequest(ctx context.Context, repo *repo_model.Repository, issue *Issue, labelIDs []int64, uuids []string, pr *PullRequest) (err error) {
467503
ctx, committer, err := db.TxContext(ctx)

modules/structs/pull.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ type PullRequest struct {
6060
Closed *time.Time `json:"closed_at"`
6161

6262
PinOrder int `json:"pin_order"`
63+
64+
// swagger:enum["agit","github"]
65+
Flow string `json:"flow"`
6366
}
6467

6568
// PRBranchInfo information about a branch
@@ -107,6 +110,7 @@ type EditPullRequestOption struct {
107110
Deadline *time.Time `json:"due_date"`
108111
RemoveDeadline *bool `json:"unset_due_date"`
109112
AllowMaintainerEdit *bool `json:"allow_maintainer_edit"`
113+
FlowType string `json:"flow_type"`
110114
}
111115

112116
// ChangedFile store information about files affected by the pull request

routers/api/v1/repo/pull.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,46 @@ func EditPullRequest(ctx *context.APIContext) {
777777
notify_service.PullRequestChangeTargetBranch(ctx, ctx.Doer, pr, form.Base)
778778
}
779779

780+
// change pull request type from branch or from AGit
781+
if !pr.HasMerged && len(form.FlowType) != 0 {
782+
flow, err := issues_model.PullRequestFlowFromString(form.FlowType)
783+
if err != nil {
784+
ctx.APIErrorInternal(err)
785+
return
786+
}
787+
err = nil
788+
if !issue.IsPoster(ctx.Doer.ID) && !ctx.Repo.CanWrite(unit.TypeCode) {
789+
// not implemented
790+
ctx.Status(http.StatusForbidden)
791+
return
792+
}
793+
if flow != pr.Flow {
794+
switch flow {
795+
case issues_model.PullRequestFlowGithub:
796+
// not implemented
797+
ctx.Status(http.StatusForbidden)
798+
return
799+
case issues_model.PullRequestFlowAGit:
800+
err = pull_service.ChangePullRequestFlowToAgit(ctx, pr, ctx.Doer)
801+
}
802+
}
803+
if err != nil {
804+
if issues_model.IsErrPullRequestAlreadyExists(err) {
805+
ctx.APIError(http.StatusConflict, err)
806+
return
807+
} else if issues_model.IsErrIssueIsClosed(err) {
808+
ctx.APIError(http.StatusUnprocessableEntity, err)
809+
return
810+
} else if pull_service.IsErrPullRequestHasMerged(err) {
811+
ctx.APIError(http.StatusConflict, err)
812+
return
813+
}
814+
ctx.APIErrorInternal(err)
815+
return
816+
}
817+
notify_service.PullRequestChangeFlowType(ctx, ctx.Doer, pr)
818+
}
819+
780820
// update allow edits
781821
if form.AllowMaintainerEdit != nil {
782822
if err := pull_service.SetAllowEdits(ctx, ctx.Doer, pr, *form.AllowMaintainerEdit); err != nil {

services/actions/notifier.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,32 @@ func (n *actionsNotifier) PullRequestSynchronized(ctx context.Context, doer *use
688688
Notify(ctx)
689689
}
690690

691+
func (n *actionsNotifier) PullRequestChangeFlowType(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
692+
ctx = withMethod(ctx, "PullRequestChangeFlowType")
693+
694+
if err := pr.LoadIssue(ctx); err != nil {
695+
log.Error("LoadAttributes: %v", err)
696+
return
697+
}
698+
699+
if err := pr.Issue.LoadRepo(ctx); err != nil {
700+
log.Error("pr.Issue.LoadRepo: %v", err)
701+
return
702+
}
703+
704+
permission, _ := access_model.GetUserRepoPermission(ctx, pr.Issue.Repo, pr.Issue.Poster)
705+
newNotifyInput(pr.Issue.Repo, doer, webhook_module.HookEventPullRequest).
706+
WithPayload(&api.PullRequestPayload{
707+
Action: api.HookIssueEdited,
708+
Index: pr.Issue.Index,
709+
PullRequest: convert.ToAPIPullRequest(ctx, pr, nil),
710+
Repository: convert.ToRepo(ctx, pr.Issue.Repo, permission),
711+
Sender: convert.ToUser(ctx, doer, nil),
712+
}).
713+
WithPullRequest(pr).
714+
Notify(ctx)
715+
}
716+
691717
func (n *actionsNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
692718
ctx = withMethod(ctx, "PullRequestChangeTargetBranch")
693719

services/convert/pull.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func ToAPIPullRequest(ctx context.Context, pr *issues_model.PullRequest, doer *u
9797
Created: pr.Issue.CreatedUnix.AsTimePtr(),
9898
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
9999
PinOrder: util.Iif(apiIssue.PinOrder == -1, 0, apiIssue.PinOrder),
100+
Flow: issues_model.PullRequestFlowTypeToString(pr.Flow),
100101

101102
// output "[]" rather than null to align to github outputs
102103
RequestedReviewers: []*api.User{},

services/forms/user_form_hidden_comments.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ var hiddenCommentTypeGroups = hiddenCommentTypeGroupsType{
3636
"branch": {
3737
/*11*/ issues_model.CommentTypeDeleteBranch,
3838
/*25*/ issues_model.CommentTypeChangeTargetBranch,
39+
/*39*/ issues_model.CommentTypeChangePRFlowType,
3940
},
4041
"time_tracking": {
4142
/*12*/ issues_model.CommentTypeStartTracking,

services/notify/notifier.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type Notifier interface {
4949
PullRequestReview(ctx context.Context, pr *issues_model.PullRequest, review *issues_model.Review, comment *issues_model.Comment, mentions []*user_model.User)
5050
PullRequestCodeComment(ctx context.Context, pr *issues_model.PullRequest, comment *issues_model.Comment, mentions []*user_model.User)
5151
PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string)
52+
PullRequestChangeFlowType(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest)
5253
PullRequestPushCommits(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, comment *issues_model.Comment)
5354
PullReviewDismiss(ctx context.Context, doer *user_model.User, review *issues_model.Review, comment *issues_model.Comment)
5455

services/notify/notify.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ func PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, p
155155
}
156156
}
157157

158+
// PullRequestChangeFlowType notifies when a pull request's source branch was changed
159+
func PullRequestChangeFlowType(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
160+
for _, notifier := range notifiers {
161+
notifier.PullRequestChangeFlowType(ctx, doer, pr)
162+
}
163+
}
164+
158165
// PullRequestPushCommits notifies when push commits to pull request's head branch
159166
func PullRequestPushCommits(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, comment *issues_model.Comment) {
160167
for _, notifier := range notifiers {

services/notify/null.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func (*NullNotifier) PullRequestSynchronized(ctx context.Context, doer *user_mod
7070
func (*NullNotifier) PullRequestChangeTargetBranch(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, oldBranch string) {
7171
}
7272

73+
// PullRequestChangeFlowType places a place holder function
74+
func (*NullNotifier) PullRequestChangeFlowType(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest) {
75+
}
76+
7377
// PullRequestPushCommits notifies when push commits to pull request's head branch
7478
func (*NullNotifier) PullRequestPushCommits(ctx context.Context, doer *user_model.User, pr *issues_model.PullRequest, comment *issues_model.Comment) {
7579
}

0 commit comments

Comments
 (0)