@@ -6,11 +6,12 @@ package private
66import (
77 "fmt"
88 "net/http"
9- "strconv"
109
1110 git_model "code.gitea.io/gitea/models/git"
1211 issues_model "code.gitea.io/gitea/models/issues"
12+ access_model "code.gitea.io/gitea/models/perm/access"
1313 repo_model "code.gitea.io/gitea/models/repo"
14+ user_model "code.gitea.io/gitea/models/user"
1415 "code.gitea.io/gitea/modules/git"
1516 "code.gitea.io/gitea/modules/gitrepo"
1617 "code.gitea.io/gitea/modules/log"
@@ -159,8 +160,10 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
159160 }
160161 }
161162
163+ isPrivate := opts .GitPushOptions .Bool (private .GitPushOptionRepoPrivate )
164+ isTemplate := opts .GitPushOptions .Bool (private .GitPushOptionRepoTemplate )
162165 // Handle Push Options
163- if len ( opts . GitPushOptions ) > 0 {
166+ if isPrivate . Has () || isTemplate . Has () {
164167 // load the repository
165168 if repo == nil {
166169 repo = loadRepository (ctx , ownerName , repoName )
@@ -171,13 +174,49 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
171174 wasEmpty = repo .IsEmpty
172175 }
173176
174- repo .IsPrivate = opts .GitPushOptions .Bool (private .GitPushOptionRepoPrivate , repo .IsPrivate )
175- repo .IsTemplate = opts .GitPushOptions .Bool (private .GitPushOptionRepoTemplate , repo .IsTemplate )
176- if err := repo_model .UpdateRepositoryCols (ctx , repo , "is_private" , "is_template" ); err != nil {
177+ pusher , err := user_model .GetUserByID (ctx , opts .UserID )
178+ if err != nil {
177179 log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
178180 ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
179181 Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
180182 })
183+ return
184+ }
185+ perm , err := access_model .GetUserRepoPermission (ctx , repo , pusher )
186+ if err != nil {
187+ log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
188+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
189+ Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
190+ })
191+ return
192+ }
193+ if ! perm .IsOwner () && ! perm .IsAdmin () {
194+ ctx .JSON (http .StatusNotFound , private.HookPostReceiveResult {
195+ Err : "Permissions denied" ,
196+ })
197+ return
198+ }
199+
200+ cols := make ([]string , 0 , len (opts .GitPushOptions ))
201+
202+ if isPrivate .Has () {
203+ repo .IsPrivate = isPrivate .Value ()
204+ cols = append (cols , "is_private" )
205+ }
206+
207+ if isTemplate .Has () {
208+ repo .IsTemplate = isTemplate .Value ()
209+ cols = append (cols , "is_template" )
210+ }
211+
212+ if len (cols ) > 0 {
213+ if err := repo_model .UpdateRepositoryCols (ctx , repo , cols ... ); err != nil {
214+ log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
215+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
216+ Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
217+ })
218+ return
219+ }
181220 }
182221 }
183222
@@ -192,42 +231,6 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
192231 refFullName := opts .RefFullNames [i ]
193232 newCommitID := opts .NewCommitIDs [i ]
194233
195- // post update for agit pull request
196- // FIXME: use pr.Flow to test whether it's an Agit PR or a GH PR
197- if git .DefaultFeatures .SupportProcReceive && refFullName .IsPull () {
198- if repo == nil {
199- repo = loadRepository (ctx , ownerName , repoName )
200- if ctx .Written () {
201- return
202- }
203- }
204-
205- pullIndex , _ := strconv .ParseInt (refFullName .PullName (), 10 , 64 )
206- if pullIndex <= 0 {
207- continue
208- }
209-
210- pr , err := issues_model .GetPullRequestByIndex (ctx , repo .ID , pullIndex )
211- if err != nil && ! issues_model .IsErrPullRequestNotExist (err ) {
212- log .Error ("Failed to get PR by index %v Error: %v" , pullIndex , err )
213- ctx .JSON (http .StatusInternalServerError , private.Response {
214- Err : fmt .Sprintf ("Failed to get PR by index %v Error: %v" , pullIndex , err ),
215- })
216- return
217- }
218- if pr == nil {
219- continue
220- }
221-
222- results = append (results , private.HookPostReceiveBranchResult {
223- Message : setting .Git .PullRequestPushMessage && repo .AllowsPulls (ctx ),
224- Create : false ,
225- Branch : "" ,
226- URL : fmt .Sprintf ("%s/pulls/%d" , repo .HTMLURL (), pr .Index ),
227- })
228- continue
229- }
230-
231234 // If we've pushed a branch (and not deleted it)
232235 if ! git .IsEmptyCommitID (newCommitID ) && refFullName .IsBranch () {
233236 // First ensure we have the repository loaded, we're allowed pulls requests and we can get the base repo
0 commit comments