Class GitHub
java.lang.Object
io.github.intisy.gradle.github.impl.GitHub
GitHub helper class for managing GitHub repositories, releases, and assets.
Provides methods for cloning, pulling, and fetching repository information.
-
Constructor Summary
ConstructorsConstructorDescriptionGitHub(Logger logger, ResourcesExtension resourcesExtension, GithubExtension githubExtension) Constructs a new GitHub helper instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidcloneOrPullRepository(File path) Clones the configured resource repository if it doesn't exist, otherwise pulls the latest changes from the current branch.voidcloneOrPullRepository(File path, String branch) Clones the configured resource repository if it doesn't exist, otherwise pulls the latest changes.voidcloneOrPullRepository(File path, String repoOwner, String repoName, String branch) Clones a repository if it doesn't exist, otherwise pulls the latest changes.voidcloneRepository(File path) Clones the configured resource repository to the specified path.voidcloneRepository(File path, String repoOwner, String repoName) Clones a GitHub repository to the specified path.com.google.gson.JsonObjectcreateRelease(String owner, String repo, String tagName) Creates a GitHub release for the given tag.booleandoesRepoExist(File path) Checks if a Git repository exists at the specified path.voiddownloadAsset(File direction, Object asset, String repoOwner, String repoName) Deprecated.Use downloadAssetFromUrl insteadcom.google.gson.JsonObjectfetchReleaseByTag(String repoOwner, String repoName, 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.Gets the GitHub API key, resolving it from a file if necessary.Downloads and caches a release asset JAR file from the configured resource repository.Downloads and caches a release asset JAR file from a GitHub repository.voidgetAssetWithTransitives(String repoOwner, String repoName, String version, Set<String> resolved, List<File> collected) Downloads a release asset JAR and recursively resolves its transitive GitHub dependencies.org.eclipse.jgit.transport.CredentialsProvidergetCredentialsProvider(String repoOwner) Creates a credentials provider for Git operations.com.google.gson.JsonObjectFetches the latest release from the configured resource repository.com.google.gson.JsonObjectgetLatestRelease(String repoOwner, String repoName) Fetches the latest release from a GitHub repository.Gets the latest version tag from the configured resource repository.getLatestVersion(String repoOwner, String repoName) Gets the latest version tag from a GitHub repository.String[]getRemoteOwnerAndRepo(File projectDir) Reads the git remote "origin" URL from the project directory and parses it into[owner, repo].Extracts the repository name from the configured repository URL.Extracts the repository owner from the configured repository URL.booleanisRepoUpToDate(File path) Checks if the local repository is up-to-date with the remote.voidpullRepository(File path) Pulls the latest changes from the current branch of the remote repository.voidpullRepository(File path, String branch) Pulls the latest changes from the remote repository.Reads the embedded github-dependencies metadata from a JAR file.com.google.gson.JsonObjectselectJarAsset(com.google.gson.JsonArray assets, String repoName, String version) Selects the best JAR asset from a release using a prioritized matching strategy: (1) exactrepoName.jar, (2)repoName-version.jar, (3)repoName-standalone.jar, (4) first.jarnot ending in-sources.jaror-javadoc.jar.voiduploadReleaseAsset(String uploadUrl, File file, String assetName) Uploads a file as a release asset to GitHub.
-
Constructor Details
-
GitHub
public GitHub(Logger logger, ResourcesExtension resourcesExtension, GithubExtension githubExtension) Constructs a new GitHub helper instance.- Parameters:
logger- the logger instance for debug and error messagesresourcesExtension- the resources extension containing repository configurationgithubExtension- the github extension containing access token configuration
-
-
Method Details
-
getApiKey
Gets the GitHub API key, resolving it from a file if necessary. The key is cached after the first resolution.- Returns:
- the resolved API key, or null if not configured
-
getResourceRepoName
Extracts the repository name from the configured repository URL.- Returns:
- the repository name, or null if not configured
-
getResourceRepoOwner
Extracts the repository owner from the configured repository URL.- Returns:
- the repository owner, or null if not configured
-
getCredentialsProvider
Creates a credentials provider for Git operations.- Parameters:
repoOwner- the repository owner for authentication- Returns:
- the credentials provider, or null if SSH authentication is used
-
cloneRepository
public void cloneRepository(File path, String repoOwner, String repoName) throws org.eclipse.jgit.api.errors.GitAPIException Clones a GitHub repository to the specified path.- Parameters:
path- the directory to clone the repository intorepoOwner- the repository ownerrepoName- the repository name- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if the clone operation fails
-
cloneRepository
Clones the configured resource repository to the specified path.- Parameters:
path- the directory to clone the repository into- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if the clone operation fails
-
doesRepoExist
Checks if a Git repository exists at the specified path.- Parameters:
path- the directory to check- Returns:
- true if a repository exists, false otherwise
-
isRepoUpToDate
Checks if the local repository is up-to-date with the remote.- Parameters:
path- the repository directory- Returns:
- true if up-to-date, false otherwise
-
pullRepository
public void pullRepository(File path, String branch) throws org.eclipse.jgit.api.errors.GitAPIException, IOException Pulls the latest changes from the remote repository.- Parameters:
path- the repository directorybranch- the branch to pull, or null for the current branch- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if the pull operation failsIOException- if an I/O error occurs
-
pullRepository
public void pullRepository(File path) throws org.eclipse.jgit.api.errors.GitAPIException, IOException Pulls the latest changes from the current branch of the remote repository.- Parameters:
path- the repository directory- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if the pull operation failsIOException- if an I/O error occurs
-
cloneOrPullRepository
public void cloneOrPullRepository(File path, String repoOwner, String repoName, String branch) throws org.eclipse.jgit.api.errors.GitAPIException, IOException Clones a repository if it doesn't exist, otherwise pulls the latest changes.- Parameters:
path- the repository directoryrepoOwner- the repository ownerrepoName- the repository namebranch- the branch to pull, or null for the current branch- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if the clone or pull operation failsIOException- if an I/O error occurs
-
cloneOrPullRepository
public void cloneOrPullRepository(File path, String branch) throws org.eclipse.jgit.api.errors.GitAPIException, IOException Clones the configured resource repository if it doesn't exist, otherwise pulls the latest changes.- Parameters:
path- the repository directorybranch- the branch to pull, or null for the current branch- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if the clone or pull operation failsIOException- if an I/O error occurs
-
cloneOrPullRepository
public void cloneOrPullRepository(File path) throws org.eclipse.jgit.api.errors.GitAPIException, IOException Clones the configured resource repository if it doesn't exist, otherwise pulls the latest changes from the current branch.- Parameters:
path- the repository directory- Throws:
org.eclipse.jgit.api.errors.GitAPIException- if the clone or pull operation failsIOException- if an I/O error occurs
-
fetchReleaseByTag
public com.google.gson.JsonObject fetchReleaseByTag(String repoOwner, String repoName, 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.- Parameters:
repoOwner- the repository ownerrepoName- the repository nameversion- the release version tag as declared by the consumer- Returns:
- the parsed release JSON object
- Throws:
RuntimeException- if neither tag variant resolves to a release
-
selectJarAsset
public com.google.gson.JsonObject selectJarAsset(com.google.gson.JsonArray assets, String repoName, String version) Selects the best JAR asset from a release using a prioritized matching strategy: (1) exactrepoName.jar, (2)repoName-version.jar, (3)repoName-standalone.jar, (4) first.jarnot ending in-sources.jaror-javadoc.jar.- Parameters:
assets- the release assets JSON arrayrepoName- the repository nameversion- the release version tag- Returns:
- the selected asset JSON object, or null if no suitable JAR found
-
getAsset
Downloads and caches a release asset JAR file from a GitHub repository.- Parameters:
repoOwner- the repository ownerrepoName- the repository nameversion- the release version tag- Returns:
- the downloaded JAR file
-
getAsset
Downloads and caches a release asset JAR file from the configured resource repository.- Parameters:
version- the release version tag- Returns:
- the downloaded JAR file
-
readGithubDependencies
Reads the embedded github-dependencies metadata from a JAR file. The metadata is stored atMETA-INF/github-dependencies.jsonand 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.- Parameters:
jar- the JAR file to read metadata from- Returns:
- a list of dependency entries as [group, name, version] arrays, empty if no metadata found
-
getAssetWithTransitives
public void getAssetWithTransitives(String repoOwner, String repoName, String version, Set<String> resolved, List<File> collected) Downloads a release asset JAR and recursively resolves its transitive GitHub dependencies. Each dependency's JAR is inspected for embeddedMETA-INF/github-dependencies.jsonmetadata, and any listed dependencies are downloaded recursively. A resolved-set prevents cycles and duplicate downloads.- Parameters:
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 to
-
downloadAsset
@Deprecated public void downloadAsset(File direction, Object asset, String repoOwner, String repoName) Deprecated.Use downloadAssetFromUrl insteadDownloads a GitHub release asset to the specified file location.- Parameters:
direction- the destination fileasset- the GitHub asset object (no longer supported)repoOwner- the repository ownerrepoName- the repository name
-
getLatestRelease
Fetches the latest release from a GitHub repository.- Parameters:
repoOwner- the repository ownerrepoName- the repository name- Returns:
- JSON object representing the latest release, or null if no releases exist
-
getLatestRelease
public com.google.gson.JsonObject getLatestRelease()Fetches the latest release from the configured resource repository.- Returns:
- JSON object representing the latest release, or null if no releases exist
-
getLatestVersion
Gets the latest version tag from a GitHub repository.- Parameters:
repoOwner- the repository ownerrepoName- the repository name- Returns:
- the latest version tag, or null if no releases exist
-
getLatestVersion
Gets the latest version tag from the configured resource repository.- Returns:
- the latest version tag, or null if no releases exist
-
getRemoteOwnerAndRepo
Reads the git remote "origin" URL from the project directory and parses it into[owner, repo]. Supports both HTTPS and SSH remote URLs.- Parameters:
projectDir- the root directory of the Git repository- Returns:
- a two-element array
{owner, repo} - Throws:
RuntimeException- if no "origin" remote is configured or the URL cannot be parsed
-
createRelease
Creates a GitHub release for the given tag.- Parameters:
owner- the repository ownerrepo- the repository nametagName- the tag to create the release for (GitHub auto-creates a lightweight tag if absent)- Returns:
- the created release JSON object (contains
upload_url) - Throws:
RuntimeException- if the release already exists (422), auth fails, or the API errors
-
uploadReleaseAsset
Uploads a file as a release asset to GitHub.- Parameters:
uploadUrl- theupload_urlfrom the release object (URI template stripped automatically)file- the file to uploadassetName- the asset name as it will appear in the release- Throws:
IOException- if the upload fails
-