Skip to content

Commit

Permalink
Summary files (#60)
Browse files Browse the repository at this point in the history
* events to be consumed by the summary generator

* testing summaries, automatic GitHub summary

* include gradle scans

* updated docs

* fixed tests on GitHub, updated summaries

* fixed no-token test

* fixed checkstyle violation

* license bump
  • Loading branch information
musketyr authored Jul 21, 2023
1 parent 67c45dc commit db89346
Show file tree
Hide file tree
Showing 91 changed files with 738 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
run: gu install native-image
- uses: gradle/gradle-command-action@v2
with:
arguments: check coveralls --stacktrace
arguments: check coveralls --stacktrace --scan
- name: Show Reports
uses: actions/upload-artifact@v1
if: failure()
Expand Down
6 changes: 5 additions & 1 deletion apps/pierrot/pierrot.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -115,3 +115,7 @@ sdkman {
hashtag = 'PierrotCLI'
}

tasks.withType(Test).configureEach { Test test ->
test.environment('GITHUB_STEP_SUMMARY', System.getenv('GITHUB_STEP_SUMMARY') ?: new File(buildDir, 'step-summary.txt').canonicalPath)
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,16 +18,24 @@
package com.agorapulse.pierrot.cli;

import com.agorapulse.pierrot.api.util.LoggerWithOptionalStacktrace;
import com.agorapulse.pierrot.api.summary.SummaryCollector;
import com.agorapulse.pierrot.cli.summary.SummaryWriter;
import io.micronaut.configuration.picocli.MicronautFactory;
import io.micronaut.context.ApplicationContext;
import io.micronaut.context.ApplicationContextBuilder;
import io.micronaut.context.env.CommandLinePropertySource;
import io.micronaut.context.env.Environment;
import io.micronaut.context.env.MapPropertySource;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.annotation.TypeHint;
import jakarta.inject.Inject;
import picocli.CommandLine;
import picocli.CommandLine.Command;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static io.micronaut.core.annotation.TypeHint.AccessType.*;
Expand Down Expand Up @@ -66,6 +74,10 @@ public class PierrotCommand implements Runnable {
private static final String GITHUB_TOKEN_NAME = "token";
private static final String GITHUB_TOKEN_PARAMETER = "--" + GITHUB_TOKEN_NAME;

private static final String SUMMARY_TOKEN_NAME = "summary-file";

private static final String SUMMARY_TOKEN_PARAMETER = "--" + SUMMARY_TOKEN_NAME;

@CommandLine.Option(
names = {"-s", "--stacktrace"},
description = "Print stack traces",
Expand All @@ -84,25 +96,46 @@ void setStacktrace(boolean stacktrace) {
)
String token;

@CommandLine.Option(
names = {SUMMARY_TOKEN_PARAMETER},
description = "Markdown summary file path",
scope = CommandLine.ScopeType.INHERIT
)
File summaryFile;

@Inject @Nullable
SummaryCollector summaryCollector;

public static void main(String[] args) {
System.exit(execute(args));
}

static int execute(String[] args) {
if (args.length == 0) {
args = new String[] {"--help"};
args = new String[]{"--help"};
}

io.micronaut.core.cli.CommandLine commandLine = io.micronaut.core.cli.CommandLine.parse(args);

CommandLinePropertySource commandLinePropertySource = new CommandLinePropertySource(commandLine);

Map<String, Object> map = commandLine.getUndeclaredOptions().containsKey(GITHUB_TOKEN_NAME)
? Map.of("pierrot.token", commandLine.getUndeclaredOptions().get(GITHUB_TOKEN_NAME))
: Map.of();
Map<String, Object> map = new HashMap<>();

if (commandLine.getUndeclaredOptions().containsKey(GITHUB_TOKEN_NAME)) {
map.put("pierrot.token", commandLine.getUndeclaredOptions().get(GITHUB_TOKEN_NAME));
}

if (commandLine.getUndeclaredOptions().containsKey(SUMMARY_TOKEN_NAME)) {
try {
map.put("summary.file", ((File) commandLine.getUndeclaredOptions().get(SUMMARY_TOKEN_NAME)).getCanonicalPath());
} catch (IOException e) {
System.out.println("Cannot get canonical path for " + commandLine.getUndeclaredOptions().get(SUMMARY_TOKEN_NAME) + ": " + e.getMessage());
}
} else if (System.getenv("GITHUB_STEP_SUMMARY") != null) {
map.put("summary.file", System.getenv("GITHUB_STEP_SUMMARY"));
}

MapPropertySource mapSource = MapPropertySource.of("token-from-command-line", map);
MapPropertySource mapSource = MapPropertySource.of("command-line-properties", map);

ApplicationContextBuilder builder = ApplicationContext
.builder(PierrotCommand.class, Environment.CLI)
Expand All @@ -113,7 +146,13 @@ static int execute(String[] args) {
try (ApplicationContext ctx = builder.start()) {
CommandLine cmd = new CommandLine(PierrotCommand.class, new MicronautFactory(ctx));
exitCode = cmd.execute(args);
if (ctx.containsBean(SummaryWriter.class) && !Arrays.asList(args).contains("--help")) {
File summaryFileLocation = ctx.getBean(SummaryWriter.class).write();
System.out.println("Summary file written to " + summaryFileLocation);
}
}


return exitCode;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2022 Vladimir Orany.
* Copyright 2021-2023 Vladimir Orany.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* Copyright 2021-2023 Vladimir Orany.
*
* 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.
*/
package com.agorapulse.pierrot.cli.summary;

import com.agorapulse.pierrot.api.summary.SummaryCollector;
import com.agorapulse.pierrot.api.util.LoggerWithOptionalStacktrace;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.annotation.Value;
import jakarta.inject.Singleton;
import org.slf4j.Logger;

import java.io.File;
import java.nio.file.Files;

@Singleton
@Requires(property = "summary.file", bean = SummaryCollector.class)
public class SummaryWriter {

private static final Logger LOGGER = LoggerWithOptionalStacktrace.create(SummaryWriter.class);

private final String summaryFilePath;
private final SummaryCollector collector;

public SummaryWriter(@Value("${summary.file}") String summaryFilePath, SummaryCollector collector) {
this.summaryFilePath = summaryFilePath;
this.collector = collector;
}

public File write() {
try {
File file = new File(summaryFilePath);
file.getParentFile().mkdirs();
Files.writeString(file.toPath(), collector.getSummary());
return file;
} catch (Exception e) {
LOGGER.error("Cannot write summary to " + summaryFilePath, e);
return null;
}
}
}
2 changes: 1 addition & 1 deletion apps/pierrot/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
SPDX-License-Identifier: Apache-2.0
Copyright 2021-2022 Vladimir Orany.
Copyright 2021-2023 Vladimir Orany.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
Loading

0 comments on commit db89346

Please sign in to comment.