public class GitHub extends java.lang.Object implements Credentials, Repositories, Releases, Publishing
| Constructor and Description |
|---|
GitHub(GitHubLogger logger,
ResourceSettings resourcesExtension,
GitHubConfig githubExtension)
Constructs a new GitHub helper instance.
|
| Modifier and Type | Method and Description |
|---|---|
java.lang.String |
apiKey() |
void |
cloneOrPull(java.io.File target,
java.lang.String owner,
java.lang.String repo,
java.lang.String branch)
Clones
owner/repo into target if no checkout exists there yet, otherwise
pulls the latest changes for branch (or the current branch, if null). |
void |
cloneOrPullFrom(java.io.File target,
java.lang.String cloneUrl,
java.lang.String branch)
Clones or pulls
cloneUrl into target, using the given URL directly rather
than reconstructing one from an owner and repo, so any git host is honoured exactly as
configured (github.com, GitHub Enterprise, or any other host). |
void |
cloneOrPullFromUrl(java.io.File target,
java.lang.String cloneUrl,
java.lang.String authUsername,
java.lang.String branch)
Clones or pulls
cloneUrl into target, using the given URL directly for a
fresh clone instead of deriving one via getRepositoryURL(java.lang.String, java.lang.String), so any git host works. |
void |
cloneOrPullRepository(java.io.File path)
Clones the configured resource repository if it doesn't exist, otherwise pulls the latest changes from the current branch.
|
void |
cloneOrPullRepository(java.io.File path,
java.lang.String branch)
Clones the configured resource repository if it doesn't exist, otherwise pulls the latest changes.
|
void |
cloneOrPullRepository(java.io.File path,
java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String branch)
Clones a repository if it doesn't exist, otherwise pulls the latest changes.
|
void |
cloneRepository(java.io.File path)
Clones the configured resource repository to the specified path.
|
void |
cloneRepository(java.io.File path,
java.lang.String repoOwner,
java.lang.String repoName)
Clones a GitHub repository to the specified path.
|
RemoteRepo |
configuredRepo()
The owner and repository parsed from the implementation's own configured repository URL.
|
com.google.gson.JsonObject |
createRelease(java.lang.String owner,
java.lang.String repo,
java.lang.String tagName,
java.lang.String releaseName)
Creates a GitHub release for the given tag, reusing the existing release if the tag already exists.
|
java.util.List<DeclaredDependency> |
declaredDependencies(java.io.File jar) |
boolean |
doesRepoExist(java.io.File path)
Checks if a Git repository exists at the specified path.
|
java.util.List<java.io.File> |
downloadAllModuleJars(java.lang.String owner,
java.lang.String repo,
java.lang.String version)
Downloads every module asset published under the reserved
:all classifier, so a
consumer of a multi-module release can pull every module jar without naming each one. |
void |
downloadAsset(java.io.File direction,
java.lang.Object asset,
java.lang.String repoOwner,
java.lang.String repoName)
Deprecated.
Use downloadAssetFromUrl instead
|
java.util.Optional<java.io.File> |
downloadJar(java.lang.String owner,
java.lang.String repo,
java.lang.String version)
Downloads the default release jar, matching
repo.jar, repo-version.jar,
repo-standalone.jar, or the first plain .jar asset in that order. |
java.util.Optional<java.io.File> |
downloadJar(java.lang.String owner,
java.lang.String repo,
java.lang.String version,
java.lang.String classifier)
Downloads a specific classifier asset, matching
repo-classifier.jar exactly. |
Release |
ensureRelease(java.lang.String owner,
java.lang.String repo,
java.lang.String tag,
java.lang.String name)
Creates a release for
tag, or returns the existing release if one already exists
for that tag. |
boolean |
exists(java.io.File path) |
com.google.gson.JsonObject |
fetchReleaseByTag(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version)
Attempts to fetch a GitHub release by tag, trying the given tag first and then
a "v"-prefixed or "v"-stripped variant as a fallback.
|
void |
getAllModuleAssets(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version,
java.util.List<java.io.File> collected)
Downloads every module asset from a multi-module release (all assets named
repoName-<classifier>.jar, excluding -sources.jar/-javadoc.jar). |
java.lang.String |
getApiKey()
Gets the GitHub token used for REST calls and HTTPS git operations, resolving it from the
auth extension (or the deprecated accessToken fallback). |
java.io.File |
getAsset(java.lang.String version)
Downloads and caches a release asset JAR file from the configured resource repository.
|
java.io.File |
getAsset(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version)
Downloads and caches a release asset JAR file from a GitHub repository.
|
java.io.File |
getAssetWithClassifier(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version,
java.lang.String classifier)
Downloads and caches a classifier-specific JAR asset from a GitHub release.
|
void |
getAssetWithTransitives(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version,
java.util.Set<java.lang.String> resolved,
java.util.List<java.io.File> collected)
Downloads a release asset JAR and recursively resolves its transitive GitHub dependencies.
|
org.eclipse.jgit.transport.CredentialsProvider |
getCredentialsProvider(java.lang.String repoOwner,
java.lang.String cloneUrl)
Creates a credentials provider for Git operations, scoped to github.com only.
|
com.google.gson.JsonObject |
getLatestRelease()
Fetches the latest release from the configured resource repository.
|
com.google.gson.JsonObject |
getLatestRelease(java.lang.String repoOwner,
java.lang.String repoName)
Fetches the latest release from a GitHub repository.
|
java.lang.String |
getLatestVersion()
Gets the latest version tag from the configured resource repository.
|
java.lang.String |
getLatestVersion(java.lang.String repoOwner,
java.lang.String repoName)
Gets the latest version tag from a GitHub repository.
|
java.lang.String[] |
getRemoteOwnerAndRepo(java.io.File projectDir)
Reads the git remote "origin" URL from the project directory and parses it
into
[owner, repo]. |
java.lang.String |
getRepositoryURL(java.lang.String repoOwner,
java.lang.String repoName)
Constructs the appropriate github.com Git repository URL based on authentication type.
|
java.lang.String |
getResourceRepoName()
Extracts the repository name from the configured repository URL.
|
java.lang.String |
getResourceRepoOwner()
Extracts the repository owner from the configured repository URL.
|
java.lang.String |
getSshKey()
Gets the SSH private key contents used for git transport, resolving it from
auth.sshKey
(or the deprecated accessToken fallback when that holds an SSH key). |
boolean |
isRepoUpToDate(java.io.File path)
Checks if the local repository is up-to-date with the remote.
|
boolean |
isUpToDate(java.io.File path) |
Release |
latestRelease(java.lang.String owner,
java.lang.String repo) |
java.lang.String |
latestVersion(java.lang.String owner,
java.lang.String repo) |
void |
pullRepository(java.io.File path)
Pulls the latest changes from the current branch of the remote repository.
|
void |
pullRepository(java.io.File path,
java.lang.String branch)
Pulls the latest changes from the remote repository.
|
java.util.List<java.lang.String[]> |
readGithubDependencies(java.io.File jar)
Reads the embedded github-dependencies metadata from a JAR file.
|
Release |
releaseByTag(java.lang.String owner,
java.lang.String repo,
java.lang.String tag) |
RemoteRepo |
remoteOf(java.io.File projectDir) |
java.util.List<java.io.File> |
resolveWithDependencies(java.lang.String owner,
java.lang.String repo,
java.lang.String version)
Resolves
owner:repo:version and its full transitive closure of GitHub-hosted
dependencies declared via each jar's embedded META-INF/github-dependencies.json,
including the root jar itself. |
com.google.gson.JsonObject |
selectJarAsset(com.google.gson.JsonArray assets,
java.lang.String repoName,
java.lang.String version)
Selects the best JAR asset from a release using a prioritized matching strategy:
(1) exact
repoName.jar, (2) repoName-version.jar,
(3) repoName-standalone.jar, (4) first .jar not ending in
-sources.jar or -javadoc.jar. |
java.lang.String |
sshKey() |
void |
uploadAsset(Release release,
java.io.File file,
java.lang.String assetName)
Uploads a file as an asset attached to
release, replacing an asset of the same name
if the release already carries one. |
void |
uploadReleaseAsset(java.lang.String uploadUrl,
java.io.File file,
java.lang.String assetName)
Uploads a file as a release asset to GitHub.
|
public GitHub(GitHubLogger logger, ResourceSettings resourcesExtension, GitHubConfig githubExtension)
logger - the logger instance for debug and error messagesresourcesExtension - the resources extension containing repository configurationgithubExtension - the github extension containing access token configurationpublic java.lang.String getApiKey()
auth extension (or the deprecated accessToken fallback). Cached after the
first resolution.public java.lang.String getSshKey()
auth.sshKey
(or the deprecated accessToken fallback when that holds an SSH key). Cached after the
first resolution.public java.lang.String getResourceRepoName()
public java.lang.String getResourceRepoOwner()
public org.eclipse.jgit.transport.CredentialsProvider getCredentialsProvider(java.lang.String repoOwner,
java.lang.String cloneUrl)
repoOwner - the repository owner for authenticationcloneUrl - the clone URL this credentials provider is for; the configured GitHub token
is offered only when this is a github.com URL, never to any other hostcloneUrl does not target github.compublic java.lang.String getRepositoryURL(java.lang.String repoOwner,
java.lang.String repoName)
repoOwner - the repository ownerrepoName - the repository nameGitHubSourceBuilds) can reuse this instance's SSH-vs-HTTPS decision
instead of duplicating it.public void cloneRepository(java.io.File path,
java.lang.String repoOwner,
java.lang.String repoName)
throws org.eclipse.jgit.api.errors.GitAPIException
path - the directory to clone the repository intorepoOwner - the repository ownerrepoName - the repository nameorg.eclipse.jgit.api.errors.GitAPIException - if the clone operation failspublic void cloneRepository(java.io.File path)
throws org.eclipse.jgit.api.errors.GitAPIException
path - the directory to clone the repository intoorg.eclipse.jgit.api.errors.GitAPIException - if the clone operation failspublic boolean doesRepoExist(java.io.File path)
path - the directory to checkpublic boolean isRepoUpToDate(java.io.File path)
path - the repository directorypublic void pullRepository(java.io.File path,
java.lang.String branch)
throws org.eclipse.jgit.api.errors.GitAPIException,
java.io.IOException
path - the repository directorybranch - the branch to pull, or null for the current branchorg.eclipse.jgit.api.errors.GitAPIException - if the pull operation failsjava.io.IOException - if an I/O error occurspublic void pullRepository(java.io.File path)
throws org.eclipse.jgit.api.errors.GitAPIException,
java.io.IOException
path - the repository directoryorg.eclipse.jgit.api.errors.GitAPIException - if the pull operation failsjava.io.IOException - if an I/O error occurspublic void cloneOrPullRepository(java.io.File path,
java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String branch)
throws org.eclipse.jgit.api.errors.GitAPIException,
java.io.IOException
path - the repository directoryrepoOwner - the repository ownerrepoName - the repository namebranch - the branch to pull, or null for the current branchorg.eclipse.jgit.api.errors.GitAPIException - if the clone or pull operation failsjava.io.IOException - if an I/O error occurspublic void cloneOrPullFromUrl(java.io.File target,
java.lang.String cloneUrl,
java.lang.String authUsername,
java.lang.String branch)
throws java.io.IOException
cloneUrl into target, using the given URL directly for a
fresh clone instead of deriving one via getRepositoryURL(java.lang.String, java.lang.String), so any git host works.
An existing checkout is updated through its own origin remote, which already
points at cloneUrl from a previous clone, so the URL is not needed again there.target - the repository directorycloneUrl - the exact URL to clone from on a fresh checkoutauthUsername - the username presented for HTTPS token auth on a fresh clone, and for
fetch/pull when this instance's resourcesExtension.repoUrl does not resolve onebranch - the branch to clone or pull, or null for the current/default branchjava.io.IOException - if the clone, fetch, or checkout operation failspublic void cloneOrPullRepository(java.io.File path,
java.lang.String branch)
throws org.eclipse.jgit.api.errors.GitAPIException,
java.io.IOException
path - the repository directorybranch - the branch to pull, or null for the current branchorg.eclipse.jgit.api.errors.GitAPIException - if the clone or pull operation failsjava.io.IOException - if an I/O error occurspublic void cloneOrPullRepository(java.io.File path)
throws org.eclipse.jgit.api.errors.GitAPIException,
java.io.IOException
path - the repository directoryorg.eclipse.jgit.api.errors.GitAPIException - if the clone or pull operation failsjava.io.IOException - if an I/O error occurspublic com.google.gson.JsonObject fetchReleaseByTag(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version)
repoOwner - the repository ownerrepoName - the repository nameversion - the release version tag as declared by the consumerjava.lang.RuntimeException - if neither tag variant resolves to a releasepublic com.google.gson.JsonObject selectJarAsset(com.google.gson.JsonArray assets,
java.lang.String repoName,
java.lang.String version)
repoName.jar, (2) repoName-version.jar,
(3) repoName-standalone.jar, (4) first .jar not ending in
-sources.jar or -javadoc.jar.assets - the release assets JSON arrayrepoName - the repository nameversion - the release version tagpublic java.io.File getAsset(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version)
repoOwner - the repository ownerrepoName - the repository nameversion - the release version tagpublic java.io.File getAsset(java.lang.String version)
version - the release version tagpublic java.util.List<java.lang.String[]> readGithubDependencies(java.io.File jar)
META-INF/github-dependencies.json and contains
a JSON array of objects with group, name, and version fields. This location is
safe from obfuscation tools (ProGuard, R8, etc.) which only process class files.jar - the JAR file to read metadata fromjar.public void getAssetWithTransitives(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version,
java.util.Set<java.lang.String> resolved,
java.util.List<java.io.File> collected)
META-INF/github-dependencies.json
metadata, and any listed dependencies are downloaded recursively. A resolved-set prevents
cycles and duplicate downloads.repoOwner - the repository ownerrepoName - the repository nameversion - the release version tagresolved - set of already-resolved dependency keys ("owner:name:version") for cycle detectioncollected - list that all resolved JAR files (including transitives) are added topublic java.io.File getAssetWithClassifier(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version,
java.lang.String classifier)
The expected asset name on the release is repoName-classifier.jar.
The file is cached under the same owner directory as getAsset(java.lang.String, java.lang.String, java.lang.String).
repoOwner - the repository ownerrepoName - the repository nameversion - the release version tagclassifier - the artifact classifier (e.g. "api", "fat")public void getAllModuleAssets(java.lang.String repoOwner,
java.lang.String repoName,
java.lang.String version,
java.util.List<java.io.File> collected)
repoName-<classifier>.jar, excluding -sources.jar/-javadoc.jar). Backs the
reserved :all classifier so a consumer can pull the whole library without listing each module.
Each jar is cached under the same owner directory as getAsset(java.lang.String, java.lang.String, java.lang.String).repoOwner - the repository ownerrepoName - the repository nameversion - the release version tagcollected - list that the downloaded module JAR files are added to@Deprecated
public void downloadAsset(java.io.File direction,
java.lang.Object asset,
java.lang.String repoOwner,
java.lang.String repoName)
direction - the destination fileasset - the GitHub asset object (no longer supported)repoOwner - the repository ownerrepoName - the repository namepublic com.google.gson.JsonObject getLatestRelease(java.lang.String repoOwner,
java.lang.String repoName)
repoOwner - the repository ownerrepoName - the repository namepublic com.google.gson.JsonObject getLatestRelease()
public java.lang.String getLatestVersion(java.lang.String repoOwner,
java.lang.String repoName)
repoOwner - the repository ownerrepoName - the repository namepublic java.lang.String getLatestVersion()
public java.lang.String[] getRemoteOwnerAndRepo(java.io.File projectDir)
[owner, repo]. Supports both HTTPS and SSH remote URLs.projectDir - a directory inside the Git repository{owner, repo}java.lang.RuntimeException - if no "origin" remote is configured or the URL cannot be parsedprojectDir, because a Gradle subproject directory
holds no .git of its own: only the build root does.public com.google.gson.JsonObject createRelease(java.lang.String owner,
java.lang.String repo,
java.lang.String tagName,
java.lang.String releaseName)
If a release for tagName already exists it is returned as-is so that
additional assets can still be uploaded to it without failing the build.
owner - the repository ownerrepo - the repository nametagName - the git tag for the release (GitHub auto-creates a lightweight tag if absent)releaseName - the human-readable release title; if null, defaults to tagNameupload_url)java.lang.RuntimeException - if auth fails or the API errorspublic void uploadReleaseAsset(java.lang.String uploadUrl,
java.io.File file,
java.lang.String assetName)
throws java.io.IOException
uploadUrl - the upload_url from the release object (URI template stripped automatically)file - the file to uploadassetName - the asset name as it will appear in the releasejava.io.IOException - if the upload failspublic java.lang.String apiKey()
apiKey in interface Credentialspublic java.lang.String sshKey()
sshKey in interface Credentialspublic void cloneOrPull(java.io.File target,
java.lang.String owner,
java.lang.String repo,
java.lang.String branch)
throws java.io.IOException
Repositoriesowner/repo into target if no checkout exists there yet, otherwise
pulls the latest changes for branch (or the current branch, if null).cloneOrPull in interface Repositoriestarget - the local directory to clone into or pull within.owner - the GitHub account or organization that owns the repository.repo - the repository name, without the owner prefix.branch - the branch to clone or pull, or null for the current/default branch.java.io.IOException - if the clone or pull fails.public void cloneOrPullFrom(java.io.File target,
java.lang.String cloneUrl,
java.lang.String branch)
throws java.io.IOException
RepositoriescloneUrl into target, using the given URL directly rather
than reconstructing one from an owner and repo, so any git host is honoured exactly as
configured (github.com, GitHub Enterprise, or any other host).cloneOrPullFrom in interface Repositoriestarget - the local directory to clone into or pull within.cloneUrl - the exact URL to clone from.branch - the branch to clone or pull, or null for the current/default branch.java.io.IOException - if the clone or pull fails.public boolean exists(java.io.File path)
exists in interface Repositoriespath - the directory to check.path.public boolean isUpToDate(java.io.File path)
isUpToDate in interface Repositoriespath - the checkout to check, whose own origin remote identifies the repository.path's current branch matches its remote counterpart, false if it
is behind or the check itself fails (e.g. no network access).public RemoteRepo remoteOf(java.io.File projectDir)
remoteOf in interface RepositoriesprojectDir - a directory inside the checkout whose origin remote is parsed; the
search walks up to the enclosing repository, so a Gradle subproject directory works.origin remote URL.public RemoteRepo configuredRepo()
RepositoriesconfiguredRepo in interface RepositoriesRemoteRepo with null fields if none is configured.public java.lang.String latestVersion(java.lang.String owner,
java.lang.String repo)
latestVersion in interface Releasesowner - the GitHub account or organization that owns the repository.repo - the repository name, without the owner prefix.public Release releaseByTag(java.lang.String owner, java.lang.String repo, java.lang.String tag)
releaseByTag in interface Releasesowner - the GitHub account or organization that owns the repository.repo - the repository name, without the owner prefix.tag - the release tag to resolve (a "v" prefix is tried both with and without).tag.public Release latestRelease(java.lang.String owner, java.lang.String repo)
latestRelease in interface Releasesowner - the GitHub account or organization that owns the repository.repo - the repository name, without the owner prefix.public java.util.Optional<java.io.File> downloadJar(java.lang.String owner,
java.lang.String repo,
java.lang.String version)
Releasesrepo.jar, repo-version.jar,
repo-standalone.jar, or the first plain .jar asset in that order.downloadJar in interface Releasesowner - the GitHub account or organization that owns the repository.repo - the repository name, without the owner prefix.version - the release tag to resolve (a "v" prefix is tried both with and without).Optional if the release
exists but has no matching jar asset. A release that does not exist at all is a different
kind of absence and is never represented this way (see @throws below): the two are
not the same thing, since a missing release is almost always a caller mistake (a typo'd
version, a deleted or renamed tag) while a missing asset within a release that does exist is
a normal outcome.public java.util.Optional<java.io.File> downloadJar(java.lang.String owner,
java.lang.String repo,
java.lang.String version,
java.lang.String classifier)
Releasesrepo-classifier.jar exactly.downloadJar in interface Releasesowner - the GitHub account or organization that owns the repository.repo - the repository name, without the owner prefix.version - the release tag to resolve (a "v" prefix is tried both with and without).classifier - the artifact classifier identifying the asset (e.g. "api").Optional if the release exists but
has no asset named repo-classifier.jar. As with the 3-argument overload above, a
release that does not exist at all is a different kind of absence and is never represented
this way.public java.util.List<java.io.File> downloadAllModuleJars(java.lang.String owner,
java.lang.String repo,
java.lang.String version)
Releases:all classifier, so a
consumer of a multi-module release can pull every module jar without naming each one.downloadAllModuleJars in interface Releasesowner - the GitHub account or organization that owns the repository.repo - the repository name, without the owner prefix.version - the release tag to resolve (a "v" prefix is tried both with and without).public java.util.List<java.io.File> resolveWithDependencies(java.lang.String owner,
java.lang.String repo,
java.lang.String version)
Releasesowner:repo:version and its full transitive closure of GitHub-hosted
dependencies declared via each jar's embedded META-INF/github-dependencies.json,
including the root jar itself.
Cycle detection is local to a single call. A caller resolving many independent coordinates that may share transitive dependencies (for example, one call per declared dependency across several build configurations) must deduplicate the combined results itself if it wants each distinct jar added only once.
resolveWithDependencies in interface Releasesowner - the GitHub account or organization that owns the root repository.repo - the root repository name, without the owner prefix.version - the release tag to resolve (a "v" prefix is tried both with and without).public java.util.List<DeclaredDependency> declaredDependencies(java.io.File jar)
declaredDependencies in interface Releasesjar - the jar file to inspect; not modified.jar's embedded
META-INF/github-dependencies.json, or an empty list if the jar has no such entry,
or if the entry exists but is unreadable or malformed. The unreadable case logs a warning
naming jar so a corrupt artifact is not silently treated as having no dependencies.public Release ensureRelease(java.lang.String owner, java.lang.String repo, java.lang.String tag, java.lang.String name)
Publishingtag, or returns the existing release if one already exists
for that tag.ensureRelease in interface Publishingowner - the GitHub account or organization that owns the repository.repo - the repository name, without the owner prefix.tag - the git tag for the release (GitHub auto-creates a lightweight tag if absent).name - the human-readable release title; if null, defaults to tag.public void uploadAsset(Release release, java.io.File file, java.lang.String assetName) throws java.io.IOException
Publishingrelease, replacing an asset of the same name
if the release already carries one.uploadAsset in interface Publishingrelease - the release to attach the asset to, as returned by Publishing.ensureRelease(java.lang.String, java.lang.String, java.lang.String, java.lang.String).file - the file to upload.assetName - the asset name as it will appear in the release.java.io.IOException - if the upload request fails, or if an existing asset of that name could
not be removed first.