@@ -40,8 +40,7 @@ public class GitlabApiClient {
4040
4141 private static final Logger logger = LoggerFactory .getLogger (GitlabApiClient .class );
4242 private static final DateTimeFormatter formatter = DateTimeFormatter .ISO_DATE_TIME ;
43- private static final ZonedDateTime initialSinceDate = ZonedDateTime .of (
44- 2022 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneOffset .UTC );
43+ private static final ZonedDateTime initialSinceDate = ZonedDateTime .of (2022 , 1 , 1 , 0 , 0 , 0 , 0 , ZoneOffset .UTC );
4544 private static final String membershipParam = "&membership=true" ;
4645 private static final String paginationParam = "&per_page=100" ;
4746 private static final String refNameParam = "&ref_name=" ;
@@ -50,52 +49,55 @@ public class GitlabApiClient {
5049 private static final String uriPartForCommits = "/repository/commits/" ;
5150 private static final String uriPartForDiff = "/diff/" ;
5251 private static final String uriPartForProjects = "/api/v4/projects/" ;
52+
5353 private final String baseUri ;
54- private final HttpClient httpClient = HttpClient .newBuilder ().version (HttpClient .Version .HTTP_2 )
55- .connectTimeout (Duration .ofMinutes (5 )).build ();
54+ private final HttpClient httpClient ;
5655 private final String uriParams ;
56+
5757 private ZonedDateTime lastRun = null ;
5858
5959 public GitlabApiClient (String accessToken , String gitlabBaseUri ) {
60- String accessTokenParam = "private_token=" + accessToken ;
60+ logger .info ("Creating new {} for {}" , this .getClass ().getSimpleName (), gitlabBaseUri );
61+ final String accessTokenParam = "private_token=" + accessToken ;
6162 this .uriParams = "?" + accessTokenParam + membershipParam + paginationParam ;
6263 this .baseUri = gitlabBaseUri + uriPartForProjects ;
64+ this .httpClient = HttpClient .newBuilder ()
65+ .version (HttpClient .Version .HTTP_2 )
66+ .connectTimeout (Duration .ofMinutes (5 ))
67+ .build ();
6368 }
6469
6570 /**
66- * generates gitMessages for given user
67- * creates separate message for the commit in all the branches
71+ * Generates a list of {@link GitlabCommitMessage}s for user identified by the access token passed to constructor.
72+ * Creates separate message for each commit in all branches of all projects the user has access to.
6873 *
6974 * @return List<GitMessage>
7075 */
7176 public List <GitlabCommitMessage > getGitlabCommitMessages () {
7277 // if we are running for the first time get all commits since `initialSinceDate`
7378 // otherwise get all commits since last run
7479 final ZonedDateTime nowAtUtc = ZonedDateTime .now (ZoneOffset .UTC );
75- final String sinceDateTime = (lastRun == null )
76- ? initialSinceDate .format (formatter )
77- : lastRun .format (formatter );
80+ final String sinceDateTime = (lastRun == null ) ? initialSinceDate .format (formatter ) : lastRun .format (formatter );
7881 // adjust the time of last run
7982 lastRun = nowAtUtc ;
8083
8184 // first get all user projects
8285 JsonArray projects = getUserProjects ();
83- logger .info ("Total {} user projects are found " , projects .size ());
86+ logger .info ("Found {} user projects" , projects .size ());
8487 List <GitlabCommitMessage > gitlabCommitMessages = new LinkedList <>();
8588 for (JsonElement project : projects ) {
8689 JsonObject projectJsonObject = project .getAsJsonObject ();
8790 // get project id
8891 String projectId = projectJsonObject .get ("id" ).getAsString ();
8992 // get all branches for given project, create a new GitMessage
9093 JsonArray branches = getAllBranchesForGivenProject (projectId );
91- logger .info ("Total {} branches for project with given ID {} are found " , branches .size (), projectId );
94+ logger .info ("Found {} branches for project with ID {}" , branches .size (), projectId );
9295
9396 for (JsonElement branch : branches ) {
9497 String branchName = branch .getAsJsonObject ().get ("name" ).getAsString ();
9598 // get all commits for given branch
9699 JsonArray commitsInBranch = getCommitsForGivenBranch (projectId , branchName , sinceDateTime );
97- logger .info ("Total {} commits are found in a given branch with name '{}' since {}" ,
98- commitsInBranch .size (), branchName , sinceDateTime );
100+ logger .info ("Found {} commits in branch '{}' since {}" , commitsInBranch .size (), branchName , sinceDateTime );
99101
100102 for (JsonElement commit : commitsInBranch ) {
101103 JsonObject commitJsonObject = commit .getAsJsonObject ();
@@ -131,8 +133,8 @@ private int calculateTimeSinceLastCommit(String projectId, JsonObject commit) {
131133 try {
132134 ZonedDateTime commitCreationDate = ZonedDateTime .parse (commitCreationDateStr , formatter );
133135 ZonedDateTime parentCommitCreationDate = ZonedDateTime .parse (parentCommitCreationDateStr , formatter );
134- long longDifference = commitCreationDate .toInstant ().getEpochSecond () -
135- parentCommitCreationDate .toInstant ().getEpochSecond ();
136+ long longDifference = commitCreationDate .toInstant ().getEpochSecond ()
137+ - parentCommitCreationDate .toInstant ().getEpochSecond ();
136138 if (longDifference <= (long ) Integer .MAX_VALUE ) {
137139 difference = (int ) longDifference ;
138140 }
@@ -149,26 +151,29 @@ private int calculateTimeSinceLastCommit(String projectId, JsonObject commit) {
149151 }
150152
151153 private JsonArray getAllBranchesForGivenProject (String projectId ) {
152- return parseHttpResponseToJsonArray (makeGetCallToGitlab (baseUri + projectId + uriPartForBranches + uriParams ));
154+ final String uri = baseUri + projectId + uriPartForBranches + uriParams ;
155+ return parseHttpResponseToJsonArray (makeGetCallToGitlab (uri ));
153156 }
154157
155158 private JsonObject getCommitById (String projectId , String commitId ) {
156- return parseHttpResponseToJsonObject ( makeGetCallToGitlab ( baseUri + projectId
157- + uriPartForCommits + commitId + uriParams ));
159+ final String uri = baseUri + projectId + uriPartForCommits + commitId + uriParams ;
160+ return parseHttpResponseToJsonObject ( makeGetCallToGitlab ( uri ));
158161 }
159162
160163 private JsonArray getCommitDiff (String projectId , String commitId ) {
161- return parseHttpResponseToJsonArray ( makeGetCallToGitlab ( baseUri + projectId
162- + uriPartForCommits + commitId + uriPartForDiff + uriParams ));
164+ final String uri = baseUri + projectId + uriPartForCommits + commitId + uriPartForDiff + uriParams ;
165+ return parseHttpResponseToJsonArray ( makeGetCallToGitlab ( uri ));
163166 }
164167
165168 private JsonArray getCommitsForGivenBranch (String projectId , String branchName , String sinceDateTime ) {
166- return parseHttpResponseToJsonArray (makeGetCallToGitlab (baseUri + projectId
167- + uriPartForCommits + uriParams + refNameParam + branchName + sinceParam + sinceDateTime ));
169+ final String uri =
170+ baseUri + projectId + uriPartForCommits + uriParams + refNameParam + branchName + sinceParam + sinceDateTime ;
171+ return parseHttpResponseToJsonArray (makeGetCallToGitlab (uri ));
168172 }
169173
170174 private JsonArray getUserProjects () {
171- return parseHttpResponseToJsonArray (makeGetCallToGitlab (baseUri + uriParams ));
175+ final String uri = baseUri + uriParams ;
176+ return parseHttpResponseToJsonArray (makeGetCallToGitlab (uri ));
172177 }
173178
174179 /**
0 commit comments