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

Populate github owner/repo keys with default values from git remote #440

Merged
merged 2 commits into from
Mar 4, 2020
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
2 changes: 1 addition & 1 deletion docs/docs/docs/build-the-microsite.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ From version [`0.5.4`](https://github.com/47deg/sbt-microsites/releases/tag/v0.5
Before publishing, a couple of requirements should be satisfied:

1. Initialize the **gh-pages** branch, you can follow the instructions defined in the [sbt-ghpages](https://github.com/sbt/sbt-ghpages/blob/master/README.md#initializing-the-gh-pages-branch) repository.
2. Define `micrositeGithubOwner` and `micrositeGithubRepo` settings and maybe the `micrositePushSiteWith` and `micrositeGithubRepo` settings.
2. Define `micrositeGithubOwner` and `micrositeGithubRepo` settings (if they can't be infered from git remotes) and maybe the `micrositePushSiteWith` and `micrositeGithubRepo` settings.
You can see more details regarding this in the [Configuring the Microsite]({% link docs/settings.md %}) section.

Once both requirements are satisfied, you can just run:
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ micrositeTwitterCreator := "@47deg"
```


- `micrositeGithubOwner` and `micrositeGithubRepo`: Used to add links to the `GitHub` repo. It's also needed for publishing the site when `github4s` is chosen (see `micrositePushSiteWith` setting). Both `micrositeGithubOwner` and `micrositeGithubRepo` are required:
- `micrositeGithubOwner` and `micrositeGithubRepo`: Used to add links to the `GitHub` repo. It's also needed for publishing the site when `github4s` is chosen (see `micrositePushSiteWith` setting). Defaults to the information found in the 'origin' Git remote, if such remote exists; otherwise they must be set like:

```scala
micrositeGithubOwner := "47deg"
Expand Down
8 changes: 6 additions & 2 deletions src/main/scala/microsites/MicrositeKeys.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,12 @@ trait MicrositeKeys {
val micrositeFavicons: SettingKey[Seq[MicrositeFavicon]] = settingKey[Seq[MicrositeFavicon]](
"Optional. List of filenames and sizes for the PNG/ICO files to be used as favicon for the generated site, located in '/microsite/img'. The sizes should be described with a string (i.e.: \"16x16\"). By default, favicons with different sizes will be generated from the navbar_brand2x.jpg file."
)
val micrositeGithubOwner: SettingKey[String] = settingKey[String]("Microsite Github owner")
val micrositeGithubRepo: SettingKey[String] = settingKey[String]("Microsite Github repo")
val micrositeGithubOwner: SettingKey[String] = settingKey[String](
"Microsite Github owner, defaults to the information found in the 'origin' Git remote"
)
val micrositeGithubRepo: SettingKey[String] = settingKey[String](
"Microsite Github repo, defaults to the information found in the 'origin' Git remote"
)
val micrositeGithubToken: SettingKey[Option[String]] =
settingKey[Option[String]]("Microsite Github token for pushing the microsite")
val micrositeGithubLinks: SettingKey[Boolean] = settingKey[Boolean](
Expand Down
29 changes: 27 additions & 2 deletions src/main/scala/microsites/MicrositesPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import tut.TutPlugin.autoImport._
import mdoc.MdocPlugin
import mdoc.MdocPlugin.autoImport._

import scala.util.control.NonFatal

object MicrositesPlugin extends AutoPlugin {

object autoImport extends MicrositeAutoImportSettings
Expand Down Expand Up @@ -112,8 +114,8 @@ object MicrositesPlugin extends AutoPlugin {
},
micrositeFavicons := Seq(),
micrositeVersionList := Seq(),
micrositeGithubOwner := "",
micrositeGithubRepo := "",
micrositeGithubOwner := gitRemoteInfo._1,
micrositeGithubRepo := gitRemoteInfo._2,
micrositeGithubToken := None,
micrositeGitHostingService := GitHub,
micrositeGitHostingUrl := "",
Expand All @@ -130,4 +132,27 @@ object MicrositesPlugin extends AutoPlugin {
commands ++= Seq(publishMicrositeCommand),
javaOptions += "-Djava.awt.headless=true"
)

/** Gets the Github user and repository from the git remote info */
private val gitRemoteInfo = {
import scala.sys.process._

val identifier = """([^\/]+)"""

val GitHubHttps = s"https://github.com/$identifier/$identifier".r
val SSHConnection = s"git@github.com:$identifier/$identifier.git".r

try {
val remote = List("git", "ls-remote", "--get-url", "origin").!!.trim()

remote match {
case GitHubHttps(user, repo) => (user, repo)
case SSHConnection(user, repo) => (user, repo)
case _ => ("", "")
}
} catch {
case NonFatal(_) => ("", "")
}
}

}