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