25
25
26
26
@ ApplicationScoped
27
27
public class ProjectService {
28
+
28
29
public static final Logger LOGGER = LoggerFactory .getLogger (ProjectService .class );
29
30
31
+ private static final String DEPLOYMENT_KEY_PREFIX = "LodeStar DK" ;
32
+ private static final String DEPLOYMENT_KEY_POSTFIX = "DK" ;
33
+
30
34
@ Inject
31
35
@ RestClient
32
36
GitLabService gitLabService ;
33
37
38
+ @ ConfigProperty (name = "environment.id" )
39
+ String environmentId ;
40
+
34
41
@ ConfigProperty (name = "engagements.do.not.delete" )
35
42
boolean doNotDelete ;
36
-
43
+
37
44
@ ConfigProperty (name = "commit.page.size" )
38
45
int commitPageSize ;
39
-
46
+
40
47
@ ConfigProperty (name = "commit.filter.list" )
41
48
List <String > commitFilteredEmails ;
42
49
43
- // get a project - this could be a replaced by a direct call to the project via the path
50
+ // get a project - this could be a replaced by a direct call to the project via
51
+ // the path
44
52
// but must consider changes to the path for special characters
45
53
public Optional <Project > getProjectByName (Integer namespaceId , String name ) {
46
54
47
55
Optional <Project > optional = Optional .empty ();
48
-
56
+
49
57
PagedResults <ProjectSearchResults > page = new PagedResults <>();
50
-
51
- while (page .hasMore ()) {
58
+
59
+ while (page .hasMore ()) {
52
60
Response response = gitLabService .getProjectByName (name , commitPageSize , page .getNumber ());
53
- page .update (response , new GenericType <List <ProjectSearchResults >>() {});
61
+ page .update (response , new GenericType <List <ProjectSearchResults >>() {
62
+ });
54
63
}
55
64
56
65
// look for a project with name that matches the namespace id and the path
@@ -64,34 +73,36 @@ public Optional<Project> getProjectByName(Integer namespaceId, String name) {
64
73
return optional ;
65
74
66
75
}
67
-
76
+
68
77
public List <Project > getProjectsByGroup (int groupId , Boolean includeSubgroups ) {
69
-
78
+
70
79
PagedResults <Project > page = new PagedResults <>();
71
- while (page .hasMore ()) {
72
- Response response = gitLabService .getProjectsbyGroup (groupId , includeSubgroups , commitPageSize , page .getNumber ());
73
- page .update (response , new GenericType <List <Project >>() {});
80
+ while (page .hasMore ()) {
81
+ Response response = gitLabService .getProjectsbyGroup (groupId , includeSubgroups , commitPageSize ,
82
+ page .getNumber ());
83
+ page .update (response , new GenericType <List <Project >>() {
84
+ });
74
85
}
75
-
76
- if (LOGGER .isDebugEnabled ()) {
86
+
87
+ if (LOGGER .isDebugEnabled ()) {
77
88
LOGGER .debug ("project count group id ({}) {}" , groupId , page .size ());
78
89
page .getResults ().stream ().forEach (project -> LOGGER .debug ("Project {}" , project .getPathWithNamespace ()));
79
90
}
80
-
91
+
81
92
return page .getResults ();
82
93
}
83
94
84
95
public Optional <Project > getProjectById (Integer projectId ) {
85
96
return getProjectByIdOrPath (String .valueOf (projectId ));
86
97
}
87
-
98
+
88
99
public Optional <Project > getProjectByIdOrPath (String idOrPath ) {
89
100
90
101
try {
91
102
return Optional .ofNullable (gitLabService .getProjectById (idOrPath ));
92
- } catch (WebApplicationException wae ) {
103
+ } catch (WebApplicationException wae ) {
93
104
94
- if (wae .getResponse ().getStatus () == 404 ) {
105
+ if (wae .getResponse ().getStatus () == 404 ) {
95
106
return Optional .empty ();
96
107
}
97
108
@@ -100,13 +111,13 @@ public Optional<Project> getProjectByIdOrPath(String idOrPath) {
100
111
}
101
112
102
113
}
103
-
114
+
104
115
// create a project
105
116
public Optional <Project > createProject (Project project ) {
106
117
107
118
Optional <Project > optional = Optional .empty ();
108
119
109
- if (doNotDelete ) {
120
+ if (doNotDelete ) {
110
121
project .preserve ();
111
122
}
112
123
@@ -143,30 +154,39 @@ public void deleteProject(Integer projectId) {
143
154
gitLabService .deleteProjectById (projectId );
144
155
}
145
156
146
- // enable deployment key - by default it's ready only but we need to write so let's 2-step
157
+ // enable deployment key - by default it's ready only but we need to write so
158
+ // let's 2-step
147
159
public void enableDeploymentKeyOnProject (Integer projectId , Integer deployKey ) {
148
-
160
+
149
161
gitLabService .enableDeployKey (projectId , deployKey );
150
- gitLabService .updateDeployKey (projectId , deployKey , DeployKey .builder ().title ("LodeStar DK" ).canPush (true ).build ());
162
+ gitLabService
163
+ .updateDeployKey (projectId , deployKey ,
164
+ DeployKey
165
+ .builder ().title (new StringBuilder (DEPLOYMENT_KEY_PREFIX ).append (" " )
166
+ .append (environmentId ).append (" " ).append (DEPLOYMENT_KEY_POSTFIX ).toString ())
167
+ .canPush (true ).build ());
151
168
}
152
-
169
+
153
170
public List <Commit > getCommitLog (String projectId ) {
154
171
PagedResults <Commit > page = new PagedResults <>(commitPageSize );
155
-
156
- while (page .hasMore ()) {
172
+
173
+ while (page .hasMore ()) {
157
174
Response response = gitLabService .getCommitLog (projectId , commitPageSize , page .getNumber ());
158
- page .update (response , new GenericType <List <Commit >>() {});
175
+ page .update (response , new GenericType <List <Commit >>() {
176
+ });
159
177
}
160
-
178
+
161
179
LOGGER .debug ("total commits for project {} {}" , projectId , page .size ());
162
-
163
- return page .getResults ().stream ().filter (e -> !commitFilteredEmails .contains (e .getAuthorEmail ())).collect (Collectors .toList ());
180
+
181
+ return page .getResults ().stream ().filter (e -> !commitFilteredEmails .contains (e .getAuthorEmail ()))
182
+ .collect (Collectors .toList ());
164
183
165
184
}
166
185
167
186
public Optional <Project > transferProject (Integer projectId , Integer newGroupId ) {
168
187
169
- return gitLabService .transferProject (projectId , ProjectTransfer .builder ().id (projectId ).namespace (newGroupId ).build ());
188
+ return gitLabService .transferProject (projectId ,
189
+ ProjectTransfer .builder ().id (projectId ).namespace (newGroupId ).build ());
170
190
171
191
}
172
192
0 commit comments