返回 CodeWhale
watch_event.rs
根目录 / crates / app-server-protocol / src / runtime / watch_event.rs
1 use serde::{Deserialize, Serialize};
2
3 pub const MAX_WATCH_EVENT_RECEIPT_BYTES: usize = 4096;
4 const MAX_SAFE_INTEGER: u64 = 9_007_199_254_740_991;
5
6 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
7 #[serde(rename_all = "camelCase", deny_unknown_fields)]
8 pub struct WatchEventReceipt {
9 pub version: u32,
10 pub platform_account_id: String,
11 pub project_id: String,
12 pub watch_id: String,
13 pub watch_revision: u64,
14 pub definition_sha256: String,
15 pub source_kind: WatchEventSourceKind,
16 pub occurrence_sha256: String,
17 pub event_sha256: String,
18 pub run_id: String,
19 pub room_thread_root_id: String,
20 pub source: WatchEventSource,
21 }
22
23 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
24 pub enum WatchEventSourceKind {
25 #[serde(rename = "schedule")]
26 Schedule,
27 #[serde(rename = "github.pull_request")]
28 GitHubPullRequest,
29 }
30
31 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
32 #[serde(untagged)]
33 pub enum WatchEventSource {
34 Schedule(WatchScheduleSource),
35 GitHubPullRequest(WatchPullRequestSource),
36 }
37
38 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
39 #[serde(deny_unknown_fields)]
40 pub struct WatchScheduleSource {
41 pub slot: String,
42 }
43
44 #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
45 #[serde(rename_all = "camelCase", deny_unknown_fields)]
46 pub struct WatchPullRequestSource {
47 pub installation_id: String,
48 pub binding_id: String,
49 pub repo: String,
50 pub number: u64,
51 pub delivery_id: String,
52 pub event_name: String,
53 }
54
55 fn reference(value: &str) -> bool {
56 (1..=200).contains(&value.len())
57 && value.as_bytes()[0].is_ascii_alphanumeric()
58 && value
59 .bytes()
60 .all(|byte| byte.is_ascii_alphanumeric() || b"._:/@#-".contains(&byte))
61 }
62
63 fn fingerprint(value: &str) -> bool {
64 value.len() == 64
65 && value
66 .bytes()
67 .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
68 }
69
70 fn canonical_utc_slot(value: &str) -> bool {
71 value.len() == 24
72 && chrono::DateTime::parse_from_rfc3339(value).is_ok_and(|parsed| {
73 parsed.timestamp_subsec_nanos() < 1_000_000_000
74 && parsed.to_rfc3339_opts(chrono::SecondsFormat::Millis, true) == value
75 })
76 }
77
78 impl WatchEventReceipt {
79 pub fn validate(&self) -> Result<(), &'static str> {
80 if self.version != 1 || self.watch_revision == 0 || self.watch_revision > MAX_SAFE_INTEGER {
81 return Err("unsupported watch event version or revision");
82 }
83 if [
84 &self.platform_account_id,
85 &self.project_id,
86 &self.watch_id,
87 &self.run_id,
88 ]
89 .iter()
90 .any(|value| !reference(value))
91 || [
92 &self.definition_sha256,
93 &self.occurrence_sha256,
94 &self.event_sha256,
95 ]
96 .iter()
97 .any(|value| !fingerprint(value))
98 {
99 return Err("invalid watch event reference or fingerprint");
100 }
101 if !self.room_thread_root_id.is_empty()
102 && (self.room_thread_root_id.len() != 35
103 || !self.room_thread_root_id.starts_with("pm_")
104 || !self.room_thread_root_id.as_bytes()[3..]
105 .iter()
106 .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(byte)))
107 {
108 return Err("invalid watch event room thread");
109 }
110 match (&self.source_kind, &self.source) {
111 (WatchEventSourceKind::Schedule, WatchEventSource::Schedule(source)) => {
112 if source.slot.len() != 24
113 || !reference(&source.slot)
114 || !canonical_utc_slot(&source.slot)
115 {
116 return Err("invalid watch event schedule occurrence");
117 }
118 }
119 (
120 WatchEventSourceKind::GitHubPullRequest,
121 WatchEventSource::GitHubPullRequest(source),
122 ) => {
123 let valid_repo = source.repo.split_once('/').is_some_and(|(owner, name)| {
124 [owner, name].iter().all(|part| {
125 (1..=100).contains(&part.len())
126 && part
127 .bytes()
128 .all(|byte| byte.is_ascii_alphanumeric() || b"_.-".contains(&byte))
129 })
130 });
131 if source.number == 0
132 || source.number > MAX_SAFE_INTEGER
133 || [
134 &source.installation_id,
135 &source.binding_id,
136 &source.delivery_id,
137 &source.event_name,
138 ]
139 .iter()
140 .any(|value| !reference(value))
141 || !valid_repo
142 {
143 return Err("invalid watch event pull request occurrence");
144 }
145 }
146 _ => return Err("watch event kind does not match its source"),
147 }
148 if serde_json::to_vec(self)
149 .map_err(|_| "watch event cannot be encoded")?
150 .len()
151 > MAX_WATCH_EVENT_RECEIPT_BYTES
152 {
153 return Err("watch event receipt exceeds 4 KiB");
154 }
155 Ok(())
156 }
157 }
158
158 lines RUST