Skip to content

Commit

Permalink
refactor(swagger): refactor with best style[backend][frontend]
Browse files Browse the repository at this point in the history
  • Loading branch information
guzhongren committed Jan 22, 2024
1 parent a4649a9 commit 7bf8602
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import heartbeat.controller.board.dto.response.JiraVerifyResponse;
import heartbeat.exception.BadRequestException;
import heartbeat.service.board.jira.JiraService;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
Expand All @@ -33,14 +34,18 @@ public BoardConfigDTO getBoard(@PathVariable @NotBlank BoardType boardType,
}

@PostMapping("/{boardType}/verify")
public JiraVerifyResponse verify(@PathVariable @NotBlank BoardType boardType,
public JiraVerifyResponse verify(
@Schema(type = "string", allowableValues = { "jira", "classic-jira" },
accessMode = Schema.AccessMode.READ_ONLY) @PathVariable @NotBlank BoardType boardType,
@Valid @RequestBody BoardVerifyRequestParam boardRequestParam) {
String projectKey = jiraService.verify(boardType, boardRequestParam);
return JiraVerifyResponse.builder().projectKey(projectKey).build();
}

@PostMapping("/{boardType}/info")
public BoardConfigDTO getInfo(@PathVariable @NotBlank BoardType boardType,
public BoardConfigDTO getInfo(
@Schema(type = "string", allowableValues = { "jira", "classic-jira" },
accessMode = Schema.AccessMode.READ_ONLY) @PathVariable @NotBlank BoardType boardType,
@Valid @RequestBody BoardRequestParam boardRequestParam) {
checkTime(boardRequestParam.getStartTime(), boardRequestParam.getEndTime());
return jiraService.getInfo(boardType, boardRequestParam);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import heartbeat.controller.pipeline.dto.response.BuildKiteResponseDTO;
import heartbeat.controller.pipeline.dto.response.PipelineStepsDTO;
import heartbeat.service.pipeline.buildkite.BuildKiteService;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
Expand Down Expand Up @@ -33,14 +34,18 @@ public class PipelineController {
private final BuildKiteService buildKiteService;

@PostMapping("/{pipelineType}/verify")
public ResponseEntity<Void> verifyBuildKiteToken(@PathVariable PipelineType pipelineType,
public ResponseEntity<Void> verifyBuildKiteToken(
@Schema(type = "string", allowableValues = { "buildkite" },
accessMode = Schema.AccessMode.READ_ONLY) @PathVariable PipelineType pipelineType,
@Valid @RequestBody TokenParam tokenParam) {
buildKiteService.verifyToken(tokenParam.getToken());
return ResponseEntity.noContent().build();
}

@PostMapping("/{pipelineType}/info")
public ResponseEntity<BuildKiteResponseDTO> fetchBuildKiteInfo(@PathVariable PipelineType pipelineType,
public ResponseEntity<BuildKiteResponseDTO> fetchBuildKiteInfo(
@Schema(type = "string", allowableValues = { "buildkite" },
accessMode = Schema.AccessMode.READ_ONLY) @PathVariable PipelineType pipelineType,
@Valid @RequestBody TokenParam tokenParam) {
BuildKiteResponseDTO buildKiteResponse = buildKiteService.getBuildKiteInfo(tokenParam);
if (buildKiteResponse.getPipelineList().isEmpty()) {
Expand All @@ -54,8 +59,10 @@ public ResponseEntity<BuildKiteResponseDTO> fetchBuildKiteInfo(@PathVariable Pip
@GetMapping("/{pipelineType}/{organizationId}/pipelines/{buildId}/steps")
public ResponseEntity<PipelineStepsDTO> getPipelineSteps(
@RequestHeader("Authorization") @NotBlank(message = "Token must not be blank") String token,
@PathVariable String pipelineType, @PathVariable String organizationId,
@PathVariable("buildId") String pipelineId, @Valid @ModelAttribute PipelineStepsParam params) {
@Schema(type = "string", allowableValues = { "buildkite" },
accessMode = Schema.AccessMode.READ_ONLY) @PathVariable String pipelineType,
@PathVariable String organizationId, @PathVariable("buildId") String pipelineId,
@Valid @ModelAttribute PipelineStepsParam params) {

log.info("Start to get pipeline steps, organization id: {}, pipeline id: {}", organizationId, pipelineId);
PipelineStepsDTO pipelineSteps = buildKiteService.fetchPipelineSteps(token, organizationId, pipelineId, params);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package heartbeat.controller.source;

import heartbeat.controller.source.dto.GitHubResponse;
import heartbeat.controller.source.dto.SourceControlResponse;
import heartbeat.controller.source.dto.SourceControlDTO;
import heartbeat.controller.source.dto.VerifyBranchRequest;
import heartbeat.service.source.github.GitHubService;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -36,16 +37,18 @@ public class SourceController {
@CrossOrigin
@Deprecated(since = "frontend completed")
@ResponseStatus(HttpStatus.OK)
public GitHubResponse getRepos(@RequestBody @Valid SourceControlDTO sourceControlDTO) {
public SourceControlResponse getRepos(@RequestBody @Valid SourceControlDTO sourceControlDTO) {
log.info("Start to get repos by token");
GitHubResponse gitHubRepos = gitHubService.verifyToken(sourceControlDTO.getToken());
SourceControlResponse gitHubRepos = gitHubService.verifyToken(sourceControlDTO.getToken());
log.info("Successfully get the repos by token, repos size: {}", gitHubRepos.getGithubRepos().size());
return gitHubRepos;

}

@PostMapping("/{sourceType}/verify")
public ResponseEntity<Void> verifyToken(@PathVariable SourceType sourceType,
public ResponseEntity<Void> verifyToken(
@Schema(type = "string", allowableValues = { "github" },
accessMode = Schema.AccessMode.READ_ONLY) @PathVariable SourceType sourceType,
@RequestBody @Valid SourceControlDTO sourceControlDTO) {
log.info("Start to verify source type: {} token.", sourceType);
switch (sourceType) {
Expand All @@ -60,7 +63,9 @@ public ResponseEntity<Void> verifyToken(@PathVariable SourceType sourceType,
}

@PostMapping("/{sourceType}/repos/branches/verify")
public ResponseEntity<Void> verifyBranch(@PathVariable SourceType sourceType,
public ResponseEntity<Void> verifyBranch(
@Schema(type = "string", allowableValues = { "github" },
accessMode = Schema.AccessMode.READ_ONLY) @PathVariable SourceType sourceType,
@RequestBody @Valid VerifyBranchRequest request) {
log.info("Start to verify source type: {} branch: {}.", sourceType, request.getBranch());
switch (sourceType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
@Log4j2
public enum SourceType {

GITHUB;
GITHUB("GitHub");

public final String sourceType;

SourceType(String sourceType) {
this.sourceType = sourceType;
}

public static SourceType fromValue(String type) {
return switch (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Data
@Builder
@Deprecated
public class GitHubResponse {
public class SourceControlResponse {

private LinkedHashSet<String> githubRepos;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import heartbeat.client.dto.codebase.github.PullRequestInfo;
import heartbeat.client.dto.pipeline.buildkite.DeployInfo;
import heartbeat.client.dto.pipeline.buildkite.DeployTimes;
import heartbeat.controller.source.dto.GitHubResponse;
import heartbeat.controller.source.dto.SourceControlResponse;
import heartbeat.exception.BaseException;
import heartbeat.exception.GithubRepoEmptyException;
import heartbeat.exception.InternalServerErrorException;
Expand Down Expand Up @@ -55,7 +55,7 @@ public void shutdownExecutor() {
}

@Deprecated
public GitHubResponse verifyToken(String githubToken) {
public SourceControlResponse verifyToken(String githubToken) {
try {
String token = TOKEN_TITLE + githubToken;
log.info("Start to query repository url by token");
Expand All @@ -78,7 +78,7 @@ public GitHubResponse verifyToken(String githubToken) {
Set<String> allGitHubRepos = githubReposByOrganizations.join();
log.info("Successfully get all repositories by token, repos size: {}", allGitHubRepos.size());
githubRepos.addAll(allGitHubRepos);
return GitHubResponse.builder().githubRepos(githubRepos).build();
return SourceControlResponse.builder().githubRepos(githubRepos).build();
}, customTaskExecutor)
.join();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.JsonPath;
import heartbeat.controller.source.dto.GitHubResponse;
import heartbeat.controller.source.dto.SourceControlDTO;
import heartbeat.controller.source.dto.SourceControlResponse;
import heartbeat.controller.source.dto.VerifyBranchRequest;
import heartbeat.service.source.github.GitHubService;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -37,7 +37,7 @@
@WebMvcTest(SourceController.class)
@ExtendWith(SpringExtension.class)
@AutoConfigureJsonTesters
class GithubControllerTest {
class SourceControllerTest {

public static final String BAD_SOURCE_TYPE = "GitHub";

Expand All @@ -59,7 +59,7 @@ void shouldReturnOkStatusAndCorrectResponseWithRepos() throws Exception {
LinkedHashSet<String> repos = new LinkedHashSet<>(
List.of("https://github.com/xxxx1/repo1", "https://github.com/xxxx2/repo2"));

GitHubResponse githubReposResponse = GitHubResponse.builder().githubRepos(repos).build();
SourceControlResponse githubReposResponse = SourceControlResponse.builder().githubRepos(repos).build();

when(gitHubVerifyService.verifyToken(any())).thenReturn(githubReposResponse);
SourceControlDTO sourceControlDTO = SourceControlDTO.builder().token(GITHUB_TOKEN).build();
Expand Down

0 comments on commit 7bf8602

Please sign in to comment.