Skip to content

Commit da15d61

Browse files
lunnyKN4CK3R
andauthored
A small refactor for agit implementation (#29614)
This PR made the code simpler, reduced unnecessary database queries and fixed some warnning for the errors.New . --------- Co-authored-by: KN4CK3R <[email protected]>
1 parent 8d32f3c commit da15d61

File tree

1 file changed

+33
-40
lines changed

1 file changed

+33
-40
lines changed

services/agit/agit.go

+33-40
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"os"
10+
"strconv"
1011
"strings"
1112

1213
issues_model "code.gitea.io/gitea/models/issues"
@@ -21,26 +22,17 @@ import (
2122

2223
// ProcReceive handle proc receive work
2324
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-
3225
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?
3930
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
31+
userName := strings.ToLower(opts.UserName)
4032

4133
pusher, err := user_model.GetUserByID(ctx, opts.UserID)
4234
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)
4436
}
4537

4638
for i := range opts.OldCommitIDs {
@@ -85,16 +77,14 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
8577
continue
8678
}
8779

88-
var headBranch string
89-
userName := strings.ToLower(opts.UserName)
90-
9180
if len(curentTopicBranch) == 0 {
9281
curentTopicBranch = topicBranch
9382
}
9483

9584
// because different user maybe want to use same topic,
9685
// So it's better to make sure the topic branch name
9786
// has user name prefix
87+
var headBranch string
9888
if !strings.HasPrefix(curentTopicBranch, userName+"/") {
9989
headBranch = userName + "/" + curentTopicBranch
10090
} else {
@@ -104,21 +94,26 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
10494
pr, err := issues_model.GetUnmergedPullRequest(ctx, repo.ID, repo.ID, headBranch, baseBranchName, issues_model.PullRequestFlowAGit)
10595
if err != nil {
10696
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)
10898
}
10999

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)
120105
}
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
122117
}
123118

124119
prIssue := &issues_model.Issue{
@@ -160,12 +155,12 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
160155

161156
// update exist pull request
162157
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)
164159
}
165160

166161
oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
167162
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)
169164
}
170165

171166
if oldCommitID == opts.NewCommitIDs[i] {
@@ -179,9 +174,11 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
179174
}
180175

181176
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()})
183180
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)
185182
} else if len(output) > 0 {
186183
results = append(results, private.HookProcReceiveRefResult{
187184
OriginalRef: opts.RefFullNames[i],
@@ -195,17 +192,13 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
195192

196193
pr.HeadCommitID = opts.NewCommitIDs[i]
197194
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)
199196
}
200197

201198
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-
}
206199
err = pr.LoadIssue(ctx)
207200
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)
209202
}
210203
comment, err := pull_service.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i])
211204
if err == nil && comment != nil {

0 commit comments

Comments
 (0)