Skip to content

Commit

Permalink
Add support for GITHUB_TOKEN and GH_TOKEN to match the gh tool. (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Jul 6, 2024
2 parents 23d8e56 + 5fd8603 commit 5251040
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- In addition to `GRGIT_USER`, `GRGIT_PASS`, and `gh_token`, we also now look for:
- `GH_TOKEN`, `GITHUB_TOKEN` ([#53](https://github.com/diffplug/spotless-changelog/pull/53))

## [3.1.1] - 2024-07-06
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.jcraft.jsch.Session;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
Expand All @@ -41,6 +42,7 @@
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.transport.ssh.jsch.JschConfigSessionFactory;
import org.eclipse.jgit.transport.ssh.jsch.OpenSshConfig;
import pl.tlinkowski.annotation.basic.NullOr;

/** API for doing the commit, tag, and push operations. See {@link GitCfg#withChangelog(File, ChangelogAndNext)}. */
public class GitActions implements AutoCloseable {
Expand Down Expand Up @@ -200,7 +202,7 @@ protected void configure(OpenSshConfig.Host host, Session session) {
}

// similar to https://github.com/ajoberstar/grgit/blob/5766317fbe67ec39faa4632e2b80c2b056f5c124/grgit-core/src/main/groovy/org/ajoberstar/grgit/auth/AuthConfig.groovy
private static CredentialsProvider creds() {
private static @NullOr CredentialsProvider creds() {
String username = System.getenv(GRGIT_USERNAME_ENV_VAR);
if (username != null) {
String password = System.getenv(GRGIT_PASSWORD_ENV_VAR);
Expand All @@ -209,18 +211,25 @@ private static CredentialsProvider creds() {
}
return new UsernamePasswordCredentialsProvider(username, password);
}
username = System.getenv(GH_TOKEN_ENV_VAR);
if (username != null) {
return new UsernamePasswordCredentialsProvider(username, "");
String githubToken = GITHUB_VARS.stream()
.map(System::getenv)
.filter(Objects::nonNull)
.findFirst().orElse(null);
if (githubToken != null) {
return new UsernamePasswordCredentialsProvider(githubToken, "");
}
return null;
}

private static final String GRGIT_USERNAME_ENV_VAR = "GRGIT_USER";
private static final String GRGIT_PASSWORD_ENV_VAR = "GRGIT_PASS";
private static final String GH_TOKEN_ENV_VAR = "gh_token";
private static final List<String> GITHUB_VARS = Arrays.asList("GH_TOKEN", "GITHUB_TOKEN", "gh_token");

private static List<String> envVars() {
return Arrays.asList(GRGIT_USERNAME_ENV_VAR, GRGIT_PASSWORD_ENV_VAR, GH_TOKEN_ENV_VAR);
var list = new ArrayList<String>();
list.addAll(GITHUB_VARS);
list.add(GRGIT_USERNAME_ENV_VAR);
list.add(GRGIT_PASSWORD_ENV_VAR);
return list;
}
}

0 comments on commit 5251040

Please sign in to comment.