|
| 1 | +package org.kohsuke.github; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 4 | +import org.apache.commons.lang3.StringUtils; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.net.URL; |
| 8 | +import java.util.Collections; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.Objects; |
| 11 | + |
| 12 | +/** |
| 13 | + * A workflow. |
| 14 | + * |
| 15 | + * @author Guillaume Smet |
| 16 | + * @see GHRepository#getWorkflow(long) |
| 17 | + */ |
| 18 | +public class GHWorkflow extends GHObject { |
| 19 | + |
| 20 | + // Not provided by the API. |
| 21 | + @JsonIgnore |
| 22 | + private GHRepository owner; |
| 23 | + |
| 24 | + private String name; |
| 25 | + private String path; |
| 26 | + private String state; |
| 27 | + |
| 28 | + private String htmlUrl; |
| 29 | + private String badgeUrl; |
| 30 | + |
| 31 | + /** |
| 32 | + * The name of the workflow. |
| 33 | + * |
| 34 | + * @return the name |
| 35 | + */ |
| 36 | + public String getName() { |
| 37 | + return name; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * The path of the workflow e.g. .github/workflows/blank.yaml |
| 42 | + * |
| 43 | + * @return the path |
| 44 | + */ |
| 45 | + public String getPath() { |
| 46 | + return path; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * The state of the workflow. |
| 51 | + * |
| 52 | + * @return the state |
| 53 | + */ |
| 54 | + public String getState() { |
| 55 | + return state; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public URL getHtmlUrl() throws IOException { |
| 60 | + return GitHubClient.parseURL(htmlUrl); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * The badge URL, like https://github.com/octo-org/octo-repo/workflows/CI/badge.svg |
| 65 | + * |
| 66 | + * @return the badge url |
| 67 | + */ |
| 68 | + public URL getBadgeUrl() { |
| 69 | + return GitHubClient.parseURL(badgeUrl); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Disable the workflow. |
| 74 | + * |
| 75 | + * @throws IOException |
| 76 | + * the io exception |
| 77 | + */ |
| 78 | + public void disable() throws IOException { |
| 79 | + root.createRequest().method("PUT").withUrlPath(getApiRoute(), "disable").fetchHttpStatusCode(); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Enable the workflow. |
| 84 | + * |
| 85 | + * @throws IOException |
| 86 | + * the io exception |
| 87 | + */ |
| 88 | + public void enable() throws IOException { |
| 89 | + root.createRequest().method("PUT").withUrlPath(getApiRoute(), "enable").fetchHttpStatusCode(); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Create a workflow dispatch event which triggers a manual workflow run. |
| 94 | + * |
| 95 | + * @param ref |
| 96 | + * the git reference for the workflow. The reference can be a branch or tag name. |
| 97 | + * @throws IOException |
| 98 | + * the io exception |
| 99 | + */ |
| 100 | + public void dispatch(String ref) throws IOException { |
| 101 | + dispatch(ref, Collections.emptyMap()); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Create a workflow dispatch event which triggers a manual workflow run. |
| 106 | + * |
| 107 | + * @param ref |
| 108 | + * the git reference for the workflow. The reference can be a branch or tag name. |
| 109 | + * @param inputs |
| 110 | + * input keys and values configured in the workflow file. The maximum number of properties is 10. Any |
| 111 | + * default properties configured in the workflow file will be used when inputs are omitted. |
| 112 | + * @throws IOException |
| 113 | + * the io exception |
| 114 | + */ |
| 115 | + public void dispatch(String ref, Map<String, Object> inputs) throws IOException { |
| 116 | + Requester requester = root.createRequest() |
| 117 | + .method("POST") |
| 118 | + .withUrlPath(getApiRoute(), "dispatches") |
| 119 | + .with("ref", ref); |
| 120 | + |
| 121 | + if (!inputs.isEmpty()) { |
| 122 | + requester.with("inputs", inputs); |
| 123 | + } |
| 124 | + |
| 125 | + requester.fetchHttpStatusCode(); |
| 126 | + } |
| 127 | + |
| 128 | + private String getApiRoute() { |
| 129 | + if (owner == null) { |
| 130 | + // Workflow runs returned from search to do not have an owner. Attempt to use url. |
| 131 | + final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!"); |
| 132 | + return StringUtils.prependIfMissing(url.toString().replace(root.getApiUrl(), ""), "/"); |
| 133 | + |
| 134 | + } |
| 135 | + return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/actions/workflows/" + getId(); |
| 136 | + } |
| 137 | + |
| 138 | + GHWorkflow wrapUp(GHRepository owner) { |
| 139 | + this.owner = owner; |
| 140 | + return wrapUp(owner.root); |
| 141 | + } |
| 142 | + |
| 143 | + GHWorkflow wrapUp(GitHub root) { |
| 144 | + this.root = root; |
| 145 | + if (owner != null) |
| 146 | + owner.wrap(root); |
| 147 | + return this; |
| 148 | + } |
| 149 | +} |
0 commit comments