Skip to content

Commit

Permalink
adjusts and add samples workflow (#72)
Browse files Browse the repository at this point in the history
* add samples workflow

* generate fake data for tenantRelationship

* fix TenantIdentifier

* drop liquibase header

* ignore some builtIn entities in spring-boot generator
  • Loading branch information
mshima authored Apr 23, 2024
1 parent 2291e83 commit 9ce2878
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 20 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/samples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Verify Sample Projects
on:
push:
branches:
- 'main'
- '**maintenance'
paths-ignore:
- 'package*.json'
pull_request:
branches:
- '*'
permissions:
contents: read
jobs:
build-matrix:
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.build.outputs.matrix }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- id: build
run: |
npm install
npm link
jhipster-tenantview github-build-matrix
samples:
name: ${{ matrix.sample-name }}
runs-on: ubuntu-latest
needs: build-matrix
defaults:
run:
working-directory: ${{ github.workspace }}/app
timeout-minutes: 30
strategy:
fail-fast: false
matrix: ${{fromJson(needs.build-matrix.outputs.matrix)}}
steps:
- uses: jhipster/actions/setup-runner@v0
with:
node-version: ${{ matrix.node-version }}
java-version: ${{ matrix.java-version }}
npm-version: ${{ matrix.npm-version }}
maven-cache: true
gradle-cache: true
- uses: actions/checkout@v4
with:
path: generator-jhipster-tenantview
fetch-depth: 2
- name: 'Install blueprint'
run: npm install && npm link
working-directory: ${{ github.workspace }}/generator-jhipster-tenantview
- run: jhipster-tenantview generate-sample ${{ matrix.sample-name }} --skip-install
- uses: jhipster/actions/compare-sample@v0
id: compare
with:
generator-path: generator-jhipster-tenantview
cmd: jhipster-tenantview generate-sample ${{ matrix.sample-name }} --skip-install --skip-git
- run: npm run ci:backend:test
id: backend
if: steps.compare.outputs.equals != 'true'
continue-on-error: ${{matrix.continue-on-backend-tests-error || false}}
timeout-minutes: 15
- run: ./npmw install
if: steps.compare.outputs.equals != 'true'
timeout-minutes: 7
- run: npm run ci:frontend:test
if: steps.compare.outputs.equals != 'true'
timeout-minutes: 15
- run: npm run ci:e2e:package
if: steps.compare.outputs.equals != 'true'
timeout-minutes: 12
- run: npm run ci:e2e:prepare
if: steps.compare.outputs.equals != 'true'
timeout-minutes: 5
- run: npm run ci:e2e:run
id: e2e
if: steps.compare.outputs.equals != 'true'
timeout-minutes: 15
- name: 'BACKEND: Store failure logs'
uses: actions/upload-artifact@v4
if: always() && steps.backend.outcome == 'failure'
with:
name: log-${{ matrix.sample-name }}
path: |
${{ github.workspace }}/app/build/test-results/**/*.xml
${{ github.workspace }}/app/target/surefire-reports
- name: 'E2E: Store failure screenshots'
uses: actions/upload-artifact@v4
if: always() && steps.e2e.outcome == 'failure'
with:
name: screenshots-${{ matrix.sample-name }}
path: ${{ github.workspace }}/app/**/cypress/screenshots
- name: Dump docker logs
if: always()
uses: jwalton/gh-docker-logs@v2
check-samples:
permissions:
contents: none
runs-on: ubuntu-latest
needs: [samples]
if: always()
steps:
- run: |
echo '${{ toJSON(needs) }}'
if [ 'skipped' == '${{ needs.samples.result }}' ] || [ 'success' == '${{ needs.samples.result }}' ] || [ 'closed' == '${{ github.event.action }}' ]; then
exit 0
fi
exit 1
2 changes: 1 addition & 1 deletion generators/angular/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class extends BaseApplicationGenerator {
get [BaseApplicationGenerator.POST_WRITING_ENTITIES]() {
return this.asPostWritingEntitiesTaskGroup({
async postWritingEntitiesTemplateTask({ application, entities }) {
for (const entity of entities) {
for (const entity of entities.filter(entity => !entity.skipClient || entity.builtInUserManagement)) {
await patcherTask(this, { ...application, ...entity }, { options: { autoLoadPath: 'patcher-entities' } });
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default {
@if (${data.builtInUserManagement ? 'user' : data.entityInstance}.${data.builtInUserManagement ? 'id' : data.primaryKey.name} !== 0) {
<button
type="submit"
(click)="setCurrentTenantId(${data.builtInUserManagement ? 'user' : data.entityInstance}.${data.builtInUserManagement ? 'id' : data.primaryKey.name})"
(click)="setCurrentTenantId(${data.builtInUserManagement ? 'user' : data.entityInstance}.${data.builtInUserManagement ? 'id' : data.primaryKey.name}!)"
class="btn btn-primary btn-sm"
data-cy="entitySelectButton"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ export default {
},
{
type: 'rewriteFile',
target: ' = inject(',
target: data => (data.builtInUserManagement ? 'private userService = ' : 'public router = inject'),
tmpl: `
currentTenantService = inject(CurrentTenantService);
currentTenant = this.currentTenantService.trackTenantId();
setCurrentTenantId = (tenantId: number) => this.currentTenantService.setTenantId(tenantId);
currentTenant = inject(CurrentTenantService).trackTenantId();
`,
},
{
type: 'rewriteFile',
target: data => (data.builtInUserManagement ? 'ngOnInit()' : `track${data.primaryKey.nameCapitalized} =`),
tmpl: `
private currentTenantService = inject(CurrentTenantService);
setCurrentTenantId = (tenantId: number):void => this.currentTenantService.setTenantId(tenantId);
`,
},
],
Expand Down
7 changes: 0 additions & 7 deletions generators/liquibase/templates/liquibase-changelog-header.ejs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<%#
Copyright 2013-2024 the original author or authors from the JHipster project.
This file is part of the JHipster project, see https://www.jhipster.tech/
for more information.
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
https://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.
-%>
<%_
let table = [];
// Generate CSV header
let header = [];
const fieldsToGenerate = [...allFields.filter(f => f.id), ...fields.filter(f => !f.id)];
for (field of fieldsToGenerate) {
header.push(field.columnName);
if (field.shouldCreateContentType) {
header.push(field.columnName + '_content_type');
}
}
for (relationship of relationships.filter(relationship => (relationship.columnRequired && relationship.persistableRelationship && !relationship.collection) || relationship.tenantRelationship)) {
const { joinColumnNames } = relationship;
header.push(joinColumnNames[0]);
}
table.push(header);
// Generate CSV rows
for (lineNb = 0; lineNb < entity.fakeDataCount; lineNb++) {
const rowData = entity.liquibaseFakeData[lineNb];
let line = [];
for (field of fieldsToGenerate) {
let data = rowData[field.fieldName];
// manage required
if (data === undefined) {
if (field.id || field.columnRequired) {
// Remove row;
line = [];
break;
}
data = '';
}
line.push(data);
if (field.shouldCreateContentType) {
line.push('image/png');
}
}
for (relationship of relationships.filter(relationship => (relationship.columnRequired && relationship.persistableRelationship && !relationship.collection) || relationship.tenantRelationship)) {
const relationshipType = relationship.relationshipType;
const otherLiquibaseFakeData = relationship.otherEntity.liquibaseFakeData;
let relationshipRow = lineNb;
if (relationship.otherEntity.fakeDataCount > 0 && relationshipRow >= relationship.otherEntity.fakeDataCount && !relationship.unique) {
relationshipRow = entity.faker.number.int({min: 1, max: relationship.otherEntity.fakeDataCount}) - 1;
}
if (relationshipRow < relationship.otherEntity.fakeDataCount) {
line.push(otherLiquibaseFakeData[relationshipRow][relationship.otherEntity.primaryKey.name]);
}
}
if (line.length === header.length) {
table.push(line);
}
}
let result = table
.map(line => line.join(';'))
.join('\n');
_%>
<%- result %>
2 changes: 1 addition & 1 deletion generators/spring-boot/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export default class extends BaseApplicationGenerator {
get [BaseApplicationGenerator.WRITING_ENTITIES]() {
return this.asWritingEntitiesTaskGroup({
async writingEntitiesTemplateTask({ application, entities }) {
for (const entity of entities) {
for (const entity of entities.filter(entity => !entity.builtInUserManagement && !entity.builtInAuthority)) {
await this.writeFiles({
blocks: [
javaMainPackageTemplatesBlock({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public abstract class <%- tenantEntity.entityClass %>AwareAspect {
@Autowired
protected TenantIdentifier currentTenantIdentifier;

protected <%- tenantEntity.persistClass %> newCurrentTenantInstance() {
protected <%- tenantEntity.persistClass %> newCurrent<%- tenantEntity.entityClass %>Instance() {
<%- tenantEntity.persistClass %> <%- tenantEntity.entityInstance %> = new <%- tenantEntity.persistClass %>();
<%- tenantEntity.entityInstance %>.setId(currentTenantIdentifier.getEffectiveTenantId());
return <%- tenantEntity.entityInstance %>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class <%- tenantEntity.entityClass %>Aware<%- entityClass %>Aspect extend
private final Logger log = LoggerFactory.getLogger(<%- tenantEntity.entityClass %>Aware<%- entityClass %>Aspect.class);

@Pointcut("target(<%- packageName %>.repository.<%- entityClass %>Repository)")
private void <%- entityInstance %>RepositoryAsTarget() {}
private void <%- entityInstance %>RepositoryAsTarget() {}

@Pointcut("execution(* *.deleteById(..))")
private void deleteByIdOperation() {}
Expand Down Expand Up @@ -71,7 +71,7 @@ public class <%- tenantEntity.entityClass %>Aware<%- entityClass %>Aspect extend

if (this.isCurrentUserRootAdmin()) {
if (bean.get<%- tenantEntity.entityClass %>() == null) {
bean.setTenant(newCurrentTenantInstance());
bean.setTenant(newCurrent<%- tenantEntity.entityClass %>Instance());
}
return;
}
Expand All @@ -87,7 +87,7 @@ public class <%- tenantEntity.entityClass %>Aware<%- entityClass %>Aspect extend
);
throw new org.springframework.security.access.AccessDeniedException("Not allowed");
}
bean.set<%- tenantEntity.entityClass %>(newCurrentTenantInstance());
bean.set<%- tenantEntity.entityClass %>(newCurrent<%- tenantEntity.entityClass %>Instance());
}

@Around(value = "<%- entityInstance %>RepositoryAsTarget() && deleteByIdOperation()")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class <%- tenantEntity.entityClass %>Aware<%- entityClass %>Aspect extend
private final Logger log = LoggerFactory.getLogger(<%- tenantEntity.entityClass %>Aware<%- entityClass %>Aspect.class);

@Pointcut("target(<%- packageName %>.repository.<%- entityClass %>Repository)")
private void <%- entityInstance %>RepositoryAsTarget() {}
private void <%- entityInstance %>RepositoryAsTarget() {}

@Pointcut("execution(* *.existsById(..))")
private void existsByIdOperation() {}
Expand Down Expand Up @@ -72,7 +72,7 @@ public class <%- tenantEntity.entityClass %>Aware<%- entityClass %>Aspect extend

if (bean.get<%- tenantEntity.entityClass %>() == null) {
this.assertThatHaveEffectiveTenantId();
bean.set<%- tenantEntity.entityClass %>(newCurrentTenantInstance());
bean.set<%- tenantEntity.entityClass %>(newCurrent<%- tenantEntity.entityClass %>Instance());
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class TenantIdentifier implements Serializable {
this.selectedTenantId = tenantId;
return this;
}
if (this.selectedTenantId == tenantId) {
return this;
}
throw new Error("Not allowed");
}

Expand Down
1 change: 1 addition & 0 deletions generators/tenantview/support/relationship.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export const createTenantAwareRelationship = (tenantName, tenantLabel, relations
columnRequired: true,
autoGenerate: true,
persistableRelationship: false,
tenantRelationship: true,
});

0 comments on commit 9ce2878

Please sign in to comment.