1010
1111import com .google .common .reflect .ClassPath ;
1212import com .salesforce .dockerfileimageupdate .subcommands .ExecutableWithNamespace ;
13- import com .salesforce .dockerfileimageupdate .utils .Constants ;
1413import com .salesforce .dockerfileimageupdate .utils .DockerfileGitHubUtil ;
1514import com .salesforce .dockerfileimageupdate .utils .GitHubUtil ;
1615import net .sourceforge .argparse4j .ArgumentParsers ;
2726
2827import java .io .IOException ;
2928import java .util .Comparator ;
29+ import java .util .Objects ;
30+ import java .util .Optional ;
3031import java .util .Set ;
3132import java .util .TreeSet ;
33+ import java .util .function .Consumer ;
34+ import java .util .function .Function ;
35+ import java .util .function .Supplier ;
3236import java .util .stream .Collectors ;
3337import static com .salesforce .dockerfileimageupdate .utils .Constants .*;
3438
@@ -49,8 +53,8 @@ public static void main(String[] args)
4953 Namespace ns = handleArguments (parser , args );
5054 if (ns == null )
5155 System .exit (1 );
52- Class <?> runClass = loadCommand (allClasses , ns .get (Constants . COMMAND ));
53- DockerfileGitHubUtil dockerfileGitHubUtil = initializeDockerfileGithubUtil (ns . get ( Constants . GIT_API ));
56+ Class <?> runClass = loadCommand (allClasses , ns .get (COMMAND ));
57+ DockerfileGitHubUtil dockerfileGitHubUtil = initializeDockerfileGithubUtil (gitApiUrl ( ns ), gitApiToken (), ns . getBoolean ( DEBUG ));
5458
5559 /* Execute given command. */
5660 ((ExecutableWithNamespace )runClass .getDeclaredConstructor ().newInstance ()).execute (ns , dockerfileGitHubUtil );
@@ -61,30 +65,30 @@ static ArgumentParser getArgumentParser() {
6165 ArgumentParsers .newFor ("dockerfile-image-update" ).addHelp (true ).build ()
6266 .description ("Image Updates through Pull Request Automator" );
6367
64- parser .addArgument ("-l" , "--" + Constants . GIT_API_SEARCH_LIMIT )
68+ parser .addArgument ("-l" , "--" + GIT_API_SEARCH_LIMIT )
6569 .type (Integer .class )
6670 .setDefault (1000 )
6771 .help ("limit the search results for github api (default: 1000)" );
68- parser .addArgument ("-o" , "--" + Constants . GIT_ORG )
72+ parser .addArgument ("-o" , "--" + GIT_ORG )
6973 .help ("search within specific organization (default: all of github)" );
7074 /* Currently, because of argument passing reasons, you can only specify one branch. */
71- parser .addArgument ("-b" , "--" + Constants . GIT_BRANCH )
75+ parser .addArgument ("-b" , "--" + GIT_BRANCH )
7276 .help ("make pull requests for given branch name (default: main)" );
73- parser .addArgument ("-g" , "--" + Constants . GIT_API )
77+ parser .addArgument ("-g" , "--" + GIT_API )
7478 .help ("link to github api; overrides environment variable" );
7579 parser .addArgument ("-f" , "--auto-merge" ).action (Arguments .storeTrue ())
7680 .help ("NOT IMPLEMENTED / set to automatically merge pull requests if available" );
7781 parser .addArgument ("-m" )
7882 .help ("message to provide for pull requests" );
7983 parser .addArgument ("-c" )
8084 .help ("additional commit message for the commits in pull requests" );
81- parser .addArgument ("-e" , "--" + Constants . GIT_REPO_EXCLUDES )
85+ parser .addArgument ("-e" , "--" + GIT_REPO_EXCLUDES )
8286 .help ("regex of repository names to exclude from pull request generation" );
8387 parser .addArgument ("-B" )
8488 .help ("additional body text to include in pull requests" );
85- parser .addArgument ("-s" , "--" + Constants . SKIP_PR_CREATION )
89+ parser .addArgument ("-s" , "--" + SKIP_PR_CREATION )
8690 .type (Boolean .class )
87- .setDefault (false )
91+ .setDefault (false ) //To prevent null from being returned by the argument
8892 .help ("Only update image tag store. Skip creating PRs" );
8993 parser .addArgument ("-x" )
9094 .help ("comment snippet mentioned in line just before 'FROM' instruction(Dockerfile)" +
@@ -99,14 +103,19 @@ static ArgumentParser getArgumentParser() {
99103 .type (String .class )
100104 .required (false )
101105 .help ("Use RateLimiting when sending PRs. RateLimiting is enabled only if this value is set it's disabled by default." );
106+ parser .addArgument ("-d" , "--" + DEBUG )
107+ .type (Boolean .class )
108+ .setDefault (false ) //To prevent null from being returned by the argument
109+ .required (false )
110+ .help ("Enable debug logging, including git wire logs." );
102111 return parser ;
103112 }
104113
105114 /* Adding subcommands to the subcommands list.
106115 argparse4j allows commands to be truncated, so users can type the first letter (a,c,p) for commands */
107116 public static Set <ClassPath .ClassInfo > findSubcommands (ArgumentParser parser ) throws IOException {
108117 Subparsers subparsers = parser .addSubparsers ()
109- .dest (Constants . COMMAND )
118+ .dest (COMMAND )
110119 .help ("FEATURE" )
111120 .title ("subcommands" )
112121 .description ("Specify which feature to perform" )
@@ -185,46 +194,65 @@ public static Class<?> loadCommand(Set<ClassPath.ClassInfo> allClasses, String c
185194 return runClass ;
186195 }
187196
188- /* Validate API URL and connect to the API using credentials. */
189- public static DockerfileGitHubUtil initializeDockerfileGithubUtil (String gitApiUrl ) throws IOException {
190- if (gitApiUrl == null ) {
191- gitApiUrl = System .getenv ("git_api_url" );
192- if (gitApiUrl == null ) {
193- throw new IOException ("No Git API URL in environment variables." );
194- }
195- }
196- String token = System .getenv ("git_api_token" );
197- if (token == null ) {
198- log .error ("Please provide GitHub token in environment variables." );
199- System .exit (3 );
200- }
197+ public static String gitApiToken () {
198+ return gitApiToken (System ::getenv , System ::exit );
199+ }
201200
202- HttpLoggingInterceptor logger = new HttpLoggingInterceptor ();
203- logger .setLevel (HttpLoggingInterceptor .Level .HEADERS );
204- logger .redactHeader ("Authorization" );
201+ public static String gitApiToken (final Function <String , String > envFunc , final Consumer <Integer > exitFunc ) {
202+ final String token = envFunc .apply ("git_api_token" );
203+ if (Objects .isNull (token )) {
204+ log .error ("Please provide GitHub token in environment variables." );
205+ exitFunc .accept (3 );
206+ }
207+ return token ;
208+ }
209+
210+ public static String gitApiUrl (final Namespace ns ) throws IOException {
211+ return gitApiUrl (ns , System ::getenv );
212+ }
205213
206- GitHub github = shouldAddWireLogger (new GitHubBuilder ()).withEndpoint (gitApiUrl )
214+ public static String gitApiUrl (final Namespace ns , final Function <String , String > envFunc ) throws IOException {
215+ return Optional .ofNullable (
216+ Optional .ofNullable (ns .getString (GIT_API ))
217+ .orElse (envFunc .apply ("git_api_url" ))
218+ )
219+ .orElseThrow (() -> new IOException ("No Git API URL in environment variables nor on the commmand line." ));
220+ }
221+
222+ public static DockerfileGitHubUtil initializeDockerfileGithubUtil (
223+ final String gitApiUrl ,
224+ final String token ,
225+ final boolean debug ) throws IOException
226+ {
227+ return initializeDockerfileGithubUtil (gitApiUrl , token , () -> new GitHubBuilder (), debug );
228+ }
229+
230+ /* Validate API URL and connect to the API using credentials. */
231+ public static DockerfileGitHubUtil initializeDockerfileGithubUtil (
232+ final String gitApiUrl ,
233+ final String token ,
234+ final Supplier <GitHubBuilder > builderFunc ,
235+ final boolean debug ) throws IOException {
236+
237+ GitHub github = shouldAddWireLogger (builderFunc .get (), debug )
238+ .withEndpoint (gitApiUrl )
207239 .withOAuthToken (token )
208- .withConnector (new OkHttpGitHubConnector (new OkHttpClient .Builder ()
209- .addInterceptor (logger )
210- .build ()))
211240 .build ();
212241 github .checkApiUrlValidity ();
213242
214- GitHubUtil gitHubUtil = new GitHubUtil (github );
215-
216- return new DockerfileGitHubUtil (gitHubUtil );
243+ return new DockerfileGitHubUtil (new GitHubUtil (github ));
217244 }
218245
219- public static GitHubBuilder shouldAddWireLogger (final GitHubBuilder builder )
220- {
221- HttpLoggingInterceptor logger = new HttpLoggingInterceptor ();
222- logger .setLevel (HttpLoggingInterceptor .Level .HEADERS );
223- logger .redactHeader ("Authorization" );
224-
225- builder .withConnector (new OkHttpGitHubConnector (new OkHttpClient .Builder ()
226- .addInterceptor (logger )
227- .build ()));
246+ public static GitHubBuilder shouldAddWireLogger (final GitHubBuilder builder , final boolean debug ) {
247+ if (debug ) {
248+ HttpLoggingInterceptor logger = new HttpLoggingInterceptor ();
249+ logger .setLevel (HttpLoggingInterceptor .Level .HEADERS );
250+ logger .redactHeader ("Authorization" );
251+
252+ builder .withConnector (new OkHttpGitHubConnector (new OkHttpClient .Builder ()
253+ .addInterceptor (logger )
254+ .build ()));
255+ }
228256 return builder ;
229257 }
230258}
0 commit comments