Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| GitHub Actions on g1t, part two: running workflows | 1 | //! `github.event`: the webhook-shaped payload GitHub gives a workflow, |
| 2 | //! built from g1t's own records so `github.event.pull_request.number`, | |
| 3 | //! `github.event.issue.labels.*.name` and the like read as they do there. | |
| 4 | ||
| g1t is the stored author of what it opens; the person who asked is requested_by and keeps the author's rights | 5 | use g1t_contracts::{PrincipalKind, User}; |
| GitHub Actions on g1t, part two: running workflows | 6 | use g1t_contracts::repos::{Commit, Repo}; |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 7 | use g1t_contracts::work::{Comment, Issue, MilestoneRef, Pull, PullStatus, State}; |
| GitHub Actions on g1t, part two: running workflows | 8 | use serde_json::{Value, json}; |
| 9 | ||
| 10 | use crate::{API, SITE}; | |
| 11 | ||
| 12 | pub fn repository(repo: &Repo) -> Value { | |
| 13 | let full_name = format!("{}/{}", repo.namespace, repo.name); | |
| 14 | json!({ | |
| 15 | "id": repo.id, | |
| 16 | "node_id": repo.id, | |
| 17 | "name": repo.name, | |
| 18 | "full_name": full_name, | |
| 19 | "private": repo.is_private, | |
| 20 | "owner": { "login": repo.namespace, "type": "Organization" }, | |
| 21 | "html_url": format!("{SITE}/{full_name}"), | |
| 22 | "url": format!("{API}/repos/{full_name}"), | |
| 23 | "clone_url": format!("{SITE}/{full_name}.git"), | |
| 24 | "description": repo.description, | |
| 25 | "default_branch": repo.default_branch, | |
| 26 | "fork": false, | |
| 27 | "visibility": if repo.is_private { "private" } else { "public" }, | |
| 28 | }) | |
| 29 | } | |
| 30 | ||
| 31 | pub fn user(login: &str) -> Value { | |
| 32 | json!({ "login": login, "type": "User", "html_url": format!("{SITE}/{login}") }) | |
| 33 | } | |
| 34 | ||
| g1t is the stored author of what it opens; the person who asked is requested_by and keeps the author's rights | 35 | /// Someone as GitHub names them. g1t, the author of what it makes and |
| 36 | /// files, is a `Bot`, as an app is there. | |
| GitHub Actions on g1t, part two: running workflows | 37 | fn person(user: &User) -> Value { |
| g1t is the stored author of what it opens; the person who asked is requested_by and keeps the author's rights | 38 | let mut named = self::user(&user.username); |
| 39 | if matches!(user.kind, PrincipalKind::Agent | PrincipalKind::System) { | |
| 40 | named["type"] = json!("Bot"); | |
| 41 | } | |
| 42 | named | |
| GitHub Actions on g1t, part two: running workflows | 43 | } |
| 44 | ||
| 45 | fn labels(names: &[String]) -> Value { | |
| 46 | Value::Array(names.iter().map(|name| json!({ "name": name })).collect()) | |
| 47 | } | |
| 48 | ||
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 49 | /// A milestone as an issue or a pull request names it. |
| 50 | fn milestone(milestone: Option<&MilestoneRef>) -> Value { | |
| 51 | milestone.map_or(Value::Null, |milestone| json!({ "number": milestone.number, "title": milestone.title })) | |
| 52 | } | |
| 53 | ||
| GitHub Actions on g1t, part two: running workflows | 54 | pub fn commit(repo: &Repo, commit: &Commit) -> Value { |
| 55 | json!({ | |
| 56 | "id": commit.hash, | |
| 57 | "tree_id": commit.tree_hash, | |
| 58 | "message": commit.message, | |
| 59 | "timestamp": commit.authored_at, | |
| 60 | "url": format!("{SITE}/{}/{}/commit/{}", repo.namespace, repo.name, commit.hash), | |
| 61 | "author": { "name": commit.author.name, "email": commit.author.email }, | |
| 62 | "committer": { "name": commit.author.name, "email": commit.author.email }, | |
| 63 | "distinct": true, | |
| 64 | }) | |
| 65 | } | |
| 66 | ||
| 67 | pub fn push(repo: &Repo, git_ref: &str, before: Option<&str>, after: &str, commits: &[Commit], pusher: &str) -> Value { | |
| 68 | let zero = "0000000000000000000000000000000000000000"; | |
| 69 | let commits: Vec<Value> = commits.iter().map(|c| commit(repo, c)).collect(); | |
| 70 | json!({ | |
| 71 | "ref": git_ref, | |
| 72 | "before": before.unwrap_or(zero), | |
| 73 | "after": after, | |
| 74 | "created": before.is_none(), | |
| 75 | "deleted": false, | |
| 76 | "forced": false, | |
| 77 | "base_ref": null, | |
| 78 | "compare": format!("{SITE}/{}/{}/commit/{after}", repo.namespace, repo.name), | |
| 79 | "head_commit": commits.first().cloned().unwrap_or(Value::Null), | |
| 80 | "commits": commits, | |
| 81 | "pusher": { "name": pusher, "email": null }, | |
| 82 | "repository": repository(repo), | |
| 83 | "sender": user(pusher), | |
| 84 | }) | |
| 85 | } | |
| 86 | ||
| 87 | pub fn issue(repo: &Repo, issue: &Issue) -> Value { | |
| 88 | let full_name = format!("{}/{}", repo.namespace, repo.name); | |
| 89 | json!({ | |
| 90 | "id": issue.id, | |
| 91 | "number": issue.number, | |
| 92 | "title": issue.title, | |
| 93 | "body": issue.body, | |
| 94 | "state": if issue.state == State::Open { "open" } else { "closed" }, | |
| 95 | "state_reason": issue.reason, | |
| 96 | "labels": labels(&issue.labels), | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 97 | "milestone": milestone(issue.milestone.as_ref()), |
| GitHub Actions on g1t, part two: running workflows | 98 | "user": person(&issue.author), |
| g1t is the stored author of what it opens; the person who asked is requested_by and keeps the author's rights | 99 | // g1t's own field: for an issue its agent filed, who it worked for. |
| 100 | "requested_by": issue.requested_by.as_ref().map(person), | |
| GitHub Actions on g1t, part two: running workflows | 101 | "assignees": issue.assignees.iter().map(|a| user(a)).collect::<Vec<_>>(), |
| 102 | "comments": issue.comment_count, | |
| 103 | "created_at": issue.created_at, | |
| 104 | "updated_at": issue.updated_at, | |
| 105 | "closed_at": issue.closed_at, | |
| 106 | "html_url": format!("{SITE}/{full_name}/issues/{}", issue.number), | |
| 107 | "url": format!("{API}/repos/{full_name}/issues/{}", issue.number), | |
| 108 | }) | |
| 109 | } | |
| 110 | ||
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 111 | /// A pull request, with its own labels. |
| 112 | pub fn pull(repo: &Repo, pull: &Pull) -> Value { | |
| GitHub Actions on g1t, part two: running workflows | 113 | let full_name = format!("{}/{}", repo.namespace, repo.name); |
| 114 | let head_ref = head_ref(pull); | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 115 | let base_ref = pull.base_branch(&repo.default_branch); |
| GitHub Actions on g1t, part two: running workflows | 116 | let head_repo = match &pull.fork { |
| 117 | Some(fork) => json!({ "full_name": format!("{}/{}", fork.namespace, fork.name), "fork": true }), | |
| 118 | None => json!({ "full_name": full_name, "fork": false }), | |
| 119 | }; | |
| 120 | json!({ | |
| 121 | "id": pull.id, | |
| 122 | "number": pull.number, | |
| 123 | "title": pull.title, | |
| 124 | "body": pull.body, | |
| 125 | "state": if pull.status.is_active() { "open" } else { "closed" }, | |
| 126 | "draft": pull.status == PullStatus::Draft, | |
| 127 | "merged": pull.status == PullStatus::Merged, | |
| 128 | "merged_at": pull.merged_at, | |
| 129 | "merged_by": pull.merged_by.as_deref().map(user), | |
| 130 | "merge_commit_sha": if pull.status == PullStatus::Merged { pull.head_commit.clone() } else { None }, | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 131 | "labels": labels(&pull.labels), |
| 132 | "milestone": milestone(pull.milestone.as_ref()), | |
| GitHub Actions on g1t, part two: running workflows | 133 | "user": person(&pull.author), |
| g1t is the stored author of what it opens; the person who asked is requested_by and keeps the author's rights | 134 | // g1t's own field: for a change g1t made, who asked for it. |
| 135 | "requested_by": pull.requested_by.as_ref().map(person), | |
| GitHub Actions on g1t, part two: running workflows | 136 | "assignees": pull.assignees.iter().map(|a| user(a)).collect::<Vec<_>>(), |
| 137 | "requested_reviewers": pull.reviewers.iter().map(|r| user(r)).collect::<Vec<_>>(), | |
| 138 | "head": { | |
| 139 | "ref": head_ref, | |
| 140 | "sha": pull.head_commit, | |
| 141 | "label": format!("{}:{head_ref}", repo.namespace), | |
| 142 | "repo": head_repo, | |
| 143 | }, | |
| 144 | "base": { | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 145 | "ref": base_ref, |
| GitHub Actions on g1t, part two: running workflows | 146 | "sha": pull.merge_base, |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 147 | "label": format!("{}:{base_ref}", repo.namespace), |
| GitHub Actions on g1t, part two: running workflows | 148 | "repo": repository(repo), |
| 149 | }, | |
| 150 | "html_url": format!("{SITE}/{full_name}/pull/{}", pull.number), | |
| 151 | "url": format!("{API}/repos/{full_name}/pulls/{}", pull.number), | |
| 152 | "issue_url": pull.issue.map(|n| format!("{API}/repos/{full_name}/issues/{n}")), | |
| 153 | }) | |
| 154 | } | |
| 155 | ||
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 156 | /// What a `labeled`, `unlabeled`, `milestoned`, `demilestoned` or base |
| 157 | /// change was about, from the g1t event: `label`, `milestone`, and for a | |
| 158 | /// new base, `changes.base.ref.from` is not known, so only the new base is | |
| 159 | /// in `pull_request.base`. | |
| 160 | pub fn changed(payload: &mut Value, data: &Value) { | |
| 161 | if let Some(label) = data.get("label").filter(|label| label.is_object()) { | |
| 162 | payload["label"] = json!({ "name": label["name"], "color": label["color"] }); | |
| 163 | } | |
| 164 | if let Some(milestone) = data.get("milestone").filter(|milestone| milestone.is_object()) { | |
| 165 | payload["milestone"] = json!({ "number": milestone["number"], "title": milestone["title"] }); | |
| 166 | } | |
| 167 | } | |
| 168 | ||
| GitHub Actions on g1t, part two: running workflows | 169 | /// The branch a pull request comes from, or a name for its fork. |
| 170 | pub fn head_ref(pull: &Pull) -> String { | |
| 171 | pull.branch.clone().unwrap_or_else(|| format!("pull/{}", pull.number)) | |
| 172 | } | |
| 173 | ||
| 174 | pub fn comment(repo: &Repo, number: u32, comment: &Comment, on_pull: bool) -> Value { | |
| 175 | let full_name = format!("{}/{}", repo.namespace, repo.name); | |
| 176 | let page = if on_pull { "pull" } else { "issues" }; | |
| 177 | json!({ | |
| 178 | "id": comment.id, | |
| 179 | "body": comment.body, | |
| 180 | "user": person(&comment.author), | |
| 181 | "created_at": comment.created_at, | |
| 182 | "updated_at": comment.created_at, | |
| 183 | "path": comment.path, | |
| 184 | "line": comment.line, | |
| 185 | "html_url": format!("{SITE}/{full_name}/{page}/{number}#{}", comment.id), | |
| 186 | }) | |
| 187 | } | |
| 188 | ||
| 189 | /// An issue as `issue_comment` gives it for a pull request: the pull | |
| 190 | /// request's number and title, with `pull_request` set. | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 191 | pub fn pull_as_issue(repo: &Repo, pull: &Pull) -> Value { |
| GitHub Actions on g1t, part two: running workflows | 192 | let full_name = format!("{}/{}", repo.namespace, repo.name); |
| 193 | json!({ | |
| 194 | "id": pull.id, | |
| 195 | "number": pull.number, | |
| 196 | "title": pull.title, | |
| 197 | "body": pull.body, | |
| 198 | "state": if pull.status.is_active() { "open" } else { "closed" }, | |
| Teams and CODEOWNERS, labels and milestones, dependency updates, the security suite, and a clearer top bar | 199 | "labels": labels(&pull.labels), |
| 200 | "milestone": milestone(pull.milestone.as_ref()), | |
| GitHub Actions on g1t, part two: running workflows | 201 | "user": person(&pull.author), |
| g1t is the stored author of what it opens; the person who asked is requested_by and keeps the author's rights | 202 | "requested_by": pull.requested_by.as_ref().map(person), |
| GitHub Actions on g1t, part two: running workflows | 203 | "html_url": format!("{SITE}/{full_name}/pull/{}", pull.number), |
| 204 | "pull_request": { | |
| 205 | "url": format!("{API}/repos/{full_name}/pulls/{}", pull.number), | |
| 206 | "html_url": format!("{SITE}/{full_name}/pull/{}", pull.number), | |
| 207 | "merged_at": pull.merged_at, | |
| 208 | }, | |
| 209 | }) | |
| 210 | } | |
| 211 | ||
| Merge Actions: cross-repo workflows and actions, release and deployment triggers, step timeouts | 212 | /// A comment as the event that deleted it kept it (`comment.deleted`'s |
| 213 | /// `comment`), since it can no longer be read. | |
| 214 | pub fn deleted_comment(repo: &Repo, number: u32, kept: &Value, on_pull: bool) -> Value { | |
| 215 | let full_name = format!("{}/{}", repo.namespace, repo.name); | |
| 216 | let page = if on_pull { "pull" } else { "issues" }; | |
| 217 | let id = kept["id"].as_str().unwrap_or_default(); | |
| 218 | let login = kept["author"]["username"].as_str().unwrap_or_default(); | |
| 219 | json!({ | |
| 220 | "id": id, | |
| 221 | "body": kept["body"], | |
| 222 | "user": user(login), | |
| 223 | "created_at": kept["createdAt"], | |
| 224 | "updated_at": kept["createdAt"], | |
| 225 | "path": kept["path"], | |
| 226 | "line": kept["line"], | |
| 227 | "html_url": format!("{SITE}/{full_name}/{page}/{number}#{id}"), | |
| 228 | }) | |
| 229 | } | |
| 230 | ||
| 231 | /// A release, from what a `release.*` event carries | |
| 232 | /// (`g1t_contracts::about::Release`). | |
| 233 | pub fn release(repo: &Repo, release: &Value) -> Value { | |
| 234 | let full_name = format!("{}/{}", repo.namespace, repo.name); | |
| 235 | let tag = release["tagName"].as_str().unwrap_or_default(); | |
| 236 | let id = release["id"].as_str().unwrap_or_default(); | |
| 237 | json!({ | |
| 238 | "id": id, | |
| 239 | "node_id": id, | |
| 240 | "tag_name": tag, | |
| 241 | "target_commitish": release["target"], | |
| 242 | "name": release["name"].as_str().unwrap_or(tag), | |
| 243 | "body": release["body"], | |
| 244 | "draft": release["draft"].as_bool().unwrap_or(false), | |
| 245 | "prerelease": release["prerelease"].as_bool().unwrap_or(false), | |
| 246 | "created_at": release["createdAt"], | |
| 247 | "published_at": release["publishedAt"], | |
| 248 | "author": user(release["author"].as_str().unwrap_or(&repo.namespace)), | |
| 249 | "assets": [], | |
| 250 | "html_url": format!("{SITE}/{full_name}/releases/tag/{tag}"), | |
| 251 | "url": format!("{API}/repos/{full_name}/releases/{id}"), | |
| 252 | }) | |
| 253 | } | |
| 254 | ||
| 255 | /// A deployment, from what `deployment.created` and | |
| 256 | /// `deployment_status.created` carry (`RepoDeployment`, already in GitHub's | |
| 257 | /// spelling). | |
| 258 | pub fn deployment(repo: &Repo, deployment: &Value) -> Value { | |
| 259 | let full_name = format!("{}/{}", repo.namespace, repo.name); | |
| 260 | let id = deployment["id"].as_str().unwrap_or_default(); | |
| 261 | json!({ | |
| 262 | "id": id, | |
| 263 | "node_id": id, | |
| 264 | "sha": deployment["sha"], | |
| 265 | "ref": deployment["ref"], | |
| 266 | "task": deployment["task"].as_str().unwrap_or("deploy"), | |
| 267 | "payload": deployment.get("payload").filter(|p| p.is_object()).cloned().unwrap_or_else(|| json!({})), | |
| 268 | "environment": deployment["environment"], | |
| 269 | "original_environment": deployment["environment"], | |
| 270 | "description": deployment["description"], | |
| 271 | "creator": user(deployment["creator"].as_str().unwrap_or("g1t")), | |
| 272 | "transient_environment": deployment["transient_environment"].as_bool().unwrap_or(false), | |
| 273 | "production_environment": deployment["production_environment"].as_bool().unwrap_or(false), | |
| 274 | "created_at": deployment["created_at"], | |
| 275 | "updated_at": deployment["updated_at"], | |
| 276 | "url": format!("{API}/repos/{full_name}/deployments/{id}"), | |
| 277 | "statuses_url": format!("{API}/repos/{full_name}/deployments/{id}/statuses"), | |
| 278 | }) | |
| 279 | } | |
| 280 | ||
| 281 | /// A deployment's status (`DeploymentStatus`), for `deployment_status`. | |
| 282 | pub fn deployment_status(repo: &Repo, status: &Value, deployment: &Value) -> Value { | |
| 283 | let full_name = format!("{}/{}", repo.namespace, repo.name); | |
| 284 | let deployment_id = deployment["id"].as_str().unwrap_or_default(); | |
| 285 | let id = status["id"].as_str().unwrap_or_default(); | |
| 286 | json!({ | |
| 287 | "id": id, | |
| 288 | "node_id": id, | |
| 289 | "state": status["state"], | |
| 290 | "description": status["description"].as_str().unwrap_or_default(), | |
| 291 | "environment": deployment["environment"], | |
| 292 | "environment_url": status["environment_url"].as_str().unwrap_or_default(), | |
| 293 | "log_url": status["log_url"].as_str().unwrap_or_default(), | |
| 294 | "target_url": status["log_url"].as_str().unwrap_or_default(), | |
| 295 | "creator": user(status["creator"].as_str().unwrap_or("g1t")), | |
| 296 | "created_at": status["created_at"], | |
| 297 | "updated_at": status["created_at"], | |
| 298 | "deployment_url": format!("{API}/repos/{full_name}/deployments/{deployment_id}"), | |
| 299 | "url": format!("{API}/repos/{full_name}/deployments/{deployment_id}/statuses/{id}"), | |
| 300 | }) | |
| 301 | } | |
| 302 |
This file's history is long; its oldest lines are credited to the oldest commit read.