Browse Source

fix bug on status API (#1533)

Lunny Xiao 2 years ago
parent
commit
3012971e92
1 changed files with 5 additions and 16 deletions
  1. 5 16
      models/status.go

+ 5 - 16
models/status.go

@@ -139,18 +139,13 @@ func (status *CommitStatus) APIFormat() *api.Status {
139 139
 // GetCommitStatuses returns all statuses for a given commit.
140 140
 func GetCommitStatuses(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
141 141
 	statuses := make([]*CommitStatus, 0, 10)
142
-	sess := x.NewSession()
143
-	defer sess.Close()
144
-	return statuses, sess.Limit(10, page*10).Where("repo_id = ?", repo.ID).And("sha = ?", sha).Find(&statuses)
142
+	return statuses, x.Limit(10, page*10).Where("repo_id = ?", repo.ID).And("sha = ?", sha).Find(&statuses)
145 143
 }
146 144
 
147 145
 // GetLatestCommitStatus returns all statuses with a unique context for a given commit.
148 146
 func GetLatestCommitStatus(repo *Repository, sha string, page int) ([]*CommitStatus, error) {
149 147
 	statuses := make([]*CommitStatus, 0, 10)
150
-	sess := x.NewSession()
151
-	defer sess.Close()
152
-
153
-	return statuses, sess.Limit(10, page*10).
148
+	return statuses, x.Limit(10, page*10).
154 149
 		Where("repo_id = ?", repo.ID).And("sha = ?", sha).Select("*").
155 150
 		GroupBy("context").Desc("created_unix").Find(&statuses)
156 151
 }
@@ -184,7 +179,7 @@ type NewCommitStatusOptions struct {
184 179
 	CommitStatus *CommitStatus
185 180
 }
186 181
 
187
-func newCommitStatus(e Engine, opts NewCommitStatusOptions) error {
182
+func newCommitStatus(sess *xorm.Session, opts NewCommitStatusOptions) error {
188 183
 	opts.CommitStatus.Description = strings.TrimSpace(opts.CommitStatus.Description)
189 184
 	opts.CommitStatus.Context = strings.TrimSpace(opts.CommitStatus.Context)
190 185
 	opts.CommitStatus.TargetURL = strings.TrimSpace(opts.CommitStatus.TargetURL)
@@ -208,12 +203,6 @@ func newCommitStatus(e Engine, opts NewCommitStatusOptions) error {
208 203
 		return fmt.Errorf("GetCommit[%s]: %v", opts.SHA, err)
209 204
 	}
210 205
 
211
-	sess := x.NewSession()
212
-	defer sess.Close()
213
-	if err = sess.Begin(); err != nil {
214
-		return fmt.Errorf("newCommitStatus[%s, %s]: %v", opts.Repo.RepoPath(), opts.SHA, err)
215
-	}
216
-
217 206
 	// Get the next Status Index
218 207
 	var nextIndex int64
219 208
 	lastCommitStatus := &CommitStatus{
@@ -238,7 +227,7 @@ func newCommitStatus(e Engine, opts NewCommitStatusOptions) error {
238 227
 		return fmt.Errorf("newCommitStatus[%s, %s]: %v", opts.Repo.RepoPath(), opts.SHA, err)
239 228
 	}
240 229
 
241
-	return sess.Commit()
230
+	return nil
242 231
 }
243 232
 
244 233
 // NewCommitStatus creates a new CommitStatus given a bunch of parameters
@@ -261,5 +250,5 @@ func NewCommitStatus(repo *Repository, creator *User, sha string, status *Commit
261 250
 		return fmt.Errorf("NewCommitStatus[repo_id: %d, user_id: %d, sha: %s]: %v", repo.ID, creator.ID, sha, err)
262 251
 	}
263 252
 
264
-	return nil
253
+	return sess.Commit()
265 254
 }