Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
biopet.biopet
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Analyze
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirrors
biopet.biopet
Commits
fa92e13a
Commit
fa92e13a
authored
8 years ago
by
Peter van 't Hof
Browse files
Options
Downloads
Patches
Plain Diff
Output is now as csv
parent
82b2d530
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/GitlabIssueToJira.scala
+70
-98
70 additions, 98 deletions
...n/scala/nl/lumc/sasc/biopet/tools/GitlabIssueToJira.scala
with
70 additions
and
98 deletions
biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/GitlabIssueToJira.scala
+
70
−
98
View file @
fa92e13a
package
nl.lumc.sasc.biopet.tools
import
java.io.
{
BufferedReader
,
File
,
InputStreamRead
er
}
import
java.io.
{
File
,
PrintWrit
er
}
import
nl.lumc.sasc.biopet.utils.
{
ConfigUtils
,
ToolCommand
}
import
play.api.libs.ws.
{
InMemoryBody
,
WSAuthScheme
,
WSRequest
}
import
scala.concurrent.
{
Await
,
Future
}
import
scala.concurrent.duration._
import
nl.lumc.sasc.biopet.utils.
{
ConfigUtils
,
ToolCommand
}
import
play.api.libs.ws.WSRequest
import
play.api.libs.ws.ning.NingWSClient
import
scala.concurrent.ExecutionContext.Implicits.global
import
scala.concurrent.duration._
import
scala.concurrent.
{
Await
,
Future
}
import
scala.io.Source
/**
...
...
@@ -20,13 +19,8 @@ object GitlabIssueToJira extends ToolCommand {
case
class
Args
(
gitlabUrl
:
String
=
null
,
gitlabProject
:
String
=
null
,
gitlabToken
:
String
=
null
,
jiraUrl
:
String
=
null
,
jiraProject
:
String
=
null
,
jiraToken
:
String
=
null
,
jiraUsername
:
String
=
null
,
jiraIssueType
:
String
=
null
,
userMappings
:
Option
[
File
]
=
null
,
jiraPassword
:
Option
[
String
]
=
None
)
extends
AbstractArgs
outputFile
:
File
=
null
,
userMappings
:
Option
[
File
]
=
null
)
extends
AbstractArgs
class
OptParser
extends
AbstractOptParser
{
opt
[
String
](
"gitlabUrl"
)
required
()
maxOccurs
1
valueName
"<url>"
action
{
(
x
,
c
)
=>
...
...
@@ -38,20 +32,8 @@ object GitlabIssueToJira extends ToolCommand {
opt
[
String
](
"gitlabToken"
)
required
()
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
c
.
copy
(
gitlabToken
=
x
)
}
opt
[
String
](
"jiraUrl"
)
required
()
maxOccurs
1
valueName
"<url>"
action
{
(
x
,
c
)
=>
c
.
copy
(
jiraUrl
=
x
)
}
opt
[
String
](
"jiraProject"
)
required
()
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
c
.
copy
(
jiraProject
=
x
)
}
opt
[
String
](
"jiraUsername"
)
required
()
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
c
.
copy
(
jiraUsername
=
x
)
}
opt
[
String
](
"jiraPassword"
)
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
c
.
copy
(
jiraPassword
=
Some
(
x
))
}
opt
[
String
](
"jiraIssueType"
)
required
()
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
c
.
copy
(
jiraIssueType
=
x
)
opt
[
File
](
"outputFile"
)
required
()
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
c
.
copy
(
outputFile
=
x
)
}
opt
[
File
](
"userMappings"
)
required
()
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
c
.
copy
(
userMappings
=
Some
(
x
))
...
...
@@ -64,14 +46,6 @@ object GitlabIssueToJira extends ToolCommand {
val
argsParser
=
new
OptParser
val
cmdArgs
=
argsParser
.
parse
(
args
,
Args
())
getOrElse
(
throw
new
IllegalArgumentException
)
val
c
=
System
.
console
()
val
pw
=
cmdArgs
.
jiraPassword
match
{
case
Some
(
pass
)
=>
pass
case
_
if
c
!=
null
=>
c
.
readPassword
(
"Jira password >"
).
toString
case
_
=>
""
}
// Instantiation of the client
// In a real-life application, you would instantiate one, share it everywhere,
// and call wsClient.close() when you're done
...
...
@@ -86,45 +60,8 @@ object GitlabIssueToJira extends ToolCommand {
println
(
gitlabIssues
.
head
.
issue
(
"author"
))
println
(
gitlabIssues
.
head
.
comments
.
map
(
_
.
keys
))
val
createmetaUrl
=
s
"${cmdArgs.jiraUrl}/rest/api/2/issue/createmeta"
logger
.
info
(
"Project URL: "
+
createmetaUrl
)
val
metacreateFuture
=
getWsrequestJsonToMap
(
wsClient
.
url
(
createmetaUrl
)
.
withAuth
(
cmdArgs
.
jiraUsername
,
pw
,
WSAuthScheme
.
BASIC
)
.
withQueryString
(
"projectKeys"
->
cmdArgs
.
jiraProject
,
"issuetypeNames"
->
cmdArgs
.
jiraIssueType
))
logger
.
info
(
"Done1"
)
val
metacreate
=
waitForFuture
(
metacreateFuture
)
val
projects
=
metacreate
(
"projects"
).
asInstanceOf
[
List
[
Map
[
String
,
Any
]]]
require
(
projects
.
size
==
1
)
val
issuesTypes
=
projects
.
head
(
"issuetypes"
).
asInstanceOf
[
List
[
Map
[
String
,
Any
]]]
require
(
issuesTypes
.
size
==
1
)
val
projectId
=
projects
.
head
(
"id"
)
val
issueTypeId
=
issuesTypes
.
head
(
"id"
)
logger
.
info
(
"projectid: "
+
projectId
)
logger
.
info
(
"issueTypeId: "
+
issueTypeId
)
def
issueBody
=
s
"""
{
"fields": {
"project": {
"id": "${projectId}"
},
"summary": "something's wrong",
"issuetype": {
"id": "${issueTypeId}"
},
"reporter": {
"name": "pjvan_thof"
},
"description": "description"
}
}
"""
val
bla
=
cmdArgs
.
userMappings
.
map
(
file
=>
Source
.
fromFile
(
file
).
getLines
().
map
(
_
.
split
(
"\t"
,
2
)).
map
(
x
=>
(
x
(
0
),
x
(
1
))).
toMap
val
userMappings
=
cmdArgs
.
userMappings
.
map
(
file
=>
Source
.
fromFile
(
file
).
getLines
().
filter
(
_
.
nonEmpty
).
map
(
_
.
split
(
"\t"
,
2
)).
map
(
x
=>
(
x
(
0
),
x
(
1
))).
toMap
).
getOrElse
(
Map
())
val
gitlabUsers
=
(
gitlabIssues
.
map
(
_
.
issue
(
"author"
).
asInstanceOf
[
Map
[
String
,
Any
]](
"username"
).
toString
)
:::
...
...
@@ -133,31 +70,65 @@ object GitlabIssueToJira extends ToolCommand {
else
Some
(
x
.
issue
(
"assignee"
).
asInstanceOf
[
Map
[
String
,
Any
]](
"username"
).
toString
)
}).
distinct
gitlabUsers
.
foreach
(
user
=>
require
(
bla
.
contains
(
user
),
s
"User $user can't be found"
))
gitlabUsers
.
foreach
(
user
=>
require
(
userMappings
.
contains
(
user
),
s
"User $user can't be found"
))
val
issueValues
=
gitlabIssues
.
reverse
.
map
{
gitlabIssue
=>
val
reporter
=
userMappings
(
gitlabIssue
.
issue
(
"author"
).
asInstanceOf
[
Map
[
String
,
Any
]](
"username"
).
toString
)
val
assignee
=
if
(!
gitlabIssue
.
issue
(
"author"
).
isInstanceOf
[
Map
[
String
,
Any
]])
None
else
Some
(
userMappings
(
gitlabIssue
.
issue
(
"author"
).
asInstanceOf
[
Map
[
String
,
Any
]](
"username"
).
toString
))
val
milestone
=
if
(!
gitlabIssue
.
issue
(
"milestone"
).
isInstanceOf
[
Map
[
String
,
Any
]])
None
else
Some
(
gitlabIssue
.
issue
(
"milestone"
).
asInstanceOf
[
Map
[
String
,
Any
]](
"title"
).
toString
)
val
comments
=
gitlabIssue
.
comments
.
map
{
comment
=>
Map
(
"body"
->
comment
(
"body"
),
"author"
->
userMappings
(
comment
(
"author"
).
asInstanceOf
[
Map
[
String
,
Any
]](
"username"
).
toString
),
"created_at"
->
comment
(
"created_at"
)
)
}
val
closedDate
:
Option
[
String
]
=
gitlabIssue
.
issue
(
"state"
)
match
{
case
"closed"
=>
Some
(
comments
.
filter
(
c
=>
c
(
"body"
).
toString
.
contains
(
"Status changed to closed"
)).
last
(
"created_at"
).
toString
)
case
_
=>
None
}
val
commentFields
=
comments
.
zipWithIndex
.
map
{
case
(
comment
,
key
)
=>
s
"comment-$key"
->
s
"${comment("
created_at
")};${comment("
author
")};${comment("
body
")}"
}
toMap
gitlabIssues
.
map
{
gitlabIssue
=>
val
fields
=
Map
(
"project"
->
Map
(
"id"
->
projectId
),
"summary"
->
gitlabIssue
.
issue
(
"title"
),
"issuetype"
->
Map
(
"id"
->
issueTypeId
),
"description"
->
gitlabIssue
.
issue
(
"description"
),
"reporter"
->
Map
(
"name"
->
gitlabIssue
.
issue
(
""
)),
"labels"
->
gitlabIssue
.
issue
(
"labels"
),
"created"
->
gitlabIssue
.
issue
(
"created_at"
)
)
val
body
=
ConfigUtils
.
mapToJson
(
Map
(
"fields"
->
fields
)).
nospaces
"reporter"
->
reporter
,
"gitlab_iid"
->
gitlabIssue
.
issue
(
"iid"
),
"gitlab_id"
->
gitlabIssue
.
issue
(
"id"
),
"state"
->
gitlabIssue
.
issue
(
"state"
),
"labels"
->
gitlabIssue
.
issue
(
"labels"
).
asInstanceOf
[
List
[
String
]].
mkString
(
" "
),
"weburl"
->
gitlabIssue
.
issue
(
"web_url"
),
"created"
->
gitlabIssue
.
issue
(
"created_at"
),
"updated_at"
->
gitlabIssue
.
issue
(
"updated_at"
)
)
++
assignee
.
map
(
"assignee"
->
_
)
++
milestone
.
map
(
"milestone"
->
_
)
++
closedDate
.
map
(
"resolution_date"
->
_
)
++
closedDate
.
map
(
x
=>
"resolution"
->
"Done"
)
fields
++
commentFields
}
val
issueUrl
=
s
"${cmdArgs.jiraUrl}/rest/api/2/issue"
logger
.
info
(
"Issue URL: "
+
issueUrl
)
val
issueFuture
=
getWsrequestJsonToMap
(
wsClient
.
url
(
issueUrl
)
.
withAuth
(
cmdArgs
.
jiraUsername
,
pw
,
WSAuthScheme
.
BASIC
),
Some
(
issueBody
),
"POST"
)
val
issue
=
waitForFuture
(
issueFuture
)
println
(
issue
)
val
keys
=
issueValues
.
flatMap
(
_
.
keys
).
distinct
val
writer
=
new
PrintWriter
(
cmdArgs
.
outputFile
)
writer
.
println
(
keys
.
mkString
(
","
))
issueValues
.
foreach
{
issue
=>
writer
.
print
(
"\""
+
issue
.
get
(
keys
.
head
).
getOrElse
(
""
).
toString
.
replaceAll
(
"\""
,
"\"\""
)
+
"\""
)
keys
.
tail
.
foreach
{
key
=>
val
value
=
issue
.
get
(
key
).
getOrElse
(
""
).
toString
.
replaceAll
(
"\""
,
"\"\""
)
writer
.
print
(
","
+
(
if
(
value
.
nonEmpty
)
"\""
+
value
+
" \""
else
""
))
}
writer
.
println
}
writer
.
close
()
logger
.
info
(
"Done2"
)
wsClient
.
close
()
...
...
@@ -168,9 +139,9 @@ object GitlabIssueToJira extends ToolCommand {
def
getWsrequestJsonToMap
(
request
:
WSRequest
,
body
:
Option
[
String
]
=
None
,
method
:
String
=
"GET"
)
=
{
val
f
=
(
method
,
body
)
match
{
case
(
"POST"
,
Some
(
body
))
=>
request
.
withHeaders
(
"Content-Type"
->
"application/json"
).
post
(
body
)
case
(
"PUT"
,
Some
(
body
))
=>
request
.
withHeaders
(
"Content-Type"
->
"application/json"
).
put
(
body
)
case
(
"GET"
,
None
)
=>
request
.
get
()
case
(
"DELETE"
,
None
)
=>
request
.
delete
()
case
(
"PUT"
,
Some
(
body
))
=>
request
.
withHeaders
(
"Content-Type"
->
"application/json"
).
put
(
body
)
case
(
"GET"
,
None
)
=>
request
.
get
()
case
(
"DELETE"
,
None
)
=>
request
.
delete
()
}
val
r
=
f
.
map
{
wsResponse
=>
if
(
wsResponse
.
status
>=
200
&&
wsResponse
.
status
<=
299
)
...
...
@@ -202,8 +173,9 @@ object GitlabIssueToJira extends ToolCommand {
getGitlabPagedOutput
(
issuesUrl
,
token
,
wsClient
,
Map
(
"per_page"
->
"100"
))
.
map
{
issues
=>
issues
.
map
{
issue
=>
if
(
issue
(
"user_notes_count"
)
==
0
)
GitlabIssue
(
issue
,
Nil
)
else
GitlabIssue
(
issue
,
waitForFuture
(
getGitlabPagedOutput
(
issuesUrl
+
s
"/${issue("
id
")}/notes"
,
token
,
wsClient
)))
//if (issue("user_notes_count") == 0) GitlabIssue(issue, Nil)
GitlabIssue
(
issue
,
waitForFuture
(
getGitlabPagedOutput
(
issuesUrl
+
s
"/${issue("
id
")}/notes"
,
token
,
wsClient
)))
// GitlabIssue(issue, Nil)
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment