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 ;
1716import net .sourceforge .argparse4j .impl .Arguments ;
1817import net .sourceforge .argparse4j .inf .*;
18+ import okhttp3 .OkHttpClient ;
19+ import okhttp3 .logging .HttpLoggingInterceptor ;
20+
1921import org .kohsuke .github .GitHub ;
2022import org .kohsuke .github .GitHubBuilder ;
23+ import org .kohsuke .github .extras .okhttp3 .OkHttpGitHubConnector ;
2124import org .slf4j .Logger ;
2225import org .slf4j .LoggerFactory ;
2326
2427import java .io .IOException ;
2528import java .util .Comparator ;
29+ import java .util .Objects ;
30+ import java .util .Optional ;
2631import java .util .Set ;
2732import java .util .TreeSet ;
33+ import java .util .function .Consumer ;
34+ import java .util .function .Function ;
35+ import java .util .function .Supplier ;
2836import java .util .stream .Collectors ;
2937import static com .salesforce .dockerfileimageupdate .utils .Constants .*;
3038
@@ -45,8 +53,8 @@ public static void main(String[] args)
4553 Namespace ns = handleArguments (parser , args );
4654 if (ns == null )
4755 System .exit (1 );
48- Class <?> runClass = loadCommand (allClasses , ns .get (Constants . COMMAND ));
49- 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 ));
5058
5159 /* Execute given command. */
5260 ((ExecutableWithNamespace )runClass .getDeclaredConstructor ().newInstance ()).execute (ns , dockerfileGitHubUtil );
@@ -57,30 +65,30 @@ static ArgumentParser getArgumentParser() {
5765 ArgumentParsers .newFor ("dockerfile-image-update" ).addHelp (true ).build ()
5866 .description ("Image Updates through Pull Request Automator" );
5967
60- parser .addArgument ("-l" , "--" + Constants . GIT_API_SEARCH_LIMIT )
68+ parser .addArgument ("-l" , "--" + GIT_API_SEARCH_LIMIT )
6169 .type (Integer .class )
6270 .setDefault (1000 )
6371 .help ("limit the search results for github api (default: 1000)" );
64- parser .addArgument ("-o" , "--" + Constants . GIT_ORG )
72+ parser .addArgument ("-o" , "--" + GIT_ORG )
6573 .help ("search within specific organization (default: all of github)" );
6674 /* Currently, because of argument passing reasons, you can only specify one branch. */
67- parser .addArgument ("-b" , "--" + Constants . GIT_BRANCH )
75+ parser .addArgument ("-b" , "--" + GIT_BRANCH )
6876 .help ("make pull requests for given branch name (default: main)" );
69- parser .addArgument ("-g" , "--" + Constants . GIT_API )
77+ parser .addArgument ("-g" , "--" + GIT_API )
7078 .help ("link to github api; overrides environment variable" );
7179 parser .addArgument ("-f" , "--auto-merge" ).action (Arguments .storeTrue ())
7280 .help ("NOT IMPLEMENTED / set to automatically merge pull requests if available" );
7381 parser .addArgument ("-m" )
7482 .help ("message to provide for pull requests" );
7583 parser .addArgument ("-c" )
7684 .help ("additional commit message for the commits in pull requests" );
77- parser .addArgument ("-e" , "--" + Constants . GIT_REPO_EXCLUDES )
85+ parser .addArgument ("-e" , "--" + GIT_REPO_EXCLUDES )
7886 .help ("regex of repository names to exclude from pull request generation" );
7987 parser .addArgument ("-B" )
8088 .help ("additional body text to include in pull requests" );
81- parser .addArgument ("-s" , "--" + Constants . SKIP_PR_CREATION )
89+ parser .addArgument ("-s" , "--" + SKIP_PR_CREATION )
8290 .type (Boolean .class )
83- .setDefault (false )
91+ .setDefault (false ) //To prevent null from being returned by the argument
8492 .help ("Only update image tag store. Skip creating PRs" );
8593 parser .addArgument ("-x" )
8694 .help ("comment snippet mentioned in line just before 'FROM' instruction(Dockerfile)" +
@@ -95,14 +103,19 @@ static ArgumentParser getArgumentParser() {
95103 .type (String .class )
96104 .required (false )
97105 .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." );
98111 return parser ;
99112 }
100113
101114 /* Adding subcommands to the subcommands list.
102115 argparse4j allows commands to be truncated, so users can type the first letter (a,c,p) for commands */
103116 public static Set <ClassPath .ClassInfo > findSubcommands (ArgumentParser parser ) throws IOException {
104117 Subparsers subparsers = parser .addSubparsers ()
105- .dest (Constants . COMMAND )
118+ .dest (COMMAND )
106119 .help ("FEATURE" )
107120 .title ("subcommands" )
108121 .description ("Specify which feature to perform" )
@@ -181,27 +194,65 @@ public static Class<?> loadCommand(Set<ClassPath.ClassInfo> allClasses, String c
181194 return runClass ;
182195 }
183196
197+ public static String gitApiToken () {
198+ return gitApiToken (System ::getenv , System ::exit );
199+ }
200+
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+ }
213+
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+
184230 /* Validate API URL and connect to the API using credentials. */
185- public static DockerfileGitHubUtil initializeDockerfileGithubUtil (String gitApiUrl ) throws IOException {
186- if (gitApiUrl == null ) {
187- gitApiUrl = System .getenv ("git_api_url" );
188- if (gitApiUrl == null ) {
189- throw new IOException ("No Git API URL in environment variables." );
190- }
191- }
192- String token = System .getenv ("git_api_token" );
193- if (token == null ) {
194- log .error ("Please provide GitHub token in environment variables." );
195- System .exit (3 );
196- }
231+ public static DockerfileGitHubUtil initializeDockerfileGithubUtil (
232+ final String gitApiUrl ,
233+ final String token ,
234+ final Supplier <GitHubBuilder > builderFunc ,
235+ final boolean debug ) throws IOException {
197236
198- GitHub github = new GitHubBuilder ().withEndpoint (gitApiUrl )
237+ GitHub github = shouldAddWireLogger (builderFunc .get (), debug )
238+ .withEndpoint (gitApiUrl )
199239 .withOAuthToken (token )
200240 .build ();
201241 github .checkApiUrlValidity ();
202242
203- GitHubUtil gitHubUtil = new GitHubUtil (github );
243+ return new DockerfileGitHubUtil (new GitHubUtil (github ));
244+ }
245+
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" );
204251
205- return new DockerfileGitHubUtil (gitHubUtil );
252+ builder .withConnector (new OkHttpGitHubConnector (new OkHttpClient .Builder ()
253+ .addInterceptor (logger )
254+ .build ()));
255+ }
256+ return builder ;
206257 }
207258}
0 commit comments