Skip to content

Commit 70cb15b

Browse files
committed
add ghUpload for publishing to GitHub releases
1 parent 1337c1c commit 70cb15b

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

build.sbt

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ScalaDist.upload
1+
import ScalaDist.{s3Upload, ghUpload}
22

33
// so we don't require a native git install
44
useJGit
@@ -22,9 +22,9 @@ Versioning.settings
2222
// are known/understood, at scala/scala-dist#171
2323
scalaVersion := version.value
2424

25-
upload / mappings := Seq()
25+
s3Upload / mappings := Seq()
2626

27-
upload := {
27+
s3Upload := {
2828
import com.amazonaws.services.s3.AmazonS3ClientBuilder
2929
import com.amazonaws.services.s3.model.PutObjectRequest
3030
import com.amazonaws.regions.Regions
@@ -33,12 +33,39 @@ upload := {
3333
val client = AmazonS3ClientBuilder.standard.withRegion(Regions.US_EAST_1).build
3434

3535
val log = streams.value.log
36-
(upload / mappings).value map { case (file, key) =>
36+
(s3Upload / mappings).value map { case (file, key) =>
3737
log.info("Uploading "+ file.getAbsolutePath() +" as "+ key)
3838
client.putObject(new PutObjectRequest("downloads.typesafe.com", key, file))
3939
}
4040
}
4141

42+
ghUpload := {
43+
import sttp.client3._
44+
import java.io.File
45+
46+
val log = streams.value.log
47+
val ghRelease = s"v${(Universal / version).value}"
48+
val backend = HttpURLConnectionBackend()
49+
(s3Upload / mappings).value map { case (file, _) =>
50+
log.info(s"Uploading ${file.getAbsolutePath} as ${file.getName} to https://github.com/scala/scala/releases/tag/$ghRelease")
51+
52+
val token = sys.env.getOrElse("GITHUB_OAUTH_TOKEN", throw new MessageOnlyException("GITHUB_OAUTH_TOKEN missing"))
53+
54+
// https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset
55+
val request = basicRequest
56+
.post(uri"https://uploads.github.com/repos/scala/scala/releases/${ghRelease}/assets?name=${file.getName}")
57+
.contentType("application/octet-stream")
58+
.header("Accept", "application/vnd.github+json")
59+
.header("Authorization", s"Bearer $token")
60+
.header("X-GitHub-Api-Version", "2022-11-28")
61+
.body(file)
62+
63+
val response = request.send(backend)
64+
if (response.code.code != 201)
65+
throw new MessageOnlyException(s"Upload failed: status=${response.code}\n${response.body}")
66+
}
67+
}
68+
4269
ScalaDist.settings
4370

4471
Docs.settings

project/ScalaDist.scala

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import com.amazonaws.services.s3.model.PutObjectResult
1010

1111
// can't call it Universal -- that's taken by the packager
1212
object ScalaDist {
13-
val upload=TaskKey[Seq[PutObjectResult]]("s3-upload","Uploads files to an S3 bucket.")
13+
val s3Upload = TaskKey[Seq[PutObjectResult]]("s3Upload","Uploads files to an S3 bucket.")
14+
val ghUpload = TaskKey[Seq[Unit]]("ghUpload","Uploads files to GitHub Releases.")
1415

1516
def createMappingsWith(deps: Seq[(sbt.librarymanagement.ConfigRef, ModuleID, Artifact, File)],
1617
distMappingGen: (ModuleID, Artifact, File) => Seq[(File, String)]): Seq[(File, String)] =
@@ -21,30 +22,30 @@ object ScalaDist {
2122
case _ => Seq()
2223
}
2324

24-
// used to make s3-upload upload the file produced by fileTask to the path scala/$version/${file.name}
25+
// used to make s3Upload upload the file produced by fileTask to the path scala/$version/${file.name}
2526
private def uploadMapping(fileTask: TaskKey[File]) = Def.task {
2627
val file = fileTask.value
2728
file -> s"scala/${version.value}/${file.getName}"
2829
}
2930

30-
// make it so that s3-upload will upload the msi when we're running on windows, and everything else when we're on linux
31-
// s3-upload thus depends on the package tasks listed below
31+
// make it so that s3Upload will upload the msi when we're running on windows, and everything else when we're on linux
32+
// s3Upload thus depends on the package tasks listed below
3233
def platformSettings =
3334
if (sys.props("os.name").toLowerCase(java.util.Locale.US) contains "windows")
34-
Wix.settings :+ (upload / mappings += uploadMapping(Windows / packageBin).value)
35+
Wix.settings :+ (s3Upload / mappings += uploadMapping(Windows / packageBin).value)
3536
else Unix.settings ++ Seq(
36-
upload / mappings += uploadMapping(Universal / packageBin).value,
37-
upload / mappings += uploadMapping(Universal / packageZipTarball).value,
38-
upload / mappings += uploadMapping(UniversalDocs / packageBin).value,
39-
upload / mappings += uploadMapping(UniversalDocs / packageZipTarball).value,
40-
upload / mappings += uploadMapping(UniversalDocs / packageXzTarball).value,
41-
upload / mappings += uploadMapping(Rpm / packageBin).value,
37+
s3Upload / mappings += uploadMapping(Universal / packageBin).value,
38+
s3Upload / mappings += uploadMapping(Universal / packageZipTarball).value,
39+
s3Upload / mappings += uploadMapping(UniversalDocs / packageBin).value,
40+
s3Upload / mappings += uploadMapping(UniversalDocs / packageZipTarball).value,
41+
s3Upload / mappings += uploadMapping(UniversalDocs / packageXzTarball).value,
42+
s3Upload / mappings += uploadMapping(Rpm / packageBin).value,
4243
// Debian needs special handling because the value sbt-native-packager
4344
// gives us for `Debian / packageBin` (coming from the archiveFilename
4445
// method) includes the debian version and arch information,
4546
// which we historically have not included. I don't see a way to
4647
// override the filename on disk, so we re-map at upload time
47-
upload / mappings += Def.task {
48+
s3Upload / mappings += Def.task {
4849
(Debian / packageBin).value ->
4950
s"scala/${version.value}/${(Debian / name).value}-${version.value}.deb"
5051
}.value

project/plugins.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ addSbtPlugin("com.typesafe.sbt" % "sbt-native-packager" % "1.8.1")
77

88
libraryDependencies += "com.amazonaws" % "aws-java-sdk-s3" % "1.12.5"
99

10+
libraryDependencies += "com.softwaremill.sttp.client3" %% "core" % "3.11.0"
11+
1012
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.1")
1113

0 commit comments

Comments
 (0)