Skip to content

Commit

Permalink
Added GitHub Actions generator
Browse files Browse the repository at this point in the history
adds the feature of generation a GitHub Actions YML file to the generated projects
see https://github.com/features/actions

Fix jhipster#422
  • Loading branch information
Hawkurane committed Feb 14, 2022
1 parent 3207ac3 commit 8244319
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tech.jhipster.lite.generator.githubactions.application;

import org.springframework.stereotype.Service;
import tech.jhipster.lite.generator.githubactions.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 init(Project project) {
githubActionsService.init(project);
}

public void addYml(Project project) {
githubActionsService.addYml(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tech.jhipster.lite.generator.githubactions.domain;

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

public class GithubActionsDomainService implements GithubActionsService {

public static final String SOURCE = "githubactions";
public static final String GITHUB_ACTIONS_FOLDER = ".github/workflows/";
public static final String GITHUB_ACTIONS_YML = "github-actions.yml.mustache";

private final ProjectRepository projectRepository;

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

@Override
public void init(Project project) {
addYml(project);
}

@Override
public void addYml(Project project) {
projectRepository.template(project, SOURCE, GITHUB_ACTIONS_YML, GITHUB_ACTIONS_FOLDER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package tech.jhipster.lite.generator.githubactions.domain;

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

public interface GithubActionsService {
void init(Project project);

void addYml(Project project);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tech.jhipster.lite.generator.githubactions.infrastructure.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.objenesis.SpringObjenesis;
import tech.jhipster.lite.generator.githubactions.domain.GithubActionsDomainService;
import tech.jhipster.lite.generator.githubactions.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
@@ -0,0 +1,34 @@
package tech.jhipster.lite.generator.githubactions.infrastructure.primary.rest;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
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.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 {

private final GithubActionsApplicationService githubActionsApplicationService;

GithubActionsResource(GithubActionsApplicationService githubActionsApplicationService) {
this.githubActionsApplicationService = githubActionsApplicationService;
}

@Operation(summary = "Init Github Actions YML file")
@ApiResponse(responseCode = "500", description = "An error occurred while initializing the github-actions.yml.mustache file")
@PostMapping("/init")
@GeneratorStep(id = "init")
public void init(@RequestBody ProjectDTO projectDTO) {
Project project = ProjectDTO.toProject(projectDTO);
githubActionsApplicationService.init(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@tech.jhipster.lite.BusinessContext
package tech.jhipster.lite.generator.githubactions;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: GitHub Actions
on: [push]
jobs:
Explore-GitHub-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v2
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tech.jhipster.lite.generator.githubactions.application;

import static tech.jhipster.lite.TestUtils.tmpProject;
import static tech.jhipster.lite.generator.githubactions.application.GithubActionsAssertFiles.assertFilesYml;

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 shouldInit() {
Project project = tmpProject();

githubActionsApplicationService.init(project);

assertFilesYml(project);
}

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

githubActionsApplicationService.addYml(project);

assertFilesYml(project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tech.jhipster.lite.generator.githubactions.application;

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

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

public class GithubActionsAssertFiles {

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

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

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static tech.jhipster.lite.TestUtils.tmpProject;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import tech.jhipster.lite.UnitTest;
import tech.jhipster.lite.generator.project.domain.Project;
import tech.jhipster.lite.generator.project.domain.ProjectRepository;

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

@Mock
private ProjectRepository projectRepository;

@InjectMocks
private GithubActionsDomainService githubActionsDomainService;

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

assertThatCode(() -> githubActionsDomainService.init(project)).doesNotThrowAnyException();
}

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

assertThatCode(() -> githubActionsDomainService.addYml(project)).doesNotThrowAnyException();

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

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.githubactions.domain.GithubActionsDomainService;
import tech.jhipster.lite.generator.init.domain.InitDomainService;

@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
@@ -0,0 +1,42 @@
package tech.jhipster.lite.generator.githubactions.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 org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
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.project.domain.Project;
import tech.jhipster.lite.generator.project.infrastructure.primary.dto.ProjectDTO;

@IntegrationTest
@AutoConfigureMockMvc
class GithubActionsResourceIT {

@Autowired
MockMvc mockMvc;

@Autowired
GithubActionsApplicationService githubActionsApplicationService;

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

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

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

0 comments on commit 8244319

Please sign in to comment.