API: Fix GetQueryBeforeSince (#13559)
This commit is contained in:
		
							parent
							
								
									374ff60465
								
							
						
					
					
						commit
						3f3447a1ea
					
				
					 5 changed files with 45 additions and 25 deletions
				
			
		|  | @ -101,7 +101,7 @@ func ListRepoNotifications(ctx *context.APIContext) { | ||||||
| 
 | 
 | ||||||
| 	before, since, err := utils.GetQueryBeforeSince(ctx) | 	before, since, err := utils.GetQueryBeforeSince(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.InternalServerError(err) | 		ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	opts := models.FindNotificationOptions{ | 	opts := models.FindNotificationOptions{ | ||||||
|  |  | ||||||
|  | @ -63,7 +63,7 @@ func ListNotifications(ctx *context.APIContext) { | ||||||
| 
 | 
 | ||||||
| 	before, since, err := utils.GetQueryBeforeSince(ctx) | 	before, since, err := utils.GetQueryBeforeSince(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.InternalServerError(err) | 		ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	opts := models.FindNotificationOptions{ | 	opts := models.FindNotificationOptions{ | ||||||
|  |  | ||||||
|  | @ -57,7 +57,7 @@ func ListIssueComments(ctx *context.APIContext) { | ||||||
| 
 | 
 | ||||||
| 	before, since, err := utils.GetQueryBeforeSince(ctx) | 	before, since, err := utils.GetQueryBeforeSince(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.Error(http.StatusInternalServerError, "GetQueryBeforeSince", err) | 		ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) | 	issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index")) | ||||||
|  | @ -133,7 +133,7 @@ func ListRepoIssueComments(ctx *context.APIContext) { | ||||||
| 
 | 
 | ||||||
| 	before, since, err := utils.GetQueryBeforeSince(ctx) | 	before, since, err := utils.GetQueryBeforeSince(ctx) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		ctx.Error(http.StatusInternalServerError, "GetQueryBeforeSince", err) | 		ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -86,7 +86,7 @@ func ListTrackedTimes(ctx *context.APIContext) { | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil { | 	if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil { | ||||||
| 		ctx.InternalServerError(err) | 		ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -491,7 +491,7 @@ func ListTrackedTimesByRepository(ctx *context.APIContext) { | ||||||
| 
 | 
 | ||||||
| 	var err error | 	var err error | ||||||
| 	if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil { | 	if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil { | ||||||
| 		ctx.InternalServerError(err) | 		ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | @ -554,7 +554,7 @@ func ListMyTrackedTimes(ctx *context.APIContext) { | ||||||
| 
 | 
 | ||||||
| 	var err error | 	var err error | ||||||
| 	if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil { | 	if opts.CreatedBeforeUnix, opts.CreatedAfterUnix, err = utils.GetQueryBeforeSince(ctx); err != nil { | ||||||
| 		ctx.InternalServerError(err) | 		ctx.Error(http.StatusUnprocessableEntity, "GetQueryBeforeSince", err) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ | ||||||
| package utils | package utils | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"net/url" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
| 
 | 
 | ||||||
|  | @ -15,30 +16,49 @@ import ( | ||||||
| 
 | 
 | ||||||
| // GetQueryBeforeSince return parsed time (unix format) from URL query's before and since
 | // GetQueryBeforeSince return parsed time (unix format) from URL query's before and since
 | ||||||
| func GetQueryBeforeSince(ctx *context.APIContext) (before, since int64, err error) { | func GetQueryBeforeSince(ctx *context.APIContext) (before, since int64, err error) { | ||||||
| 	qCreatedBefore := strings.Trim(ctx.Query("before"), " ") | 	qCreatedBefore, err := prepareQueryArg(ctx, "before") | ||||||
| 	if qCreatedBefore != "" { | 	if err != nil { | ||||||
| 		createdBefore, err := time.Parse(time.RFC3339, qCreatedBefore) | 		return 0, 0, err | ||||||
| 		if err != nil { |  | ||||||
| 			return 0, 0, err |  | ||||||
| 		} |  | ||||||
| 		if !createdBefore.IsZero() { |  | ||||||
| 			before = createdBefore.Unix() |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	qCreatedAfter := strings.Trim(ctx.Query("since"), " ") | 	qCreatedSince, err := prepareQueryArg(ctx, "since") | ||||||
| 	if qCreatedAfter != "" { | 	if err != nil { | ||||||
| 		createdAfter, err := time.Parse(time.RFC3339, qCreatedAfter) | 		return 0, 0, err | ||||||
| 		if err != nil { | 	} | ||||||
| 			return 0, 0, err | 
 | ||||||
| 		} | 	before, err = parseTime(qCreatedBefore) | ||||||
| 		if !createdAfter.IsZero() { | 	if err != nil { | ||||||
| 			since = createdAfter.Unix() | 		return 0, 0, err | ||||||
| 		} | 	} | ||||||
|  | 
 | ||||||
|  | 	since, err = parseTime(qCreatedSince) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return 0, 0, err | ||||||
| 	} | 	} | ||||||
| 	return before, since, nil | 	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, err | ||||||
|  | 		} | ||||||
|  | 		if !t.IsZero() { | ||||||
|  | 			return t.Unix(), 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
 | // GetListOptions returns list options using the page and limit parameters
 | ||||||
| func GetListOptions(ctx *context.APIContext) models.ListOptions { | func GetListOptions(ctx *context.APIContext) models.ListOptions { | ||||||
| 	return models.ListOptions{ | 	return models.ListOptions{ | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue