@@ -40,8 +40,7 @@ public class GitlabApiClient {
40
40
41
41
private static final Logger logger = LoggerFactory .getLogger (GitlabApiClient .class );
42
42
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 );
45
44
private static final String membershipParam = "&membership=true" ;
46
45
private static final String paginationParam = "&per_page=100" ;
47
46
private static final String refNameParam = "&ref_name=" ;
@@ -50,52 +49,55 @@ public class GitlabApiClient {
50
49
private static final String uriPartForCommits = "/repository/commits/" ;
51
50
private static final String uriPartForDiff = "/diff/" ;
52
51
private static final String uriPartForProjects = "/api/v4/projects/" ;
52
+
53
53
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 ;
56
55
private final String uriParams ;
56
+
57
57
private ZonedDateTime lastRun = null ;
58
58
59
59
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 ;
61
62
this .uriParams = "?" + accessTokenParam + membershipParam + paginationParam ;
62
63
this .baseUri = gitlabBaseUri + uriPartForProjects ;
64
+ this .httpClient = HttpClient .newBuilder ()
65
+ .version (HttpClient .Version .HTTP_2 )
66
+ .connectTimeout (Duration .ofMinutes (5 ))
67
+ .build ();
63
68
}
64
69
65
70
/**
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.
68
73
*
69
74
* @return List<GitMessage>
70
75
*/
71
76
public List <GitlabCommitMessage > getGitlabCommitMessages () {
72
77
// if we are running for the first time get all commits since `initialSinceDate`
73
78
// otherwise get all commits since last run
74
79
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 );
78
81
// adjust the time of last run
79
82
lastRun = nowAtUtc ;
80
83
81
84
// first get all user projects
82
85
JsonArray projects = getUserProjects ();
83
- logger .info ("Total {} user projects are found " , projects .size ());
86
+ logger .info ("Found {} user projects" , projects .size ());
84
87
List <GitlabCommitMessage > gitlabCommitMessages = new LinkedList <>();
85
88
for (JsonElement project : projects ) {
86
89
JsonObject projectJsonObject = project .getAsJsonObject ();
87
90
// get project id
88
91
String projectId = projectJsonObject .get ("id" ).getAsString ();
89
92
// get all branches for given project, create a new GitMessage
90
93
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 );
92
95
93
96
for (JsonElement branch : branches ) {
94
97
String branchName = branch .getAsJsonObject ().get ("name" ).getAsString ();
95
98
// get all commits for given branch
96
99
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 );
99
101
100
102
for (JsonElement commit : commitsInBranch ) {
101
103
JsonObject commitJsonObject = commit .getAsJsonObject ();
@@ -131,8 +133,8 @@ private int calculateTimeSinceLastCommit(String projectId, JsonObject commit) {
131
133
try {
132
134
ZonedDateTime commitCreationDate = ZonedDateTime .parse (commitCreationDateStr , formatter );
133
135
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 ();
136
138
if (longDifference <= (long ) Integer .MAX_VALUE ) {
137
139
difference = (int ) longDifference ;
138
140
}
@@ -149,26 +151,29 @@ private int calculateTimeSinceLastCommit(String projectId, JsonObject commit) {
149
151
}
150
152
151
153
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 ));
153
156
}
154
157
155
158
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 ));
158
161
}
159
162
160
163
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 ));
163
166
}
164
167
165
168
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 ));
168
172
}
169
173
170
174
private JsonArray getUserProjects () {
171
- return parseHttpResponseToJsonArray (makeGetCallToGitlab (baseUri + uriParams ));
175
+ final String uri = baseUri + uriParams ;
176
+ return parseHttpResponseToJsonArray (makeGetCallToGitlab (uri ));
172
177
}
173
178
174
179
/**
0 commit comments