]> source.dussan.org Git - gitea.git/commitdiff
API: Fix GetQueryBeforeSince (#13561)
author6543 <6543@obermui.de>
Thu, 19 Nov 2020 02:21:21 +0000 (03:21 +0100)
committerGitHub <noreply@github.com>
Thu, 19 Nov 2020 02:21:21 +0000 (02:21 +0000)
routers/api/v1/notify/repo.go
routers/api/v1/notify/user.go
routers/api/v1/repo/issue_comment.go
routers/api/v1/repo/issue_tracked_time.go
routers/api/v1/utils/utils.go

index 49b493aa4fc594ca9c07d3b261e6f19184dcd058..cc66e0f743c273154e697c07b95564a463194e75 100644 (file)
@@ -101,7 +101,7 @@ func ListRepoNotifications(ctx *context.APIContext) {
 
        before, since, err := utils.GetQueryBeforeSince(ctx)
        if err != nil {
-               ctx.InternalServerError(err)
+               ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
                return
        }
        opts := models.FindNotificationOptions{
index 9c3f9b1472b43c82d052b479839cca919ea9f846..373c88d372db88127d4ac3af53edfd940cc003ca 100644 (file)
@@ -63,7 +63,7 @@ func ListNotifications(ctx *context.APIContext) {
 
        before, since, err := utils.GetQueryBeforeSince(ctx)
        if err != nil {
-               ctx.InternalServerError(err)
+               ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
                return
        }
        opts := models.FindNotificationOptions{
index bf86b42402f3233a442d1b34767ba6ca6d0de1cc..1206415bd24a536d913452da578f3251ff8e292f 100644 (file)
@@ -56,7 +56,7 @@ func ListIssueComments(ctx *context.APIContext) {
 
        before, since, err := utils.GetQueryBeforeSince(ctx)
        if err != nil {
-               ctx.Error(http.StatusInternalServerError, "GetQueryBeforeSince", err)
+               ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
                return
        }
        issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
@@ -132,7 +132,7 @@ func ListRepoIssueComments(ctx *context.APIContext) {
 
        before, since, err := utils.GetQueryBeforeSince(ctx)
        if err != nil {
-               ctx.Error(http.StatusInternalServerError, "GetQueryBeforeSince", err)
+               ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
                return
        }
 
index fd437753abf8d26aca892945a75d32e57cf92b8a..67c47d858a111d1ffcc3a7027e13f340daa969fb 100644 (file)
@@ -86,7 +86,7 @@ func ListTrackedTimes(ctx *context.APIContext) {
        }
 
        if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil {
-               ctx.InternalServerError(err)
+               ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
                return
        }
 
@@ -491,7 +491,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) {
 
        var err error
        if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil {
-               ctx.InternalServerError(err)
+               ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
                return
        }
 
@@ -554,7 +554,7 @@ func ListMyTrackedTimes(ctx *context.APIContext) {
 
        var err error
        if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil {
-               ctx.InternalServerError(err)
+               ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err)
                return
        }
 
index 092ea3dbb6497d80c73e06fa7f5551772473daeb..ad1a136db463af1449e9da9a82f68ee83f0a7296 100644 (file)
@@ -5,6 +5,7 @@
 package utils
 
 import (
+       "net/url"
        "strings"
        "time"
 
@@ -15,28 +16,47 @@ import (
 
 // GetQueryBeforeSince return parsed time (unix format) from URL query's before and since
 func GetQueryBeforeSince(ctx *context.APIContext) (before, since int64, err error) {
-       qCreatedBefore := strings.Trim(ctx.Query("before"), " ")
-       if qCreatedBefore != "" {
-               createdBefore, err := time.Parse(time.RFC3339, qCreatedBefore)
-               if err != nil {
-                       return 0, 0, err
-               }
-               if !createdBefore.IsZero() {
-                       before = createdBefore.Unix()
-               }
+       qCreatedBefore, err := prepareQueryArg(ctx, "before")
+       if err != nil {
+               return 0, 0, err
+       }
+
+       qCreatedSince, err := prepareQueryArg(ctx, "since")
+       if err != nil {
+               return 0, 0, err
+       }
+
+       before, err = parseTime(qCreatedBefore)
+       if err != nil {
+               return 0, 0, err
        }
 
-       qCreatedAfter := strings.Trim(ctx.Query("since"), " ")
-       if qCreatedAfter != "" {
-               createdAfter, err := time.Parse(time.RFC3339, qCreatedAfter)
+       since, err = parseTime(qCreatedSince)
+       if err != nil {
+               return 0, 0, err
+       }
+       return before, since, nil
+}
+
+// parseTime parse time and return unix timestamp
+func parseTime(value string) (int64, error) {
+       if len(value) != 0 {
+               t, err := time.Parse(time.RFC3339, value)
                if err != nil {
-                       return 0, 0, err
+                       return 0, err
                }
-               if !createdAfter.IsZero() {
-                       since = createdAfter.Unix()
+               if !t.IsZero() {
+                       return t.Unix(), nil
                }
        }
-       return before, since, nil
+       return 0, nil
+}
+
+// prepareQueryArg unescape and trim a query arg
+func prepareQueryArg(ctx *context.APIContext, name string) (value string, err error) {
+       value, err = url.PathUnescape(ctx.Query(name))
+       value = strings.Trim(value, " ")
+       return
 }
 
 // GetListOptions returns list options using the page and limit parameters