7
7
"context"
8
8
"fmt"
9
9
"os"
10
+ "strconv"
10
11
"strings"
11
12
12
13
issues_model "code.gitea.io/gitea/models/issues"
@@ -21,26 +22,17 @@ import (
21
22
22
23
// ProcReceive handle proc receive work
23
24
func ProcReceive (ctx context.Context , repo * repo_model.Repository , gitRepo * git.Repository , opts * private.HookOptions ) ([]private.HookProcReceiveRefResult , error ) {
24
- // TODO: Add more options?
25
- var (
26
- topicBranch string
27
- title string
28
- description string
29
- forcePush bool
30
- )
31
-
32
25
results := make ([]private.HookProcReceiveRefResult , 0 , len (opts .OldCommitIDs ))
33
-
34
- ownerName := repo .OwnerName
35
- repoName := repo .Name
36
-
37
- topicBranch = opts .GitPushOptions ["topic" ]
38
- _ , forcePush = opts .GitPushOptions ["force-push" ]
26
+ topicBranch := opts .GitPushOptions ["topic" ]
27
+ forcePush , _ := strconv .ParseBool (opts .GitPushOptions ["force-push" ])
28
+ title := strings .TrimSpace (opts .GitPushOptions ["title" ])
29
+ description := strings .TrimSpace (opts .GitPushOptions ["description" ]) // TODO: Add more options?
39
30
objectFormat := git .ObjectFormatFromName (repo .ObjectFormatName )
31
+ userName := strings .ToLower (opts .UserName )
40
32
41
33
pusher , err := user_model .GetUserByID (ctx , opts .UserID )
42
34
if err != nil {
43
- return nil , fmt .Errorf ("Failed to get user. Error: %w" , err )
35
+ return nil , fmt .Errorf ("failed to get user. Error: %w" , err )
44
36
}
45
37
46
38
for i := range opts .OldCommitIDs {
@@ -85,16 +77,14 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
85
77
continue
86
78
}
87
79
88
- var headBranch string
89
- userName := strings .ToLower (opts .UserName )
90
-
91
80
if len (curentTopicBranch ) == 0 {
92
81
curentTopicBranch = topicBranch
93
82
}
94
83
95
84
// because different user maybe want to use same topic,
96
85
// So it's better to make sure the topic branch name
97
86
// has user name prefix
87
+ var headBranch string
98
88
if ! strings .HasPrefix (curentTopicBranch , userName + "/" ) {
99
89
headBranch = userName + "/" + curentTopicBranch
100
90
} else {
@@ -104,21 +94,26 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
104
94
pr , err := issues_model .GetUnmergedPullRequest (ctx , repo .ID , repo .ID , headBranch , baseBranchName , issues_model .PullRequestFlowAGit )
105
95
if err != nil {
106
96
if ! issues_model .IsErrPullRequestNotExist (err ) {
107
- return nil , fmt .Errorf ("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %w" , ownerName , repoName , err )
97
+ return nil , fmt .Errorf ("failed to get unmerged agit flow pull request in repository: %s Error: %w" , repo . FullName () , err )
108
98
}
109
99
110
- // create a new pull request
111
- if len (title ) == 0 {
112
- var has bool
113
- title , has = opts .GitPushOptions ["title" ]
114
- if ! has || len (title ) == 0 {
115
- commit , err := gitRepo .GetCommit (opts .NewCommitIDs [i ])
116
- if err != nil {
117
- return nil , fmt .Errorf ("Failed to get commit %s in repository: %s/%s Error: %w" , opts .NewCommitIDs [i ], ownerName , repoName , err )
118
- }
119
- title = strings .Split (commit .CommitMessage , "\n " )[0 ]
100
+ var commit * git.Commit
101
+ if title == "" || description == "" {
102
+ commit , err = gitRepo .GetCommit (opts .NewCommitIDs [i ])
103
+ if err != nil {
104
+ return nil , fmt .Errorf ("failed to get commit %s in repository: %s Error: %w" , opts .NewCommitIDs [i ], repo .FullName (), err )
120
105
}
121
- description = opts .GitPushOptions ["description" ]
106
+ }
107
+
108
+ // create a new pull request
109
+ if title == "" {
110
+ title = strings .Split (commit .CommitMessage , "\n " )[0 ]
111
+ }
112
+ if description == "" {
113
+ _ , description , _ = strings .Cut (commit .CommitMessage , "\n \n " )
114
+ }
115
+ if description == "" {
116
+ description = title
122
117
}
123
118
124
119
prIssue := & issues_model.Issue {
@@ -160,12 +155,12 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
160
155
161
156
// update exist pull request
162
157
if err := pr .LoadBaseRepo (ctx ); err != nil {
163
- return nil , fmt .Errorf ("Unable to load base repository for PR[%d] Error: %w" , pr .ID , err )
158
+ return nil , fmt .Errorf ("unable to load base repository for PR[%d] Error: %w" , pr .ID , err )
164
159
}
165
160
166
161
oldCommitID , err := gitRepo .GetRefCommitID (pr .GetGitRefName ())
167
162
if err != nil {
168
- return nil , fmt .Errorf ("Unable to get ref commit id in base repository for PR[%d] Error: %w" , pr .ID , err )
163
+ return nil , fmt .Errorf ("unable to get ref commit id in base repository for PR[%d] Error: %w" , pr .ID , err )
169
164
}
170
165
171
166
if oldCommitID == opts .NewCommitIDs [i ] {
@@ -179,9 +174,11 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
179
174
}
180
175
181
176
if ! forcePush {
182
- output , _ , err := git .NewCommand (ctx , "rev-list" , "--max-count=1" ).AddDynamicArguments (oldCommitID , "^" + opts .NewCommitIDs [i ]).RunStdString (& git.RunOpts {Dir : repo .RepoPath (), Env : os .Environ ()})
177
+ output , _ , err := git .NewCommand (ctx , "rev-list" , "--max-count=1" ).
178
+ AddDynamicArguments (oldCommitID , "^" + opts .NewCommitIDs [i ]).
179
+ RunStdString (& git.RunOpts {Dir : repo .RepoPath (), Env : os .Environ ()})
183
180
if err != nil {
184
- return nil , fmt .Errorf ("Fail to detect force push: %w" , err )
181
+ return nil , fmt .Errorf ("failed to detect force push: %w" , err )
185
182
} else if len (output ) > 0 {
186
183
results = append (results , private.HookProcReceiveRefResult {
187
184
OriginalRef : opts .RefFullNames [i ],
@@ -195,17 +192,13 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
195
192
196
193
pr .HeadCommitID = opts .NewCommitIDs [i ]
197
194
if err = pull_service .UpdateRef (ctx , pr ); err != nil {
198
- return nil , fmt .Errorf ("Failed to update pull ref. Error: %w" , err )
195
+ return nil , fmt .Errorf ("failed to update pull ref. Error: %w" , err )
199
196
}
200
197
201
198
pull_service .AddToTaskQueue (ctx , pr )
202
- pusher , err := user_model .GetUserByID (ctx , opts .UserID )
203
- if err != nil {
204
- return nil , fmt .Errorf ("Failed to get user. Error: %w" , err )
205
- }
206
199
err = pr .LoadIssue (ctx )
207
200
if err != nil {
208
- return nil , fmt .Errorf ("Failed to load pull issue. Error: %w" , err )
201
+ return nil , fmt .Errorf ("failed to load pull issue. Error: %w" , err )
209
202
}
210
203
comment , err := pull_service .CreatePushPullComment (ctx , pusher , pr , oldCommitID , opts .NewCommitIDs [i ])
211
204
if err == nil && comment != nil {
0 commit comments