Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API for Issue set Subscription #8729

Merged
merged 41 commits into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
06daf4e
add issue subscriber API
6543 Oct 29, 2019
deaf69c
subscribers return []user.APIFormat
6543 Oct 29, 2019
a7c7674
add comments
6543 Oct 29, 2019
413b2b6
more meaningfull description
6543 Oct 29, 2019
1aced19
Merge branch 'master' into add-api-issues-subscriptions
6543 Oct 29, 2019
7c24ae3
without "reqToken()" api works ...
6543 Oct 29, 2019
2de7290
Merge branch 'master' into add-api-issues-subscriptions
6543 Oct 29, 2019
5eca929
FIX: getIssueWatchers() get only aktive suscriber
6543 Oct 29, 2019
e8bb557
add return avter error on right position
6543 Oct 29, 2019
2ca4a7e
Revert "FIX: getIssueWatchers() get only aktive suscriber"
6543 Oct 29, 2019
6e948d7
Merge branch 'master' into add-api-issues-subscriptions
6543 Oct 29, 2019
b2b6c05
Merge branch 'master' into add-api-issues-subscriptions
6543 Oct 30, 2019
89c5ba9
Merge branch 'master' into add-api-issues-subscriptions
6543 Oct 30, 2019
f04f7c9
Merge branch 'master' into add-api-issues-subscriptions
6543 Oct 31, 2019
432a38c
Update routers/api/v1/repo/issue.go
6543 Nov 1, 2019
bab1235
test go linter again
6543 Nov 1, 2019
69b5d7e
update swagger
6543 Nov 1, 2019
3d693fa
GetIssueWatchers -> GetIssueSubscribers
6543 Nov 1, 2019
dea6928
GetIssueWatchers -> GetIssueSubscribers
6543 Nov 1, 2019
59ca419
Revert "test go linter again"
6543 Nov 1, 2019
c7a0e7b
Merge branch 'master' into add-api-issues-subscriptions
6543 Nov 1, 2019
bb6185c
change description for unsubscribe too
6543 Nov 1, 2019
3de794d
golangci-lint timeout avter 5min
6543 Nov 1, 2019
da005ac
move issueSubscription to seperate file
6543 Nov 1, 2019
fb325d7
dont create black entitys
6543 Nov 1, 2019
bd3c737
use IsWatching until refactoring
6543 Nov 1, 2019
6f451e0
Merge branch 'master' into add-api-issues-subscriptions
6543 Nov 1, 2019
9c3bbcc
Update License Info
6543 Nov 1, 2019
d52dbcb
better swagger description
6543 Nov 1, 2019
02ea6db
Merge branch 'master' into add-api-issues-subscriptions
6543 Nov 2, 2019
1fcd434
Update .golangci.yml
6543 Nov 2, 2019
4a8d2d7
add IssueWatchList type
6543 Nov 2, 2019
577aa9c
batch tasks
6543 Nov 2, 2019
dbc2796
Merge branch 'master' into add-api-issues-subscriptions
6543 Nov 2, 2019
34f3143
use e Engien
6543 Nov 2, 2019
8bb2043
add error handling
6543 Nov 2, 2019
ad29e4c
error should be the last type when returning multiple items
6543 Nov 2, 2019
e0b11f9
short version
6543 Nov 2, 2019
2056321
reurn empy UserList instead of nil
6543 Nov 2, 2019
94d3aa3
Merge branch 'master' into add-api-issues-subscriptions
6543 Nov 2, 2019
5ce8c04
Merge branch 'master' into add-api-issues-subscriptions
6543 Nov 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ issues:
- path: routers/routes/routes.go
linters:
- dupl
- path: routers/api/v1/repo/issue_subscription.go
linters:
- dupl
zeripath marked this conversation as resolved.
Show resolved Hide resolved
- path: routers/repo/view.go
linters:
- dupl
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -521,4 +521,4 @@ golangci-lint:
export BINARY="golangci-lint"; \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(GOPATH)/bin v1.20.0; \
fi
golangci-lint run
golangci-lint run --timeout 5m
27 changes: 27 additions & 0 deletions models/issue_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type IssueWatch struct {
UpdatedUnix timeutil.TimeStamp `xorm:"updated NOT NULL"`
}

// IssueWatchList contains IssueWatch
type IssueWatchList []*IssueWatch

// CreateOrUpdateIssueWatch set watching for a user and issue
func CreateOrUpdateIssueWatch(userID, issueID int64, isWatching bool) error {
iw, exists, err := getIssueWatch(x, userID, issueID)
Expand Down Expand Up @@ -83,3 +86,27 @@ func removeIssueWatchersByRepoID(e Engine, userID int64, repoID int64) error {
Update(iw)
return err
}

// LoadWatchUsers return watching users
func (iwl IssueWatchList) LoadWatchUsers() UserList {
return iwl.loadWatchUsers(x)
}

func (iwl IssueWatchList) loadWatchUsers(e Engine) UserList {
if len(iwl) == 0 {
return nil
}

var userIDs = make([]int64, 0, len(iwl))
for _, iw := range iwl {
if iw.IsWatching {
userIDs = append(userIDs, iw.UserID)
}
}

var users UserList

x.In("id", userIDs).Find(&users)
6543 marked this conversation as resolved.
Show resolved Hide resolved

return users
}
10 changes: 10 additions & 0 deletions models/userlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"code.gitea.io/gitea/modules/log"
api "code.gitea.io/gitea/modules/structs"
)

//UserList is a list of user.
Expand Down Expand Up @@ -93,3 +94,12 @@ func (users UserList) loadTwoFactorStatus(e Engine) (map[int64]*TwoFactor, error
}
return tokenMaps, nil
}

//APIFormat return list of users in api format
func (users UserList) APIFormat() []*api.User {
var result []*api.User
for _, u := range users {
result = append(result, u.APIFormat())
}
return result
}
5 changes: 5 additions & 0 deletions routers/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,11 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("/start", reqToken(), repo.StartIssueStopwatch)
m.Post("/stop", reqToken(), repo.StopIssueStopwatch)
})
m.Group("/subscriptions", func() {
m.Get("", bind(api.User{}), repo.GetIssueSubscribers)
m.Put("/:user", repo.AddIssueSubscription)
m.Delete("/:user", repo.DelIssueSubscription)
})
})
}, mustEnableIssuesOrPulls)
m.Group("/labels", func() {
Expand Down
212 changes: 212 additions & 0 deletions routers/api/v1/repo/issue_subscription.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package repo

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
api "code.gitea.io/gitea/modules/structs"
)

// AddIssueSubscription Subscribe user to issue
func AddIssueSubscription(ctx *context.APIContext) {
// swagger:operation PUT /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueAddSubscription
// ---
// summary: Subscribe user to issue
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: index
// in: path
// description: index of the issue
// type: integer
// format: int64
// required: true
// - name: user
// in: path
// description: user to subscribe
// type: string
// required: true
// responses:
// "201":
// "$ref": "#/responses/empty"
// "304":
// description: User can only subscribe itself if he is no admin
// "404":
// description: Issue not found
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrIssueNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(500, "GetIssueByIndex", err)
}

return
}

user, err := models.GetUserByName(ctx.Params(":user"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(500, "GetUserByName", err)
}

return
}

//only admin and user for itself can change subscription
if user.ID != ctx.User.ID && !ctx.User.IsAdmin {
ctx.Error(403, "User", nil)
return
}

if err := models.CreateOrUpdateIssueWatch(user.ID, issue.ID, true); err != nil {
ctx.Error(500, "CreateOrUpdateIssueWatch", err)
return
}

ctx.Status(201)
}

// DelIssueSubscription Unsubscribe user from issue
func DelIssueSubscription(ctx *context.APIContext) {
// swagger:operation DELETE /repos/{owner}/{repo}/issues/{index}/subscriptions/{user} issue issueDeleteSubscription
// ---
// summary: Unsubscribe user from issue
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: index
// in: path
// description: index of the issue
// type: integer
// format: int64
// required: true
// - name: user
// in: path
// description: user witch unsubscribe
// type: string
// required: true
// responses:
// "201":
// "$ref": "#/responses/empty"
// "304":
// description: User can only subscribe itself if he is no admin
// "404":
// description: Issue not found
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrIssueNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(500, "GetIssueByIndex", err)
}

return
}

user, err := models.GetUserByName(ctx.Params(":user"))
if err != nil {
if models.IsErrUserNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(500, "GetUserByName", err)
}

return
}

//only admin and user for itself can change subscription
if user.ID != ctx.User.ID && !ctx.User.IsAdmin {
ctx.Error(403, "User", nil)
return
}

if err := models.CreateOrUpdateIssueWatch(user.ID, issue.ID, false); err != nil {
ctx.Error(500, "CreateOrUpdateIssueWatch", err)
return
}

ctx.Status(201)
}

// GetIssueSubscribers return subscribers of an issue
func GetIssueSubscribers(ctx *context.APIContext, form api.User) {
// swagger:operation GET /repos/{owner}/{repo}/issues/{index}/subscriptions issue issueSubscriptions
// ---
// summary: Get users who subscribed on an issue.
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: index
// in: path
// description: index of the issue
// type: integer
// format: int64
// required: true
// responses:
// "201":
// "$ref": "#/responses/empty"
// "404":
// description: Issue not found
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
if err != nil {
if models.IsErrIssueNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(500, "GetIssueByIndex", err)
}

return
}

iwl, err := models.GetIssueWatchers(issue.ID)
6543 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
ctx.Error(500, "GetIssueWatchers", err)
return
}

users := models.UserList.APIFormat(models.IssueWatchList.LoadWatchUsers(iwl))

ctx.JSON(200, users)
}
Loading