upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/nostr/policy/pr_event.rs
blob: c7602b0648265e71a9433272c81dc6d92ff20e02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/// PR Event Policy - PR/PR Update validation
///
/// Handles validation of NIP-34 PR events (kind 1618) and PR Update events (kind 1619)
/// according to GRASP-01 specification.
use nostr_relay_builder::prelude::{Alphabet, Event, Filter, Kind, PublicKey, SingleLetterTag};

use super::PolicyContext;
use crate::git;
use crate::nostr::events::{RepositoryAnnouncement, KIND_REPOSITORY_ANNOUNCEMENT};

/// Policy for validating PR and PR Update events
#[derive(Clone)]
pub struct PrEventPolicy {
    ctx: PolicyContext,
}

impl PrEventPolicy {
    pub fn new(ctx: PolicyContext) -> Self {
        Self { ctx }
    }

    /// Check if git data exists for a PR event
    ///
    /// This checks:
    /// 1. If a placeholder exists (git-data-first scenario)
    /// 2. If the commit exists in any relevant repository
    ///
    /// # Returns
    /// - `Ok(true)` if git data ready (either placeholder found or commit exists)
    /// - `Ok(false)` if git data missing (should add to purgatory)
    /// - `Err(msg)` on errors
    pub async fn check_git_data_exists(&self, event: &Event) -> Result<bool, String> {
        let event_id = event.id.to_hex();

        // Extract the `c` tag (commit hash) from the PR event
        let commit = event.tags.iter().find_map(|tag| {
            let tag_vec = tag.clone().to_vec();
            if tag_vec.len() >= 2 && tag_vec[0] == "c" {
                Some(tag_vec[1].clone())
            } else {
                None
            }
        });

        let commit = match commit {
            Some(c) => c,
            None => {
                return Err(format!("PR event {} has no 'c' tag", event_id));
            }
        };

        // Check for placeholder first (git-data-first scenario)
        if let Some(placeholder_commit) = self.ctx.purgatory.find_pr_placeholder(&event_id) {
            if placeholder_commit == commit {
                // Perfect match - git data arrived first with matching commit
                tracing::debug!(
                    "Found matching placeholder for PR event {} with commit {}",
                    event_id,
                    commit
                );
                // Remove placeholder - event processing will continue normally
                self.ctx.purgatory.remove_pr(&event_id);
                return Ok(true);
            } else {
                // Placeholder has different commit - incoming event supersedes
                tracing::info!(
                    "PR event {} supersedes placeholder: event expects commit {}, placeholder has {}",
                    event_id,
                    commit,
                    placeholder_commit
                );
                // Remove placeholder with old commit data
                self.ctx.purgatory.remove_pr(&event_id);
                // TODO: Also remove git data (refs/nostr/<event-id>) - Phase 5
                // Fall through to check if new commit exists
            }
        }

        // Check if commit exists in any repository referenced by this PR
        // Extract ALL `a` tags (repository references) from the PR event
        let repo_refs: Vec<String> = event
            .tags
            .iter()
            .filter_map(|tag| {
                let tag_vec = tag.clone().to_vec();
                if tag_vec.len() >= 2 && tag_vec[0] == "a" && tag_vec[1].starts_with("30617:") {
                    Some(tag_vec[1].clone())
                } else {
                    None
                }
            })
            .collect();

        if repo_refs.is_empty() {
            // No repo references - cannot check git data
            // This is unusual but let it through (other validation will catch issues)
            return Ok(true);
        }

        // Check each repository to see if commit exists
        for repo_ref in repo_refs {
            // Parse the repo reference: 30617:<pubkey>:<identifier>
            let parts: Vec<&str> = repo_ref.split(':').collect();
            if parts.len() < 3 {
                continue;
            }

            let repo_pubkey = match PublicKey::from_hex(parts[1]) {
                Ok(pk) => pk,
                Err(_) => continue,
            };
            let identifier = parts[2];

            // Look up repository announcement to get the npub for path
            let filter = Filter::new()
                .kind(Kind::from(KIND_REPOSITORY_ANNOUNCEMENT))
                .author(repo_pubkey)
                .custom_tag(
                    SingleLetterTag::lowercase(Alphabet::D),
                    identifier.to_string(),
                );

            let announcements: Vec<Event> = match self.ctx.database.query(filter).await {
                Ok(events) => events.into_iter().collect(),
                Err(e) => {
                    tracing::warn!(
                        "Failed to query for repository announcement for PR {}: {}",
                        event_id,
                        e
                    );
                    continue;
                }
            };

            if announcements.is_empty() {
                continue;
            }

            // Check each matching announcement
            for announcement_event in announcements {
                let announcement = match RepositoryAnnouncement::from_event(announcement_event) {
                    Ok(a) => a,
                    Err(_) => continue,
                };

                // Build repository path
                let repo_path = self.ctx.git_data_path.join(announcement.repo_path());

                // Check if commit exists
                if git::commit_exists(&repo_path, &commit) {
                    tracing::debug!(
                        "Found commit {} for PR event {} in repository {}",
                        commit,
                        event_id,
                        repo_path.display()
                    );
                    return Ok(true);
                }
            }
        }

        // No git data found - should add to purgatory
        tracing::debug!(
            "No git data found for PR event {} with commit {}",
            event_id,
            commit
        );
        Ok(false)
    }

    /// Validate refs/nostr/<event-id> ref against a PR or PR Update event's `c` tag
    ///
    /// When a PR event (kind 1618) or PR Update event (kind 1619) is received,
    /// this checks if a corresponding refs/nostr/<event-id> ref exists in the
    /// repository and validates that it points to the correct commit (from the
    /// `c` tag). If the ref exists but points to a different commit, the ref is
    /// deleted.
    ///
    /// PR and PR Update events can have multiple `a` tags to update multiple
    /// repositories simultaneously.
    ///
    /// This is part of GRASP-01 compliance: ensuring refs/nostr refs are consistent
    /// with their corresponding events.
    ///
    /// # Returns
    /// Ok(Some(n)) if n refs were deleted, Ok(None) if no action taken, Err on failure
    pub async fn validate_nostr_ref(&self, event: &Event) -> Result<Option<usize>, String> {
        let event_id = event.id.to_hex();

        // Extract the `c` tag (commit hash) from the PR event
        let expected_commit = event.tags.iter().find_map(|tag| {
            let tag_vec = tag.clone().to_vec();
            if tag_vec.len() >= 2 && tag_vec[0] == "c" {
                Some(tag_vec[1].clone())
            } else {
                None
            }
        });

        let expected_commit = match expected_commit {
            Some(c) => c,
            None => {
                tracing::debug!(
                    "PR event {} has no 'c' tag, skipping ref validation",
                    event_id
                );
                return Ok(None);
            }
        };

        // Extract ALL `a` tags (repository references) from the PR event
        // PR events can reference multiple repositories
        // Format: 30617:<pubkey>:<identifier>
        let repo_refs: Vec<String> = event
            .tags
            .iter()
            .filter_map(|tag| {
                let tag_vec = tag.clone().to_vec();
                if tag_vec.len() >= 2 && tag_vec[0] == "a" && tag_vec[1].starts_with("30617:") {
                    Some(tag_vec[1].clone())
                } else {
                    None
                }
            })
            .collect();

        if repo_refs.is_empty() {
            tracing::debug!(
                "PR event {} has no repo 'a' tags, skipping ref validation",
                event_id
            );
            return Ok(None);
        }

        let mut deleted_count = 0;

        // Process each repository reference
        for repo_ref in repo_refs {
            // Parse the repo reference: 30617:<pubkey>:<identifier>
            let parts: Vec<&str> = repo_ref.split(':').collect();
            if parts.len() < 3 {
                tracing::debug!(
                    "PR event {} has invalid 'a' tag format: {}",
                    event_id,
                    repo_ref
                );
                continue;
            }

            let repo_pubkey = match PublicKey::from_hex(parts[1]) {
                Ok(pk) => pk,
                Err(_) => {
                    tracing::debug!(
                        "PR event {} has invalid pubkey in 'a' tag: {}",
                        event_id,
                        parts[1]
                    );
                    continue;
                }
            };
            let identifier = parts[2];

            // Look up repository announcement to get the npub for path
            let filter = Filter::new()
                .kind(Kind::from(KIND_REPOSITORY_ANNOUNCEMENT))
                .author(repo_pubkey)
                .custom_tag(
                    SingleLetterTag::lowercase(Alphabet::D),
                    identifier.to_string(),
                );

            let announcements: Vec<Event> = match self.ctx.database.query(filter).await {
                Ok(events) => events.into_iter().collect(),
                Err(e) => {
                    tracing::warn!(
                        "Failed to query for repository announcement for PR {}: {}",
                        event_id,
                        e
                    );
                    continue;
                }
            };

            if announcements.is_empty() {
                tracing::debug!(
                    "No repository announcement found for PR event {} (repo {}:{})",
                    event_id,
                    repo_pubkey.to_hex(),
                    identifier
                );
                continue;
            }

            // Process each matching announcement (there could be multiple)
            for announcement_event in announcements {
                let announcement = match RepositoryAnnouncement::from_event(announcement_event) {
                    Ok(a) => a,
                    Err(e) => {
                        tracing::warn!(
                            "Failed to parse announcement for PR {} validation: {}",
                            event_id,
                            e
                        );
                        continue;
                    }
                };

                // Build repository path
                let repo_path = self.ctx.git_data_path.join(announcement.repo_path());

                // Validate the ref
                match git::validate_nostr_ref(&repo_path, &event_id, &expected_commit) {
                    Ok(true) => {
                        tracing::info!(
                            "Deleted mismatched refs/nostr/{} in {} (expected commit {})",
                            event_id,
                            repo_path.display(),
                            expected_commit
                        );
                        deleted_count += 1;
                    }
                    Ok(false) => {
                        tracing::debug!(
                            "refs/nostr/{} in {} is valid or doesn't exist",
                            event_id,
                            repo_path.display()
                        );
                    }
                    Err(e) => {
                        tracing::warn!(
                            "Failed to validate refs/nostr/{} in {}: {}",
                            event_id,
                            repo_path.display(),
                            e
                        );
                    }
                }
            }
        }

        if deleted_count > 0 {
            Ok(Some(deleted_count))
        } else {
            Ok(None)
        }
    }
}