-
Notifications
You must be signed in to change notification settings - Fork 0
/
IssueMigrator.scala
70 lines (61 loc) · 2.29 KB
/
IssueMigrator.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@main def main() = {
val token = os.read(os.home / "IdeaProjects" / "Hands-On Scala" / "resources" / "github_token.txt" ).trim
def fetchPaginated(url: String, params: (String, String)*) = {
var done = false
var page = 1
var responses = collection.mutable.Buffer.empty[ujson.Value]
while (!done) {
println("page " + page + "...")
val resp = requests.get(
url,
params = Map("page" -> page.toString) ++ params,
headers = Map("Authorization" -> s"token $token")
)
val parsed = ujson.read(resp).arr
if (parsed.isEmpty) done = true
else responses.appendAll(parsed)
page += 1
}
responses
}
val issues = fetchPaginated(s"https://api.github.com/repos/$srcRepo/issues", "state" -> "all")
val nonPullRequests = issues.filter(!_.obj.contains("pull_request"))
val issueData = for (issue <- nonPullRequests) yield (
issue("number").num.toInt,
issue("title").str,
issue("body").str,
issue("user")("login").str
)
val comments = fetchPaginated(s"https://api.github.com/repos/$srcRepo/issues/comments")
val commentData = for (comment <- comments) yield (
comment("issue_url").str match {
case s"https://api.github.com/repos/lihaoyi/$repo/issues/$id" => id.toInt
},
comment("user")("login").str,
comment("body").str
)
val issueNums = for ((number, title, body, user) <- issueData.sortBy(_._1)) yield {
println(s"Creating issue $number")
val resp = requests.post(
s"https://api.github.com/repos/$destRepo/issues",
data = ujson.Obj(
"title" -> title,
"body" -> s"$body\nID: $number\nOriginal Author: $user"
),
headers = Map("Authorization" -> s"token $token")
)
println(resp.statusCode)
val newIssueNumber = ujson.read(resp)("number").num.toInt
(number, newIssueNumber)
}
val issueNumMap = issueNums.toMap
for ((issueId, user, body) <- commentData; newIssueId <- issueNumMap.get(issueId)) {
println(s"Commenting on issue old_id=$issueId new_id=$newIssueId")
val resp = requests.post(
s"https://api.github.com/repos/lihaoyi/test/issues/$newIssueId/comments",
data = ujson.Obj("body" -> s"$body\nOriginal Author:$user"),
headers = Map("Authorization" -> s"token $token")
)
println(resp.statusCode)
}
}