This repository has been archived by the owner on Jul 29, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allows to define manual rules for the reviewer which will affec…
…t the API quality fix gravitee-io/issues#2601
- Loading branch information
Showing
6 changed files
with
291 additions
and
10 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
192 changes: 192 additions & 0 deletions
192
src/main/java/io/gravitee/repository/jdbc/management/JdbcApiQualityRuleRepository.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,192 @@ | ||
/** | ||
* Copyright (C) 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* 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 io.gravitee.repository.jdbc.management; | ||
|
||
import io.gravitee.repository.exceptions.TechnicalException; | ||
import io.gravitee.repository.jdbc.orm.JdbcObjectMapper; | ||
import io.gravitee.repository.management.api.ApiQualityRuleRepository; | ||
import io.gravitee.repository.management.model.ApiQualityRule; | ||
import io.gravitee.repository.management.model.ApiQualityRule; | ||
import io.gravitee.repository.management.model.Membership; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.Types; | ||
import java.util.Date; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static io.gravitee.repository.jdbc.management.JdbcHelper.WHERE_CLAUSE; | ||
import static java.lang.String.format; | ||
|
||
/** | ||
* @author Azize ELAMRANI (azize.elamrani at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
@Repository | ||
public class JdbcApiQualityRuleRepository implements ApiQualityRuleRepository { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(JdbcApiQualityRuleRepository.class); | ||
|
||
@Autowired | ||
private JdbcTemplate jdbcTemplate; | ||
|
||
private static final JdbcObjectMapper ORM = JdbcObjectMapper.builder(ApiQualityRule.class, "api_quality_rules") | ||
.updateSql("update api_quality_rules set " | ||
+ " api = ?" | ||
+ " , quality_rule = ?" | ||
+ " , checked = ?" | ||
+ " , created_at = ? " | ||
+ " , updated_at = ? " | ||
+ WHERE_CLAUSE | ||
+ " api = ? " | ||
+ " and quality_rule = ? " | ||
) | ||
.addColumn("api", Types.NVARCHAR, String.class) | ||
.addColumn("quality_rule", Types.NVARCHAR, String.class) | ||
.addColumn("checked", Types.BOOLEAN, boolean.class) | ||
.addColumn("created_at", Types.TIMESTAMP, Date.class) | ||
.addColumn("updated_at", Types.TIMESTAMP, Date.class) | ||
.build(); | ||
|
||
@Override | ||
public Optional<ApiQualityRule> findById(String api, String qualityRule) throws TechnicalException { | ||
LOGGER.debug("JdbcApiQualityRuleRepository.findById({}, {})", api, qualityRule); | ||
try { | ||
final List<ApiQualityRule> apiQualityRules = jdbcTemplate.query("select" | ||
+ " api, quality_rule, checked, created_at, updated_at " | ||
+ " from api_quality_rules where api = ? and quality_rule = ?" | ||
, ORM.getRowMapper() | ||
, api | ||
, qualityRule | ||
); | ||
return apiQualityRules.stream().findFirst(); | ||
} catch (final Exception ex) { | ||
final String error = "Failed to find apiQualityRule by id"; | ||
LOGGER.error(error, ex); | ||
throw new TechnicalException(error, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public ApiQualityRule create(ApiQualityRule apiQualityRule) throws TechnicalException { | ||
LOGGER.debug("JdbcApiQualityRuleRepository.create({})", apiQualityRule); | ||
try { | ||
jdbcTemplate.update(ORM.buildInsertPreparedStatementCreator(apiQualityRule)); | ||
return findById(apiQualityRule.getApi(), apiQualityRule.getQualityRule()).orElse(null); | ||
} catch (final Exception ex) { | ||
final String error = "Failed to create apiQualityRule"; | ||
LOGGER.error(error, ex); | ||
throw new TechnicalException(error, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public ApiQualityRule update(ApiQualityRule apiQualityRule) throws TechnicalException { | ||
LOGGER.debug("JdbcApiQualityRuleRepository.update({})", apiQualityRule); | ||
if (apiQualityRule == null) { | ||
throw new IllegalStateException("Failed to update null"); | ||
} | ||
try { | ||
jdbcTemplate.update(ORM.buildUpdatePreparedStatementCreator(apiQualityRule | ||
, apiQualityRule.getApi() | ||
, apiQualityRule.getQualityRule() | ||
)); | ||
return findById(apiQualityRule.getApi(), apiQualityRule.getQualityRule()).orElseThrow(() -> | ||
new IllegalStateException(format("No apiQualityRule found with id [%s, %s]", apiQualityRule.getApi(), apiQualityRule.getQualityRule()))); | ||
} catch (final IllegalStateException ex) { | ||
throw ex; | ||
} catch (final Exception ex) { | ||
final String error = "Failed to update apiQualityRule"; | ||
LOGGER.error(error, ex); | ||
throw new TechnicalException(error, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void delete(String api, String qualityRule) throws TechnicalException { | ||
LOGGER.debug("JdbcApiQualityRuleRepository.delete({}, {})", api, qualityRule); | ||
try { | ||
jdbcTemplate.update("delete from api_quality_rules where api = ? and quality_rule = ? " | ||
, api | ||
, qualityRule | ||
); | ||
} catch (final Exception ex) { | ||
final String error = "Failed to delete apiQualityRule"; | ||
LOGGER.error(error, ex); | ||
throw new TechnicalException(error, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public List<ApiQualityRule> findByQualityRule(String qualityRule) throws TechnicalException { | ||
LOGGER.debug("JdbcApiQualityRuleRepository.findByQualityRule({})", qualityRule); | ||
try { | ||
final String query = "select " | ||
+ " api, quality_rule, checked, created_at, updated_at " | ||
+ " from api_quality_rules where quality_rule = ? "; | ||
return jdbcTemplate.query(query, (PreparedStatement ps) -> ps.setString(1, qualityRule), ORM.getRowMapper()); | ||
} catch (final Exception ex) { | ||
final String error = "Failed to find apiQualityRule by quality rule"; | ||
LOGGER.error(error, ex); | ||
throw new TechnicalException(error, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public List<ApiQualityRule> findByApi(String api) throws TechnicalException { | ||
LOGGER.debug("JdbcApiQualityRuleRepository.findByApi({})", api); | ||
try { | ||
final String query = "select " | ||
+ " api, quality_rule, checked, created_at, updated_at " | ||
+ " from api_quality_rules where api = ? "; | ||
return jdbcTemplate.query(query, (PreparedStatement ps) -> ps.setString(1, api), ORM.getRowMapper()); | ||
} catch (final Exception ex) { | ||
final String error = "Failed to find apiQualityRule by api"; | ||
LOGGER.error(error, ex); | ||
throw new TechnicalException(error, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteByQualityRule(String qualityRule) throws TechnicalException { | ||
LOGGER.debug("JdbcApiQualityRuleRepository.deleteByQualityRule({})", qualityRule); | ||
try { | ||
jdbcTemplate.update("delete from api_quality_rules where quality_rule = ? ", qualityRule); | ||
} catch (final Exception ex) { | ||
final String error = "Failed to delete apiQualityRule by quality rule"; | ||
LOGGER.error(error, ex); | ||
throw new TechnicalException(error, ex); | ||
} | ||
} | ||
|
||
@Override | ||
public void deleteByApi(String api) throws TechnicalException { | ||
LOGGER.debug("JdbcApiQualityRuleRepository.deleteByApi({})", api); | ||
try { | ||
jdbcTemplate.update("delete from api_quality_rules where api = ? ", api); | ||
} catch (final Exception ex) { | ||
final String error = "Failed to delete apiQualityRule by api"; | ||
LOGGER.error(error, ex); | ||
throw new TechnicalException(error, ex); | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/io/gravitee/repository/jdbc/management/JdbcQualityRuleRepository.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,51 @@ | ||
/** | ||
* Copyright (C) 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* 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 io.gravitee.repository.jdbc.management; | ||
|
||
import io.gravitee.repository.jdbc.orm.JdbcObjectMapper; | ||
import io.gravitee.repository.management.api.QualityRuleRepository; | ||
import io.gravitee.repository.management.model.QualityRule; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.sql.Types; | ||
import java.util.Date; | ||
|
||
/** | ||
* @author Azize ELAMRANI (azize.elamrani at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
@Repository | ||
public class JdbcQualityRuleRepository extends JdbcAbstractCrudRepository<QualityRule, String> implements QualityRuleRepository { | ||
|
||
private static final JdbcObjectMapper ORM = JdbcObjectMapper.builder(QualityRule.class, "quality_rules", "id") | ||
.addColumn("id", Types.NVARCHAR, String.class) | ||
.addColumn("name", Types.NVARCHAR, String.class) | ||
.addColumn("description", Types.NVARCHAR, String.class) | ||
.addColumn("weight", Types.INTEGER, int.class) | ||
.addColumn("created_at", Types.TIMESTAMP, Date.class) | ||
.addColumn("updated_at", Types.TIMESTAMP, Date.class) | ||
.build(); | ||
|
||
@Override | ||
protected JdbcObjectMapper getOrm() { | ||
return ORM; | ||
} | ||
|
||
@Override | ||
protected String getId(QualityRule item) { | ||
return item.getId(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/resources/liquibase/changelogs/v1_30_0/schema.yml
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,33 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: 1.30.0 | ||
author: GraviteeSource Team | ||
changes: | ||
- createTable: | ||
tableName: quality_rules | ||
columns: | ||
- column: {name: id, type: nvarchar(64), constraints: { nullable: false } } | ||
- column: {name: name, type: nvarchar(64), constraints: { nullable: false } } | ||
- column: {name: description, type: nvarchar(256), constraints: { nullable: false } } | ||
- column: {name: weight, type: int, constraints: { nullable: false } } | ||
- column: {name: created_at, type: timestamp(6), constraints: { nullable: false } } | ||
- column: {name: updated_at, type: timestamp(6), constraints: { nullable: true } } | ||
|
||
- addPrimaryKey: | ||
constraintName: pk_quality_rules | ||
columnNames: id | ||
tableName: quality_rules | ||
|
||
- createTable: | ||
tableName: api_quality_rules | ||
columns: | ||
- column: {name: api, type: nvarchar(64), constraints: { nullable: false } } | ||
- column: {name: quality_rule, type: nvarchar(64), constraints: { nullable: false } } | ||
- column: {name: checked, type: boolean, constraints: { nullable: false } } | ||
- column: {name: created_at, type: timestamp(6), constraints: { nullable: false } } | ||
- column: {name: updated_at, type: timestamp(6), constraints: { nullable: true } } | ||
|
||
- addPrimaryKey: | ||
constraintName: pk_api_quality_rules | ||
columnNames: api, quality_rule | ||
tableName: api_quality_rules |
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