From e166bee03a2230e9ff379797e480a89cc8c3fc8c Mon Sep 17 00:00:00 2001 From: Vadim Shalts Date: Sun, 15 Apr 2018 10:43:45 +0200 Subject: [PATCH 1/2] Set default GithubRelease.repoName from git config --- src/main/scala/GithubRelease.scala | 3 +++ src/main/scala/SbtGithubReleasePlugin.scala | 22 +++++++++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/scala/GithubRelease.scala b/src/main/scala/GithubRelease.scala index 7b352cc..737cfeb 100644 --- a/src/main/scala/GithubRelease.scala +++ b/src/main/scala/GithubRelease.scala @@ -12,6 +12,8 @@ case object GithubRelease { type DefTask[X] = Def.Initialize[Task[X]] type DefSetting[X] = Def.Initialize[Setting[X]] + case class Origin(organization: String, name: String) + case object keys { type TagName = String @@ -22,6 +24,7 @@ case object GithubRelease { lazy val ghreleaseTitle = settingKey[TagName => String]("The title of the release") lazy val ghreleaseIsPrerelease = settingKey[TagName => Boolean]("A function to determine release as a prerelease based on the tag name") lazy val ghreleaseGithubToken = settingKey[Option[String]]("Credentials for accessing the GitHub API") + lazy val ghreleaseGithubOrigin = settingKey[Option[Origin]]("GitHub origin") lazy val ghreleaseAssets = taskKey[Seq[File]]("The artifact files to upload") lazy val ghreleaseGetRepo = taskKey[GHRepository]("Checks repo existence and returns it if it's fine") diff --git a/src/main/scala/SbtGithubReleasePlugin.scala b/src/main/scala/SbtGithubReleasePlugin.scala index f32dbb7..175b69a 100644 --- a/src/main/scala/SbtGithubReleasePlugin.scala +++ b/src/main/scala/SbtGithubReleasePlugin.scala @@ -18,8 +18,10 @@ case object SbtGithubReleasePlugin extends AutoPlugin { val ver = tagName.stripPrefix("v") IO.read(baseDirectory.value / "notes" / s"${ver}.markdown") }, - ghreleaseRepoOrg := organization.value, - ghreleaseRepoName := name.value, + ghreleaseGithubOrigin := githubOrigin(baseDirectory.value), + + ghreleaseRepoOrg := ghreleaseGithubOrigin.value.map(_.organization).getOrElse(organization.value), + ghreleaseRepoName := ghreleaseGithubOrigin.value.map(_.name).getOrElse(name.value), ghreleaseTitle := { tagName => s"${name.value} ${tagName}" }, // According to the Semantic Versioning Specification (rule 9) // a version containing a hyphen is a pre-release version @@ -57,4 +59,20 @@ case object SbtGithubReleasePlugin extends AutoPlugin { (Space ~> suggestions.getOrElse(StringBasic)) ?? fallback } + + def githubOrigin(base: File): Option[GithubRelease.Origin] = { + val gitOut: Try[String] = Try { + sys.process.Process(Seq("git", "ls-remote", "--get-url", "origin"), base).!! + } + + val repoExtractPattern = """/([^/]*)/([^/]*)""".r + + gitOut.map { out => + val path = new URI(out.trim).getPath + path match { + case repoExtractPattern(organization, name) => Some(GithubRelease.Origin(organization, name)) + case _ => None + } + }.toOption.flatten + } } From bd701c51e91177f98b9c82a81579de99b120dcb9 Mon Sep 17 00:00:00 2001 From: Vadim Shalts Date: Sun, 15 Apr 2018 13:00:57 +0200 Subject: [PATCH 2/2] Add Readme notes --- Readme.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Readme.md b/Readme.md index 1a673d4..bbf3f81 100644 --- a/Readme.md +++ b/Readme.md @@ -43,6 +43,10 @@ The main task is `githubRelease`, it creates the release and publishes the asset You can find their defaults in the plugin [code](src/main/scala/SbtGithubReleasePlugin.scala). +#### Autodetect repository organization and name + +By default, this plugin will try to auto-detect settings for `ghreleaseRepoOrg` and `ghreleaseRepoName` based on git remote with name `origin`. If such remote not exist then plugin will fallback to sbt organization/name. If you would like to avoid auto-detect behavior you should set `ghreleaseRepoOrg` and `ghreleaseRepoName` explicitly. + #### Assets You can set which files to attach to the release using the `ghreleaseAssets` task (of `Seq[File]` type). By default it refers to the `packagedArtifacts` task.