Skip to content

Commit 011ad74

Browse files
committed
Add errors
1 parent 97e3347 commit 011ad74

File tree

3 files changed

+86
-32
lines changed

3 files changed

+86
-32
lines changed

models/issues/issue.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,6 @@ func (err ErrNewIssueInsert) Error() string {
7878
return err.OriginalError.Error()
7979
}
8080

81-
// ErrIssueWasClosed is used when close a closed issue
82-
type ErrIssueWasClosed struct {
83-
ID int64
84-
Index int64
85-
}
86-
87-
// IsErrIssueWasClosed checks if an error is a ErrIssueWasClosed.
88-
func IsErrIssueWasClosed(err error) bool {
89-
_, ok := err.(ErrIssueWasClosed)
90-
return ok
91-
}
92-
93-
func (err ErrIssueWasClosed) Error() string {
94-
return fmt.Sprintf("Issue [%d] %d was already closed", err.ID, err.Index)
95-
}
96-
9781
var ErrIssueAlreadyChanged = util.NewInvalidArgumentErrorf("the issue is already changed")
9882

9983
// Issue represents an issue or pull request of repository.

models/issues/issue_update.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,50 @@ func UpdateIssueCols(ctx context.Context, issue *Issue, cols ...string) error {
3232
return err
3333
}
3434

35+
// ErrIssueWasClosed is used when close a closed issue
36+
type ErrIssueWasClosed struct {
37+
ID int64
38+
Index int64
39+
}
40+
41+
// IsErrIssueWasClosed checks if an error is a ErrIssueWasClosed.
42+
func IsErrIssueWasClosed(err error) bool {
43+
_, ok := err.(ErrIssueWasClosed)
44+
return ok
45+
}
46+
47+
func (err ErrIssueWasClosed) Error() string {
48+
return fmt.Sprintf("Issue [%d] %d was already closed", err.ID, err.Index)
49+
}
50+
51+
// ErrPullWasClosed is used close a closed pull request
52+
type ErrPullWasClosed struct {
53+
ID int64
54+
Index int64
55+
}
56+
57+
// IsErrPullWasClosed checks if an error is a ErrErrPullWasClosed.
58+
func IsErrPullWasClosed(err error) bool {
59+
_, ok := err.(ErrPullWasClosed)
60+
return ok
61+
}
62+
63+
func (err ErrPullWasClosed) Error() string {
64+
return fmt.Sprintf("Pull request [%d] %d was already closed", err.ID, err.Index)
65+
}
66+
3567
func closeIssue(ctx context.Context, issue *Issue, doer *user_model.User, isMergePull bool) (*Comment, error) {
68+
if issue.IsClosed {
69+
if !issue.IsPull {
70+
return nil, ErrIssueWasClosed{
71+
ID: issue.ID,
72+
}
73+
}
74+
return nil, ErrPullWasClosed{
75+
ID: issue.ID,
76+
}
77+
}
78+
3679
// Check for open dependencies
3780
if issue.Repo.IsDependenciesEnabled(ctx) {
3881
// only check if dependencies are enabled and we're about to close an issue, otherwise reopening an issue would fail when there are unsatisfied dependencies
@@ -60,7 +103,50 @@ func closeIssue(ctx context.Context, issue *Issue, doer *user_model.User, isMerg
60103
return updateIssueNumbers(ctx, issue, doer, util.Iif(isMergePull, CommentTypeMergePull, CommentTypeClose))
61104
}
62105

106+
// ErrIssueWasOpened is used when reopen an opened issue
107+
type ErrIssueWasOpened struct {
108+
ID int64
109+
Index int64
110+
}
111+
112+
// IsErrIssueWasOpened checks if an error is a ErrIssueWasOpened.
113+
func IsErrIssueWasOpened(err error) bool {
114+
_, ok := err.(ErrIssueWasOpened)
115+
return ok
116+
}
117+
118+
func (err ErrIssueWasOpened) Error() string {
119+
return fmt.Sprintf("Issue [%d] %d was already opened", err.ID, err.Index)
120+
}
121+
122+
// ErrPullWasOpened is used reopen an opened pull request
123+
type ErrPullWasOpened struct {
124+
ID int64
125+
Index int64
126+
}
127+
128+
// ErrPullWasOpened checks if an error is a ErrPullWasOpened.
129+
func IsErrPullWasOpened(err error) bool {
130+
_, ok := err.(ErrPullWasOpened)
131+
return ok
132+
}
133+
134+
func (err ErrPullWasOpened) Error() string {
135+
return fmt.Sprintf("Pull request [%d] %d was already opened", err.ID, err.Index)
136+
}
137+
63138
func SetIssueAsReopen(ctx context.Context, issue *Issue, doer *user_model.User, isMergePull bool) (*Comment, error) {
139+
if !issue.IsClosed {
140+
if !issue.IsPull {
141+
return nil, ErrIssueWasOpened{
142+
ID: issue.ID,
143+
}
144+
}
145+
return nil, ErrPullWasOpened{
146+
ID: issue.ID,
147+
}
148+
}
149+
64150
issue.IsClosed = false
65151
issue.ClosedUnix = 0
66152

models/issues/pull.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,6 @@ func (err ErrPullRequestAlreadyExists) Unwrap() error {
8080
return util.ErrAlreadyExist
8181
}
8282

83-
// ErrPullWasClosed is used close a closed pull request
84-
type ErrPullWasClosed struct {
85-
ID int64
86-
Index int64
87-
}
88-
89-
// IsErrPullWasClosed checks if an error is a ErrErrPullWasClosed.
90-
func IsErrPullWasClosed(err error) bool {
91-
_, ok := err.(ErrPullWasClosed)
92-
return ok
93-
}
94-
95-
func (err ErrPullWasClosed) Error() string {
96-
return fmt.Sprintf("Pull request [%d] %d was already closed", err.ID, err.Index)
97-
}
98-
9983
// PullRequestType defines pull request type
10084
type PullRequestType int
10185

0 commit comments

Comments
 (0)