Skip to content

Commit

Permalink
Merge branch 'main' into fix-17077
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny authored Sep 18, 2021
2 parents 4016938 + 0b368aa commit 5e20a94
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 42 deletions.
8 changes: 4 additions & 4 deletions models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -772,20 +772,20 @@ func setRepoNotificationStatusReadIfUnread(e Engine, userID, repoID int64) error
}

// SetNotificationStatus change the notification status
func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) error {
func SetNotificationStatus(notificationID int64, user *User, status NotificationStatus) (*Notification, error) {
notification, err := getNotificationByID(x, notificationID)
if err != nil {
return err
return notification, err
}

if notification.UserID != user.ID {
return fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
return nil, fmt.Errorf("Can't change notification of another user: %d, %d", notification.UserID, user.ID)
}

notification.Status = status

_, err = x.ID(notificationID).Update(notification)
return err
return notification, err
}

// GetNotificationByID return notification by ID
Expand Down
9 changes: 6 additions & 3 deletions models/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ func TestSetNotificationStatus(t *testing.T) {
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
notf := AssertExistsAndLoadBean(t,
&Notification{UserID: user.ID, Status: NotificationStatusRead}).(*Notification)
assert.NoError(t, SetNotificationStatus(notf.ID, user, NotificationStatusPinned))
_, err := SetNotificationStatus(notf.ID, user, NotificationStatusPinned)
assert.NoError(t, err)
AssertExistsAndLoadBean(t,
&Notification{ID: notf.ID, Status: NotificationStatusPinned})

assert.Error(t, SetNotificationStatus(1, user, NotificationStatusRead))
assert.Error(t, SetNotificationStatus(NonexistentID, user, NotificationStatusRead))
_, err = SetNotificationStatus(1, user, NotificationStatusRead)
assert.Error(t, err)
_, err = SetNotificationStatus(NonexistentID, user, NotificationStatusRead)
assert.Error(t, err)
}

func TestUpdateNotificationStatuses(t *testing.T) {
Expand Down
23 changes: 21 additions & 2 deletions modules/git/batch_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"math"
"runtime"
"strconv"
"strings"

Expand Down Expand Up @@ -40,9 +42,14 @@ func CatFileBatchCheck(repoPath string) (WriteCloserError, *bufio.Reader, func()
<-closed
}

_, filename, line, _ := runtime.Caller(2)
filename = strings.TrimPrefix(filename, callerPrefix)

go func() {
stderr := strings.Builder{}
err := NewCommandContext(ctx, "cat-file", "--batch-check").RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
err := NewCommandContext(ctx, "cat-file", "--batch-check").
SetDescription(fmt.Sprintf("%s cat-file --batch-check [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)).
RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
if err != nil {
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
Expand Down Expand Up @@ -76,9 +83,14 @@ func CatFileBatch(repoPath string) (WriteCloserError, *bufio.Reader, func()) {
<-closed
}

_, filename, line, _ := runtime.Caller(2)
filename = strings.TrimPrefix(filename, callerPrefix)

go func() {
stderr := strings.Builder{}
err := NewCommandContext(ctx, "cat-file", "--batch").RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
err := NewCommandContext(ctx, "cat-file", "--batch").
SetDescription(fmt.Sprintf("%s cat-file --batch [repo_path: %s] (%s:%d)", GitExecutable, repoPath, filename, line)).
RunInDirFullPipeline(repoPath, batchStdoutWriter, &stderr, batchStdinReader)
if err != nil {
_ = batchStdoutWriter.CloseWithError(ConcatenateError(err, (&stderr).String()))
_ = batchStdinReader.CloseWithError(ConcatenateError(err, (&stderr).String()))
Expand Down Expand Up @@ -292,3 +304,10 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
sha = shaBuf
return
}

var callerPrefix string

func init() {
_, filename, _, _ := runtime.Caller(0)
callerPrefix = strings.TrimSuffix(filename, "modules/git/batch_reader.go")
}
2 changes: 1 addition & 1 deletion options/locale/locale_el-GR.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2732,7 +2732,7 @@ comment_issue=`σχολίασε στο ζήτημα <a href="%s/issues/%s">%s#%[
comment_pull=`σχολίασε στο pull request <a href="%s/pulls/%s">%s#%[2]s</a>`
merge_pull_request=`συγχώνευσε το pull request <a href="%s/pulls/%s">%s#%[2]s</a>`
transfer_repo=μετέφερε το αποθετήριο <code>%s</code> σε <a href="%s">%s</a>
push_tag=ώθησε την ετικέτα <a href="%s/src/tag/%s">%[4]s</a> σε <a href="%[1]s">%[3]s</a>
push_tag=ώθησε την ετικέτα <a href="%s/src/tag/%s">%[4]s</a> στο <a href="%[1]s">%[3]s</a>
delete_tag=διέγραψε την ετικέτα %[2]s από <a href="%[1]s">%[3]s</a>
delete_branch=διέγραψε το κλάδο %[2]s από <a href="%[1]s">%[3]s</a>
compare_branch=Σύγκριση
Expand Down
13 changes: 8 additions & 5 deletions routers/api/v1/notify/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/structs"
)

func statusStringToNotificationStatus(status string) models.NotificationStatus {
Expand Down Expand Up @@ -176,7 +177,7 @@ func ReadRepoNotifications(ctx *context.APIContext) {
// required: false
// responses:
// "205":
// "$ref": "#/responses/empty"
// "$ref": "#/responses/NotificationThreadList"

lastRead := int64(0)
qLastRead := ctx.FormTrim("last_read_at")
Expand Down Expand Up @@ -213,14 +214,16 @@ func ReadRepoNotifications(ctx *context.APIContext) {
targetStatus = models.NotificationStatusRead
}

changed := make([]*structs.NotificationThread, len(nl))

for _, n := range nl {
err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
notif, err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
if err != nil {
ctx.InternalServerError(err)
return
}
ctx.Status(http.StatusResetContent)
_ = notif.LoadAttributes()
changed = append(changed, convert.ToNotificationThread(notif))
}

ctx.Status(http.StatusResetContent)
ctx.JSON(http.StatusResetContent, changed)
}
10 changes: 7 additions & 3 deletions routers/api/v1/notify/threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func ReadThread(ctx *context.APIContext) {
// required: false
// responses:
// "205":
// "$ref": "#/responses/empty"
// "$ref": "#/responses/NotificationThread"
// "403":
// "$ref": "#/responses/forbidden"
// "404":
Expand All @@ -87,12 +87,16 @@ func ReadThread(ctx *context.APIContext) {
targetStatus = models.NotificationStatusRead
}

err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
notif, err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
if err != nil {
ctx.InternalServerError(err)
return
}
ctx.Status(http.StatusResetContent)
if err = notif.LoadAttributes(); err != nil {
ctx.InternalServerError(err)
return
}
ctx.JSON(http.StatusResetContent, convert.ToNotificationThread(notif))
}

func getThread(ctx *context.APIContext) *models.Notification {
Expand Down
12 changes: 8 additions & 4 deletions routers/api/v1/notify/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/convert"
"code.gitea.io/gitea/modules/structs"
)

// ListNotifications list users's notification threads
Expand Down Expand Up @@ -125,7 +126,7 @@ func ReadNotifications(ctx *context.APIContext) {
// required: false
// responses:
// "205":
// "$ref": "#/responses/empty"
// "$ref": "#/responses/NotificationThreadList"

lastRead := int64(0)
qLastRead := ctx.FormTrim("last_read_at")
Expand Down Expand Up @@ -158,14 +159,17 @@ func ReadNotifications(ctx *context.APIContext) {
targetStatus = models.NotificationStatusRead
}

changed := make([]*structs.NotificationThread, 0, len(nl))

for _, n := range nl {
err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
notif, err := models.SetNotificationStatus(n.ID, ctx.User, targetStatus)
if err != nil {
ctx.InternalServerError(err)
return
}
ctx.Status(http.StatusResetContent)
_ = notif.LoadAttributes()
changed = append(changed, convert.ToNotificationThread(notif))
}

ctx.Status(http.StatusResetContent)
ctx.JSON(http.StatusResetContent, changed)
}
14 changes: 8 additions & 6 deletions routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,15 @@ func GetArchive(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"

repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
return
if ctx.Repo.GitRepo == nil {
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
return
}
ctx.Repo.GitRepo = gitRepo
defer gitRepo.Close()
}
ctx.Repo.GitRepo = gitRepo
defer gitRepo.Close()

repo.Download(ctx.Context)
}
Expand Down
10 changes: 5 additions & 5 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,27 +317,27 @@ func ListIssues(ctx *context.APIContext) {
// type: string
// - name: since
// in: query
// description: Only show notifications updated after the given time. This is a timestamp in RFC 3339 format
// description: Only show items updated after the given time. This is a timestamp in RFC 3339 format
// type: string
// format: date-time
// required: false
// - name: before
// in: query
// description: Only show notifications updated before the given time. This is a timestamp in RFC 3339 format
// description: Only show items updated before the given time. This is a timestamp in RFC 3339 format
// type: string
// format: date-time
// required: false
// - name: created_by
// in: query
// description: filter (issues / pulls) created to
// description: Only show items which were created by the the given user
// type: string
// - name: assigned_by
// in: query
// description: filter (issues / pulls) assigned to
// description: Only show items for which the given user is assigned
// type: string
// - name: mentioned_by
// in: query
// description: filter (issues / pulls) mentioning to
// description: Only show items in which the given user was mentioned
// type: string
// - name: page
// in: query
Expand Down
2 changes: 1 addition & 1 deletion routers/web/user/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func NotificationStatusPost(c *context.Context) {
return
}

if err := models.SetNotificationStatus(notificationID, c.User, status); err != nil {
if _, err := models.SetNotificationStatus(notificationID, c.User, status); err != nil {
c.ServerError("SetNotificationStatus", err)
return
}
Expand Down
16 changes: 8 additions & 8 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@
],
"responses": {
"205": {
"$ref": "#/responses/empty"
"$ref": "#/responses/NotificationThreadList"
}
}
}
Expand Down Expand Up @@ -822,7 +822,7 @@
],
"responses": {
"205": {
"$ref": "#/responses/empty"
"$ref": "#/responses/NotificationThread"
},
"403": {
"$ref": "#/responses/forbidden"
Expand Down Expand Up @@ -4334,32 +4334,32 @@
{
"type": "string",
"format": "date-time",
"description": "Only show notifications updated after the given time. This is a timestamp in RFC 3339 format",
"description": "Only show items updated after the given time. This is a timestamp in RFC 3339 format",
"name": "since",
"in": "query"
},
{
"type": "string",
"format": "date-time",
"description": "Only show notifications updated before the given time. This is a timestamp in RFC 3339 format",
"description": "Only show items updated before the given time. This is a timestamp in RFC 3339 format",
"name": "before",
"in": "query"
},
{
"type": "string",
"description": "filter (issues / pulls) created to",
"description": "Only show items which were created by the the given user",
"name": "created_by",
"in": "query"
},
{
"type": "string",
"description": "filter (issues / pulls) assigned to",
"description": "Only show items for which the given user is assigned",
"name": "assigned_by",
"in": "query"
},
{
"type": "string",
"description": "filter (issues / pulls) mentioning to",
"description": "Only show items in which the given user was mentioned",
"name": "mentioned_by",
"in": "query"
},
Expand Down Expand Up @@ -7058,7 +7058,7 @@
],
"responses": {
"205": {
"$ref": "#/responses/empty"
"$ref": "#/responses/NotificationThreadList"
}
}
}
Expand Down

0 comments on commit 5e20a94

Please sign in to comment.