Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default GithubRelease.repoName from git config #27

Merged
merged 2 commits into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/GithubRelease.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand Down
22 changes: 20 additions & 2 deletions src/main/scala/SbtGithubReleasePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}