@@ -6,11 +6,12 @@ package private
6
6
import (
7
7
"fmt"
8
8
"net/http"
9
- "strconv"
10
9
11
10
git_model "code.gitea.io/gitea/models/git"
12
11
issues_model "code.gitea.io/gitea/models/issues"
12
+ access_model "code.gitea.io/gitea/models/perm/access"
13
13
repo_model "code.gitea.io/gitea/models/repo"
14
+ user_model "code.gitea.io/gitea/models/user"
14
15
"code.gitea.io/gitea/modules/git"
15
16
"code.gitea.io/gitea/modules/gitrepo"
16
17
"code.gitea.io/gitea/modules/log"
@@ -159,8 +160,10 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
159
160
}
160
161
}
161
162
163
+ isPrivate := opts .GitPushOptions .Bool (private .GitPushOptionRepoPrivate )
164
+ isTemplate := opts .GitPushOptions .Bool (private .GitPushOptionRepoTemplate )
162
165
// Handle Push Options
163
- if len ( opts . GitPushOptions ) > 0 {
166
+ if isPrivate . Has () || isTemplate . Has () {
164
167
// load the repository
165
168
if repo == nil {
166
169
repo = loadRepository (ctx , ownerName , repoName )
@@ -171,13 +174,49 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
171
174
wasEmpty = repo .IsEmpty
172
175
}
173
176
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 {
177
179
log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
178
180
ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
179
181
Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
180
182
})
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
+ }
181
220
}
182
221
}
183
222
@@ -192,42 +231,6 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
192
231
refFullName := opts .RefFullNames [i ]
193
232
newCommitID := opts .NewCommitIDs [i ]
194
233
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
-
231
234
// If we've pushed a branch (and not deleted it)
232
235
if ! git .IsEmptyCommitID (newCommitID ) && refFullName .IsBranch () {
233
236
// First ensure we have the repository loaded, we're allowed pulls requests and we can get the base repo
0 commit comments