Class GitHubAccessor
java.lang.Object
com.atlassian.bamboo.plugins.github.accessor.GitHubAccessor
- Direct Known Subclasses:
GitHubAccessorWithBasicAuth,GitHubAccessorWithTokenAuth
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static classstatic enumstatic final recordprotected static interface -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionGitHubAccessor(com.atlassian.plugins.whitelist.OutboundWhitelist outboundWhitelist, String apiBaseUrl) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidbooleancommentOnCommit(String commitId, VcsRepositoryData repositoryData, String message) Adds a comment to a commit.createWebhook(@NotNull String owner, @NotNull String repo, @NotNull String bambooEntrypoint) Creates a webhook for the specified repository.protected GitHubAccessor.HttpResponsevoiddestroyWebhook(@NotNull String owner, @NotNull String repo, @NotNull String webhookId) Destroys a webhook for the specified repository.foldArrayAndSort(org.json.JSONArray repoJson, String field) foldIntegerArrayAndSort(org.json.JSONArray repoJson, String field) Retrieves a list of accessible repositories.getBranches(String userRepo) Retrieves a list of branches for the specified repository.getCommits(String owner, String repo) protected <M extends org.apache.http.client.methods.HttpRequestBase>
Pair<org.json.JSONArray, Multimap<String, String>> getJSONArrayResponseFromUrl(@NotNull String url, @NotNull Consumer<RESTCall<M>> authenticationProvider, @NotNull Optional<String> nestedArrayFieldToAccess) protected <T,M extends org.apache.http.client.methods.HttpRequestBase>
List<T> getPageableResults(@NotNull GitHubAccessor.ParserFunction<org.json.JSONArray, List<T>> parser, @NotNull String endpoint, @NotNull Consumer<RESTCall<M>> authenticationProvider, @NotNull Optional<String> nestedArrayField) getPullRequests(VcsRepositoryData repositoryData) Retrieves a list of pull requests for the specified repository.protected <M extends org.apache.http.client.methods.HttpRequestBase>
GitHubAccessor.HttpResponsegetStringFromUrl(@NotNull String url, @NotNull Consumer<RESTCall<M>> authenticationProvider) Get a string from a given URL.protected GitHubAccessor.HttpResponsegetStringFromUrl(String url) getWebhooks(@NotNull String owner, @NotNull String repo) Retrieves a list of webhooks for the specified repository.protected GitHubAccessor.HttpResponsestatic StringsanitizeBaseUrl(@Nullable String apiBaseUrl) Sanitizes the provided base URL by ensuring it's properly formatted as a GitHub API URL.booleansendBuildStatus(String commitId, VcsRepositoryData repositoryData, Map<String, String> payload) Sends a build status for a commit.protected abstract <M extends org.apache.http.client.methods.HttpRequestBase>
voidsetAuthentication(@NotNull RESTCall<M> restCall) Sets the authentication details for the given REST call.
-
Field Details
-
GITHUB_API_BASE_URL
- See Also:
-
MAX_PAGE_SIZE
public static final int MAX_PAGE_SIZEA maximum 'per_page' allowed by GitHub API. Greater values will be ignored and results will be returned in bulks of 100s anyway.See: https://developer.github.com/v3/#pagination
- See Also:
-
outboundWhitelist
protected final com.atlassian.plugins.whitelist.OutboundWhitelist outboundWhitelist -
apiBaseUrl
-
-
Constructor Details
-
GitHubAccessor
public GitHubAccessor(com.atlassian.plugins.whitelist.OutboundWhitelist outboundWhitelist, String apiBaseUrl)
-
-
Method Details
-
getAccessibleRepositories
Retrieves a list of accessible repositories.- Returns:
- an iterable collection of accessible repository names
- Throws:
IOException- if an I/O error occursGitHubException- if a GitHub-specific error occurs
-
getBranches
Retrieves a list of branches for the specified repository.- Parameters:
userRepo- the user repository in the format "owner/repo"- Returns:
- an iterable collection of branch names
- Throws:
IOException- if an I/O error occursGitHubException- if a GitHub-specific error occurs
-
getWebhooks
public Iterable<GithubWebhookDto> getWebhooks(@NotNull @NotNull String owner, @NotNull @NotNull String repo) throws IOException, GitHubException Retrieves a list of webhooks for the specified repository.- Parameters:
owner- the owner of the repositoryrepo- the name of the repository- Returns:
- an iterable collection of GitHub webhook DTOs
- Throws:
IOException- if an I/O error occursGitHubException- if a GitHub-specific error occurs
-
createWebhook
public GithubWebhookDto createWebhook(@NotNull @NotNull String owner, @NotNull @NotNull String repo, @NotNull @NotNull String bambooEntrypoint) throws GitHubException Creates a webhook for the specified repository.- Parameters:
owner- the owner of the repositoryrepo- the name of the repositorybambooEntrypoint- the Bamboo entry point URL- Returns:
- the created GitHub webhook DTO
- Throws:
GitHubException- if a GitHub-specific error occurs
-
destroyWebhook
public void destroyWebhook(@NotNull @NotNull String owner, @NotNull @NotNull String repo, @NotNull @NotNull String webhookId) throws GitHubException Destroys a webhook for the specified repository.- Parameters:
owner- the owner of the repositoryrepo- the name of the repositorywebhookId- the ID of the webhook to be destroyed- Throws:
GitHubException- if a GitHub-specific error occurs
-
commentOnCommit
public boolean commentOnCommit(String commitId, VcsRepositoryData repositoryData, String message) throws RepositoryException, GitHubException Adds a comment to a commit.- Parameters:
commitId- the ID of the commitrepositoryData- the repository datamessage- the comment message- Returns:
- true if the comment was successfully added, false otherwise
- Throws:
RepositoryException- if a repository-specific error occursGitHubException- if a GitHub-specific error occurs
-
sendBuildStatus
public boolean sendBuildStatus(String commitId, VcsRepositoryData repositoryData, Map<String, String> payload) throws RepositoryException, GitHubExceptionSends a build status for a commit.- Parameters:
commitId- the ID of the commitrepositoryData- the repository datapayload- the payload containing the build status- Returns:
- true if the build status was successfully sent, false otherwise
- Throws:
RepositoryException- if a repository-specific error occursGitHubException- if a GitHub-specific error occurs
-
getPullRequests
public List<GithubPullRequestDto> getPullRequests(VcsRepositoryData repositoryData) throws IOException, GitHubException Retrieves a list of pull requests for the specified repository.- Parameters:
repositoryData- the repository data- Returns:
- a list of GitHub pull request DTOs
- Throws:
IOException- if an I/O error occursGitHubException- if a GitHub-specific error occurs
-
getCommits
- Throws:
IOExceptionGitHubException
-
setAuthentication
protected abstract <M extends org.apache.http.client.methods.HttpRequestBase> void setAuthentication(@NotNull @NotNull RESTCall<M> restCall) Sets the authentication details for the given REST call.- Type Parameters:
M- the type of the HTTP request- Parameters:
restCall- the REST call to set the authentication details for
-
getPageableResults
protected <T,M extends org.apache.http.client.methods.HttpRequestBase> List<T> getPageableResults(@NotNull @NotNull GitHubAccessor.ParserFunction<org.json.JSONArray, List<T>> parser, @NotNull @NotNull String endpoint, @NotNull @NotNull Consumer<RESTCall<M>> authenticationProvider, @NotNull @NotNull Optional<String> nestedArrayField) throws GitHubException, IOException- Throws:
GitHubExceptionIOException
-
checkIfBaseUrlIsAllowed
- Throws:
GitHubException
-
foldArrayAndSort
protected List<String> foldArrayAndSort(org.json.JSONArray repoJson, String field) throws GitHubException - Throws:
GitHubException
-
foldIntegerArrayAndSort
protected List<Integer> foldIntegerArrayAndSort(org.json.JSONArray repoJson, String field) throws GitHubException - Throws:
GitHubException
-
getJSONArrayResponseFromUrl
protected <M extends org.apache.http.client.methods.HttpRequestBase> Pair<org.json.JSONArray,Multimap<String, getJSONArrayResponseFromUrlString>> (@NotNull @NotNull String url, @NotNull @NotNull Consumer<RESTCall<M>> authenticationProvider, @NotNull @NotNull Optional<String> nestedArrayFieldToAccess) throws IOException, GitHubException - Throws:
IOExceptionGitHubException
-
getStringFromUrl
protected <M extends org.apache.http.client.methods.HttpRequestBase> GitHubAccessor.HttpResponse getStringFromUrl(@NotNull @NotNull String url, @NotNull @NotNull Consumer<RESTCall<M>> authenticationProvider) throws IOException Get a string from a given URL.- Parameters:
url- url to GET from.- Returns:
- a Pair of String (response body) and a map of response headers.
- Throws:
IOException- if a problem occurred in the IO layer.
-
getStringFromUrl
- Throws:
IOException
-
sanitizeBaseUrl
Sanitizes the provided base URL by ensuring it's properly formatted as a GitHub API URL.This method:
- First removes any query parameters and URL fragments using
UrlUtils.stripQueryAndRef(java.lang.String) - Then transforms the clean URL to the appropriate GitHub API format
For GitHub URLs:
- If the input is null, empty, or a github.com URL, returns
https://api.github.com/ - For enterprise GitHub instances, appends "/api/v3/" to the base URL
- Parameters:
apiBaseUrl- the URL to sanitize, can be null- Returns:
- a sanitized GitHub API URL without query parameters or fragments
- First removes any query parameters and URL fragments using
-
post
protected GitHubAccessor.HttpResponse post(@NotNull @NotNull String url, @NotNull @NotNull String payload, @Nullable @Nullable Multimap<String, String> headers) throws IOException- Throws:
IOException
-
delete
protected GitHubAccessor.HttpResponse delete(@NotNull @NotNull String url, @Nullable @Nullable Multimap<String, String> headers) throws IOException- Throws:
IOException
-