Skip to content

Commit

Permalink
Merge pull request #987 from pascalgrimaud/refactoring-github-actions
Browse files Browse the repository at this point in the history
Refactoring GitHub Actions
  • Loading branch information
pascalgrimaud authored Mar 10, 2022
2 parents d79ee6b + 41c771e commit 6bbfe1e
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 125 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tech.jhipster.lite.generator.ci.github.actions.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.ci.github.actions.domain.GitHubActionsService;
import tech.jhipster.lite.generator.project.domain.Project;

@Service
public class GitHubActionsApplicationService {

private final GitHubActionsService gitHubActionsService;

public GitHubActionsApplicationService(GitHubActionsService gitHubActionsService) {
this.gitHubActionsService = gitHubActionsService;
}

public void addGitHubActionsForMaven(Project project) {
gitHubActionsService.addGitHubActionsForMaven(project);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package tech.jhipster.lite.generator.githubactions.domain;
package tech.jhipster.lite.generator.ci.github.actions.domain;

import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

public class GithubActionsDomainService implements GithubActionsService {
public class GitHubActionsDomainService implements GitHubActionsService {

public static final String GITHUB_ACTIONS_CI_SOURCE_FOLDER = "githubactions/.github/workflows/";
public static final String GITHUB_ACTIONS_CI_SOURCE_FOLDER = "ci/github/actions/.github/workflows/";
public static final String GITHUB_ACTIONS_CI_YML = "github-actions.yml.mustache";
public static final String GITHUB_ACTIONS_CI_DESTINATION_FOLDER = ".github/workflows/";

public static final String GITHUB_ACTIONS_SETUP_SOURCE_FOLDER = "githubactions/.github/actions/setup/";
public static final String GITHUB_ACTIONS_SETUP_SOURCE_FOLDER = "ci/github/actions/.github/actions/setup/";
public static final String GITHUB_ACTIONS_SETUP_YML = "action.yml.mustache";
public static final String GITHUB_ACTIONS_SETUP_DESTINATION_FOLDER = ".github/actions/setup/";

private final ProjectRepository projectRepository;

public GithubActionsDomainService(ProjectRepository projectRepository) {
public GitHubActionsDomainService(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tech.jhipster.lite.generator.githubactions.domain;
package tech.jhipster.lite.generator.ci.github.actions.domain;

import tech.jhipster.lite.generator.project.domain.Project;

public interface GithubActionsService {
public interface GitHubActionsService {
void addGitHubActionsForMaven(Project project);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package tech.jhipster.lite.generator.ci.github.actions.infrastructure.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tech.jhipster.lite.generator.ci.github.actions.domain.GitHubActionsDomainService;
import tech.jhipster.lite.generator.ci.github.actions.domain.GitHubActionsService;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

@Configuration
public class GitHubActionsBeanConfiguration {

private final ProjectRepository projectRepository;

public GitHubActionsBeanConfiguration(ProjectRepository projectRepository) {
this.projectRepository = projectRepository;
}

@Bean
public GitHubActionsService gitHubActionsService() {
return new GitHubActionsDomainService(projectRepository);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tech.jhipster.lite.generator.githubactions.infrastructure.primary.rest;
package tech.jhipster.lite.generator.ci.github.actions.infrastructure.primary.rest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -7,28 +7,28 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tech.jhipster.lite.generator.githubactions.application.GithubActionsApplicationService;
import tech.jhipster.lite.generator.ci.github.actions.application.GitHubActionsApplicationService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;
import tech.jhipster.lite.technical.infrastructure.primary.annotation.GeneratorStep;

@RestController
@RequestMapping("/api/github-actions/")
@Tag(name = "GitHub Actions")
class GithubActionsResource {
@Tag(name = "Continuous Integration")
class GitHubActionsResource {

private final GithubActionsApplicationService githubActionsApplicationService;
private final GitHubActionsApplicationService gitHubActionsApplicationService;

GithubActionsResource(GithubActionsApplicationService githubActionsApplicationService) {
this.githubActionsApplicationService = githubActionsApplicationService;
GitHubActionsResource(GitHubActionsApplicationService gitHubActionsApplicationService) {
this.gitHubActionsApplicationService = gitHubActionsApplicationService;
}

@Operation(summary = "Init Github Actions YML files")
@Operation(summary = "Add GitHub Actions")
@ApiResponse(responseCode = "500", description = "An error occurred while adding GitHub Actions")
@PostMapping("/maven")
@GeneratorStep(id = "github-actions")
public void addGitHubActionsForMaven(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
githubActionsApplicationService.init(project);
gitHubActionsApplicationService.addGitHubActionsForMaven(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.BusinessContext
package tech.jhipster.lite.generator.ci.github.actions;

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tech.jhipster.lite.generator.ci.github.actions.application;

import static tech.jhipster.lite.TestUtils.tmpProject;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import tech.jhipster.lite.IntegrationTest;
import tech.jhipster.lite.generator.project.domain.Project;

@IntegrationTest
class GitHubActionsApplicationServiceIT {

@Autowired
GitHubActionsApplicationService gitHubActionsApplicationService;

@Test
void shouldAddGitHubActionsForMaven() {
Project project = tmpProject();

gitHubActionsApplicationService.addGitHubActionsForMaven(project);

GitHubActionsAssertFiles.assertFilesYml(project);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package tech.jhipster.lite.generator.githubactions.application;
package tech.jhipster.lite.generator.ci.github.actions.application;

import static tech.jhipster.lite.TestUtils.assertFileExist;

import tech.jhipster.lite.generator.project.domain.Project;

public class GithubActionsAssertFiles {
public class GitHubActionsAssertFiles {

public static void assertFilesYml(Project project) {
assertFileExist(project, ".github/workflows/github-actions.yml");
assertFileExist(project, ".github/actions/setup/action.yml");
}

public static void assertFilesGithubActions(Project project) {
public static void assertFilesGitHubActions(Project project) {
assertFilesYml(project);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tech.jhipster.lite.generator.githubactions.domain;
package tech.jhipster.lite.generator.ci.github.actions.domain;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -17,23 +17,33 @@

@UnitTest
@ExtendWith(MockitoExtension.class)
class GithubActionsDomainServiceTest {
class GitHubActionsDomainServiceTest {

@Mock
private ProjectRepository projectRepository;

@InjectMocks
private GithubActionsDomainService githubActionsDomainService;
private GitHubActionsDomainService gitHubActionsDomainService;

@Test
void shouldInit() {
void shouldAddGitHubActionsForMaven() {
Project project = tmpProject();

assertThatCode(() -> githubActionsDomainService.addGitHubActionsForMaven(project)).doesNotThrowAnyException();
assertThatCode(() -> gitHubActionsDomainService.addGitHubActionsForMaven(project)).doesNotThrowAnyException();

verify(projectRepository)
.template(any(Project.class), eq("githubactions/.github/actions/setup/"), eq("action.yml.mustache"), eq(".github/actions/setup/"));
.template(
any(Project.class),
eq("ci/github/actions/.github/actions/setup/"),
eq("action.yml.mustache"),
eq(".github/actions/setup/")
);
verify(projectRepository)
.template(any(Project.class), eq("githubactions/.github/workflows/"), eq("github-actions.yml.mustache"), eq(".github/workflows/"));
.template(
any(Project.class),
eq("ci/github/actions/.github/workflows/"),
eq("github-actions.yml.mustache"),
eq(".github/workflows/")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tech.jhipster.lite.generator.ci.github.actions.infrastructure.config;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import tech.jhipster.lite.IntegrationTest;
import tech.jhipster.lite.generator.ci.github.actions.domain.GitHubActionsDomainService;

@IntegrationTest
class GitHubActionsBeanConfigurationIT {

@Autowired
ApplicationContext applicationContext;

@Test
void shouldGetBean() {
assertThat(applicationContext.getBean("gitHubActionsService")).isNotNull().isInstanceOf(GitHubActionsDomainService.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package tech.jhipster.lite.generator.githubactions.infrastructure.primary.rest;
package tech.jhipster.lite.generator.ci.github.actions.infrastructure.primary.rest;

import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static tech.jhipster.lite.TestUtils.convertObjectToJsonBytes;
import static tech.jhipster.lite.TestUtils.readFileToObject;
import static tech.jhipster.lite.generator.githubactions.application.GithubActionsAssertFiles.assertFilesGithubActions;
import static tech.jhipster.lite.generator.ci.github.actions.application.GitHubActionsAssertFiles.assertFilesGitHubActions;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,29 +13,29 @@
import org.springframework.test.web.servlet.MockMvc;
import tech.jhipster.lite.IntegrationTest;
import tech.jhipster.lite.common.domain.FileUtils;
import tech.jhipster.lite.generator.githubactions.application.GithubActionsApplicationService;
import tech.jhipster.lite.generator.ci.github.actions.application.GitHubActionsApplicationService;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;

@IntegrationTest
@AutoConfigureMockMvc
class GithubActionsResourceIT {
class GitHubActionsResourceIT {

@Autowired
MockMvc mockMvc;

@Autowired
GithubActionsApplicationService githubActionsApplicationService;
GitHubActionsApplicationService gitHubActionsApplicationService;

@Test
void shouldInit() throws Exception {
void shouldAddGitHubActionsForMaven() throws Exception {
ProjectDTO projectDTO = readFileToObject("json/chips.json", ProjectDTO.class).folder(FileUtils.tmpDirForTest());

mockMvc
.perform(post("/api/github-actions/maven").contentType(MediaType.APPLICATION_JSON).content(convertObjectToJsonBytes(projectDTO)))
.andExpect(status().isOk());

Project project = ProjectDTO.toProject(projectDTO);
assertFilesGithubActions(project);
assertFilesGitHubActions(project);
}
}

This file was deleted.

This file was deleted.

0 comments on commit 6bbfe1e

Please sign in to comment.