Skip to content

Commit d62d367

Browse files
author
Derek Wasinger
committed
engagment population working...i think
1 parent f123f5f commit d62d367

File tree

11 files changed

+138
-67
lines changed

11 files changed

+138
-67
lines changed

src/main/java/com/redhat/labs/omp/models/gitlab/Action.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,16 @@ public class Action {
3838

3939
public void encodeActionAttributes() throws UnsupportedEncodingException {
4040

41-
// encode file path
42-
if (null != filePath) {
43-
String encodedFilePath = EncodingUtils.urlEncode(this.filePath);
44-
this.filePath = encodedFilePath;
45-
}
46-
47-
// encode previous path
48-
if (null != previousPath) {
49-
String encodedFilePath = EncodingUtils.urlEncode(this.previousPath);
50-
this.previousPath = encodedFilePath;
51-
}
52-
5341
// encode contents
5442
if (null != content) {
55-
byte[] encodedContents = EncodingUtils.base64Encode(this.filePath.getBytes());
43+
byte[] encodedContents = EncodingUtils.base64Encode(this.content.getBytes());
5644
this.content = new String(encodedContents, StandardCharsets.UTF_8);
5745
}
5846

5947
}
6048

6149
public void decodeActionAttributes() throws UnsupportedEncodingException {
6250

63-
// decode file path
64-
if (null != filePath) {
65-
String decodedFilePath = EncodingUtils.urlDecode(this.filePath);
66-
this.filePath = decodedFilePath;
67-
}
68-
69-
// decode previous path
70-
if (null != previousPath) {
71-
String decodedFilePath = EncodingUtils.urlDecode(this.previousPath);
72-
this.previousPath = decodedFilePath;
73-
}
74-
7551
// decode contents
7652
if (null != content) {
7753
byte[] decodedContents = EncodingUtils.base64Decode(this.content);

src/main/java/com/redhat/labs/omp/models/gitlab/Group.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public class Group {
2626
private String description;
2727
@JsonbProperty("membership_lock")
2828
private Boolean membershipLock;
29+
@Builder.Default
2930
@JsonbProperty("visibility")
30-
private String visibility;
31+
private String visibility = "private";
3132
@JsonbProperty("share_with_group_lock")
3233
private Boolean shareWithGroupLock;
3334
@JsonbProperty("require_two_factor_authentication")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.redhat.labs.omp.models.gitlab;
2+
3+
import javax.json.bind.annotation.JsonbProperty;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
@Data
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class Namespace {
13+
14+
@JsonbProperty("id")
15+
private Integer id;
16+
@JsonbProperty("name")
17+
private String name;
18+
@JsonbProperty("path")
19+
private String path;
20+
@JsonbProperty("kind")
21+
private String kind;
22+
@JsonbProperty("full_path")
23+
private String fullPath;
24+
@JsonbProperty("parent_id")
25+
private Integer parentId;
26+
27+
}

src/main/java/com/redhat/labs/omp/models/gitlab/Project.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,12 @@ public class Project {
131131
// TODO: Can expose avatar as well, just need to figure out what type to use.
132132
// avatar mixed no Image file for avatar of the group. Introduced in GitLab 12.9
133133

134+
public static Project from(ProjectSearchResults result) {
135+
136+
Project p = Project.builder().id(result.getId()).name(result.getName()).path(result.getPath())
137+
.namespaceId(result.getNamespace().getId()).build();
138+
return p;
139+
140+
}
141+
134142
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.redhat.labs.omp.models.gitlab;
2+
3+
import javax.json.bind.annotation.JsonbProperty;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
@Data
10+
@NoArgsConstructor
11+
@AllArgsConstructor
12+
public class ProjectSearchResults {
13+
14+
@JsonbProperty("id")
15+
private Integer id;
16+
@JsonbProperty("description")
17+
private String description;
18+
@JsonbProperty("name")
19+
private String name;
20+
@JsonbProperty("path")
21+
private String path;
22+
@JsonbProperty("namespace")
23+
private Namespace namespace;
24+
25+
}

src/main/java/com/redhat/labs/omp/resources/ProjectResource.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public Response post(Project project) {
4343
}
4444

4545
@GET
46-
@Path("/names/{name}")
47-
public Project getByName(@PathParam("name") String name) {
46+
@Path("{id}/names/{name}")
47+
public Project getByName(@PathParam("id") Integer projectId, @PathParam("name") String name) {
4848

4949
Optional<Project> optional;
5050
try {
51-
optional = projectService.getProjectByName(name);
51+
optional = projectService.getProjectByName(projectId, name);
5252
} catch (UnexpectedGitLabResponseException e) {
5353
optional = Optional.empty();
5454
}

src/main/java/com/redhat/labs/omp/rest/client/GitLabService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.redhat.labs.omp.models.gitlab.File;
2222
import com.redhat.labs.omp.models.gitlab.Group;
2323
import com.redhat.labs.omp.models.gitlab.Project;
24+
import com.redhat.labs.omp.models.gitlab.ProjectSearchResults;
2425
import com.redhat.labs.omp.resources.filters.Logged;
2526

2627
@Path("/api/v4")
@@ -143,7 +144,7 @@ public interface GitLabService {
143144
@Logged
144145
@Path("/projects")
145146
@Produces("application/json")
146-
List<Project> getProjectByName(@QueryParam("search") @Encoded String name);
147+
List<ProjectSearchResults> getProjectByName(@QueryParam("search") @Encoded String name);
147148

148149
@GET
149150
@Logged

src/main/java/com/redhat/labs/omp/service/EngagementService.java

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ public class EngagementService {
2525
private final String COMMIT_MSG = "\uD83E\uDD84 Created by OMP Git API \uD83D\uDE80 \uD83C\uDFC1";
2626
private final String DEFAULT_BRANCH = "master";
2727

28+
@ConfigProperty(name = "residenciesParentRepositoryId")
2829
Integer engagementParentId;
2930

31+
@ConfigProperty(name = "stripPathPrefix", defaultValue = "schema/")
32+
String stripPathPrefix;
33+
3034
@ConfigProperty(name = "deployKey")
3135
Integer deployKey;
3236

@@ -42,6 +46,18 @@ public class EngagementService {
4246
@Inject
4347
FileService fileService;
4448

49+
/*
50+
*
51+
*
52+
* Start here: Not all files are being generated
53+
*
54+
* looks like they are being read and are in the templatesFiles. just never make to to the commit
55+
*
56+
*
57+
*
58+
*/
59+
60+
4561
// create an engagement
4662
public Project createEngagement(Engagement engagement) {
4763

@@ -58,7 +74,7 @@ public Project createEngagement(Engagement engagement) {
5874
CommitMultiple commit = createCommitMultiple(templateFiles, project.getId(), DEFAULT_BRANCH);
5975

6076
// send commit to gitlab
61-
if(!fileService.createFiles(project.getId(), commit)) {
77+
if (!fileService.createFiles(project.getId(), commit)) {
6278
throw new UnexpectedGitLabResponseException("failed to commit files for engagement creation.");
6379
}
6480

@@ -74,18 +90,13 @@ private Project createProjectStucture(Engagement engagement) {
7490
.parentId(engagementParentId).build());
7591

7692
// create group for project name
77-
Group projectGroup = getOrCreateGroup(engagement.getCustomerName(),
93+
Group projectGroup = getOrCreateGroup(engagement.getProjectName(),
7894
Group.builder().name(engagement.getProjectName()).path(engagement.getProjectName())
7995
.parentId(customerGroup.getId()).build());
8096

8197
// create project under project name group
82-
Optional<Project> projectOptional = projectService.createProject(
98+
Project project = getOrCreateProject(projectGroup.getId(), ENGAGEMENT_PROJECT_NAME,
8399
Project.builder().name(ENGAGEMENT_PROJECT_NAME).namespaceId(projectGroup.getId()).build());
84-
if (projectOptional.isEmpty()) {
85-
throw new UnexpectedGitLabResponseException("failed to create project.");
86-
}
87-
88-
Project project = projectOptional.get();
89100

90101
// enable deployment key on project
91102
projectService.enableDeploymentKeyOnProject(project.getId(), deployKey);
@@ -113,6 +124,25 @@ private Group getOrCreateGroup(String groupName, Group groupToCreate) {
113124

114125
}
115126

127+
private Project getOrCreateProject(Integer namespaceId, String projectName, Project project) {
128+
129+
Optional<Project> optional = projectService.getProjectByName(namespaceId, projectName);
130+
131+
if (optional.isEmpty()) {
132+
133+
// try to create project
134+
optional = projectService.createProject(project);
135+
136+
if (optional.isEmpty()) {
137+
throw new UnexpectedGitLabResponseException("failed to create project");
138+
}
139+
140+
}
141+
142+
return optional.get();
143+
144+
}
145+
116146
private CommitMultiple createCommitMultiple(List<File> filesToCommit, Integer projectId, String branch) {
117147

118148
List<Action> actions = new ArrayList<>();
@@ -125,7 +155,15 @@ private CommitMultiple createCommitMultiple(List<File> filesToCommit, Integer pr
125155
}
126156

127157
private Action createAction(File file, FileAction action) {
128-
return Action.builder().filePath(file.getFilePath()).content(file.getContent()).encoding("base64").build();
158+
return Action.builder().action(action).filePath(stripPrefix(file.getFilePath())).content(file.getContent())
159+
.encoding("base64").build();
160+
}
161+
162+
private String stripPrefix(String in) {
163+
if (in != null && in.startsWith(stripPathPrefix)) {
164+
return in.split(stripPathPrefix)[1];
165+
}
166+
return in;
129167
}
130168

131169
// update an engagement

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.redhat.labs.exception.UnexpectedGitLabResponseException;
1212
import com.redhat.labs.omp.models.gitlab.Project;
13+
import com.redhat.labs.omp.models.gitlab.ProjectSearchResults;
1314
import com.redhat.labs.omp.rest.client.GitLabService;
1415

1516
@ApplicationScoped
@@ -20,31 +21,31 @@ public class ProjectService {
2021
GitLabService gitLabService;
2122

2223
// get a project
23-
// TODO: This method needs to be name and group, or something unique. Currently, can
24-
// return the wrong one if multiple projects with the same name exists (i.e. iac)
25-
public Optional<Project> getProjectByName(String name) throws UnexpectedGitLabResponseException {
24+
public Optional<Project> getProjectByName(Integer namespaceId, String name)
25+
throws UnexpectedGitLabResponseException {
2626

2727
Optional<Project> optional = Optional.empty();
2828

29-
List<Project> projectList = gitLabService.getProjectByName(name);
29+
List<ProjectSearchResults> resultList = gitLabService.getProjectByName(name);
3030

31-
if (null == projectList || projectList.isEmpty()) {
31+
if (null == resultList || resultList.isEmpty()) {
3232
return optional;
3333
}
3434

35-
if (1 == projectList.size()) {
36-
return Optional.of(projectList.get(0));
35+
if (1 == resultList.size()) {
36+
return Optional.of(Project.from(resultList.get(0)));
3737
}
3838

3939
// found more than one project with name in either 'name' or 'path' attribute
4040
// should match path
41-
for (Project project : projectList) {
42-
if (name.equalsIgnoreCase(project.getPath())) {
43-
return Optional.of(project);
41+
// TODO: This is checking parent id here. can this be part of the search?
42+
for (ProjectSearchResults result : resultList) {
43+
if (namespaceId.equals(result.getNamespace().getId()) && name.equalsIgnoreCase(result.getPath())) {
44+
return Optional.of(Project.from(result));
4445
}
4546
}
4647

47-
throw new UnexpectedGitLabResponseException("No resource found with name equal to path attribute.");
48+
return optional;
4849

4950
}
5051

@@ -54,7 +55,7 @@ public Optional<Project> getProjectById(Integer projectId) {
5455

5556
Project project = gitLabService.getProjectById(projectId);
5657

57-
if(null != project) {
58+
if (null != project) {
5859
optional = Optional.of(project);
5960
}
6061

src/main/java/com/redhat/labs/omp/service/TemplateService.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class TemplateService {
2929
@ConfigProperty(name = "metaFileFolder", defaultValue = "schema")
3030
String metaFileFolder;
3131

32-
@ConfigProperty(name = "metaFileName", defaultValue = "schema")
32+
@ConfigProperty(name = "metaFileName", defaultValue = "meta.dat")
3333
String metaFileName;
3434

3535
@Inject
@@ -70,11 +70,7 @@ public File getTemplateInventoryFile() {
7070
throw new FileNotFoundException("could not get template inventory file from gitlab.");
7171
}
7272

73-
// decode file attributes
74-
File inventoryFile = optional.get();
75-
inventoryFile.decodeFileAttributes();
76-
77-
return inventoryFile;
73+
return optional.get();
7874

7975
}
8076

@@ -97,8 +93,6 @@ public List<File> getTemplateInventoryFiles(File file) {
9793
// decode file attributes
9894
File templateFile = optional.get();
9995

100-
templateFile.decodeFileAttributes();
101-
10296
// add template file to list
10397
templateFiles.add(templateFile);
10498

0 commit comments

Comments
 (0)