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
82b2d530
Commit
82b2d530
authored
8 years ago
by
Peter van 't Hof
Browse files
Options
Downloads
Patches
Plain Diff
Update
parent
50d045c8
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
+109
-29
109 additions, 29 deletions
...n/scala/nl/lumc/sasc/biopet/tools/GitlabIssueToJira.scala
with
109 additions
and
29 deletions
biopet-tools/src/main/scala/nl/lumc/sasc/biopet/tools/GitlabIssueToJira.scala
+
109
−
29
View file @
82b2d530
package
nl.lumc.sasc.biopet.tools
import
java.io.
{
BufferedReader
,
InputStreamReader
}
import
java.io.
{
BufferedReader
,
File
,
InputStreamReader
}
import
nl.lumc.sasc.biopet.utils.
{
ConfigUtils
,
ToolCommand
}
import
play.api.libs.ws.
{
WSAuthScheme
,
WSRequest
}
import
nl.lumc.sasc.biopet.utils.
{
ConfigUtils
,
ToolCommand
}
import
play.api.libs.ws.
{
InMemoryBody
,
WSAuthScheme
,
WSRequest
}
import
scala.concurrent.
{
Await
,
Future
}
import
scala.concurrent.
{
Await
,
Future
}
import
scala.concurrent.duration._
import
play.api.libs.ws.ning.NingWSClient
import
scala.concurrent.ExecutionContext.Implicits.global
import
scala.io.Source
/**
* Created by pjvan_thof on 10-10-16.
...
...
@@ -23,6 +24,8 @@ object GitlabIssueToJira extends ToolCommand {
jiraProject
:
String
=
null
,
jiraToken
:
String
=
null
,
jiraUsername
:
String
=
null
,
jiraIssueType
:
String
=
null
,
userMappings
:
Option
[
File
]
=
null
,
jiraPassword
:
Option
[
String
]
=
None
)
extends
AbstractArgs
class
OptParser
extends
AbstractOptParser
{
...
...
@@ -44,9 +47,16 @@ object GitlabIssueToJira extends ToolCommand {
opt
[
String
](
"jiraUsername"
)
required
()
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
c
.
copy
(
jiraUsername
=
x
)
}
opt
[
String
](
"jiraPassword"
)
required
()
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
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
](
"userMappings"
)
required
()
maxOccurs
1
valueName
"<string>"
action
{
(
x
,
c
)
=>
c
.
copy
(
userMappings
=
Some
(
x
))
}
text
"tsv to say which user belong to eachother, 1e column gitlab user, 2e column jira user"
}
def
main
(
args
:
Array
[
String
])
:
Unit
=
{
...
...
@@ -56,38 +66,98 @@ object GitlabIssueToJira extends ToolCommand {
val
c
=
System
.
console
()
//Console.out.close()
val
pw
=
cmdArgs
.
jiraPassword
match
{
case
Some
(
pass
)
=>
pass
case
_
if
c
!=
null
=>
c
.
readPassword
(
"Jira password >"
).
toString
case
_
=>
""
}
//println(pw)
// Instantiation of the client
// In a real-life application, you would instantiate one, share it everywhere,
// and call wsClient.close() when you're done
val
wsClient
=
NingWSClient
()
// val gitlabIssuesFuture = getGitlabIssues(cmdArgs.gitlabUrl, cmdArgs.gitlabProject, cmdArgs.gitlabToken, wsClient)
// gitlabIssuesFuture.onFailure { case e => throw e }
//
// val gitlabIssues = Await.result(gitlabIssuesFuture, Duration.Inf)
//
// println(gitlabIssues.size)
// println(gitlabIssues.head.issue("author"))
// println(gitlabIssues.head.comments.map(_.keys))
val
url
=
s
"${cmdArgs.jiraUrl}/rest/api/2/project/${cmdArgs.jiraProject}"
println
(
url
)
val
projectFurure
=
getWsrequestJsonToMap
(
wsClient
.
url
(
url
)
.
withAuth
(
cmdArgs
.
jiraUsername
,
pw
,
WSAuthScheme
.
BASIC
))
val
gitlabIssuesFuture
=
getGitlabIssues
(
cmdArgs
.
gitlabUrl
,
cmdArgs
.
gitlabProject
,
cmdArgs
.
gitlabToken
,
wsClient
)
gitlabIssuesFuture
.
onFailure
{
case
e
=>
throw
e
}
val
gitlabIssues
=
Await
.
result
(
gitlabIssuesFuture
,
Duration
.
Inf
)
println
(
gitlabIssues
.
size
)
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
project
=
waitForFuture
(
projectFurure
)
println
(
project
(
"id"
))
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
).
getOrElse
(
Map
())
val
gitlabUsers
=
(
gitlabIssues
.
map
(
_
.
issue
(
"author"
).
asInstanceOf
[
Map
[
String
,
Any
]](
"username"
).
toString
)
:::
gitlabIssues
.
flatMap
{
x
=>
if
(!
x
.
isInstanceOf
[
Map
[
String
,
Any
]])
None
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"
))
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
}
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
)
logger
.
info
(
"Done2"
)
wsClient
.
close
()
...
...
@@ -95,15 +165,20 @@ object GitlabIssueToJira extends ToolCommand {
logger
.
info
(
"Done3"
)
}
def
getWsrequestJsonToMap
(
request
:
WSRequest
,
putBody
:
Option
[
String
]
=
None
)
=
{
val
f
=
putBody
match
{
case
Some
(
body
)
=>
request
.
put
(
body
)
case
_
=>
request
.
get
()
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
()
}
val
r
=
f
.
map
{
wsResponse
=>
if
(
wsResponse
.
status
>=
200
&&
wsResponse
.
status
<=
299
)
ConfigUtils
.
jsonToMap
(
ConfigUtils
.
textToJson
(
wsResponse
.
body
))
else
throw
new
IllegalStateException
(
s
"Request gave status ${wsResponse.status}"
)
else
{
logger
.
error
(
s
"Request gave status ${wsResponse.status}: ${wsResponse.body}"
)
throw
new
IllegalStateException
(
s
"Request gave status ${wsResponse.status}"
)
}
}
r
.
onFailure
{
case
e
=>
throw
new
RuntimeException
(
e
)
}
r
...
...
@@ -125,7 +200,12 @@ object GitlabIssueToJira extends ToolCommand {
perPage
:
Int
=
100
)
:
Future
[
List
[
GitlabIssue
]]
=
{
val
issuesUrl
=
s
"${url}/api/v3/projects/${project.replace("
/
", "
%
2F
")}/issues"
getGitlabPagedOutput
(
issuesUrl
,
token
,
wsClient
,
Map
(
"per_page"
->
"100"
))
.
map
{
x
=>
x
.
map
(
i
=>
GitlabIssue
(
i
,
waitForFuture
(
getGitlabPagedOutput
(
issuesUrl
+
s
"/${i("
id
")}/notes"
,
token
,
wsClient
))))
}
.
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
)))
}
}
}
def
getGitlabPagedOutput
(
url
:
String
,
...
...
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