Skip to content

Commit f158483

Browse files
authored
Merge pull request #120 from dwasinge/dk-env
Project Deployment Key Includes Environment ID
2 parents b2cd694 + cc9f531 commit f158483

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

src/main/java/com/redhat/labs/lodestar/service/ProjectService.java

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,41 @@
2525

2626
@ApplicationScoped
2727
public class ProjectService {
28+
2829
public static final Logger LOGGER = LoggerFactory.getLogger(ProjectService.class);
2930

31+
private static final String DEPLOYMENT_KEY_PREFIX = "LodeStar DK";
32+
private static final String DEPLOYMENT_KEY_POSTFIX = "DK";
33+
3034
@Inject
3135
@RestClient
3236
GitLabService gitLabService;
3337

38+
@ConfigProperty(name = "environment.id")
39+
String environmentId;
40+
3441
@ConfigProperty(name = "engagements.do.not.delete")
3542
boolean doNotDelete;
36-
43+
3744
@ConfigProperty(name = "commit.page.size")
3845
int commitPageSize;
39-
46+
4047
@ConfigProperty(name = "commit.filter.list")
4148
List<String> commitFilteredEmails;
4249

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
4452
// but must consider changes to the path for special characters
4553
public Optional<Project> getProjectByName(Integer namespaceId, String name) {
4654

4755
Optional<Project> optional = Optional.empty();
48-
56+
4957
PagedResults<ProjectSearchResults> page = new PagedResults<>();
50-
51-
while(page.hasMore()) {
58+
59+
while (page.hasMore()) {
5260
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+
});
5463
}
5564

5665
// 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) {
6473
return optional;
6574

6675
}
67-
76+
6877
public List<Project> getProjectsByGroup(int groupId, Boolean includeSubgroups) {
69-
78+
7079
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+
});
7485
}
75-
76-
if(LOGGER.isDebugEnabled()) {
86+
87+
if (LOGGER.isDebugEnabled()) {
7788
LOGGER.debug("project count group id ({}) {}", groupId, page.size());
7889
page.getResults().stream().forEach(project -> LOGGER.debug("Project {}", project.getPathWithNamespace()));
7990
}
80-
91+
8192
return page.getResults();
8293
}
8394

8495
public Optional<Project> getProjectById(Integer projectId) {
8596
return getProjectByIdOrPath(String.valueOf(projectId));
8697
}
87-
98+
8899
public Optional<Project> getProjectByIdOrPath(String idOrPath) {
89100

90101
try {
91102
return Optional.ofNullable(gitLabService.getProjectById(idOrPath));
92-
} catch(WebApplicationException wae) {
103+
} catch (WebApplicationException wae) {
93104

94-
if(wae.getResponse().getStatus() == 404) {
105+
if (wae.getResponse().getStatus() == 404) {
95106
return Optional.empty();
96107
}
97108

@@ -100,13 +111,13 @@ public Optional<Project> getProjectByIdOrPath(String idOrPath) {
100111
}
101112

102113
}
103-
114+
104115
// create a project
105116
public Optional<Project> createProject(Project project) {
106117

107118
Optional<Project> optional = Optional.empty();
108119

109-
if(doNotDelete) {
120+
if (doNotDelete) {
110121
project.preserve();
111122
}
112123

@@ -143,30 +154,39 @@ public void deleteProject(Integer projectId) {
143154
gitLabService.deleteProjectById(projectId);
144155
}
145156

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
147159
public void enableDeploymentKeyOnProject(Integer projectId, Integer deployKey) {
148-
160+
149161
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());
151168
}
152-
169+
153170
public List<Commit> getCommitLog(String projectId) {
154171
PagedResults<Commit> page = new PagedResults<>(commitPageSize);
155-
156-
while(page.hasMore()) {
172+
173+
while (page.hasMore()) {
157174
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+
});
159177
}
160-
178+
161179
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());
164183

165184
}
166185

167186
public Optional<Project> transferProject(Integer projectId, Integer newGroupId) {
168187

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());
170190

171191
}
172192

src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ engagements.do.not.delete=${ENGAGEMENTS_PRESERVE:false}
4242
git.commit=${GIT_API_GIT_COMMIT:not.set}
4343
git.tag=${GIT_API_GIT_TAG:not.set}
4444

45+
environment.id=${ENV_ID:local}

0 commit comments

Comments
 (0)