grim/youtrack-import
Clone
Summary
Browse
Changes
Graph
Fix the maxIssues bug where if you didn't specify a number of issues to import it didn't import any issues.
draft
2020-08-04, Gary Kramlich
156cc34dc505
Fix the maxIssues bug where if you didn't specify a number of issues to import it didn't import any issues.
package
bitbucket
import
(
"archive/zip"
"encoding/json"
"fmt"
"io"
"keep.imfreedom.org/grim/youtrack-import/youtrack"
)
func
loadArchive
(
archive
string
)
(
*
Archive
,
error
)
{
r
,
err
:=
zip
.
OpenReader
(
archive
)
if
err
!=
nil
{
return
nil
,
err
}
for
_
,
f
:=
range
r
.
File
{
if
f
.
Name
==
"db-2.0.json"
{
rc
,
err
:=
f
.
Open
()
if
err
!=
nil
{
return
nil
,
err
}
defer
rc
.
Close
()
archive
:=
&
Archive
{}
if
err
:=
json
.
NewDecoder
(
rc
).
Decode
(
archive
);
err
!=
nil
{
return
nil
,
err
}
archive
.
zip
=
r
return
archive
,
nil
}
}
return
nil
,
fmt
.
Errorf
(
"failed to find db-2.0.json in the archive"
)
}
func
(
a
*
Archive
)
Close
()
error
{
return
a
.
zip
.
Close
()
}
func
(
a
*
Archive
)
OpenAttachment
(
path
string
)
(
io
.
ReadCloser
,
error
)
{
// find the file in the archive
for
_
,
f
:=
range
a
.
zip
.
File
{
if
f
.
Name
==
path
{
r
,
err
:=
f
.
Open
()
if
err
!=
nil
{
return
nil
,
err
}
return
r
,
nil
}
}
return
nil
,
fmt
.
Errorf
(
"failed to find attachment %q"
,
path
)
}
func
(
a
*
Archive
)
ExtractUsers
(
usersMap
*
UsersMap
)
(
map
[
string
]
*
youtrack
.
User
,
error
)
{
// key is the id of the user
users
:=
map
[
string
]
*
youtrack
.
User
{}
mapUser
:=
func
(
a
*
Author
)
error
{
if
a
.
AccountID
!=
""
{
bbUser
,
err
:=
usersMap
.
Find
(
a
.
DisplayName
)
if
err
!=
nil
{
return
err
}
users
[
a
.
AccountID
]
=
bbUser
.
ToYoutrack
()
}
return
nil
}
// find all users from issues
for
_
,
issue
:=
range
a
.
Issues
{
if
err
:=
mapUser
(
&
issue
.
Assignee
);
err
!=
nil
{
return
nil
,
err
}
if
err
:=
mapUser
(
&
issue
.
Reporter
);
err
!=
nil
{
return
nil
,
err
}
for
_
,
watcher
:=
range
issue
.
Watchers
{
if
err
:=
mapUser
(
&
watcher
);
err
!=
nil
{
return
nil
,
err
}
}
for
_
,
voter
:=
range
issue
.
Voters
{
if
err
:=
mapUser
(
&
voter
);
err
!=
nil
{
return
nil
,
err
}
}
}
// find all users from comments
for
_
,
comment
:=
range
a
.
Comments
{
if
err
:=
mapUser
(
&
comment
.
User
);
err
!=
nil
{
return
nil
,
err
}
}
// find all users from attachments
for
_
,
attachment
:=
range
a
.
Attachments
{
if
err
:=
mapUser
(
&
attachment
.
User
);
err
!=
nil
{
return
nil
,
err
}
}
// find all users from logs
for
_
,
log
:=
range
a
.
Logs
{
if
err
:=
mapUser
(
&
log
.
User
);
err
!=
nil
{
return
nil
,
err
}
}
// extract the user from the default assignee
if
err
:=
mapUser
(
&
a
.
Meta
.
DefaultAssignee
);
err
!=
nil
{
return
nil
,
err
}
return
users
,
nil
}