2020-10-24 00:46:35 +01:00
// Copyright 2020 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2020-10-24 00:46:35 +01:00
package user
import (
2021-04-05 17:30:52 +02:00
"net/http"
2021-11-13 11:28:50 +00:00
"strconv"
2021-04-05 17:30:52 +02:00
2022-08-25 10:31:57 +08:00
admin_model "code.gitea.io/gitea/models/admin"
2021-07-25 00:03:58 +08:00
"code.gitea.io/gitea/modules/json"
2024-02-27 15:12:22 +08:00
"code.gitea.io/gitea/services/context"
2020-10-24 00:46:35 +01:00
)
// TaskStatus returns task's status
func TaskStatus ( ctx * context . Context ) {
2023-09-16 16:39:12 +02:00
task , opts , err := admin_model . GetMigratingTaskByID ( ctx , ctx . ParamsInt64 ( "task" ) , ctx . Doer . ID )
2020-10-24 00:46:35 +01:00
if err != nil {
2022-08-25 10:31:57 +08:00
if admin_model . IsErrTaskDoesNotExist ( err ) {
2023-07-04 20:36:08 +02:00
ctx . JSON ( http . StatusNotFound , map [ string ] any {
2021-11-13 11:28:50 +00:00
"error" : "task `" + strconv . FormatInt ( ctx . ParamsInt64 ( "task" ) , 10 ) + "` does not exist" ,
} )
return
}
2023-07-04 20:36:08 +02:00
ctx . JSON ( http . StatusInternalServerError , map [ string ] any {
2020-10-24 00:46:35 +01:00
"err" : err ,
} )
return
}
2021-06-16 23:02:24 +01:00
message := task . Message
if task . Message != "" && task . Message [ 0 ] == '{' {
// assume message is actually a translatable string
2022-08-25 10:31:57 +08:00
var translatableMessage admin_model . TranslatableMessage
2021-06-16 23:02:24 +01:00
if err := json . Unmarshal ( [ ] byte ( message ) , & translatableMessage ) ; err != nil {
2022-08-25 10:31:57 +08:00
translatableMessage = admin_model . TranslatableMessage {
2021-06-16 23:02:24 +01:00
Format : "migrate.migrating_failed.error" ,
2023-07-04 20:36:08 +02:00
Args : [ ] any { task . Message } ,
2021-06-16 23:02:24 +01:00
}
}
2024-02-15 05:48:45 +08:00
message = ctx . Locale . TrString ( translatableMessage . Format , translatableMessage . Args ... )
2021-06-16 23:02:24 +01:00
}
2023-07-04 20:36:08 +02:00
ctx . JSON ( http . StatusOK , map [ string ] any {
2020-10-24 00:46:35 +01:00
"status" : task . Status ,
2021-06-16 23:02:24 +01:00
"message" : message ,
2020-10-24 00:46:35 +01:00
"repo-id" : task . RepoID ,
"repo-name" : opts . RepoName ,
"start" : task . StartTime ,
"end" : task . EndTime ,
} )
}