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

Support reading GitHub token from a file #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ You may also *optionally* specify a repository as the second argument. **This is

This resolver will give you access to packages published on *any* repository within the organization. If the token provided in the authentication information only has access to public repositories, then packages published on private repositories will report "not found". If the token has access to private repositories as well as public, then all packages will be visible.

#### Credentials

You will need to ensure that `githubTokenSource` is set to *your* details (i.e. the authentication information for the individual who ran `sbt`). The `TokenSource` ADT has the following possibilities:

```scala
Expand All @@ -55,6 +57,7 @@ sealed trait TokenSource extends Product with Serializable {
object TokenSource {
final case class Environment(variable: String) extends TokenSource
final case class GitConfig(key: String) extends TokenSource
final case class FromFile(file: File) extends TokenSource
final case class Or(primary: TokenSource, secondary: TokenSource) extends TokenSource
}
```
Expand All @@ -80,6 +83,14 @@ This assumes you have your token stored there like this:
token = TOKEN_DATA
```

To read a token from a local file you can use something like:

```sbt
githubTokenSource := TokenSource.FromFile(Path.userHome / ".sbt" / "github.token")
```

The first line of the file will be read in and used as the GitHub token.

The `||` combinator allows you to configure multiple token sources which will be tried in order on first-read of the setting.

Note that your CI server will need to set the `GITHUB_TOKEN` environment variable as well (if using the `Environment` token source), as well as any collaborators on your project. The environment-specific nature of these login credentials are a major part of why they are *not* just strings sitting in the `build.sbt` file. As an example, if you're using Travis, you can do something like the following:
Expand Down
4 changes: 4 additions & 0 deletions src/main/scala/sbtghpackages/GitHubPackagesPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ object GitHubPackagesPlugin extends AutoPlugin {
resolveTokenSource(primary).orElse(
resolveTokenSource(secondary))

case TokenSource.FromFile(file) =>
// Extract credentials from the first line of the file.
if (file.canRead) IO.readLines(file).headOption else None

case TokenSource.Environment(variable) =>
sys.env.get(variable)

Expand Down
3 changes: 3 additions & 0 deletions src/main/scala/sbtghpackages/TokenSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package sbtghpackages

import sbt.File

sealed trait TokenSource extends Product with Serializable {
def ||(that: TokenSource): TokenSource =
TokenSource.Or(this, that)
Expand All @@ -24,5 +26,6 @@ sealed trait TokenSource extends Product with Serializable {
object TokenSource {
final case class Environment(variable: String) extends TokenSource
final case class GitConfig(key: String) extends TokenSource
final case class FromFile(file: File) extends TokenSource
final case class Or(primary: TokenSource, secondary: TokenSource) extends TokenSource
}