5
5
package agit
6
6
7
7
import (
8
+ "context"
8
9
"fmt"
9
- "net/http"
10
10
"os"
11
11
"strings"
12
12
13
13
issues_model "code.gitea.io/gitea/models/issues"
14
14
repo_model "code.gitea.io/gitea/models/repo"
15
15
user_model "code.gitea.io/gitea/models/user"
16
- "code.gitea.io/gitea/modules/context"
17
16
"code.gitea.io/gitea/modules/git"
18
17
"code.gitea.io/gitea/modules/log"
19
18
"code.gitea.io/gitea/modules/notification"
@@ -22,7 +21,7 @@ import (
22
21
)
23
22
24
23
// ProcReceive handle proc receive work
25
- func ProcReceive (ctx * context.PrivateContext , opts * private.HookOptions ) []private.HookProcReceiveRefResult {
24
+ func ProcReceive (ctx context.Context , repo * repo_model. Repository , gitRepo * git. Repository , opts * private.HookOptions ) ( []private.HookProcReceiveRefResult , error ) {
26
25
// TODO: Add more options?
27
26
var (
28
27
topicBranch string
@@ -32,10 +31,9 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
32
31
)
33
32
34
33
results := make ([]private.HookProcReceiveRefResult , 0 , len (opts .OldCommitIDs ))
35
- repo := ctx .Repo .Repository
36
- gitRepo := ctx .Repo .GitRepo
37
- ownerName := ctx .Repo .Repository .OwnerName
38
- repoName := ctx .Repo .Repository .Name
34
+
35
+ ownerName := repo .OwnerName
36
+ repoName := repo .Name
39
37
40
38
topicBranch = opts .GitPushOptions ["topic" ]
41
39
_ , forcePush = opts .GitPushOptions ["force-push" ]
@@ -101,11 +99,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
101
99
pr , err := issues_model .GetUnmergedPullRequest (repo .ID , repo .ID , headBranch , baseBranchName , issues_model .PullRequestFlowAGit )
102
100
if err != nil {
103
101
if ! issues_model .IsErrPullRequestNotExist (err ) {
104
- log .Error ("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v" , ownerName , repoName , err )
105
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
106
- "Err" : fmt .Sprintf ("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v" , ownerName , repoName , err ),
107
- })
108
- return nil
102
+ return nil , fmt .Errorf ("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v" , ownerName , repoName , err )
109
103
}
110
104
111
105
// create a new pull request
@@ -115,11 +109,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
115
109
if ! has || len (title ) == 0 {
116
110
commit , err := gitRepo .GetCommit (opts .NewCommitIDs [i ])
117
111
if err != nil {
118
- log .Error ("Failed to get commit %s in repository: %s/%s Error: %v" , opts .NewCommitIDs [i ], ownerName , repoName , err )
119
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
120
- "Err" : fmt .Sprintf ("Failed to get commit %s in repository: %s/%s Error: %v" , opts .NewCommitIDs [i ], ownerName , repoName , err ),
121
- })
122
- return nil
112
+ return nil , fmt .Errorf ("Failed to get commit %s in repository: %s/%s Error: %v" , opts .NewCommitIDs [i ], ownerName , repoName , err )
123
113
}
124
114
title = strings .Split (commit .CommitMessage , "\n " )[0 ]
125
115
}
@@ -128,11 +118,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
128
118
129
119
pusher , err := user_model .GetUserByID (opts .UserID )
130
120
if err != nil {
131
- log .Error ("Failed to get user. Error: %v" , err )
132
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
133
- "Err" : fmt .Sprintf ("Failed to get user. Error: %v" , err ),
134
- })
135
- return nil
121
+ return nil , fmt .Errorf ("Failed to get user. Error: %v" , err )
136
122
}
137
123
138
124
prIssue := & issues_model.Issue {
@@ -158,12 +144,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
158
144
}
159
145
160
146
if err := pull_service .NewPullRequest (ctx , repo , prIssue , []int64 {}, []string {}, pr , []int64 {}); err != nil {
161
- if repo_model .IsErrUserDoesNotHaveAccessToRepo (err ) {
162
- ctx .Error (http .StatusBadRequest , "UserDoesNotHaveAccessToRepo" , err .Error ())
163
- return nil
164
- }
165
- ctx .Error (http .StatusInternalServerError , "NewPullRequest" , err .Error ())
166
- return nil
147
+ return nil , err
167
148
}
168
149
169
150
log .Trace ("Pull request created: %d/%d" , repo .ID , prIssue .ID )
@@ -179,20 +160,12 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
179
160
180
161
// update exist pull request
181
162
if err := pr .LoadBaseRepoCtx (ctx ); err != nil {
182
- log .Error ("Unable to load base repository for PR[%d] Error: %v" , pr .ID , err )
183
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
184
- "Err" : fmt .Sprintf ("Unable to load base repository for PR[%d] Error: %v" , pr .ID , err ),
185
- })
186
- return nil
163
+ return nil , fmt .Errorf ("Unable to load base repository for PR[%d] Error: %v" , pr .ID , err )
187
164
}
188
165
189
166
oldCommitID , err := gitRepo .GetRefCommitID (pr .GetGitRefName ())
190
167
if err != nil {
191
- log .Error ("Unable to get ref commit id in base repository for PR[%d] Error: %v" , pr .ID , err )
192
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
193
- "Err" : fmt .Sprintf ("Unable to get ref commit id in base repository for PR[%d] Error: %v" , pr .ID , err ),
194
- })
195
- return nil
168
+ return nil , fmt .Errorf ("Unable to get ref commit id in base repository for PR[%d] Error: %v" , pr .ID , err )
196
169
}
197
170
198
171
if oldCommitID == opts .NewCommitIDs [i ] {
@@ -208,11 +181,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
208
181
if ! forcePush {
209
182
output , _ , err := git .NewCommand (ctx , "rev-list" , "--max-count=1" , oldCommitID , "^" + opts .NewCommitIDs [i ]).RunStdString (& git.RunOpts {Dir : repo .RepoPath (), Env : os .Environ ()})
210
183
if err != nil {
211
- log .Error ("Unable to detect force push between: %s and %s in %-v Error: %v" , oldCommitID , opts .NewCommitIDs [i ], repo , err )
212
- ctx .JSON (http .StatusInternalServerError , private.Response {
213
- Err : fmt .Sprintf ("Fail to detect force push: %v" , err ),
214
- })
215
- return nil
184
+ return nil , fmt .Errorf ("Fail to detect force push: %v" , err )
216
185
} else if len (output ) > 0 {
217
186
results = append (results , private.HookProcReceiveRefResult {
218
187
OriginalRef : opts .RefFullNames [i ],
@@ -226,29 +195,17 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
226
195
227
196
pr .HeadCommitID = opts .NewCommitIDs [i ]
228
197
if err = pull_service .UpdateRef (ctx , pr ); err != nil {
229
- log .Error ("Failed to update pull ref. Error: %v" , err )
230
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
231
- "Err" : fmt .Sprintf ("Failed to update pull ref. Error: %v" , err ),
232
- })
233
- return nil
198
+ return nil , fmt .Errorf ("Failed to update pull ref. Error: %v" , err )
234
199
}
235
200
236
201
pull_service .AddToTaskQueue (pr )
237
202
pusher , err := user_model .GetUserByID (opts .UserID )
238
203
if err != nil {
239
- log .Error ("Failed to get user. Error: %v" , err )
240
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
241
- "Err" : fmt .Sprintf ("Failed to get user. Error: %v" , err ),
242
- })
243
- return nil
204
+ return nil , fmt .Errorf ("Failed to get user. Error: %v" , err )
244
205
}
245
206
err = pr .LoadIssue ()
246
207
if err != nil {
247
- log .Error ("Failed to load pull issue. Error: %v" , err )
248
- ctx .JSON (http .StatusInternalServerError , map [string ]interface {}{
249
- "Err" : fmt .Sprintf ("Failed to load pull issue. Error: %v" , err ),
250
- })
251
- return nil
208
+ return nil , fmt .Errorf ("Failed to load pull issue. Error: %v" , err )
252
209
}
253
210
comment , err := issues_model .CreatePushPullComment (ctx , pusher , pr , oldCommitID , opts .NewCommitIDs [i ])
254
211
if err == nil && comment != nil {
@@ -266,7 +223,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
266
223
})
267
224
}
268
225
269
- return results
226
+ return results , nil
270
227
}
271
228
272
229
// UserNameChanged handle user name change for agit flow pull
0 commit comments