Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spark implementation of Materials API (#6324)
- Loading branch information
akshaydewan
committed
Jun 13, 2019
1 parent
8618d15
commit 4d47d86
Showing
47 changed files
with
2,489 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright 2019 ThoughtWorks, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
apply plugin: 'jacoco' | ||
apply plugin: 'groovy' | ||
|
||
dependencies { | ||
compile project(':api:api-base') | ||
|
||
testCompile project(path: ':api:api-base', configuration: 'testOutput') | ||
|
||
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: project.versions.junit5 | ||
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: project.versions.junit5 | ||
} |
74 changes: 74 additions & 0 deletions
74
...ials-v1/src/main/java/com/thoughtworks/go/apiv1/materials/MaterialConfigControllerV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2019 ThoughtWorks, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.thoughtworks.go.apiv1.materials; | ||
|
||
import com.thoughtworks.go.api.ApiController; | ||
import com.thoughtworks.go.api.ApiVersion; | ||
import com.thoughtworks.go.api.spring.ApiAuthenticationHelper; | ||
import com.thoughtworks.go.apiv1.materials.representers.MaterialConfigsRepresenter; | ||
import com.thoughtworks.go.config.exceptions.HttpException; | ||
import com.thoughtworks.go.config.materials.MaterialConfigs; | ||
import com.thoughtworks.go.server.service.MaterialConfigService; | ||
import com.thoughtworks.go.server.service.MaterialService; | ||
import com.thoughtworks.go.spark.Routes; | ||
import com.thoughtworks.go.spark.spring.SparkSpringController; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
import java.io.IOException; | ||
|
||
import static spark.Spark.*; | ||
|
||
@Component | ||
public class MaterialConfigControllerV1 extends ApiController implements SparkSpringController { | ||
|
||
private final ApiAuthenticationHelper apiAuthenticationHelper; | ||
private final MaterialConfigService materialConfigService; | ||
|
||
@Autowired | ||
public MaterialConfigControllerV1(ApiAuthenticationHelper apiAuthenticationHelper, MaterialConfigService materialConfigService) { | ||
super(ApiVersion.v1); | ||
this.apiAuthenticationHelper = apiAuthenticationHelper; | ||
this.materialConfigService = materialConfigService; | ||
} | ||
|
||
@Override | ||
public String controllerBasePath() { | ||
return Routes.MaterialConfig.BASE; | ||
} | ||
|
||
@Override | ||
public void setupRoutes() { | ||
path(controllerBasePath(), () -> { | ||
before("", mimeType, this::setContentType); | ||
before("/*", mimeType, this::setContentType); | ||
before("", mimeType, this::verifyContentType); | ||
before("/*", mimeType, this::verifyContentType); | ||
before("", this.mimeType, this.apiAuthenticationHelper::checkUserAnd403); | ||
before("/*", this.mimeType, this.apiAuthenticationHelper::checkUserAnd403); | ||
get("", mimeType, this::index); | ||
exception(HttpException.class, this::httpException); | ||
}); | ||
} | ||
|
||
public String index(Request req, Response res) throws IOException { | ||
MaterialConfigs materialConfigs = materialConfigService.getMaterialConfigs(currentUsernameString()); | ||
return writerForTopLevelObject(req, res, writer -> MaterialConfigsRepresenter.toJSON(writer, materialConfigs)); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
.../src/main/java/com/thoughtworks/go/apiv1/materials/MaterialModificationsControllerV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2019 ThoughtWorks, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.thoughtworks.go.apiv1.materials; | ||
|
||
import com.thoughtworks.go.api.ApiController; | ||
import com.thoughtworks.go.api.ApiVersion; | ||
import com.thoughtworks.go.api.spring.ApiAuthenticationHelper; | ||
import com.thoughtworks.go.apiv1.materials.representers.MaterialConfigsRepresenter; | ||
import com.thoughtworks.go.apiv1.materials.representers.ModificationsRepresenter; | ||
import com.thoughtworks.go.config.exceptions.HttpException; | ||
import com.thoughtworks.go.config.materials.MaterialConfigs; | ||
import com.thoughtworks.go.domain.materials.MaterialConfig; | ||
import com.thoughtworks.go.domain.materials.Modifications; | ||
import com.thoughtworks.go.server.service.MaterialConfigService; | ||
import com.thoughtworks.go.server.service.MaterialService; | ||
import com.thoughtworks.go.server.service.result.HttpOperationResult; | ||
import com.thoughtworks.go.server.util.Pagination; | ||
import com.thoughtworks.go.spark.Routes; | ||
import com.thoughtworks.go.spark.spring.SparkSpringController; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
import java.io.IOException; | ||
|
||
import static spark.Spark.*; | ||
|
||
@Component | ||
public class MaterialModificationsControllerV1 extends ApiController implements SparkSpringController { | ||
|
||
private final ApiAuthenticationHelper apiAuthenticationHelper; | ||
private final MaterialConfigService materialConfigService; | ||
private final MaterialService materialService; | ||
|
||
@Autowired | ||
public MaterialModificationsControllerV1(ApiAuthenticationHelper apiAuthenticationHelper, MaterialConfigService materialConfigService, | ||
MaterialService materialService) { | ||
super(ApiVersion.v1); | ||
this.apiAuthenticationHelper = apiAuthenticationHelper; | ||
this.materialConfigService = materialConfigService; | ||
this.materialService = materialService; | ||
} | ||
|
||
@Override | ||
public String controllerBasePath() { | ||
return Routes.MaterialModifications.BASE; | ||
} | ||
|
||
@Override | ||
public void setupRoutes() { | ||
path(controllerBasePath(), () -> { | ||
before("", mimeType, this::setContentType); | ||
before("/*", mimeType, this::setContentType); | ||
before("", mimeType, this::verifyContentType); | ||
before("/*", mimeType, this::verifyContentType); | ||
before("", this.mimeType, this.apiAuthenticationHelper::checkUserAnd403); | ||
before("/*", this.mimeType, this.apiAuthenticationHelper::checkUserAnd403); | ||
get("", mimeType, this::modifications); | ||
get(Routes.MaterialModifications.OFFSET, mimeType, this::modifications); | ||
exception(HttpException.class, this::httpException); | ||
}); | ||
} | ||
|
||
public String modifications(Request req, Response res) throws IOException { | ||
String fingerprint = req.params("fingerprint"); | ||
Integer offset = req.params("offset") == null ? null : Integer.parseInt(req.params("offset")); | ||
HttpOperationResult result = new HttpOperationResult(); | ||
MaterialConfig materialConfig = materialConfigService.getMaterialConfig(currentUsernameString(), fingerprint, result); | ||
if (result.canContinue()) { | ||
Long modificationsCount = materialService.getTotalModificationsFor(materialConfig); | ||
Pagination pagination = Pagination.pageStartingAt(offset, modificationsCount.intValue(), 10); | ||
Modifications modifications = materialService.getModificationsFor(materialConfig, pagination); | ||
return writerForTopLevelObject(req, res, writer -> ModificationsRepresenter.toJSON(writer, modifications, pagination, fingerprint)); | ||
} else { | ||
return renderHTTPOperationResult(result, req, res); | ||
} | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
...ials-v1/src/main/java/com/thoughtworks/go/apiv1/materials/MaterialNotifyControllerV1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright 2019 ThoughtWorks, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.thoughtworks.go.apiv1.materials; | ||
|
||
import com.thoughtworks.go.api.ApiController; | ||
import com.thoughtworks.go.api.ApiVersion; | ||
import com.thoughtworks.go.api.representers.JsonReader; | ||
import com.thoughtworks.go.api.spring.ApiAuthenticationHelper; | ||
import com.thoughtworks.go.api.util.GsonTransformer; | ||
import com.thoughtworks.go.config.exceptions.HttpException; | ||
import com.thoughtworks.go.server.materials.MaterialUpdateService; | ||
import com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult; | ||
import com.thoughtworks.go.spark.Routes; | ||
import com.thoughtworks.go.spark.spring.SparkSpringController; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
import spark.Request; | ||
import spark.Response; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static spark.Spark.*; | ||
|
||
@Component | ||
public class MaterialNotifyControllerV1 extends ApiController implements SparkSpringController { | ||
|
||
private final ApiAuthenticationHelper apiAuthenticationHelper; | ||
private final MaterialUpdateService materialUpdateService; | ||
|
||
@Autowired | ||
public MaterialNotifyControllerV1(ApiAuthenticationHelper apiAuthenticationHelper, MaterialUpdateService materialUpdateService) { | ||
super(ApiVersion.v1); | ||
this.apiAuthenticationHelper = apiAuthenticationHelper; | ||
this.materialUpdateService = materialUpdateService; | ||
} | ||
|
||
@Override | ||
public String controllerBasePath() { | ||
return Routes.MaterialNotify.BASE; | ||
} | ||
|
||
@Override | ||
public void setupRoutes() { | ||
path(controllerBasePath(), () -> { | ||
before("", mimeType, this::setContentType); | ||
before("/*", mimeType, this::setContentType); | ||
before("", mimeType, this::verifyContentType); | ||
before("/*", mimeType, this::verifyContentType); | ||
before("", this.mimeType, this.apiAuthenticationHelper::checkAdminUserAnd403); | ||
before("/*", this.mimeType, this.apiAuthenticationHelper::checkAdminUserAnd403); | ||
post(Routes.MaterialNotify.SVN, mimeType, this::svnNotify); | ||
post(Routes.MaterialNotify.GIT, mimeType, this::gitNotify); | ||
post(Routes.MaterialNotify.HG, mimeType, this::hgNotify); | ||
post(Routes.MaterialNotify.SCM, mimeType, this::scmNotify); | ||
exception(HttpException.class, this::httpException); | ||
}); | ||
} | ||
|
||
public String svnNotify(Request req, Response res) throws IOException { | ||
return notifyWithRepoUrl(req, res, "svn"); | ||
} | ||
|
||
public String gitNotify(Request req, Response res) throws IOException { | ||
return notifyWithRepoUrl(req, res, "git"); | ||
} | ||
|
||
public String hgNotify(Request req, Response res) throws IOException { | ||
return notifyWithRepoUrl(req, res, "hg"); | ||
} | ||
|
||
public String scmNotify(Request req, Response res) throws IOException { | ||
JsonReader jsonReader = GsonTransformer.getInstance().jsonReaderFrom(req.body()); | ||
String repoUrl = jsonReader.getString("scm_name"); | ||
return notify(req, res, "scm", "scm_name", repoUrl); | ||
} | ||
|
||
private String notifyWithRepoUrl(Request req, Response res, String materialType) throws IOException { | ||
String repoUrl = getRepoUrl(req); | ||
return notify(req, res, materialType, "repository_url", repoUrl); | ||
} | ||
|
||
private String notify(Request req, Response res, String materialType, String paramName, String paramValue) throws IOException { | ||
Map<String, String> params = new HashMap<>(); | ||
params.put(MaterialUpdateService.TYPE, materialType); | ||
params.put(paramName, paramValue); | ||
|
||
HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); | ||
materialUpdateService.notifyMaterialsForUpdate(currentUsername(), params, result); | ||
|
||
return renderHTTPOperationResult(result, req, res); | ||
} | ||
|
||
private String getRepoUrl(Request req) { | ||
JsonReader jsonReader = GsonTransformer.getInstance().jsonReaderFrom(req.body()); | ||
return jsonReader.getString("repository_url"); | ||
} | ||
} |
Oops, something went wrong.