-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server to host sources of published Java libraries.
Previously, Sourcegraph was only able to LSIF index Java repositories. There was no good way to LSIF index a Java library on Maven Central because they don't have an associated git repository. This commit adds a new `PackageHub` server that is implemented as a proxy on top of Maven repositories that serves git repositories that Sourcegraph can understand. This new server can be used with lsif-java to enable navigation between repositories and their transitive Java library dependencies.
- Loading branch information
1 parent
2d9fc92
commit ebf02f9
Showing
30 changed files
with
1,084 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -55,3 +55,4 @@ test-report.json | |
dump.lsif | ||
|
||
./generated | ||
/sources |
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,11 @@ | ||
FROM openjdk:8-jdk-alpine | ||
COPY bin/coursier coursier | ||
RUN apk add --no-cache git curl \ | ||
&& git config --global user.email "[email protected]" \ | ||
&& git config --global user.name "Your Name" \ | ||
&& git config --global http.postBuffer 1048576000 \ | ||
&& curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /src \ | ||
&& chmod +x /src \ | ||
&& /coursier bootstrap -r sonatype:snapshots com.sourcegraph:packagehub_2.13:0.5.0-12-69905fcb-SNAPSHOT -o /packagehub | ||
ENV COURSIER_REPOSITORIES=central|https://maven.google.com/|jitpack | ||
ENTRYPOINT /packagehub --host 0.0.0.0 --port $PORT --src /src --coursier /coursier --postgres.username=$DB_USER --postgres.password=$DB_PASS --postgres.url=$DB_URL --auto-index-delay=PT1M |
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,11 @@ | ||
FROM openjdk:8-jdk-alpine | ||
COPY bin/coursier coursier | ||
RUN apk add --no-cache git curl \ | ||
&& git config --global user.email "[email protected]" \ | ||
&& git config --global user.name "Your Name" \ | ||
&& git config --global http.postBuffer 1048576000 \ | ||
&& curl -L https://sourcegraph.com/.api/src-cli/src_linux_amd64 -o /src \ | ||
&& chmod +x /src \ | ||
&& /coursier bootstrap -r sonatype:snapshots com.sourcegraph:packagehub_2.13:VERSION -o /packagehub | ||
ENV COURSIER_REPOSITORIES=central|https://maven.google.com/|jitpack | ||
ENTRYPOINT /packagehub --host 0.0.0.0 --port $PORT --src /src --coursier /coursier --postgres.username=$DB_USER --postgres.password=$DB_PASS --postgres.url=$DB_URL --auto-index-delay=PT1M |
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
7 changes: 7 additions & 0 deletions
7
packagehub/src/main/resources/db/migration/V1__Create_tables.sql
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,7 @@ | ||
CREATE TABLE packages ( | ||
id VARCHAR(1000) NOT NULL PRIMARY KEY | ||
); | ||
|
||
CREATE TABLE indexed_packages ( | ||
id VARCHAR(1000) NOT NULL PRIMARY KEY | ||
); |
22 changes: 22 additions & 0 deletions
22
packagehub/src/main/scala/com/sourcegraph/packagehub/EmptyJsonCodec.scala
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,22 @@ | ||
package com.sourcegraph.packagehub | ||
|
||
import moped.json.DecodingContext | ||
import moped.json.ErrorResult | ||
import moped.json.JsonCodec | ||
import moped.json.JsonElement | ||
import moped.json.JsonString | ||
import moped.json.Result | ||
import moped.macros.ClassShape | ||
import moped.reporters.Diagnostic | ||
|
||
/** | ||
* Codec that always fails the decoding step. | ||
* | ||
* Useful for types that cannot be configured from the command-line. | ||
*/ | ||
class EmptyJsonCodec[T] extends JsonCodec[T] { | ||
def decode(context: DecodingContext): Result[T] = | ||
ErrorResult(Diagnostic.error(s"not supported: $context")) | ||
def encode(value: T): JsonElement = JsonString(value.toString()) | ||
def shape: ClassShape = ClassShape.empty | ||
} |
120 changes: 120 additions & 0 deletions
120
packagehub/src/main/scala/com/sourcegraph/packagehub/Package.scala
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,120 @@ | ||
package com.sourcegraph.packagehub | ||
|
||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
|
||
import scala.util.control.NonFatal | ||
|
||
import com.sourcegraph.lsif_java.Dependencies | ||
import coursier.core.Dependency | ||
import coursier.core.Module | ||
import coursier.core.ModuleName | ||
import coursier.core.Organization | ||
import ujson.Obj | ||
|
||
/** | ||
* Package represents a published library such as a Java artifact, or the JDK. | ||
* | ||
* @param id | ||
* unique representation for this package that does not include the forward | ||
* slash character. Can be used as the primary key in a relational database. | ||
* Should ideally be human-readable and be easy to parse. | ||
* @param path | ||
* relative URL of this package. | ||
*/ | ||
sealed abstract class Package( | ||
val id: String, | ||
val path: String, | ||
val version: String | ||
) { | ||
def toJsonRepo: Obj = Obj("Name" -> path, "URI" -> s"/repos/$path") | ||
def relativePath: Path = Paths.get(path) | ||
} | ||
object Package { | ||
def jdk(version: String): JdkPackage = { | ||
JdkPackage(version) | ||
} | ||
def maven(org: String, name: String, version: String): MavenPackage = { | ||
MavenPackage( | ||
Dependency( | ||
Module(Organization(org), ModuleName(name), Map.empty), | ||
version | ||
) | ||
) | ||
} | ||
def parse(value: String): Package = { | ||
value match { | ||
case s"jdk:$version" => | ||
JdkPackage(version) | ||
case s"maven:$library" => | ||
val Right(dep) = Dependencies.parseDependencyEither(library) | ||
MavenPackage(dep) | ||
} | ||
} | ||
def fromPath(path: List[String]): Option[(Package, List[String])] = | ||
path match { | ||
case "maven" :: org :: name :: version :: requestPath => | ||
Some(Package.maven(org, name, version) -> requestPath) | ||
case "jdk" :: version :: requestPath => | ||
Some(Package.jdk(version) -> requestPath) | ||
case _ => | ||
None | ||
} | ||
def fromString(value: String, coursier: String): Either[String, Package] = { | ||
value match { | ||
case s"jdk:$version" => | ||
val exit = os | ||
.proc(coursier, "java-home", "--jvm", version) | ||
.call(check = false) | ||
if (exit.exitCode == 0) | ||
Right(JdkPackage(version)) | ||
else | ||
Left(exit.out.trim()) | ||
case s"maven:$library" => | ||
Dependencies | ||
.parseDependencyEither(library) | ||
.flatMap { dep => | ||
try { | ||
// Report an error if the dependency can't be resolved. | ||
Dependencies.resolveProvidedDeps(dep) | ||
Right(MavenPackage(dep)) | ||
} catch { | ||
case NonFatal(e) => | ||
Left(e.getMessage()) | ||
} | ||
} | ||
case other => | ||
Left( | ||
s"unsupported package '$other'. To fix this problem, use a valid syntax " + | ||
s"such as 'maven:ORGANIZATION:ARTIFACT_NAME_VERSION' for Java libraries." | ||
) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A Java library that is published "Maven style". | ||
* | ||
* The most widely used Maven package host is "Maven Central" | ||
* https://search.maven.org/. Most companies self-host an Artifactory instance | ||
* to publish internal libraries and to proxy Maven Central. | ||
*/ | ||
case class MavenPackage(dep: Dependency) | ||
extends Package( | ||
s"maven:${dep.module.repr}:${dep.version}", | ||
s"maven/${dep.module.organization.value}/${dep.module.name.value}/${dep.version}", | ||
dep.version | ||
) { | ||
def repr = id.stripPrefix("maven:") | ||
} | ||
|
||
/** | ||
* The Java standard library. | ||
* | ||
* The sources of the Java standard library are typically available under | ||
* JAVA_HOME. | ||
*/ | ||
case class JdkPackage(override val version: String) | ||
extends Package(s"jdk:${version}", s"jdk/${version}", version) { | ||
def repr = id.stripPrefix("jdk:") | ||
} |
Oops, something went wrong.