@@ -6,10 +6,11 @@ package private
6
6
import (
7
7
"fmt"
8
8
"net/http"
9
- "strconv"
10
9
11
10
issues_model "code.gitea.io/gitea/models/issues"
11
+ access_model "code.gitea.io/gitea/models/perm/access"
12
12
repo_model "code.gitea.io/gitea/models/repo"
13
+ user_model "code.gitea.io/gitea/models/user"
13
14
gitea_context "code.gitea.io/gitea/modules/context"
14
15
"code.gitea.io/gitea/modules/git"
15
16
"code.gitea.io/gitea/modules/log"
@@ -89,8 +90,10 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
89
90
}
90
91
}
91
92
93
+ isPrivate := opts .GitPushOptions .Bool (private .GitPushOptionRepoPrivate )
94
+ isTemplate := opts .GitPushOptions .Bool (private .GitPushOptionRepoTemplate )
92
95
// Handle Push Options
93
- if len ( opts . GitPushOptions ) > 0 {
96
+ if ! isPrivate . IsNone () || ! isTemplate . IsNone () {
94
97
// load the repository
95
98
if repo == nil {
96
99
repo = loadRepository (ctx , ownerName , repoName )
@@ -101,13 +104,49 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
101
104
wasEmpty = repo .IsEmpty
102
105
}
103
106
104
- repo .IsPrivate = opts .GitPushOptions .Bool (private .GitPushOptionRepoPrivate , repo .IsPrivate )
105
- repo .IsTemplate = opts .GitPushOptions .Bool (private .GitPushOptionRepoTemplate , repo .IsTemplate )
106
- if err := repo_model .UpdateRepositoryCols (ctx , repo , "is_private" , "is_template" ); err != nil {
107
+ pusher , err := user_model .GetUserByID (ctx , opts .UserID )
108
+ if err != nil {
107
109
log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
108
110
ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
109
111
Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
110
112
})
113
+ return
114
+ }
115
+ perm , err := access_model .GetUserRepoPermission (ctx , repo , pusher )
116
+ if err != nil {
117
+ log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
118
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
119
+ Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
120
+ })
121
+ return
122
+ }
123
+ if ! perm .IsOwner () && ! perm .IsAdmin () {
124
+ ctx .JSON (http .StatusNotFound , private.HookPostReceiveResult {
125
+ Err : "Permissions denied" ,
126
+ })
127
+ return
128
+ }
129
+
130
+ cols := make ([]string , 0 , len (opts .GitPushOptions ))
131
+
132
+ if ! isPrivate .IsNone () {
133
+ repo .IsPrivate = isPrivate .IsTrue ()
134
+ cols = append (cols , "is_private" )
135
+ }
136
+
137
+ if ! isTemplate .IsNone () {
138
+ repo .IsTemplate = isTemplate .IsTrue ()
139
+ cols = append (cols , "is_template" )
140
+ }
141
+
142
+ if len (cols ) > 0 {
143
+ if err := repo_model .UpdateRepositoryCols (ctx , repo , cols ... ); err != nil {
144
+ log .Error ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err )
145
+ ctx .JSON (http .StatusInternalServerError , private.HookPostReceiveResult {
146
+ Err : fmt .Sprintf ("Failed to Update: %s/%s Error: %v" , ownerName , repoName , err ),
147
+ })
148
+ return
149
+ }
111
150
}
112
151
}
113
152
@@ -122,42 +161,6 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) {
122
161
refFullName := opts .RefFullNames [i ]
123
162
newCommitID := opts .NewCommitIDs [i ]
124
163
125
- // post update for agit pull request
126
- // FIXME: use pr.Flow to test whether it's an Agit PR or a GH PR
127
- if git .SupportProcReceive && refFullName .IsPull () {
128
- if repo == nil {
129
- repo = loadRepository (ctx , ownerName , repoName )
130
- if ctx .Written () {
131
- return
132
- }
133
- }
134
-
135
- pullIndex , _ := strconv .ParseInt (refFullName .PullName (), 10 , 64 )
136
- if pullIndex <= 0 {
137
- continue
138
- }
139
-
140
- pr , err := issues_model .GetPullRequestByIndex (ctx , repo .ID , pullIndex )
141
- if err != nil && ! issues_model .IsErrPullRequestNotExist (err ) {
142
- log .Error ("Failed to get PR by index %v Error: %v" , pullIndex , err )
143
- ctx .JSON (http .StatusInternalServerError , private.Response {
144
- Err : fmt .Sprintf ("Failed to get PR by index %v Error: %v" , pullIndex , err ),
145
- })
146
- return
147
- }
148
- if pr == nil {
149
- continue
150
- }
151
-
152
- results = append (results , private.HookPostReceiveBranchResult {
153
- Message : setting .Git .PullRequestPushMessage && repo .AllowsPulls (),
154
- Create : false ,
155
- Branch : "" ,
156
- URL : fmt .Sprintf ("%s/pulls/%d" , repo .HTMLURL (), pr .Index ),
157
- })
158
- continue
159
- }
160
-
161
164
// If we've pushed a branch (and not deleted it)
162
165
if newCommitID != git .EmptySHA && refFullName .IsBranch () {
163
166
0 commit comments