summaryrefslogtreecommitdiff
path: root/ci/upload.sc
blob: 740f3b58cde9e2bc32f6072d7f14b63093c17014 (plain) (blame)
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
#!/usr/bin/env amm
import ammonite.ops._
import scalaj.http._

@main
def shorten(longUrl: String) = {
  println("shorten longUrl " + longUrl)
  val shortUrl = Http("https://git.io")
    .postForm(Seq("url" -> longUrl))
    .asString
    .headers("Location")
    .head
  println("shorten shortUrl " + shortUrl)
  shortUrl
}
@main
def apply(uploadedFile: Path,
          tagName: String,
          uploadName: String,
          authKey: String): String = {

  val response = Http(s"https://api.github.com/repos/lihaoyi/mill/releases/tags/${tagName}")
    .header("Authorization", "token " + authKey)
    .header("Accept", "application/vnd.github.v3+json")
    .asString
  val body = response.body

  val parsed = ujson.read(body)

  println("Response code: " + response.code)
  println(body)

  val snapshotReleaseId = parsed("id").num.toInt


  val uploadUrl =
    s"https://uploads.github.com/repos/lihaoyi/mill/releases/" +
      s"$snapshotReleaseId/assets?name=$uploadName"

  val res = Http(uploadUrl)
    .header("Content-Type", "application/octet-stream")
    .header("Authorization", "token " + authKey)
    .timeout(connTimeoutMs = 5000, readTimeoutMs = 60000)
    .postData(read.bytes! uploadedFile)
    .asString

  println(res.body)
  val longUrl = ujson.read(res.body)("browser_download_url").str.toString

  println("Long Url " + longUrl)

  val shortUrl = shorten(longUrl)

  println("Short Url " + shortUrl)
  shortUrl
}