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