Skip to content

Commit

Permalink
Merge pull request #62 from sentrysoftware/feature/issue-58-add-commu…
Browse files Browse the repository at this point in the history
…nity-connector-github-link-on-each-connector-page

Issue #58: add community connector GitHub link on each connector page
  • Loading branch information
NassimBtk authored Oct 3, 2024
2 parents 7c5f640 + f89169e commit 9958044
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.sentrysoftware.maven.metricshub.connector.Constants.YAML_OBJECT_MAPPER;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
Expand All @@ -48,6 +49,12 @@ private static class ConnectorFileVisitor extends SimpleFileVisitor<Path> {
@Getter
private final Map<String, JsonNode> connectorsMap = new HashMap<>();

private final Path sourceDirectory;

ConnectorFileVisitor(final Path sourceDirectory) {
this.sourceDirectory = sourceDirectory;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
// Skip this path if it is a directory or not a YAML file
Expand All @@ -62,6 +69,8 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO

final JsonNode connector = ConnectorParser.withNodeProcessor(file.getParent()).parse(file.toFile());

((ObjectNode) connector).put("relativePath", sourceDirectory.relativize(file).toString());

final Path fileNamePath = file.getFileName();

if (fileNamePath != null) {
Expand Down Expand Up @@ -107,7 +116,7 @@ private boolean isYamlFile(final String fileName) {
* @throws IOException if the file does not exist
*/
public Map<String, JsonNode> parse(@NonNull final Path sourceDirectory) throws IOException {
final ConnectorFileVisitor fileVisitor = new ConnectorFileVisitor();
final ConnectorFileVisitor fileVisitor = new ConnectorFileVisitor(sourceDirectory);

Files.walkFileTree(sourceDirectory, fileVisitor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,4 +663,14 @@ public Map<String, ConnectorDefaultVariable> getDefaultVariables() {
}
return defaultVariables;
}

/**
* Retrieves the relative path of the connector that was saved during parsing.
* This path is used to generate a link to the connector's source code.
*
* @return The saved relative path as a string.
*/
public String getRelativePath() {
return connector.get("relativePath").asText();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
*/

import static org.sentrysoftware.maven.metricshub.connector.ConnectorsDirectoryReport.CONNECTORS_DIRECTORY_OUTPUT_NAME;
import static org.sentrysoftware.maven.metricshub.connector.Constants.TAG_SUBDIRECTORY_NAME;

import com.fasterxml.jackson.databind.JsonNode;
import java.text.ChoiceFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -56,6 +58,7 @@ public class ConnectorPageProducer {
private final String connectorId;
private final JsonNode connector;
private final Log logger;
private String connectorDirectory;

/**
* Produces a report page for the current connector and generates the corresponding sink for documentation output.
Expand Down Expand Up @@ -121,6 +124,35 @@ public void produce(

produceSupersedesContent(sink, supersededMap, connectorJsonNodeReader);

// Display the connector tags
final List<String> connectorTags = connectorJsonNodeReader.getTags();
sink.paragraph();
connectorTags
.stream()
.sorted(String.CASE_INSENSITIVE_ORDER)
.collect(Collectors.toCollection(LinkedHashSet::new))
.forEach(tag ->
sink.rawText(
SinkHelper.bootstrapLabel(
SinkHelper.hyperlinkRef(
String.format("%s/%s.html", TAG_SUBDIRECTORY_NAME, tag.toLowerCase().replace(" ", "-")),
tag
),
"metricshub-tag"
)
)
);
sink.paragraph_();

// The GitHub link will be generated only for community connectors
if (!enterpriseConnectorIds.contains(connectorId)) {
connectorDirectory = connectorJsonNodeReader.getRelativePath().replace("\\", "/");
// Add a link to the connector source.
sink.paragraph();
sink.rawText(SinkHelper.gitHubHyperlinkRef(connectorDirectory, "Source"));
sink.paragraph_();
}

// End of the second heading element
sink.section2_();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,25 @@ public static String replaceWithHtmlCode(final String value) {
}

/**
* Creates an HTML hyperlink reference with the given content
* Creates an HTML hyperlink reference with the given content.
*/
public static String hyperlinkRef(final String link, final String content) {
return String.format("<a href=\"%s\">%s</a>", link, content);
}

/**
* Creates an HTMP hyperlink reference with the given content and the specified classes.
*/
public static String gitHubHyperlinkRef(final String link, final String content) {
return String.format(
"" +
"<a href=\"https://github.com/sentrysoftware/metricshub-community-connectors/tree/main/src/main/connector/%s\" " +
"<i class=\"fa-brands fa-github\">" +
"</i>" +
" %s" +
"</a>",
link,
content
);
}
}

0 comments on commit 9958044

Please sign in to comment.