Skip to content

Commit 5251040

Browse files
authored
Add support for GITHUB_TOKEN and GH_TOKEN to match the gh tool. (#53)
2 parents 23d8e56 + 5fd8603 commit 5251040

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

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

912
## [3.1.1] - 2024-07-06
1013
### Fixed

spotless-changelog-lib/src/main/java/com/diffplug/spotless/changelog/GitActions.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.jcraft.jsch.Session;
2020
import java.io.File;
2121
import java.io.IOException;
22+
import java.util.ArrayList;
2223
import java.util.Arrays;
2324
import java.util.List;
2425
import java.util.Objects;
@@ -41,6 +42,7 @@
4142
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
4243
import org.eclipse.jgit.transport.ssh.jsch.JschConfigSessionFactory;
4344
import org.eclipse.jgit.transport.ssh.jsch.OpenSshConfig;
45+
import pl.tlinkowski.annotation.basic.NullOr;
4446

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

202204
// similar to https://github.com/ajoberstar/grgit/blob/5766317fbe67ec39faa4632e2b80c2b056f5c124/grgit-core/src/main/groovy/org/ajoberstar/grgit/auth/AuthConfig.groovy
203-
private static CredentialsProvider creds() {
205+
private static @NullOr CredentialsProvider creds() {
204206
String username = System.getenv(GRGIT_USERNAME_ENV_VAR);
205207
if (username != null) {
206208
String password = System.getenv(GRGIT_PASSWORD_ENV_VAR);
@@ -209,18 +211,25 @@ private static CredentialsProvider creds() {
209211
}
210212
return new UsernamePasswordCredentialsProvider(username, password);
211213
}
212-
username = System.getenv(GH_TOKEN_ENV_VAR);
213-
if (username != null) {
214-
return new UsernamePasswordCredentialsProvider(username, "");
214+
String githubToken = GITHUB_VARS.stream()
215+
.map(System::getenv)
216+
.filter(Objects::nonNull)
217+
.findFirst().orElse(null);
218+
if (githubToken != null) {
219+
return new UsernamePasswordCredentialsProvider(githubToken, "");
215220
}
216221
return null;
217222
}
218223

219224
private static final String GRGIT_USERNAME_ENV_VAR = "GRGIT_USER";
220225
private static final String GRGIT_PASSWORD_ENV_VAR = "GRGIT_PASS";
221-
private static final String GH_TOKEN_ENV_VAR = "gh_token";
226+
private static final List<String> GITHUB_VARS = Arrays.asList("GH_TOKEN", "GITHUB_TOKEN", "gh_token");
222227

223228
private static List<String> envVars() {
224-
return Arrays.asList(GRGIT_USERNAME_ENV_VAR, GRGIT_PASSWORD_ENV_VAR, GH_TOKEN_ENV_VAR);
229+
var list = new ArrayList<String>();
230+
list.addAll(GITHUB_VARS);
231+
list.add(GRGIT_USERNAME_ENV_VAR);
232+
list.add(GRGIT_PASSWORD_ENV_VAR);
233+
return list;
225234
}
226235
}

0 commit comments

Comments
 (0)