Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get and list workflows from github actions API #197

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions src/main/java/com/spotify/github/v3/clients/GitHubClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,15 @@ public UserClient createUserClient(final String owner) {
return UserClient.create(this, owner);
}

/**
* Workflows API client
*
* @return Workflows API client
*/
public WorkflowsClient createWorkflowsClient(final String owner, final String repo) {
return WorkflowsClient.create(this, owner, repo);
}

Json json() {
return json;
}
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 updated
*/
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
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*-
* -\-\-
* 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.io.Resources;
import com.spotify.github.jackson.Json;
import com.spotify.github.v3.workflows.WorkflowsRepositoryResponseList;
import com.spotify.github.v3.workflows.WorkflowsResponse;
import com.spotify.github.v3.workflows.WorkflowsState;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.concurrent.CompletableFuture;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class WorkflowsClientTest {

private static final String FIXTURES_PATH = "com/spotify/github/v3/workflows/";
private GitHubClient github;
private WorkflowsClient workflowsClient;
private Json json;

public static String loadResource(final String path) {
try {
return Resources.toString(Resources.getResource(path), UTF_8);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@BeforeEach
public void setUp() {
github = mock(GitHubClient.class);
workflowsClient = new WorkflowsClient(github, "someowner", "somerepo");
json = Json.create();
when(github.json()).thenReturn(json);
}

@Test
public void getWorkflow() throws Exception {
final WorkflowsResponse workflowsResponse =
json.fromJson(
loadResource(FIXTURES_PATH + "workflows-get-workflow-response.json"), WorkflowsResponse.class);
final CompletableFuture<WorkflowsResponse> fixtureResponse = completedFuture(workflowsResponse);
when(github.request(any(), eq(WorkflowsResponse.class), any())).thenReturn(fixtureResponse);

final CompletableFuture<WorkflowsResponse> actualResponse =
workflowsClient.getWorkflow(161335);

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

@Test
public void listWorkflows() throws Exception {
final WorkflowsRepositoryResponseList workflowsListResponse =
json.fromJson(
loadResource(FIXTURES_PATH + "workflows-list-workflows-response.json"), WorkflowsRepositoryResponseList.class);
final CompletableFuture<WorkflowsRepositoryResponseList> fixtureResponse = completedFuture(workflowsListResponse);
when(github.request(any(), eq(WorkflowsRepositoryResponseList.class), any())).thenReturn(fixtureResponse);

final CompletableFuture<WorkflowsRepositoryResponseList> actualResponse =
workflowsClient.listWorkflows();

assertThat(actualResponse.get().totalCount(), is(2));
assertThat(actualResponse.get().workflows().get(0).name(), is("CI"));
assertThat(actualResponse.get().workflows().get(1).name(), is("Linter"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"id": 161335,
"node_id": "MDg6V29ya2Zsb3cxNjEzMzU=",
"name": "CI",
"path": ".github/workflows/blank.yaml",
"state": "active",
"created_at": "2020-01-08T23:48:37.000-08:00",
"updated_at": "2020-01-08T23:50:21.000-08:00",
"deleted_at": "2020-01-09T23:50:21.000-08:00",
"url": "https://api.github.com/repos/octo-org/octo-repo/actions/workflows/161335",
"html_url": "https://github.com/octo-org/octo-repo/blob/master/.github/workflows/161335",
"badge_url": "https://github.com/octo-org/octo-repo/workflows/CI/badge.svg"
}
Loading
Loading