Skip to content

Commit

Permalink
Add support for GITHUB_TOKEN and GH_TOKEN to match the gh tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg committed Jul 6, 2024
1 parent 23d8e56 commit fac4ba1
Showing 1 changed file with 15 additions and 6 deletions.
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 fac4ba1

Please sign in to comment.