Skip to content

Commit

Permalink
Switch to spotless and PNC formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Jan 23, 2025
1 parent 0ec093f commit ab20921
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 130 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@ release.properties
/.quarkus/cli/plugins/
# TLS Certificates
.certs/

# cache folder for formatter-maven-plugin and impsort-maven-plugin
.cache
127 changes: 24 additions & 103 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@
<quarkus.platform.version>3.17.3</quarkus.platform.version>

<lombok.version>1.18.32</lombok.version>
<formatter-maven-plugin.version>2.24.1</formatter-maven-plugin.version>
<impsort-maven-plugin.version>1.12.0</impsort-maven-plugin.version>
<quarkus.package.type>uber-jar</quarkus.package.type>

<format.skip>false</format.skip>
</properties>

<!-- Other PNC code doesn't deploy to Sonatype Snapshots -->
Expand Down Expand Up @@ -206,37 +202,36 @@
</executions>
</plugin>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<version>${formatter-maven-plugin.version}</version>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.44.2</version>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-ide-config</artifactId>
<version>${quarkus.platform.version}</version>
<groupId>org.jboss.pnc</groupId>
<artifactId>ide-config</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<configuration>
<!-- store outside of target to speed up formatting when mvn clean is used -->
<cachedir>.cache/formatter-maven-plugin-${formatter-maven-plugin.version}</cachedir>
<configFile>eclipse-format.xml</configFile>
<lineEnding>LF</lineEnding>
<skip>${format.skip}</skip>
</configuration>
</plugin>
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<version>${impsort-maven-plugin.version}</version>
<configuration>
<!-- store outside of target to speed up formatting when mvn clean is used -->
<cachedir>.cache/impsort-maven-plugin-${impsort-maven-plugin.version}</cachedir>
<groups>java.,javax.,jakarta.,org.,com.</groups>
<staticGroups>*</staticGroups>
<lineEnding>LF</lineEnding>
<skip>${format.skip}</skip>
<removeUnused>true</removeUnused>
<java>
<removeUnusedImports/>
<importOrder>
<order>java,javax,jakarta,org,com</order>
</importOrder>
<eclipse>
<file>java-formatter.xml</file>
</eclipse>
<lineEndings>UNIX</lineEndings>
</java>
</configuration>
<executions>
<execution>
<goals>
<goal>apply</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -263,80 +258,6 @@
<quarkus.native.enabled>true</quarkus.native.enabled>
</properties>
</profile>
<profile>
<id>format</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>!validate-format</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<executions>
<execution>
<id>sort-imports</id>
<goals>
<goal>sort</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>validate</id>
<activation>
<activeByDefault>false</activeByDefault>
<property>
<name>validate-format</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>net.revelc.code.formatter</groupId>
<artifactId>formatter-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>validate</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.revelc.code</groupId>
<artifactId>impsort-maven-plugin</artifactId>
<executions>
<execution>
<id>check-imports</id>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>release</id>
<build>
Expand Down
47 changes: 35 additions & 12 deletions src/main/java/org/jboss/pnc/konfluxbuilddriver/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ public class Driver {

void onStart(@Observes StartupEvent ev) {
try {
pipelineRunTemplate = IOUtils.resourceToURL("pipeline-run.yaml", Thread.currentThread().getContextClassLoader());
pipelineRunTemplate = IOUtils
.resourceToURL("pipeline-run.yaml", Thread.currentThread().getContextClassLoader());
logger.debug("Driver creating with {}", pipelineRunTemplate);
} catch (IOException e) {
throw new RuntimeException(e);
Expand All @@ -83,7 +84,8 @@ void onStart(@Observes StartupEvent ev) {

public BuildResponse create(BuildRequest buildRequest) {

logger.info("Establishing token from Indy using clientId {}",
logger.info(
"Establishing token from Indy using clientId {}",
ConfigProvider.getConfig().getConfigValue("quarkus.oidc.client-id").getValue());
IndyTokenResponseDTO tokenResponseDTO = indyService.getAuthToken(
new IndyTokenRequestDTO(buildRequest.getRepositoryBuildContentId()),
Expand Down Expand Up @@ -127,13 +129,20 @@ public BuildResponse create(BuildRequest buildRequest) {
// Various ways to create the initial PipelineRun object. We can use an objectmapper,
// client.getKubernetesSerialization() or the load calls on the Fabric8 objects.
PipelineRun pipelineRun = client.adapt(TektonClient.class).v1().pipelineRuns().load(pipelineRunTemplate).item();
pipelineRun = pipelineRun.edit().editOrNewSpec()
pipelineRun = pipelineRun.edit()
.editOrNewSpec()
.editPipelineRef()
.editFirstParam().editOrNewValue().withStringVal(config.resolverTarget()).endValue()
.editFirstParam()
.editOrNewValue()
.withStringVal(config.resolverTarget())
.endValue()
.endParam()
.endPipelineRef()
.addAllToParams(templateProperties.entrySet().stream()
.map(t -> new ParamBuilder().withName(t.getKey()).withNewValue(t.getValue()).build()).toList())
.addAllToParams(
templateProperties.entrySet()
.stream()
.map(t -> new ParamBuilder().withName(t.getKey()).withNewValue(t.getValue()).build())
.toList())
.editFirstTaskRunSpec()
.editFirstStepSpec()
.editComputeResources()
Expand All @@ -142,17 +151,24 @@ public BuildResponse create(BuildRequest buildRequest) {
.endComputeResources()
.endStepSpec()
.endTaskRunSpec()
.endSpec().build();
.endSpec()
.build();

var created = client.resource(pipelineRun).inNamespace(buildRequest.getNamespace()).create();

return BuildResponse.builder().namespace(buildRequest.getNamespace()).pipelineId(created.getMetadata().getName())
return BuildResponse.builder()
.namespace(buildRequest.getNamespace())
.pipelineId(created.getMetadata().getName())
.build();
}

public void cancel(CancelRequest request) {
var tc = client.adapt(TektonClient.class);
var pipeline = tc.v1().pipelineRuns().inNamespace(request.getNamespace()).withName(request.getPipelineId()).get();
var pipeline = tc.v1()
.pipelineRuns()
.inNamespace(request.getNamespace())
.withName(request.getPipelineId())
.get();

logger.info("Retrieved pipeline {}", pipeline.getMetadata().getName());

Expand All @@ -170,7 +186,8 @@ public void cancel(CancelRequest request) {
}

/**
* Get a fresh access token for the service account. This is done because we want to get a super-new token to be used since
* Get a fresh access token for the service account. This is done because we want to get a super-new token to be
* used since
* we're not entirely sure when the http request will be done.
*
* @return fresh access token
Expand All @@ -183,11 +200,17 @@ public void completed(PipelineNotification notification) {

// TODO: PNC build-driver uses BuildCompleted when notifying the callback.
String body = Serialization
.asJson(BuildCompleted.builder().buildId(notification.getBuildId()).status(notification.getStatus()).build());
.asJson(
BuildCompleted.builder()
.buildId(notification.getBuildId())
.status(notification.getStatus())
.build());

HttpRequest.Builder builder = HttpRequest.newBuilder()
.uri(notification.getCompletionCallback().getUri())
.method(notification.getCompletionCallback().getMethod().name(), HttpRequest.BodyPublishers.ofString(body))
.method(
notification.getCompletionCallback().getMethod().name(),
HttpRequest.BodyPublishers.ofString(body))
// TOOD: Timeouts?
// .timeout(Duration.ofSeconds(requestTimeout))
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
public interface IndyService {

/**
* Ask Indy to give us the token that we will use for Maven communication with Indy, in the builder pod for the particular
* Ask Indy to give us the token that we will use for Maven communication with Indy, in the builder pod for the
* particular
* buildId
*
* @param indyTokenRequestDTO the DTO to send to Indy
Expand Down
29 changes: 22 additions & 7 deletions src/test/java/org/jboss/pnc/konfluxbuilddriver/DriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@

@WithKubernetesTestServer
@QuarkusTest
@QuarkusTestResource(value = LogCollectingTestResource.class, restrictToAnnotatedClass = true, initArgs = @ResourceArg(name = LogCollectingTestResource.LEVEL, value = "FINE"))
@QuarkusTestResource(
value = LogCollectingTestResource.class,
restrictToAnnotatedClass = true,
initArgs = @ResourceArg(name = LogCollectingTestResource.LEVEL, value = "FINE"))
@QuarkusTestResource(WireMockExtensions.class)
public class DriverTest {

Expand Down Expand Up @@ -76,19 +79,31 @@ void cancel() {
BuildRequest request = BuildRequest.builder().namespace(namespace).podMemoryOverride("1Gi").build();
BuildResponse response = driver.create(request);

var pipelineRuns = client.adapt(TektonClient.class).v1().pipelineRuns()
.inNamespace(response.getNamespace()).list().getItems();
var pipelineRuns = client.adapt(TektonClient.class)
.v1()
.pipelineRuns()
.inNamespace(response.getNamespace())
.list()
.getItems();

assertEquals(1, pipelineRuns.size());
assertEquals(response.getPipelineId(), pipelineRuns.getFirst().getMetadata().getName());

driver.cancel(CancelRequest.builder().namespace(namespace).pipelineId(response.getPipelineId()).build());
pipelineRuns = client.adapt(TektonClient.class).v1().pipelineRuns()
.inNamespace(response.getNamespace()).list().getItems();
pipelineRuns = client.adapt(TektonClient.class)
.v1()
.pipelineRuns()
.inNamespace(response.getNamespace())
.list()
.getItems();
assertEquals("False", pipelineRuns.getFirst().getStatus().getConditions().getFirst().getStatus());
List<LogRecord> logRecords = LogCollectingTestResource.current().getRecords();
assertTrue(logRecords.stream().anyMatch(r -> LogCollectingTestResource.format(r)
.contains("Retrieved pipeline run-mw-pipeline--00000000-0000-0000-0000-000000000005")));
assertTrue(
logRecords.stream()
.anyMatch(
r -> LogCollectingTestResource.format(r)
.contains(
"Retrieved pipeline run-mw-pipeline--00000000-0000-0000-0000-000000000005")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public void setup() {
@TestSecurity(user = "testUser", roles = { "pnc-users-admin" })
void verify() {
BuildRequest request = BuildRequest.builder().namespace("default").podMemoryOverride("1Gi").build();
Response res = RestAssured.given().contentType(ContentType.JSON)
Response res = RestAssured.given()
.contentType(ContentType.JSON)
.body(request)
.when()
.post("/build");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ public class MockOidcClient implements OidcClient {
@Override
public Uni<Tokens> getTokens(Map<String, String> additionalGrantParameters) {
return Uni.createFrom()
.item(new Tokens("accessToken", 1L, Duration.of(5, ChronoUnit.MINUTES), "refreshToken", 1L, null, null));
.item(
new Tokens(
"accessToken",
1L,
Duration.of(5, ChronoUnit.MINUTES),
"refreshToken",
1L,
null,
null));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public Map<String, String> start() {
put(urlEqualTo("/invoker"))
.withRequestBody(
equalToJson("{\"status\":\"Succeeded\",\"buildId\":\"1234\"}"))
.willReturn(aResponse()
.withStatus(200)));
.willReturn(
aResponse()
.withStatus(200)));

return Map.of("quarkus.rest-client.wiremockextensions.url", wireMockServer.baseUrl());
}
Expand Down

0 comments on commit ab20921

Please sign in to comment.