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

Implement workflow job event payload #1331

Closed
Typraeurion opened this issue Nov 30, 2021 · 2 comments · Fixed by #1462
Closed

Implement workflow job event payload #1331

Typraeurion opened this issue Nov 30, 2021 · 2 comments · Fixed by #1462

Comments

@Typraeurion
Copy link

Describe the bug
#1316 added WORKFLOW_JOB to the GHEvent enum, but receiving workflow_job events is still not fully supported because it can’t be mapped to any GHEventPayload subclass. An attempt to map it to the GHEventPayload superclass results in:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of org.kohsuke.github.GHEventPayload (no Creators, like default constructor, exist)

I’ve captured the request body for a workflow_job event in order to check its structure, and it looks like implementing this is very straightforward. I used the WorkflowRun subclass as a model.

diff --git a/src/main/java/org/kohsuke/github/GHEventPayload.java b/src/main/java/org/kohsuke/github/GHEventPayload.java
index 4d47264aa..ca3b75036 100644
--- a/src/main/java/org/kohsuke/github/GHEventPayload.java
+++ b/src/main/java/org/kohsuke/github/GHEventPayload.java
@@ -1446,6 +1446,42 @@ public abstract class GHEventPayload extends GitHubInteractiveObject {
         }
     }
 
+    /**
+     * A workflow job has been queued, is in progress, or has been completed.
+     *
+     * @see <a href=
+     *      "https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#workflow_job">
+     *      workflow job event</a>
+     * @see <a href="https://docs.github.com/en/rest/reference/actions#workflow-jobs">Actions Workflow Jobs</a>
+     */
+    public static class WorkflowJob extends GHEventPayload {
+        private GHWorkflowJob workflowJob;
+
+        /**
+         * Gets the workflow job.
+         *
+         * @return the workflow job
+         */
+        @SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected")
+        public GHWorkflowJob getWorkflowJob() {
+            return workflowJob;
+        }
+
+        @Override
+        void lateBind() {
+            if (workflowJob == null) {
+                throw new IllegalStateException(
+                        "Expected workflow_job payload, but got something else.  Maybe we've got another type of event?");
+            }
+            super.lateBind();
+            GHRepository repository = getRepository();
+            if (repository == null) {
+                throw new IllegalStateException("Repository must not be null");
+            }
+            workflowJob.wrapUp(repository);
+        }
+    }
+
     /**
      * A label was created, edited or deleted.
      *

I’ve tested this locally by posting a copy of the workflow_job event to my application’s webhook endpoint, where the application is doing:

        Class<? extends GHEventPayload> eventClass;
        switch (eventName.toLowerCase()) {

            …

            case "workflow_job":
                eventClass = GHEventPayload.WorkflowJob.class; break;

            default:
                throw new UnsupportedOperationException(
                        "Event \"" + eventName + "\" is not handled");

        }

        GHEventPayload eventPayload = GitHub.offline().parseEventPayload(
                new StringReader(payload), eventClass);
@gsmet
Copy link
Contributor

gsmet commented Dec 1, 2021

The doc is not very clear about how to get this sort of event. By any chance, do you have a full payload for an event like this that we could use in the tests?

If so I would encourage you to create a pull request with your code and the payload added (See how it's done in GHEventPayloadTest, the payloads are in src/test/resources with the name of the test method).

@Typraeurion
Copy link
Author

I couldn’t figure out how this project is getting its test data, and I’m working on a private repository owned by my client so I don’t know that I’d be allowed to share this event’s actual data. But to get the event you just need to enable “Workflow jobs” events on the repository webhook (https://github.com/{owner}/{repo}/settings/hooks/#####) and then get a workflow to run. GitHub sends a workflow_run event with action “requested” when the workflow is about to start, followed by a workflow_job event with action “queued”, then a workflow_job event with action “in_progress”; and when the workflow finishes GitHub sends a workflow_job event with action “completed” and finally a workflow_run event with action “completed”.

gsmet added a commit to gsmet/github-api that referenced this issue May 30, 2022
gsmet added a commit to gsmet/github-api that referenced this issue Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants