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

Regression since 0.4.0: unable to use TokenSource.Environment("GITHUB_TOKEN") as an optional token source #20

Closed
OndrejSpanel opened this issue Apr 21, 2020 · 10 comments

Comments

@OndrejSpanel
Copy link
Contributor

Related to #16

You can compose these using ||, which should resolve the issue, at least in theory

I have this in my project:

githubTokenSource in ThisBuild := TokenSource.GitConfig("github.token") || TokenSource.Environment("GITHUB_USERTOKEN") || TokenSource.Environment("GITHUB_TOKEN")

When I open sbt and peform update, I get following error:

(credentials) unable to locate a valid GitHub token from Environment(GITHUB_TOKEN)

I have the token present in the .githubconfig file, therefore I do not understand why it is trying to load it from GITHUB_TOKEN. The configuration has worked fine with 0.3.1 and 0.3.2, but not with 0.4.0

It is possible to define a dummy GITHUB_TOKEN environment variable as as a workaround.

@OndrejSpanel OndrejSpanel changed the title Regression since 0.4.0: unable to use TokenSource.Environment("GITHUB_USERTOKEN") as an optional token source Regression since 0.4.0: unable to use TokenSource.Environment("GITHUB_TOKEN") as an optional token source Apr 21, 2020
@OndrejSpanel
Copy link
Contributor Author

Moreover, after upgrading to 0.5.0 my workflow is failing with the same error:

[error] java.lang.RuntimeException: unable to locate a valid GitHub token from Environment(GITHUB_TOKEN)
[error] 	at scala.sys.package$.error(package.scala:30)
[error] 	at sbtghpackages.GitHubPackagesPlugin$.$anonfun$authenticationSettings$2(GitHubPackagesPlugin.scala:53)

see https://github.com/gamatron/threescala/runs/604502887?check_suite_focus=true

@djspiewak
Copy link
Owner

I have the token present in the .githubconfig file

Is this a typo? It needs to be in the .gitconfig file. To test, you should be able to read this value from within your repo by running git config github.token.

Does that print out your token? It's very possible there is a regression here. I'll take a look and see if I can identify anything.

@djspiewak
Copy link
Owner

This is the current interpreter for githubTokenSource:

  def resolveTokenSource(tokenSource: TokenSource): Option[String] = {
    tokenSource match {
      case TokenSource.Or(primary, secondary) =>
        resolveTokenSource(primary).orElse(
          resolveTokenSource(secondary))

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

      case TokenSource.GitConfig(key) =>
        Try(s"git config $key".!!).map(_.trim).toOption
    }
  }

Nothing in there looks suspicious to me, though it is utterly untestable. I'm curious as to the results of running git config as I described above.

@OndrejSpanel
Copy link
Contributor Author

I confirm git config github.token prints my token fine. Yes, it was a typo (or rather I wrongly remembered the name of the file). The file is named .gitconfig and contains a section called [github].

@OndrejSpanel
Copy link
Contributor Author

OndrejSpanel commented Apr 21, 2020

Perhaps the error is given from some other place? How about following lines, which seem to reference GITHUB_TOKEN explicitly and unconditionaly:

githubTokenSource := TokenSource.Environment("GITHUB_TOKEN"),
credentials += {
val src = githubTokenSource.value

?

@djspiewak
Copy link
Owner

Ah! I see what's going on.

So the issue is that I changed the scoping of the settings to more closely match what other publication plugins do. Remove in ThisBuild and it should work.

@OndrejSpanel
Copy link
Contributor Author

I find it a bit strange, I thought ThisBuild mean something like "for everything in the build, not only for the root". If you are however certain it should be this way, I guess it can be closed here now (I am not sure if perhaps some documentation or change note should be added somewhere?)

@seblm
Copy link

seblm commented May 22, 2020

Remove in ThisBuild and it should work.

It doesn't works for me. How one can possibly define it's own githubTokenSource setting when this line seems to not care and define always the default setting?

githubTokenSource := TokenSource.Environment("GITHUB_TOKEN"),

Steps to reproduce:

  • do not define any GITHUB_TOKEN environment variable on your machine
  • define sbt setting githubTokenSource := TokenSource.Environment("MY_TOKEN")`
  • launch sbt
  • note that error is unable to locate a valid GitHub token from Environment(GITHUB_TOKEN): there is no mention of MY_TOKEN at all

@djspiewak
Copy link
Owner

djspiewak commented May 22, 2020

It doesn't works for me. How one can possibly define it's own githubTokenSource setting when this line seems to not care and define always the default setting?

That line is sequenced first, before your build definition, meaning that your build overrides it.

Can you show me a simple build.sbt which reproduces the issue? This plugin, as with most, assumes that you are running publish from within the root project, and thus all publication settings need to at least be defined within the root project. If you defined githubTokenSource within some project other than the one in which you're running publish, then it definitely would have this issue.

But with all that said, I wonder if I can loosen this definition scoping to make it a bit more ergonomic…

@seblm
Copy link

seblm commented May 23, 2020

Trying to reproduce issue with a simple sbt project proves that removing ThisBuild is the correct solution:

My current project is more complicated (a lot of submodules, shared settings, etc.)

Sorry for reactivating this issue. I think it could be closed. Thank you for the kind reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants