Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #58: Add community connector GitHub link on each connector page #62

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -56,6 +56,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 +122,15 @@ public void produce(

produceSupersedesContent(sink, supersededMap, connectorJsonNodeReader);

// 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\" class=\"externalLink\" " +
CherfaElyes marked this conversation as resolved.
Show resolved Hide resolved
"<i class=\"fa-brands fa-github\">" +
"</i>" +
" %s" +
"</a>",
link,
content
);
}
}
Loading