Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriBuilder;

import java.text.MessageFormat;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

@Service
public class ActiveWorkflowRunsAdapter implements ActiveWorkflowRunsQueryPort, ScheduledCacheEvictionPort {
Expand Down Expand Up @@ -47,26 +49,14 @@ public ActiveWorkflowRunsAdapter(
this.rateLimitState = rateLimitState;
}

private String getWorkflowRunsApiPath(String repoName) {
return MessageFormat.format(
"repos/{0}/{1}/actions/runs",
this.githubProperties.org(),
repoName
);
}

@Override
@Cacheable(ACTIVE_WORKFLOW_RUNS_CACHE_NAME)
public List<WorkflowRun> getActiveWorkflowRuns(Repository repository) {
LOGGER.info("Fetching fresh Active WorkflowRuns for Repository {}.", repository.getId());
var parameters = new HashMap<String, String>();
parameters.put("per_page", "100");

var workflowRuns = Objects.requireNonNull(this.restClient.get()
.uri(utilities.setPathAndParameters(
this.getWorkflowRunsApiPath(repository.getName()),
parameters
))
.uri(workflowRunsUri(repository))
.header("path", GHActionRuns.PATH)
.retrieve()
.body(GHActionRuns.class)).getActiveWorkflowRuns(repository);

Expand All @@ -78,6 +68,20 @@ public List<WorkflowRun> getActiveWorkflowRuns(Repository repository) {
return workflowRuns;
}

private Function<UriBuilder, URI> workflowRunsUri(Repository repository) {
var parameters = new HashMap<String, String>();
parameters.put("per_page", "100");

List<Object> pathVars = List.of(
this.githubProperties.org(),
repository.getName()
);
return utilities.setPathAndParameters(
GHActionRuns.PATH,
pathVars, parameters
);
}

@Override
public boolean freshDataCanWait() {
return this.rateLimitState.shouldDataWait(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,33 @@ public class GithubApiUtilities {
private static final Logger LOGGER = LoggerFactory.getLogger(GithubApiUtilities.class);
private final RestClient restClient;

public GithubApiUtilities(@Qualifier("defaultRestClient") RestClient restClient) {
public GithubApiUtilities(
@Qualifier("defaultRestClient") RestClient restClient
) {
this.restClient = restClient;
}

public Function<UriBuilder, URI> setPathAndParameters(
String path,
List<Object> uriVariables
) {
return uriBuilder ->
uriBuilder.path(path)
.build(uriVariables.toArray(new Object[0]));
}

public Function<UriBuilder, URI> setPathAndParameters(
String path,
List<Object> uriVariables,
Map<String, String> parameters
) {
return uriBuilder -> {
uriBuilder.path(path);
parameters.forEach(uriBuilder::queryParam);
return uriBuilder.build(uriVariables.toArray(new Object[0]));
};
}

public Function<UriBuilder, URI> setPathAndParameters(
String path,
Map<String, String> parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriBuilder;

import java.text.MessageFormat;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.function.Function;

@Component
public class JobsAdapter implements JobsQueryPort, ScheduledCacheEvictionPort {
Expand All @@ -45,15 +47,6 @@ public JobsAdapter(
this.rateLimitState = rateLimitState;
}

private String getJobsApiPath(String repoName, long workflowRunId) {
return MessageFormat.format(
"repos/{0}/{1}/actions/runs/{2,number,#}/jobs",
this.githubProperties.org(),
repoName,
workflowRunId
);
}

@Cacheable(JOBS_CACHE_NAME)
@Override
public List<Job> getAllJobsForWorkflowRun(WorkflowRun workflowRun) {
Expand All @@ -62,14 +55,9 @@ public List<Job> getAllJobsForWorkflowRun(WorkflowRun workflowRun) {
workflowRun.getId(), workflowRun.getName()
);

var parameters = new HashMap<String, String>();
parameters.put("per_page", "100");

ResponseEntity<GHWorkflowRunJobs> responseEntity = this.restClient.get()
.uri(utilities.setPathAndParameters(
getJobsApiPath(workflowRun.getRepository().getName(), workflowRun.getId()),
parameters
))
.uri(jobsUri(workflowRun))
.header("path", GHWorkflowRunJobs.PATH)
.retrieve()
.toEntity(GHWorkflowRunJobs.class);

Expand All @@ -87,6 +75,22 @@ public List<Job> getAllJobsForWorkflowRun(WorkflowRun workflowRun) {
return jobs;
}

private Function<UriBuilder, URI> jobsUri(WorkflowRun workflowRun) {
var parameters = new HashMap<String, String>();
parameters.put("per_page", "100");

List<Object> pathVars = List.of(
this.githubProperties.org(),
workflowRun.getRepository().getName(),
workflowRun.getId()
);

return utilities.setPathAndParameters(
GHWorkflowRunJobs.PATH,
pathVars, parameters
);
}

@Override
public boolean freshDataCanWait() {
return this.rateLimitState.shouldDataWait(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriBuilder;

import java.text.MessageFormat;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.function.Function;

@Service
public class PullRequestsAdapter implements PullRequestQueryPort, ScheduledCacheEvictionPort {
Expand All @@ -46,30 +48,17 @@ public PullRequestsAdapter(
this.rateLimitState = rateLimitState;
}

private String getPullRequestsApiPath(String repoName) {
return MessageFormat.format(
"/repos/{0}/{1}/pulls",
this.githubProperties.org(),
repoName
);
}

@Cacheable(PULL_REQUESTS_CACHE_NAME)
@Override
public List<PullRequest> getAllPullRequestsForRepository(Repository repository) {
LOGGER.debug(
"Fetching fresh PullRequests for Repository {} {}",
repository.getId(), repository.getName()
);
var parameters = new HashMap<String, String>();
parameters.put("state", "all");
parameters.put("per_page", "100");

ResponseEntity<GHPullRequest[]> responseEntity = this.restClient.get()
.uri(utilities.setPathAndParameters(
this.getPullRequestsApiPath(repository.getName()),
parameters
))
.uri(pullRequestsUri(repository))
.header("path", GHPullRequest.PATH_ALL)
.retrieve()
.toEntity(GHPullRequest[].class);

Expand All @@ -88,6 +77,20 @@ public List<PullRequest> getAllPullRequestsForRepository(Repository repository)
return pullRequests;
}

private Function<UriBuilder, URI> pullRequestsUri(Repository repository) {
List<Object> pathVars = List.of(
this.githubProperties.org(), repository.getName()
);
var parameters = new HashMap<String, String>();
parameters.put("state", "all");
parameters.put("per_page", "100");

return utilities.setPathAndParameters(
GHPullRequest.PATH_ALL,
pathVars, parameters
);
}

@Override
public boolean freshDataCanWait() {
return this.rateLimitState.shouldDataWait(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
@Service
public class RepositoriesAdapter implements RepositoriesQueryPort, ScheduledCacheEvictionPort {
private static final String REPOSITORIES_CACHE_NAME = "Repositories";

private static final Logger LOGGER = LoggerFactory.getLogger(RepositoriesAdapter.class);

private final RestClient restClient;
Expand Down Expand Up @@ -52,9 +53,10 @@ public List<Repository> getAllRepositories() {

ResponseEntity<GHRepositories> responseEntity = this.restClient.get()
.uri(utilities.setPathAndParameters(
"/installation/repositories",
GHRepositories.PATH,
parameters
))
.header("path", GHRepositories.PATH)
.retrieve()
.toEntity(GHRepositories.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Stream;
Expand Down Expand Up @@ -56,10 +55,10 @@ public List<SelfHostedRunner> getAllSelfHostedRunners(List<Repository> repositor

private List<SelfHostedRunner> getForOrganization() {
LOGGER.debug("Fetching fresh SelfHostedRunners for Organization.");
var selfHostedRunners = this.fetchSelfHostedRunners(MessageFormat.format(
"orgs/{0}/actions/runners",
this.githubProperties.org()
));
var selfHostedRunners = this.fetchSelfHostedRunners(
GHSelfHostedRunners.PATH_ORG,
List.of(this.githubProperties.org())
);
LOGGER.debug(
"Response for the SelfHostedRunner fetch of Organization returned {} SelfHostedRunners.",
selfHostedRunners.size()
Expand All @@ -74,11 +73,10 @@ private List<SelfHostedRunner> getForRepositories(List<Repository> repositories)

private List<SelfHostedRunner> getForRepository(Repository repository) {
LOGGER.debug("Fetching fresh SelfHostedRunners for Repository {}.", repository.getName());
var selfHostedRunners = this.fetchSelfHostedRunners(MessageFormat.format(
"repos/{0}/{1}/actions/runners",
this.githubProperties.org(),
repository.getName()
));
var selfHostedRunners = this.fetchSelfHostedRunners(
GHSelfHostedRunners.PATH_REPO,
List.of(this.githubProperties.org(), repository.getName())
);
LOGGER.debug(
"Response for the SelfHostedRunner fetch of Repository {} returned {} SelfHostedRunners.",
repository.getName(),
Expand All @@ -89,14 +87,16 @@ private List<SelfHostedRunner> getForRepository(Repository repository) {
}

private List<SelfHostedRunner> fetchSelfHostedRunners(
String link
String link, List<Object> pathVars
) {
var parameters = new HashMap<String, String>();
parameters.put("per_page", "100");

ResponseEntity<GHSelfHostedRunners> responseEntity = this.restClient.get()
.uri(utilities.setPathAndParameters(
link, parameters
link, pathVars, parameters
))
.header("path", link)
.retrieve()
.toEntity(GHSelfHostedRunners.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,36 @@
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriBuilder;

import java.text.MessageFormat;
import java.net.URI;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

@Service
public class WorkflowRunBuildTimesAdapter implements WorkflowRunBuildTimesQueryPort, ScheduledCacheEvictionPort {

private static final String WORKFLOW_RUN_BUILD_TIMES_CACHE_NAME = "WorkflowRunBuildTimes";
private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowRunBuildTimesAdapter.class);
private final GithubProperties githubProperties;
private final RestClient restClient;
private final CacheEvictionProperties evictionProperties;
private final ApiRateLimitState rateLimitState;
private final GithubApiUtilities utilities;

public WorkflowRunBuildTimesAdapter(
GithubProperties githubProperties,
@Qualifier("defaultRestClient") RestClient restClient,
CacheEvictionProperties evictionProperties,
ApiRateLimitState rateLimitState
ApiRateLimitState rateLimitState,
GithubApiUtilities utilities
) {
this.githubProperties = githubProperties;
this.restClient = restClient;
this.evictionProperties = evictionProperties;
this.rateLimitState = rateLimitState;
this.utilities = utilities;
}

@Cacheable(WORKFLOW_RUN_BUILD_TIMES_CACHE_NAME)
Expand All @@ -53,10 +60,13 @@ public int getWorkflowRunBuildTimes(WorkflowRun workflowRun) {
return 0;
}

var buildTime = Objects.requireNonNull(this.restClient.get()
.uri(this.getBuildTimesApiPath(workflowRun))
.retrieve()
.body(GHActionRunTiming.class))
var buildTime = Objects.requireNonNull(
this.restClient.get()
.uri(buildTimesUri(workflowRun))
.header("path", GHActionRunTiming.PATH)
.retrieve()
.body(GHActionRunTiming.class)
)
.run_duration_ms();

LOGGER.debug(
Expand All @@ -68,13 +78,16 @@ public int getWorkflowRunBuildTimes(WorkflowRun workflowRun) {
return buildTime;
}

private String getBuildTimesApiPath(WorkflowRun workflowRun) {
return MessageFormat.format(
"repos/{0}/{1}/actions/runs/{2,number,#}/timing",
private Function<UriBuilder, URI> buildTimesUri(WorkflowRun workflowRun) {
List<Object> uriVars = List.of(
this.githubProperties.org(),
workflowRun.getRepository().getName(),
workflowRun.getId()
);
return this.utilities.setPathAndParameters(
GHActionRunTiming.PATH,
uriVars
);
}

@Override
Expand Down
Loading