Skip to content

Commit

Permalink
Add get and list workflows from github actions API (#197)
Browse files Browse the repository at this point in the history
* Add get and list workflows from github actions API

* Update workflows client hierarchy according to github API

* Fix import
  • Loading branch information
hvardhanx authored Oct 21, 2024
1 parent e00d227 commit fbcd82c
Show file tree
Hide file tree
Showing 10 changed files with 474 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/ActionsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.v3.clients;

public class ActionsClient {
private final String owner;
private final String repo;
private final GitHubClient github;

ActionsClient(final GitHubClient github, final String owner, final String repo) {
this.github = github;
this.owner = owner;
this.repo = repo;
}

static ActionsClient create(final GitHubClient github, final String owner, final String repo) {
return new ActionsClient(github, owner, repo);
}

/**
* Workflows API client
*
* @return Workflows API client
*/
public WorkflowsClient createWorkflowsClient() {
return WorkflowsClient.create(github, owner, repo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ public ChecksClient createChecksApiClient() {
return new ChecksClient(github, owner, repo);
}

/**
* Actions API client
*
* @return Actions API client
*/
public ActionsClient createActionsClient() {
return ActionsClient.create(github, owner, repo);
}

/**
* Get information about this repository.
*
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/WorkflowsClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.v3.clients;

import com.google.common.collect.ImmutableMap;
import com.spotify.github.v3.workflows.WorkflowsRepositoryResponseList;
import com.spotify.github.v3.workflows.WorkflowsResponse;

import javax.ws.rs.core.HttpHeaders;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/** Workflows API client */
public class WorkflowsClient {
private static final String LIST_REPOSITORY_WORKFLOWS_URI = "/repos/%s/%s/actions/workflows";
private static final String GET_WORKFLOW_URI = "/repos/%s/%s/actions/workflows/%s";

private final GitHubClient github;
private final String owner;
private final String repo;

private final Map<String, String> extraHeaders =
ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github+json");

public WorkflowsClient(final GitHubClient github, final String owner, final String repo) {
this.github = github;
this.owner = owner;
this.repo = repo;
}

static WorkflowsClient create(final GitHubClient github, final String owner, final String repo) {
return new WorkflowsClient(github, owner, repo);
}

/**
* List workflows for a repository.
*
* @return a list of workflows for the repository
*/
public CompletableFuture<WorkflowsRepositoryResponseList> listWorkflows() {
final String path = String.format(LIST_REPOSITORY_WORKFLOWS_URI, owner, repo);
return github.request(path, WorkflowsRepositoryResponseList.class, extraHeaders);
}

/**
* Gets a workflow by id.
*
* @param id the workflow id
* @return a WorkflowsResponse
*/
public CompletableFuture<WorkflowsResponse> getWorkflow(final int id) {
final String path = String.format(GET_WORKFLOW_URI, owner, repo, id);
return github.request(path, WorkflowsResponse.class, extraHeaders);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.v3.workflows;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.spotify.github.GithubStyle;
import org.immutables.value.Value;

import java.util.List;

/**
* The WorkflowsResponse list resource
*
* @see "https://docs.github.com/en/rest/actions/workflows#list-repository-workflows"
*/
@Value.Immutable
@GithubStyle
@JsonDeserialize(as = ImmutableWorkflowsRepositoryResponseList.class)
public interface WorkflowsRepositoryResponseList {
/**
* The count of workflows in the response
*
* @return the int
*/
int totalCount();

/**
* Workflows list.
*
* @return the list of workflows
*/
List<WorkflowsResponse> workflows();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.v3.workflows;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.spotify.github.GithubStyle;
import org.immutables.value.Value;

import java.time.ZonedDateTime;

@Value.Immutable
@GithubStyle
@JsonDeserialize(as = ImmutableWorkflowsResponse.class)
public interface WorkflowsResponse {
/**
* The Workflow ID.
*
* @return the int
*/
int id();

/** Node ID */
String nodeId();

/** Name. */
String name();

/** The workflow path. */
String path();

/** Indicates the state of the workflow. */
WorkflowsState state();

/**
* Created At
*
* @return The time when the workflow was created
*/
ZonedDateTime createdAt();

/**
* Updated At
*
* @return The time when the workflow was updated
*/
ZonedDateTime updatedAt();

/**
* Deleted At
*
* @return The time when the workflow was deleted
*/
ZonedDateTime deletedAt();

/**
* Url string.
*
* @return the string
*/
String url();

/**
* Html url string.
*
* @return the string
*/
String htmlUrl();

/**
* Badge Url string.
*
* @return the string
*/
String badgeUrl();
}
30 changes: 30 additions & 0 deletions src/main/java/com/spotify/github/v3/workflows/WorkflowsState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*-
* -\-\-
* github-api
* --
* Copyright (C) 2016 - 2020 Spotify AB
* --
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* -/-/-
*/

package com.spotify.github.v3.workflows;

/** The Workflow State. */
public enum WorkflowsState {
active,
deleted,
disabled_fork,
disabled_inactivity,
disabled_manually
}
31 changes: 31 additions & 0 deletions src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import com.spotify.github.v3.exceptions.RequestNotOkException;
import com.spotify.github.v3.repos.CommitItem;
import com.spotify.github.v3.repos.RepositoryInvitation;
import com.spotify.github.v3.workflows.WorkflowsResponse;
import com.spotify.github.v3.workflows.WorkflowsState;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -233,6 +235,35 @@ public void testGetCheckSuites() throws Throwable {

}

@Test
public void testGetWorkflow() throws Throwable {
final Call call = mock(Call.class);
final ArgumentCaptor<Callback> callbackCapture = ArgumentCaptor.forClass(Callback.class);
doNothing().when(call).enqueue(callbackCapture.capture());

final Response response = new okhttp3.Response.Builder()
.code(200)
.body(
ResponseBody.create(
MediaType.get("application/json"),
getFixture("../workflows/workflows-get-workflow-response.json")))
.message("")
.protocol(Protocol.HTTP_1_1)
.request(new Request.Builder().url("http://localhost/").build())
.build();

when(client.newCall(any())).thenReturn(call);
WorkflowsClient client = github.withTracer(tracer).createRepositoryClient("testorg", "testrepo")
.createActionsClient().createWorkflowsClient();

CompletableFuture<WorkflowsResponse> future = client.getWorkflow(161335);
callbackCapture.getValue().onResponse(call, response);
var result = future.get();

assertThat(result.id(), is(161335));
assertThat(result.state(), is(WorkflowsState.active));
}

@Test
void asAppScopedClientGetsUserClientIfOrgClientNotFound() {
var appGithub = GitHubClient.create(client, URI.create("http://bogus"), new byte[] {}, 1);
Expand Down
Loading

0 comments on commit fbcd82c

Please sign in to comment.