Skip to content

Commit

Permalink
Merge pull request #1064 from gsmet/workflow-runs
Browse files Browse the repository at this point in the history
Implement GHWorkflow, GHWorkflowRun and associated payloads
  • Loading branch information
bitwiseman authored Mar 25, 2021
2 parents e522239 + d734237 commit 551dbf2
Show file tree
Hide file tree
Showing 100 changed files with 14,324 additions and 27 deletions.
51 changes: 38 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,6 @@
<argLine>${surefire.argLine}</argLine>
</configuration>
</execution>
<execution>
<id>slow-or-flaky-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<rerunFailingTestsCount>2</rerunFailingTestsCount>
<!-- There are some tests that take longer or are a little flaky. Run them here. -->
<includesFile>src/test/resources/slow-or-flaky-tests.txt</includesFile>
<argLine>${surefire.argLine}</argLine>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down Expand Up @@ -422,6 +409,12 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.0.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down Expand Up @@ -565,6 +558,38 @@
</pluginRepository>
</pluginRepositories>
<profiles>
<!-- only enable slow-or-flaky-test if -Dtest= is not present -->
<profile>
<id>slow-or-flaky-test</id>
<activation>
<property>
<name>!test</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>slow-or-flaky-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<rerunFailingTestsCount>2</rerunFailingTestsCount>
<!-- There are some tests that take longer or are a little
flaky. Run them here. -->
<includesFile>src/test/resources/slow-or-flaky-tests.txt</includesFile>
<argLine>${surefire.argLine}</argLine>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>jdk11+</id>
<activation>
Expand Down
43 changes: 39 additions & 4 deletions src/main/java/org/kohsuke/github/GHCheckRun.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.kohsuke.github.GHWorkflowRun.Conclusion;
import org.kohsuke.github.GHWorkflowRun.Status;
import org.kohsuke.github.internal.EnumUtils;
import org.kohsuke.github.internal.Previews;

import java.io.IOException;
Expand All @@ -11,6 +15,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
* Represents a check run.
Expand Down Expand Up @@ -80,12 +85,27 @@ GHPullRequest[] wrap() {
* @return Status of the check run
* @see Status
*/
public String getStatus() {
@WithBridgeMethods(value = String.class, adapterMethod = "statusAsStr")
public Status getStatus() {
return Status.from(status);
}

@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Bridge method of getStatus")
private Object statusAsStr(Status status, Class type) {
return status;
}

public static enum Status {
QUEUED, IN_PROGRESS, COMPLETED
QUEUED, IN_PROGRESS, COMPLETED, UNKNOWN;

public static Status from(String value) {
return EnumUtils.getNullableEnumOrDefault(Status.class, value, Status.UNKNOWN);
}

@Override
public String toString() {
return name().toLowerCase(Locale.ROOT);
}
}

/**
Expand All @@ -94,7 +114,13 @@ public static enum Status {
* @return Status of the check run
* @see Conclusion
*/
public String getConclusion() {
@WithBridgeMethods(value = String.class, adapterMethod = "conclusionAsStr")
public Conclusion getConclusion() {
return Conclusion.from(conclusion);
}

@SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD", justification = "Bridge method of getConclusion")
private Object conclusionAsStr(Conclusion conclusion, Class type) {
return conclusion;
}

Expand All @@ -105,7 +131,16 @@ public String getConclusion() {
* Parameters - <code>conclusion</code></a>.
*/
public static enum Conclusion {
SUCCESS, FAILURE, NEUTRAL, CANCELLED, TIMED_OUT, ACTION_REQUIRED, SKIPPED
ACTION_REQUIRED, CANCELLED, FAILURE, NEUTRAL, SUCCESS, SKIPPED, STALE, TIMED_OUT, UNKNOWN;

public static Conclusion from(String value) {
return EnumUtils.getNullableEnumOrDefault(Conclusion.class, value, Conclusion.UNKNOWN);
}

@Override
public String toString() {
return name().toLowerCase(Locale.ROOT);
}
}

/**
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/org/kohsuke/github/GHEventPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.Reader;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* Base type for types used in databinding of the event payload.
Expand Down Expand Up @@ -1326,4 +1327,86 @@ void wrapUp(GitHub root) {
}
}
}

/**
* Occurs when someone triggered a workflow run or sends a POST request to the "Create a workflow dispatch event"
* endpoint.
*
* @see <a href=
* "https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#workflow_dispatch">
* workflow dispatch event</a>
* @see <a href=
* "https://docs.github.com/en/actions/reference/events-that-trigger-workflows#workflow_dispatch">Events that
* trigger workflows</a>
*/
public static class WorkflowDispatch extends GHEventPayload {
private Map<String, Object> inputs;
private String ref;
private String workflow;

/**
* Gets the map of input parameters passed to the workflow.
*
* @return the map of input parameters
*/
public Map<String, Object> getInputs() {
return inputs;
}

/**
* Gets the ref of the branch (e.g. refs/heads/main)
*
* @return the ref of the branch
*/
public String getRef() {
return ref;
}

/**
* Gets the path of the workflow file (e.g. .github/workflows/hello-world-workflow.yml).
*
* @return the path of the workflow file
*/
public String getWorkflow() {
return workflow;
}
}

/**
* A workflow run was requested or completed.
*
* @see <a href=
* "https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#workflow_run">
* workflow run event</a>
* @see <a href="https://docs.github.com/en/rest/reference/actions#workflow-runs">Actions Workflow Runs</a>
*/
public static class WorkflowRun extends GHEventPayload {
private GHWorkflowRun workflowRun;
private GHWorkflow workflow;

public GHWorkflowRun getWorkflowRun() {
return workflowRun;
}

public GHWorkflow getWorkflow() {
return workflow;
}

@Override
void wrapUp(GitHub root) {
super.wrapUp(root);
if (workflowRun == null || workflow == null) {
throw new IllegalStateException(
"Expected workflow and workflow_run payload, but got something else. Maybe we've got another type of event?");
}
GHRepository repository = getRepository();
if (repository != null) {
workflowRun.wrapUp(repository);
workflow.wrapUp(repository);
} else {
workflowRun.wrapUp(root);
workflow.wrapUp(root);
}
}
}
}
65 changes: 65 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2898,6 +2898,71 @@ public GHIssueEvent getIssueEvent(long id) throws IOException {
.wrapUp(root);
}

/**
* Lists all the workflows of this repository.
*
* @return the paged iterable
*/
public PagedIterable<GHWorkflow> listWorkflows() {
return root.createRequest()
.withUrlPath(getApiTailUrl("actions/workflows"))
.toIterable(GHWorkflow[].class, item -> item.wrapUp(root));
}

/**
* Gets a workflow by id.
*
* @param id
* the id of the workflow run
* @return the workflow run
* @throws IOException
* the io exception
*/
public GHWorkflow getWorkflow(long id) throws IOException {
return getWorkflow(String.valueOf(id));
}

/**
* Gets a workflow by name of the file.
*
* @param nameOrId
* either the name of the file (e.g. my-workflow.yml) or the id as a string
* @return the workflow run
* @throws IOException
* the io exception
*/
public GHWorkflow getWorkflow(String nameOrId) throws IOException {
return root.createRequest()
.withUrlPath(getApiTailUrl("actions/workflows"), nameOrId)
.fetch(GHWorkflow.class)
.wrapUp(this);
}

/**
* Retrieves workflow runs.
*
* @return the workflow run query builder
*/
public GHWorkflowRunQueryBuilder queryWorkflowRuns() {
return new GHWorkflowRunQueryBuilder(this);
}

/**
* Gets a workflow run.
*
* @param id
* the id of the workflow run
* @return the workflow run
* @throws IOException
* the io exception
*/
public GHWorkflowRun getWorkflowRun(long id) throws IOException {
return root.createRequest()
.withUrlPath(getApiTailUrl("actions/runs"), String.valueOf(id))
.fetch(GHWorkflowRun.class)
.wrapUp(this);
}

// Only used within listTopics().
private static class Topics {
public List<String> names;
Expand Down
Loading

0 comments on commit 551dbf2

Please sign in to comment.