-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
e1872e8
commit 9b4be74
Showing
6 changed files
with
226 additions
and
44 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
137 changes: 137 additions & 0 deletions
137
src/main/java/com/github/andrewazores/GitHubRepositoryIntegration.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,137 @@ | ||
/* | ||
* Copyright Andrew Azores. | ||
* | ||
* 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 com.github.andrewazores; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.regex.Pattern; | ||
|
||
import io.quarkus.logging.Log; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import org.eclipse.microprofile.config.inject.ConfigProperty; | ||
|
||
@ApplicationScoped | ||
class GitHubRepositoryIntegration implements SourceIntegration { | ||
private static final Pattern GH_REPO_PATTERN = | ||
Pattern.compile( | ||
"^https?://(?:www.)?github.com/(?<owner>[\\w.-]+)/(?<repo>[\\w.-]+)/?$", | ||
Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); | ||
private static final Pattern DEP_PATTERN = | ||
Pattern.compile( | ||
"^[\\s]*(?<group>[a-z0-9._-]+):(?<artifact>[a-z0-9._-]+):(?<packaging>[a-z0-9._-]+):(?<version>[a-z0-9._-]+).*", | ||
Pattern.MULTILINE | Pattern.CASE_INSENSITIVE); | ||
|
||
@Inject CliSupport cli; | ||
|
||
@ConfigProperty(name = "maven-gav-checker.integration.github-repository.transitive-deps") | ||
boolean enableTransitiveDeps; | ||
|
||
@Override | ||
public boolean test(URL url) { | ||
Log.debugv("Testing {0} on {1}", GH_REPO_PATTERN.pattern(), url.toString()); | ||
var m = GH_REPO_PATTERN.matcher(url.toString()); | ||
return m.matches(); | ||
} | ||
|
||
@Override | ||
public List<GroupArtifactVersion> apply(URL url) throws IOException, InterruptedException { | ||
Log.debugv("Processing GitHub repository: {0}", url); | ||
var m = GH_REPO_PATTERN.matcher(url.toString()); | ||
if (!m.matches()) throw new IllegalStateException(); | ||
var owner = m.group("owner"); | ||
var repo = m.group("repo"); | ||
var repoId = String.format("%s/%s", owner, repo); | ||
var checkoutRef = getDefaultBranchRef(repoId); | ||
var pomUrl = getPomUrl(repoId, checkoutRef); | ||
var workDir = Files.createTempDirectory(getClass().getSimpleName()); | ||
var pom = workDir.resolve("pom.xml"); | ||
var depsFile = workDir.resolve("deps.txt"); | ||
|
||
try (BufferedInputStream in = new BufferedInputStream(pomUrl.openStream()); | ||
FileOutputStream fileOutputStream = new FileOutputStream(pom.toFile())) { | ||
var dataBuffer = new byte[8 * 1024]; | ||
int bytesRead; | ||
while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) { | ||
fileOutputStream.write(dataBuffer, 0, bytesRead); | ||
} | ||
|
||
Log.debug(Files.readString(pom)); | ||
|
||
var proc = | ||
cli.script( | ||
"mvn", | ||
"-B", | ||
"-q", | ||
"-Dsilent", | ||
"-DincludeScope=compile", | ||
String.format("-DexcludeTransitive=%b", !enableTransitiveDeps), | ||
"-DincludeParents", | ||
"-Dmdep.outputScope=false", | ||
"-DoutputFile=" + depsFile.toAbsolutePath().toString(), | ||
"-f", | ||
pom.toAbsolutePath().toString(), | ||
"dependency:list"); | ||
proc.assertOk(); | ||
return Files.readAllLines(depsFile).stream() | ||
.peek(l -> Log.tracev("dependency: {0}", l)) | ||
.filter(s -> DEP_PATTERN.matcher(s).matches()) | ||
.map( | ||
s -> { | ||
var m2 = DEP_PATTERN.matcher(s); | ||
if (!m2.matches()) throw new IllegalStateException(); | ||
return new GroupArtifactVersion( | ||
m2.group("group"), | ||
m2.group("artifact"), | ||
m2.group("version")); | ||
}) | ||
.toList(); | ||
} finally { | ||
Files.deleteIfExists(pom); | ||
Files.deleteIfExists(depsFile); | ||
} | ||
} | ||
|
||
private String getDefaultBranchRef(String repo) throws IOException, InterruptedException { | ||
var proc = | ||
cli.script( | ||
"gh", | ||
"repo", | ||
"view", | ||
"--json", | ||
"defaultBranchRef", | ||
"--jq", | ||
".defaultBranchRef.name", | ||
repo); | ||
proc.assertOk(); | ||
return proc.out().get(0); | ||
} | ||
|
||
private URL getPomUrl(String repo, String checkoutRef) { | ||
try { | ||
return new URL( | ||
String.format( | ||
"https://raw.githubusercontent.com/%s/%s/pom.xml", repo, checkoutRef)); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
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
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,55 @@ | ||
/* | ||
* Copyright Andrew Azores. | ||
* | ||
* 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 com.github.andrewazores; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
|
||
class XmlParser { | ||
|
||
static Optional<Node> getChild(Node parent, String childName) { | ||
NodeList list = parent.getChildNodes(); | ||
int idx = 0; | ||
while (idx < list.getLength()) { | ||
var node = list.item(idx); | ||
var name = node.getNodeName(); | ||
if (childName.equals(name)) { | ||
return Optional.of(node); | ||
} | ||
idx++; | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
static List<Node> getChildren(Node parent, String childName) { | ||
List<Node> out = new ArrayList<>(); | ||
NodeList list = parent.getChildNodes(); | ||
int idx = 0; | ||
while (idx < list.getLength()) { | ||
var node = list.item(idx); | ||
var name = node.getNodeName(); | ||
if (childName.equals(name)) { | ||
out.add(node); | ||
} | ||
idx++; | ||
} | ||
return out; | ||
} | ||
} |
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