Add boilerplate code

This commit is contained in:
Giacomo Rossetto 2023-09-28 12:03:59 +02:00
parent 413798b321
commit ba6835f9d2
5 changed files with 155 additions and 1 deletions

1
go.mod

@ -160,6 +160,7 @@ require (
github.com/urfave/cli/v2 v2.3.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/ybbus/jsonrpc/v2 v2.1.7 // indirect
github.com/yosssi/gohtml v0.0.0-20201013000340-ee4748c638f4 // indirect
go.opentelemetry.io/otel v1.26.0 // indirect
go.opentelemetry.io/otel/trace v1.26.0 // indirect

@ -148,6 +148,9 @@ const (
MigrationMicrosoftTodoClientID Key = `migration.microsofttodo.clientid`
MigrationMicrosoftTodoClientSecret Key = `migration.microsofttodo.clientsecret`
MigrationMicrosoftTodoRedirectURL Key = `migration.microsofttodo.redirecturl`
MigrationKanboardEnable Key = `migration.kanboard.enable`
MigrationKanboardApiToken Key = `migration.kanboard.token`
MigrationKanboardApiEndpoint Key = `migration.kanboard.endpoint`
CorsEnable Key = `cors.enable`
CorsOrigins Key = `cors.origins`

@ -0,0 +1,135 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-present Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public Licensee as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public Licensee for more details.
//
// You should have received a copy of the GNU Affero General Public Licensee
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package kanboard
import (
"github.com/ybbus/jsonrpc/v2"
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/modules/migration"
"code.vikunja.io/api/pkg/user"
)
// Migration represents the trello migration struct
type Migration struct {
Token string `json:"code"`
}
type kanboardBoard struct {
}
func init() {
}
// Name is used to get the name of the trello migration - we're using the docs here to annotate the status route.
// @Summary Get migration status
// @Description Returns if the current user already did the migation or not. This is useful to show a confirmation message in the frontend if the user is trying to do the same migration again.
// @tags migration
// @Produce json
// @Security JWTKeyAuth
// @Success 200 {object} migration.Status "The migration status"
// @Failure 500 {object} models.Message "Internal server error"
// @Router /migration/trello/status [get]
func (m *Migration) Name() string {
return "kanboard"
}
// AuthURL returns the url users need to authenticate against
// @Summary Get the auth url from trello
// @Description Returns the auth url where the user needs to get its auth code. This code can then be used to migrate everything from trello to Vikunja.
// @tags migration
// @Produce json
// @Security JWTKeyAuth
// @Success 200 {object} handler.AuthURL "The auth url."
// @Failure 500 {object} models.Message "Internal server error"
// @Router /migration/trello/auth [get]
func (m *Migration) AuthURL() string {
return ""
}
func getKanboardData(endpoint string, token string) (kanboardData []kanboardBoard, err error) {
rpcClient := jsonrpc.NewClient(endpoint)
err = rpcClient.CallFor(&kanboardData, "getAllProjects")
return
}
// Converts all previously obtained data from trello into the vikunja format.
// `trelloData` should contain all boards with their projects and cards respectively.
func convertKanboardDataToVikunja(kanboardData []kanboardBoard, token string) (fullVikunjaHierachie []*models.ProjectWithTasksAndBuckets, err error) {
log.Debugf("[Kanboard Migration] ")
fullVikunjaHierachie = []*models.ProjectWithTasksAndBuckets{
{
Project: models.Project{
Title: "Imported from Kanboard",
},
ChildProjects: []*models.ProjectWithTasksAndBuckets{},
},
}
log.Debugf("[Kanboard Migration] Converting %d boards to vikunja projects", len(kanboardData))
return
}
// Migrate gets all tasks from trello for a user and puts them into vikunja
// @Summary Migrate all projects, tasks etc. from trello
// @Description Migrates all projects, tasks, notes, reminders, subtasks and files from trello to vikunja.
// @tags migration
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param migrationCode body kanboard.Migration true "The auth token previously obtained from the auth url. See the docs for /migration/kanboard/auth."
// @Success 200 {object} models.Message "A message telling you everything was migrated successfully."
// @Failure 500 {object} models.Message "Internal server error"
// @Router /migration/kanboard/migrate [post]
func (m *Migration) Migrate(u *user.User) (err error) {
log.Debugf("[Kanboard Migration] Starting migration for user %d", u.ID)
log.Debugf("[Kanboard Migration] Getting all kanboard data for user %d", u.ID)
trelloData, err := getKanboardData(config.MigrationKanboardApiEndpoint.GetString(), config.MigrationKanboardApiToken.GetString())
if err != nil {
return
}
log.Debugf("[Kanboard Migration] Got all kanboard data for user %d", u.ID)
log.Debugf("[Kanboard Migration] Start converting kanboard data for user %d", u.ID)
fullVikunjaHierachie, err := convertKanboardDataToVikunja(trelloData, m.Token)
if err != nil {
return
}
log.Debugf("[Kanboard Migration] Done migrating kanboard data for user %d", u.ID)
log.Debugf("[Kanboard Migration] Start inserting kanboard data for user %d", u.ID)
err = migration.InsertFromStructure(fullVikunjaHierachie, u)
if err != nil {
return
}
log.Debugf("[Kanboard Migration] Done inserting kanboard data for user %d", u.ID)
log.Debugf("[Kanboard Migration] Migration done for user %d", u.ID)
return nil
}

@ -22,6 +22,7 @@ import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/modules/auth/openid"
"code.vikunja.io/api/pkg/modules/migration/kanboard"
microsofttodo "code.vikunja.io/api/pkg/modules/migration/microsoft-todo"
"code.vikunja.io/api/pkg/modules/migration/ticktick"
"code.vikunja.io/api/pkg/modules/migration/todoist"
@ -136,7 +137,10 @@ func Info(c echo.Context) error {
m := &microsofttodo.Migration{}
info.AvailableMigrators = append(info.AvailableMigrators, m.Name())
}
if config.MigrationKanboardEnable.GetBool() {
m := &kanboard.Migration{}
info.AvailableMigrators = append(info.AvailableMigrators, m.Name())
}
if config.BackgroundsEnabled.GetBool() {
if config.BackgroundsUploadEnabled.GetBool() {
info.EnabledBackgroundProviders = append(info.EnabledBackgroundProviders, "upload")

@ -69,6 +69,7 @@ import (
"code.vikunja.io/api/pkg/modules/background/upload"
"code.vikunja.io/api/pkg/modules/migration"
migrationHandler "code.vikunja.io/api/pkg/modules/migration/handler"
"code.vikunja.io/api/pkg/modules/migration/kanboard"
microsofttodo "code.vikunja.io/api/pkg/modules/migration/microsoft-todo"
"code.vikunja.io/api/pkg/modules/migration/ticktick"
"code.vikunja.io/api/pkg/modules/migration/todoist"
@ -658,6 +659,16 @@ func registerMigrations(m *echo.Group) {
trelloMigrationHandler.RegisterMigrator(m)
}
// Kanboard
if config.MigrationKanboardEnable.GetBool() {
kanboardMigrationHandler := &migrationHandler.MigrationWeb{
MigrationStruct: func() migration.Migrator {
return &kanboard.Migration{}
},
}
kanboardMigrationHandler.RegisterRoutes(m)
}
// Microsoft Todo
if config.MigrationMicrosoftTodoEnable.GetBool() {
microsoftTodoMigrationHandler := &migrationHandler.MigrationWeb{