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

Add support for GITHUB_TOKEN and GH_TOKEN to match the gh tool. #53

Merged
merged 2 commits into from
Jul 6, 2024
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
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;
}
}
Loading