-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
agit flow add refs/for-review/<pull index> support #31245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
70d2872
a5b38fc
695bafe
09daf03
1685a35
125e7ae
e371a21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,8 @@ func (ref *Reference) RefGroup() string { | |
// or refs/for/<targe-branch> -o topic='<topic-branch>' | ||
const ForPrefix = "refs/for/" | ||
|
||
// TODO: /refs/for-review for suggest change interface | ||
// ForReviewPrefix special ref to update a pull request: refs/for-review/<pull index> | ||
const ForReviewPrefix = "refs/for-review/" | ||
|
||
// RefName represents a full git reference name | ||
type RefName string | ||
|
@@ -104,6 +105,12 @@ func (ref RefName) IsFor() bool { | |
return strings.HasPrefix(string(ref), ForPrefix) | ||
} | ||
|
||
var forReviewPattern = regexp.MustCompile(ForReviewPrefix + `[1-9][0-9]*`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you need to ensure that the pull index is an integer greater than 0. |
||
|
||
func (ref RefName) IsForReview() bool { | ||
return forReviewPattern.MatchString(string(ref)) | ||
} | ||
|
||
func (ref RefName) nameWithoutPrefix(prefix string) string { | ||
if strings.HasPrefix(string(ref), prefix) { | ||
return strings.TrimPrefix(string(ref), prefix) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,75 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. | |
return nil, fmt.Errorf("failed to get user. Error: %w", err) | ||
} | ||
|
||
updateExistPull := func(pr *issues_model.PullRequest, i int) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about using a normal function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
// update exist pull request | ||
if err := pr.LoadBaseRepo(ctx); err != nil { | ||
return fmt.Errorf("unable to load base repository for PR[%d] Error: %w", pr.ID, err) | ||
} | ||
|
||
oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) | ||
if err != nil { | ||
return fmt.Errorf("unable to get ref commit id in base repository for PR[%d] Error: %w", pr.ID, err) | ||
} | ||
|
||
if oldCommitID == opts.NewCommitIDs[i] { | ||
results = append(results, private.HookProcReceiveRefResult{ | ||
OriginalRef: opts.RefFullNames[i], | ||
OldOID: opts.OldCommitIDs[i], | ||
NewOID: opts.NewCommitIDs[i], | ||
Err: "new commit is same with old commit", | ||
}) | ||
return nil | ||
} | ||
|
||
if !forcePush { | ||
output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1"). | ||
AddDynamicArguments(oldCommitID, "^"+opts.NewCommitIDs[i]). | ||
RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: os.Environ()}) | ||
if err != nil { | ||
return fmt.Errorf("failed to detect force push: %w", err) | ||
} else if len(output) > 0 { | ||
results = append(results, private.HookProcReceiveRefResult{ | ||
OriginalRef: opts.RefFullNames[i], | ||
OldOID: opts.OldCommitIDs[i], | ||
NewOID: opts.NewCommitIDs[i], | ||
Err: "request `force-push` push option", | ||
}) | ||
return nil | ||
} | ||
} | ||
|
||
pr.HeadCommitID = opts.NewCommitIDs[i] | ||
if err = pull_service.UpdateRef(ctx, pr); err != nil { | ||
return fmt.Errorf("failed to update pull ref. Error: %w", err) | ||
} | ||
|
||
pull_service.AddToTaskQueue(ctx, pr) | ||
err = pr.LoadIssue(ctx) | ||
if err != nil { | ||
return fmt.Errorf("failed to load pull issue. Error: %w", err) | ||
} | ||
comment, err := pull_service.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i]) | ||
if err == nil && comment != nil { | ||
notify_service.PullRequestPushCommits(ctx, pusher, pr, comment) | ||
} | ||
notify_service.PullRequestSynchronized(ctx, pusher, pr) | ||
isForcePush := comment != nil && comment.IsForcePush | ||
|
||
results = append(results, private.HookProcReceiveRefResult{ | ||
OldOID: oldCommitID, | ||
NewOID: opts.NewCommitIDs[i], | ||
Ref: pr.GetGitRefName(), | ||
OriginalRef: opts.RefFullNames[i], | ||
IsForcePush: isForcePush, | ||
IsCreatePR: false, | ||
URL: fmt.Sprintf("%s/pulls/%d", repo.HTMLURL(), pr.Index), | ||
ShouldShowMessage: setting.Git.PullRequestPushMessage && repo.AllowsPulls(ctx), | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
for i := range opts.OldCommitIDs { | ||
if opts.NewCommitIDs[i] == objectFormat.EmptyObjectID().String() { | ||
results = append(results, private.HookProcReceiveRefResult{ | ||
|
@@ -47,6 +116,37 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. | |
continue | ||
} | ||
|
||
if opts.RefFullNames[i].IsForReview() { | ||
// try match refs/for-review/<pull index> | ||
pullIndex, err := strconv.ParseInt(strings.TrimPrefix(string(opts.RefFullNames[i]), git.ForReviewPrefix), 10, 64) | ||
if err != nil { | ||
results = append(results, private.HookProcReceiveRefResult{ | ||
OriginalRef: opts.RefFullNames[i], | ||
OldOID: opts.OldCommitIDs[i], | ||
NewOID: opts.NewCommitIDs[i], | ||
Err: "Unknow pull request index", | ||
}) | ||
continue | ||
} | ||
pull, err := issues_model.GetPullRequestByIndex(ctx, repo.ID, pullIndex) | ||
if err != nil { | ||
results = append(results, private.HookProcReceiveRefResult{ | ||
OriginalRef: opts.RefFullNames[i], | ||
OldOID: opts.OldCommitIDs[i], | ||
NewOID: opts.NewCommitIDs[i], | ||
Err: "Unknow pull request index", | ||
}) | ||
continue | ||
} | ||
|
||
err = updateExistPull(pull, i) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
continue | ||
} | ||
|
||
if !opts.RefFullNames[i].IsFor() { | ||
results = append(results, private.HookProcReceiveRefResult{ | ||
IsNotMatched: true, | ||
|
@@ -158,70 +258,10 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git. | |
continue | ||
} | ||
|
||
// update exist pull request | ||
if err := pr.LoadBaseRepo(ctx); err != nil { | ||
return nil, fmt.Errorf("unable to load base repository for PR[%d] Error: %w", pr.ID, err) | ||
} | ||
|
||
oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to get ref commit id in base repository for PR[%d] Error: %w", pr.ID, err) | ||
} | ||
|
||
if oldCommitID == opts.NewCommitIDs[i] { | ||
results = append(results, private.HookProcReceiveRefResult{ | ||
OriginalRef: opts.RefFullNames[i], | ||
OldOID: opts.OldCommitIDs[i], | ||
NewOID: opts.NewCommitIDs[i], | ||
Err: "new commit is same with old commit", | ||
}) | ||
continue | ||
} | ||
|
||
if !forcePush { | ||
output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1"). | ||
AddDynamicArguments(oldCommitID, "^"+opts.NewCommitIDs[i]). | ||
RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: os.Environ()}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to detect force push: %w", err) | ||
} else if len(output) > 0 { | ||
results = append(results, private.HookProcReceiveRefResult{ | ||
OriginalRef: opts.RefFullNames[i], | ||
OldOID: opts.OldCommitIDs[i], | ||
NewOID: opts.NewCommitIDs[i], | ||
Err: "request `force-push` push option", | ||
}) | ||
continue | ||
} | ||
} | ||
|
||
pr.HeadCommitID = opts.NewCommitIDs[i] | ||
if err = pull_service.UpdateRef(ctx, pr); err != nil { | ||
return nil, fmt.Errorf("failed to update pull ref. Error: %w", err) | ||
} | ||
|
||
pull_service.AddToTaskQueue(ctx, pr) | ||
err = pr.LoadIssue(ctx) | ||
err = updateExistPull(pr, i) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to load pull issue. Error: %w", err) | ||
return nil, err | ||
} | ||
comment, err := pull_service.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i]) | ||
if err == nil && comment != nil { | ||
notify_service.PullRequestPushCommits(ctx, pusher, pr, comment) | ||
} | ||
notify_service.PullRequestSynchronized(ctx, pusher, pr) | ||
isForcePush := comment != nil && comment.IsForcePush | ||
|
||
results = append(results, private.HookProcReceiveRefResult{ | ||
OldOID: oldCommitID, | ||
NewOID: opts.NewCommitIDs[i], | ||
Ref: pr.GetGitRefName(), | ||
OriginalRef: opts.RefFullNames[i], | ||
IsForcePush: isForcePush, | ||
IsCreatePR: false, | ||
URL: fmt.Sprintf("%s/pulls/%d", repo.HTMLURL(), pr.Index), | ||
ShouldShowMessage: setting.Git.PullRequestPushMessage && repo.AllowsPulls(ctx), | ||
}) | ||
} | ||
|
||
return results, nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it should use
c *cli.Context
stdout, to make it testable.