-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
Agit flow add refs/for-review/<pull index> support #33179
base: main
Are you sure you want to change the base?
Changes from 28 commits
70d2872
a5b38fc
695bafe
09daf03
1685a35
125e7ae
e371a21
bd501a6
06c5f30
cab9bbc
35a2b9b
b99fe1c
930d8cb
cdea357
3e0c530
1660737
013d437
30713a6
a041057
3b2aef2
e875cba
56b1351
4c8ea3d
14c7ebe
fec0b43
2242557
d4f2379
59f37ce
009a1df
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 |
---|---|---|
|
@@ -4,9 +4,12 @@ | |
package private | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
asymkey_model "code.gitea.io/gitea/models/asymkey" | ||
git_model "code.gitea.io/gitea/models/git" | ||
|
@@ -123,6 +126,8 @@ func HookPreReceive(ctx *gitea_context.PrivateContext) { | |
preReceiveTag(ourCtx, refFullName) | ||
case git.DefaultFeatures().SupportProcReceive && refFullName.IsFor(): | ||
preReceiveFor(ourCtx, refFullName) | ||
case git.DefaultFeatures().SupportProcReceive && refFullName.IsForReview(): | ||
preReceiveForReview(ourCtx, refFullName) | ||
default: | ||
ourCtx.AssertCanWriteCode() | ||
} | ||
|
@@ -468,6 +473,80 @@ func preReceiveFor(ctx *preReceiveContext, refFullName git.RefName) { | |
} | ||
} | ||
|
||
func canUpdateAgitPull(ctx *preReceiveContext, pull *issues_model.PullRequest) error { | ||
if pull.Flow != issues_model.PullRequestFlowAGit { | ||
return errors.New("Pull request that are not created through agit cannot be updated using agit") | ||
} | ||
|
||
if ctx.opts.UserID == pull.Issue.PosterID { | ||
return nil | ||
} | ||
|
||
if !pull.AllowMaintainerEdit { | ||
return fmt.Errorf("The author does not allow maintainers to edit this pull request") | ||
} | ||
|
||
if !ctx.loadPusherAndPermission() { | ||
return fmt.Errorf("Internal Server Error (no specific 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. The error message seems not informative. |
||
} | ||
|
||
if ctx.userPerm.CanWrite(unit.TypeCode) { | ||
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. user with read permission to the code should be allowed to create/update pull requests? 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. Updating pull requests requires write permission. 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. Updating pull requests needs write permission of the head repository. |
||
return errors.New("You have no permission to update this pull request") | ||
} | ||
return nil | ||
} | ||
|
||
func preReceiveForReview(ctx *preReceiveContext, refFullName git.RefName) { | ||
if !ctx.AssertCreatePullRequest() { | ||
return | ||
} | ||
|
||
if ctx.Repo.Repository.IsEmpty { | ||
ctx.JSON(http.StatusForbidden, private.Response{ | ||
UserMsg: "Can't create pull request for an empty repository.", | ||
}) | ||
return | ||
} | ||
|
||
if ctx.opts.IsWiki { | ||
ctx.JSON(http.StatusForbidden, private.Response{ | ||
UserMsg: "Pull requests are not supported on the wiki.", | ||
}) | ||
return | ||
} | ||
|
||
pullIndex, err := strconv.ParseInt(strings.TrimPrefix(string(refFullName), git.ForReviewPrefix), 10, 64) | ||
if err != nil { | ||
ctx.JSON(http.StatusForbidden, private.Response{ | ||
UserMsg: "Unknow pull request index.", | ||
}) | ||
return | ||
} | ||
pull, err := issues_model.GetPullRequestByIndex(ctx, ctx.Repo.Repository.ID, pullIndex) | ||
if err != nil { | ||
log.Error("preReceiveForReview: GetPullRequestByIndex: err: %v", err) | ||
ctx.JSON(http.StatusForbidden, private.Response{ | ||
UserMsg: "Unknow pull request index.", | ||
}) | ||
return | ||
} | ||
err = pull.LoadIssue(ctx) | ||
if err != nil { | ||
log.Error("preReceiveForReview: pull.LoadIssue: err: %v", err) | ||
ctx.JSON(http.StatusForbidden, private.Response{ | ||
UserMsg: "Unknow pull request.", | ||
}) | ||
return | ||
} | ||
|
||
if err := canUpdateAgitPull(ctx, pull); err != nil { | ||
ctx.JSON(http.StatusForbidden, private.Response{ | ||
UserMsg: err.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. We should avoid putting |
||
}) | ||
return | ||
} | ||
} | ||
|
||
func generateGitEnv(opts *private.HookOptions) (env []string) { | ||
env = os.Environ() | ||
if opts.GitAlternativeObjectDirectories != "" { | ||
|
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.
The "data" is always hard-coded string
{"type":"gitea","version":2}
? Why it needs a http request