Skip to content

Commit

Permalink
default credentials location
Browse files Browse the repository at this point in the history
  • Loading branch information
kolov committed Jul 11, 2017
1 parent 35f59f0 commit c2b26bd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ The output is in markdown, ready to be copy/pasted as github release notes. Exam
$ sbt printNotesForLatest
Preparing release notes from pull requests before tag 0.0.1
[#2](https://github.com/kolov/sbt-pantarhei/pull/2)
[PR #2](https://github.com/kolov/sbt-pantarhei/pull/2) Config token
* [autoPlugin, credentials](https://github.com/kolov/sbt-pantarhei/commit/449a89324b3293db10dcade85a89ed9849b94548)
* [github token from credentials](https://github.com/kolov/sbt-pantarhei/commit/d990f551fcc2f23f53a677741bd162dd509277f0)
[#1](https://github.com/kolov/sbt-pantarhei/pull/1)
[PR #1](https://github.com/kolov/sbt-pantarhei/pull/1) test PR to use for retreiving PRs
* [parses github remote url](https://github.com/kolov/sbt-pantarhei/commit/e1e35f924ae7b242c92670fd0676063fe7b96423)
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.5")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "1.1")
addSbtPlugin("com.akolov" % "sbt-pantarhei" % "0.0.2")
addSbtPlugin("com.akolov" % "sbt-pantarhei" % "0.1.1-SNAPSHOT")
4 changes: 2 additions & 2 deletions src/it/scala/GithubIT.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import java.io.File

import com.akolov.pantarhei.{FutureCommit, Github, NotesPlugin}
import com.akolov.pantarhei.{FutureCommit, Github, NotesMaker}
import org.scalatest.WordSpecLike

import scala.io.Source
Expand All @@ -23,7 +23,7 @@ class GithubIT extends WordSpecLike {

"The Notes Plugin" should {
"make RN" in {
new NotesPlugin(new File("/Users/assen/projects/sbt-pantarhei"), token).makeNotes(FutureCommit)
new NotesMaker(new File("/Users/assen/projects/sbt-pantarhei"), token).makeNotes(FutureCommit)
}
}
"The class Github" should {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import java.text.SimpleDateFormat
import sbt._


object NotesPlugin extends sbt.AutoPlugin {
object NotesMaker extends sbt.AutoPlugin {

lazy val printNotesAfterLatest = taskKey[Unit]("Create release notes af pull requests after the latest tag")
lazy val printNotesForLatest = taskKey[Unit]("Create release notes af pull requests for the latest tag")
Expand All @@ -17,36 +17,40 @@ object NotesPlugin extends sbt.AutoPlugin {
printNotesAfterLatest := {
val baseDirectory = Keys.baseDirectory.value
val credentials = Keys.credentials.value
val token = extractToken(credentials)
new NotesPlugin(baseDirectory, token).makeNotes(FutureCommit)
processNotes(baseDirectory, credentials, FutureCommit)
},
printNotesForLatest := {
val baseDirectory = Keys.baseDirectory.value
val credentials = Keys.credentials.value
val token = extractToken(credentials)
new NotesPlugin(baseDirectory, token).makeNotes(LatestComit)
processNotes(baseDirectory, credentials, LatestComit)
}
)

private def extractToken(credentials: Seq[Credentials]) = {
credentials.map(Credentials.toDirect).find(c => c.realm.toLowerCase == "github")
.getOrElse(
throw new Exception("Can't find github token. Expected credentials with realm=Github " + "and " +
"password={token}")).passwd
}

def processNotes(baseDirectory: java.io.File, credentials: Seq[Credentials], target: Target): Unit = {
val githubCredentials = credentials.map(Credentials.toDirect).find(c => c.realm.toLowerCase == "github")
.orElse(
Credentials.loadCredentials(new File(System.getProperty("user.home") + "/.github/credentials")) match {
case Left(_) => None
case Right(dc) => Some(dc)
})
if (!githubCredentials.isDefined) {
println("Can't find github authentication token. Expected credentials with realm=Github " + "and " + "password={token}")
println("If no credentials are defined in sbt, /.github/credentials is always searched")
return ()
}

new NotesMaker(baseDirectory, githubCredentials.get.passwd).makeNotes(target)
}
}


class NotesPlugin(baseDir: java.io.File, token: String) {
class NotesMaker(baseDir: java.io.File, token: String) {


val git = Git(baseDir)
val remoteUrl = git.remote
val github = Github(remoteUrl, token)


case class Parameters(lowerBound: Option[Tag], upperBound: Option[Tag], errorMessage: Option[String]) {
require(lowerBound.isDefined || upperBound.isDefined || errorMessage.isDefined)

Expand All @@ -56,7 +60,6 @@ class NotesPlugin(baseDir: java.io.File, token: String) {
}

def makeNotes(target: Target): Unit = {

val tags = github.tags

val parameters = target match {
Expand Down Expand Up @@ -114,11 +117,12 @@ class NotesPlugin(baseDir: java.io.File, token: String) {
}

pullRequests.foreach { pr =>
println(s"[#${pr.number}](${pr.htmlUrl})")
println(s"[PR #${pr.number}](${pr.htmlUrl}) ${pr.title}")
val commits = github.getPRCommits(pr.number)
commits.foreach { record =>
println(s"* [${record.commit.message}](${record.htmlUrl})")
}
println
}
}
}
Expand Down

0 comments on commit c2b26bd

Please sign in to comment.