Skip to content

Commit

Permalink
Add --no-token flag
Browse files Browse the repository at this point in the history
  • Loading branch information
edvbld committed Nov 26, 2019
1 parent dab55bd commit ac5efe8
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions cli/src/main/java/org/openjdk/skara/cli/GitPr.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ public static void main(String[] args) throws IOException, InterruptedException
.fullname("no-decoration")
.helptext("Hide any decorations when listing PRs")
.optional(),
Switch.shortcut("")
.fullname("no-token")
.helptext("Do not use a personal access token (PAT). Only works for read-only operations.")
.optional(),
Switch.shortcut("")
.fullname("mercurial")
.helptext("Force use of Mercurial (hg)")
Expand Down Expand Up @@ -306,21 +310,36 @@ public static void main(String[] args) throws IOException, InterruptedException
var username = arguments.contains("username") ? arguments.get("username").asString() : null;
var token = isMercurial ? System.getenv("HG_TOKEN") : System.getenv("GIT_TOKEN");
var uri = Remote.toWebURI(remotePullPath);
var action = arguments.at(0).asString();
var isReadOnly = List.of("list", "fetch", "show", "checkout", "apply").contains(action);
var credentials = isReadOnly ?
var shouldUseToken = !arguments.contains("no-token");
var credentials = !shouldUseToken ?
null :
GitCredentials.fill(uri.getHost(), uri.getPath(), username, token, uri.getScheme());
var forgeURI = URI.create(uri.getScheme() + "://" + uri.getHost());
var forge = credentials == null ?
Forge.from(forgeURI) :
Forge.from(forgeURI, new Credential(credentials.username(), credentials.password()));
if (forge.isEmpty() || !forge.get().isValid()) {
if (!shouldUseToken) {
if (arguments.contains("verbose")) {
System.err.println("");
}
System.err.println("warning: using git-pr with --no-token may result in rate limiting from " + forgeURI);
if (!arguments.contains("verbose")) {
System.err.println(" Re-run git-pr with --verbose to see if you are being rate limited");
System.err.println("");
}
}
exit("error: failed to connect to host: " + forgeURI);
}

var host = forge.get();

var action = arguments.at(0).asString();
if (!shouldUseToken &&
!List.of("list", "fetch", "show", "checkout", "apply").contains(action)) {
System.err.println("error: --no-token can only be used with read-only operations");
System.exit(1);
}

if (action.equals("create")) {
if (isMercurial) {
var currentBookmark = repo.currentBookmark();
Expand Down

0 comments on commit ac5efe8

Please sign in to comment.