[APIV4] POST /teams/{team_id}/import for apiv4 (#5920)
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
36c74d7b47
Коммит
7eb09dbffd
79
api4/team.go
79
api4/team.go
@@ -4,7 +4,10 @@
|
|||||||
package api4
|
package api4
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
"github.com/mattermost/platform/app"
|
"github.com/mattermost/platform/app"
|
||||||
@@ -36,6 +39,8 @@ func InitTeam() {
|
|||||||
BaseRoutes.TeamMember.Handle("", ApiSessionRequired(getTeamMember)).Methods("GET")
|
BaseRoutes.TeamMember.Handle("", ApiSessionRequired(getTeamMember)).Methods("GET")
|
||||||
BaseRoutes.TeamByName.Handle("/exists", ApiSessionRequired(teamExists)).Methods("GET")
|
BaseRoutes.TeamByName.Handle("/exists", ApiSessionRequired(teamExists)).Methods("GET")
|
||||||
BaseRoutes.TeamMember.Handle("/roles", ApiSessionRequired(updateTeamMemberRoles)).Methods("PUT")
|
BaseRoutes.TeamMember.Handle("/roles", ApiSessionRequired(updateTeamMemberRoles)).Methods("PUT")
|
||||||
|
|
||||||
|
BaseRoutes.Team.Handle("/import", ApiSessionRequired(importTeam)).Methods("POST")
|
||||||
}
|
}
|
||||||
|
|
||||||
func createTeam(c *Context, w http.ResponseWriter, r *http.Request) {
|
func createTeam(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -468,3 +473,77 @@ func teamExists(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte(model.MapBoolToJson(resp)))
|
w.Write([]byte(model.MapBoolToJson(resp)))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func importTeam(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
c.RequireTeamId()
|
||||||
|
if c.Err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_IMPORT_TEAM) {
|
||||||
|
c.SetPermissionError(model.PERMISSION_IMPORT_TEAM)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := r.ParseMultipartForm(10000000); err != nil {
|
||||||
|
c.Err = model.NewLocAppError("importTeam", "api.team.import_team.parse.app_error", nil, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
importFromArray, ok := r.MultipartForm.Value["importFrom"]
|
||||||
|
importFrom := importFromArray[0]
|
||||||
|
|
||||||
|
fileSizeStr, ok := r.MultipartForm.Value["filesize"]
|
||||||
|
if !ok {
|
||||||
|
c.Err = model.NewLocAppError("importTeam", "api.team.import_team.unavailable.app_error", nil, "")
|
||||||
|
c.Err.StatusCode = http.StatusBadRequest
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fileSize, err := strconv.ParseInt(fileSizeStr[0], 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
c.Err = model.NewLocAppError("importTeam", "api.team.import_team.integer.app_error", nil, "")
|
||||||
|
c.Err.StatusCode = http.StatusBadRequest
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fileInfoArray, ok := r.MultipartForm.File["file"]
|
||||||
|
if !ok {
|
||||||
|
c.Err = model.NewLocAppError("importTeam", "api.team.import_team.no_file.app_error", nil, "")
|
||||||
|
c.Err.StatusCode = http.StatusBadRequest
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(fileInfoArray) <= 0 {
|
||||||
|
c.Err = model.NewLocAppError("importTeam", "api.team.import_team.array.app_error", nil, "")
|
||||||
|
c.Err.StatusCode = http.StatusBadRequest
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fileInfo := fileInfoArray[0]
|
||||||
|
|
||||||
|
fileData, err := fileInfo.Open()
|
||||||
|
defer fileData.Close()
|
||||||
|
if err != nil {
|
||||||
|
c.Err = model.NewLocAppError("importTeam", "api.team.import_team.open.app_error", nil, err.Error())
|
||||||
|
c.Err.StatusCode = http.StatusBadRequest
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var log *bytes.Buffer
|
||||||
|
switch importFrom {
|
||||||
|
case "slack":
|
||||||
|
var err *model.AppError
|
||||||
|
if err, log = app.SlackImport(fileData, fileSize, c.Params.TeamId); err != nil {
|
||||||
|
c.Err = err
|
||||||
|
c.Err.StatusCode = http.StatusBadRequest
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Disposition", "attachment; filename=MattermostImportLog.txt")
|
||||||
|
w.Header().Set("Content-Type", "application/octet-stream")
|
||||||
|
if c.Err != nil {
|
||||||
|
w.WriteHeader(c.Err.StatusCode)
|
||||||
|
}
|
||||||
|
io.Copy(w, bytes.NewReader(log.Bytes()))
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,9 +4,11 @@
|
|||||||
package api4
|
package api4
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/mattermost/platform/app"
|
"github.com/mattermost/platform/app"
|
||||||
@@ -1047,3 +1049,77 @@ func TestTeamExists(t *testing.T) {
|
|||||||
_, resp = Client.TeamExists(team.Name, "")
|
_, resp = Client.TeamExists(team.Name, "")
|
||||||
CheckUnauthorizedStatus(t, resp)
|
CheckUnauthorizedStatus(t, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestImportTeam(t *testing.T) {
|
||||||
|
th := Setup().InitBasic().InitSystemAdmin()
|
||||||
|
defer TearDown()
|
||||||
|
|
||||||
|
t.Run("ImportTeam", func(t *testing.T) {
|
||||||
|
var data []byte
|
||||||
|
var err error
|
||||||
|
data, err = readTestFile("Fake_Team_Import.zip")
|
||||||
|
if err != nil && len(data) == 0 {
|
||||||
|
t.Fatal("Error while reading the test file.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Import the channels/users/posts
|
||||||
|
fileResp, resp := th.SystemAdminClient.ImportTeam(data, binary.Size(data), "slack", "Fake_Team_Import.zip", th.BasicTeam.Id)
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
|
||||||
|
fileReturned := fmt.Sprintf("%s", fileResp)
|
||||||
|
if !strings.Contains(fileReturned, "darth.vader@stardeath.com") {
|
||||||
|
t.Log(fileReturned)
|
||||||
|
t.Fatal("failed to report the user was imported")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checking the imported users
|
||||||
|
importedUser, resp := th.SystemAdminClient.GetUserByUsername("bot_test", "")
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
if importedUser.Username != "bot_test" {
|
||||||
|
t.Fatal("username should match with the imported user")
|
||||||
|
}
|
||||||
|
|
||||||
|
importedUser, resp = th.SystemAdminClient.GetUserByUsername("lordvader", "")
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
if importedUser.Username != "lordvader" {
|
||||||
|
t.Fatal("username should match with the imported user")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checking the imported Channels
|
||||||
|
importedChannel, resp := th.SystemAdminClient.GetChannelByName("testchannel", th.BasicTeam.Id, "")
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
if importedChannel.Name != "testchannel" {
|
||||||
|
t.Fatal("names did not match expected: testchannel")
|
||||||
|
}
|
||||||
|
|
||||||
|
importedChannel, resp = th.SystemAdminClient.GetChannelByName("general", th.BasicTeam.Id, "")
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
if importedChannel.Name != "general" {
|
||||||
|
t.Fatal("names did not match expected: general")
|
||||||
|
}
|
||||||
|
|
||||||
|
posts, resp := th.SystemAdminClient.GetPostsForChannel(importedChannel.Id, 0, 60, "")
|
||||||
|
CheckNoError(t, resp)
|
||||||
|
if posts.Posts[posts.Order[3]].Message != "This is a test post to test the import process" {
|
||||||
|
t.Fatal("missing posts in the import process")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("MissingFile", func(t *testing.T) {
|
||||||
|
_, resp := th.SystemAdminClient.ImportTeam(nil, 4343, "slack", "Fake_Team_Import.zip", th.BasicTeam.Id)
|
||||||
|
CheckBadRequestStatus(t, resp)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("WrongPermission", func(t *testing.T) {
|
||||||
|
var data []byte
|
||||||
|
var err error
|
||||||
|
data, err = readTestFile("Fake_Team_Import.zip")
|
||||||
|
if err != nil && len(data) == 0 {
|
||||||
|
t.Fatal("Error while reading the test file.")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Import the channels/users/posts
|
||||||
|
_, resp := th.Client.ImportTeam(data, binary.Size(data), "slack", "Fake_Team_Import.zip", th.BasicTeam.Id)
|
||||||
|
CheckForbiddenStatus(t, resp)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
20
i18n/en.json
20
i18n/en.json
@@ -3739,6 +3739,26 @@
|
|||||||
"id": "model.client.upload_saml_cert.app_error",
|
"id": "model.client.upload_saml_cert.app_error",
|
||||||
"translation": "Error creating SAML certificate multipart form request"
|
"translation": "Error creating SAML certificate multipart form request"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "model.client.upload_post_attachment.writer.app_error",
|
||||||
|
"translation": "Error closing multipart writer"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "model.client.upload_post_attachment.file.app_error",
|
||||||
|
"translation": "Error writing file to multipart form"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "model.client.upload_post_attachment.channel_id.app_error",
|
||||||
|
"translation": "Error writing channel id to multipart form"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "model.client.upload_post_attachment.import_from.app_error",
|
||||||
|
"translation": "Error writing importFrom to multipart form"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "model.client.upload_post_attachment.file_size.app_error",
|
||||||
|
"translation": "Error writing fileSize to multipart form"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "model.command.is_valid.create_at.app_error",
|
"id": "model.command.is_valid.create_at.app_error",
|
||||||
"translation": "Create at must be a valid time"
|
"translation": "Create at must be a valid time"
|
||||||
|
|||||||
@@ -94,6 +94,10 @@ func (c *Client4) GetTeamStatsRoute(teamId string) string {
|
|||||||
return fmt.Sprintf(c.GetTeamRoute(teamId) + "/stats")
|
return fmt.Sprintf(c.GetTeamRoute(teamId) + "/stats")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client4) GetTeamImportRoute(teamId string) string {
|
||||||
|
return fmt.Sprintf(c.GetTeamRoute(teamId) + "/import")
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client4) GetChannelsRoute() string {
|
func (c *Client4) GetChannelsRoute() string {
|
||||||
return fmt.Sprintf("/channels")
|
return fmt.Sprintf("/channels")
|
||||||
}
|
}
|
||||||
@@ -277,6 +281,27 @@ func (c *Client4) DoUploadFile(url string, data []byte, contentType string) (*Fi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client4) DoUploadImportTeam(url string, data []byte, contentType string) ([]byte, *Response) {
|
||||||
|
rq, _ := http.NewRequest("POST", c.ApiUrl+url, bytes.NewReader(data))
|
||||||
|
rq.Header.Set("Content-Type", contentType)
|
||||||
|
rq.Close = true
|
||||||
|
|
||||||
|
if len(c.AuthToken) > 0 {
|
||||||
|
rq.Header.Set(HEADER_AUTH, c.AuthType+" "+c.AuthToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
if rp, err := c.HttpClient.Do(rq); err != nil {
|
||||||
|
return nil, &Response{Error: NewAppError(url, "model.client.connecting.app_error", nil, err.Error(), 0)}
|
||||||
|
} else if rp.StatusCode >= 300 {
|
||||||
|
return nil, &Response{StatusCode: rp.StatusCode, Error: AppErrorFromJson(rp.Body)}
|
||||||
|
} else if data, err := ioutil.ReadAll(rp.Body); err != nil {
|
||||||
|
return nil, &Response{StatusCode: rp.StatusCode, Error: NewAppError("UploadImportTeam", "model.client.read_file.app_error", nil, err.Error(), rp.StatusCode)}
|
||||||
|
} else {
|
||||||
|
defer closeBody(rp)
|
||||||
|
return data, BuildResponse(rp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CheckStatusOK is a convenience function for checking the standard OK response
|
// CheckStatusOK is a convenience function for checking the standard OK response
|
||||||
// from the web service.
|
// from the web service.
|
||||||
func CheckStatusOK(r *http.Response) bool {
|
func CheckStatusOK(r *http.Response) bool {
|
||||||
@@ -966,6 +991,36 @@ func (c *Client4) GetTeamUnread(teamId, userId string) (*TeamUnread, *Response)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ImportTeam will import an exported team from other app into a existing team.
|
||||||
|
func (c *Client4) ImportTeam(data []byte, filesize int, importFrom, filename, teamId string) ([]byte, *Response) {
|
||||||
|
body := &bytes.Buffer{}
|
||||||
|
writer := multipart.NewWriter(body)
|
||||||
|
|
||||||
|
if part, err := writer.CreateFormFile("file", filename); err != nil {
|
||||||
|
return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.file.app_error", nil, err.Error(), http.StatusBadRequest)}
|
||||||
|
} else if _, err = io.Copy(part, bytes.NewBuffer(data)); err != nil {
|
||||||
|
return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.file.app_error", nil, err.Error(), http.StatusBadRequest)}
|
||||||
|
}
|
||||||
|
|
||||||
|
if part, err := writer.CreateFormField("filesize"); err != nil {
|
||||||
|
return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.file_size.app_error", nil, err.Error(), http.StatusBadRequest)}
|
||||||
|
} else if _, err = io.Copy(part, strings.NewReader(strconv.Itoa(filesize))); err != nil {
|
||||||
|
return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.file_size.app_error", nil, err.Error(), http.StatusBadRequest)}
|
||||||
|
}
|
||||||
|
|
||||||
|
if part, err := writer.CreateFormField("importFrom"); err != nil {
|
||||||
|
return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.import_from.app_error", nil, err.Error(), http.StatusBadRequest)}
|
||||||
|
} else if _, err = io.Copy(part, strings.NewReader(importFrom)); err != nil {
|
||||||
|
return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.import_from.app_error", nil, err.Error(), http.StatusBadRequest)}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := writer.Close(); err != nil {
|
||||||
|
return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.writer.app_error", nil, err.Error(), http.StatusBadRequest)}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.DoUploadImportTeam(c.GetTeamImportRoute(teamId), body.Bytes(), writer.FormDataContentType())
|
||||||
|
}
|
||||||
|
|
||||||
// Channel Section
|
// Channel Section
|
||||||
|
|
||||||
// CreateChannel creates a channel based on the provided channel struct.
|
// CreateChannel creates a channel based on the provided channel struct.
|
||||||
|
|||||||
Двоичные данные
tests/Fake_Team_Import.zip
Обычный файл
Двоичные данные
tests/Fake_Team_Import.zip
Обычный файл
Двоичный файл не отображается.
Ссылка в новой задаче
Block a user