-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchangeset-webhook.pl
More file actions
306 lines (285 loc) · 15.1 KB
/
Copy pathchangeset-webhook.pl
File metadata and controls
306 lines (285 loc) · 15.1 KB
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
:- module('plugins/change_tracker', []).
:- use_module(core(transaction)).
:- use_module(core(util)).
:- use_module(library(http/http_client)).
:- use_module(library(http/json)).
:- use_module(library(terminus_store)).
plugins:post_commit_hook(Validation_Objects, Meta_Data) :-
% Log that hook was triggered
json_log_info_formatted("=== CHANGE TRACKER HOOK TRIGGERED ===", []),
json_log_info_formatted("Metadata - inserts: ~q, deletes: ~q", [Meta_Data.inserts, Meta_Data.deletes]),
% Filter for branch commits only
[Validation_Object|_] = Validation_Objects,
json_log_info_formatted("Validation object descriptor type: ~q", [Validation_Object.descriptor]),
( is_dict(Validation_Object.descriptor, branch_descriptor)
-> json_log_info_formatted("Processing branch commit...", [])
; json_log_info_formatted("Not a branch commit, skipping. Descriptor type: ~q", [Validation_Object.descriptor]),
fail
),
% Get commit ID
Branch_Name = (Validation_Object.descriptor.branch_name),
json_log_info_formatted("Branch name: ~q", [Branch_Name]),
Commit_Graph = (Validation_Object.parent),
branch_head_commit(Commit_Graph, Branch_Name, Commit_Uri),
commit_id_uri(Commit_Graph, Commit_Id, Commit_Uri),
json_log_info_formatted("Commit ID: ~q", [Commit_Id]),
% Get other commit metadata
commit_uri_to_metadata(Commit_Graph, Commit_Uri, Author, Message, Timestamp),
json_log_info_formatted("Commit metadata - Author: ~q, Message: ~q", [Author, Message]),
% Build the collection path with commit ID
atom_string(Commit_Id, Commit_Id_String),
atom_concat('commit/', Commit_Id_String, Collection_Atom),
atom_string(Collection_Atom, _Collection_Path),
/* REFERENCE: Original WOQL query structure - keeping for reference
% https://medium.com/dfrnt-com/finding-changed-json-ld-objects-using-graph-branching-commit-history-and-a-datalog-63674f44f244
% This query would detect document changes but causes transaction conflicts in hooks
% The structure matches the frontend /api/woql endpoint query
_Query = json{
query: json{
'@type': 'Limit',
limit: 50,
query: json{
'@type': 'Using',
collection: Collection_Path,
query: json{
'@type': 'Or',
or: [
json{
'@type': 'And',
and: [
json{
'@type': 'AddedQuad',
subject: json{'@type': 'NodeValue', variable: 'added'},
predicate: json{'@type': 'NodeValue', node: 'rdf:type'},
object: json{'@type': 'Value', variable: 'type1'},
graph: "instance"
},
json{
'@type': 'Not',
query: json{
'@type': 'Triple',
subject: json{'@type': 'NodeValue', variable: 'type1'},
predicate: json{'@type': 'NodeValue', node: 'sys:subdocument'},
object: json{'@type': 'Value', node: 'rdf:nil'},
graph: "schema"
}
},
json{
'@type': 'Equals',
left: json{'@type': 'Value', variable: 'id'},
right: json{'@type': 'Value', variable: 'added'}
},
json{
'@type': 'Equals',
left: json{'@type': 'Value', variable: 'action'},
right: json{'@type': 'Value', node: 'added'}
}
]
},
json{
'@type': 'Distinct',
variables: ['id'],
query: json{
'@type': 'And',
and: [
json{
'@type': 'Or',
or: [
json{
'@type': 'AddedQuad',
subject: json{'@type': 'NodeValue', variable: 'updated_triple'},
predicate: json{'@type': 'NodeValue', variable: 'upd_pred'},
object: json{'@type': 'Value', variable: 'upd_o'},
graph: "instance"
},
json{
'@type': 'RemovedQuad',
subject: json{'@type': 'NodeValue', variable: 'updated_triple'},
predicate: json{'@type': 'NodeValue', variable: 'upd_pred'},
object: json{'@type': 'Value', variable: 'upd_o'},
graph: "instance"
}
]
},
json{
'@type': 'Path',
subject: json{'@type': 'NodeValue', variable: 'updated'},
pattern: json{
'@type': 'PathStar',
star: json{'@type': 'InversePathPredicate', predicate: ''}
},
object: json{'@type': 'Value', variable: 'updated_triple'}
},
json{
'@type': 'Triple',
subject: json{'@type': 'NodeValue', variable: 'updated'},
predicate: json{'@type': 'NodeValue', node: 'rdf:type'},
object: json{'@type': 'Value', variable: 'type3'}
},
json{
'@type': 'Not',
query: json{
'@type': 'Triple',
subject: json{'@type': 'NodeValue', variable: 'type3'},
predicate: json{'@type': 'NodeValue', node: 'sys:subdocument'},
object: json{'@type': 'Value', node: 'rdf:nil'},
graph: "schema"
}
},
json{
'@type': 'Equals',
left: json{'@type': 'Value', variable: 'id'},
right: json{'@type': 'Value', variable: 'updated'}
},
json{
'@type': 'Equals',
left: json{'@type': 'Value', variable: 'action'},
right: json{'@type': 'Value', node: 'updated'}
}
]
}
},
json{
'@type': 'And',
and: [
json{
'@type': 'RemovedQuad',
subject: json{'@type': 'NodeValue', variable: 'deleted'},
predicate: json{'@type': 'NodeValue', node: 'rdf:type'},
object: json{'@type': 'Value', variable: 'type2'},
graph: "instance"
},
json{
'@type': 'Not',
query: json{
'@type': 'Triple',
subject: json{'@type': 'NodeValue', variable: 'type2'},
predicate: json{'@type': 'NodeValue', node: 'sys:subdocument'},
object: json{'@type': 'Value', node: 'rdf:nil'},
graph: "schema"
}
},
json{
'@type': 'Equals',
left: json{'@type': 'Value', variable: 'id'},
right: json{'@type': 'Value', variable: 'deleted'}
},
json{
'@type': 'Equals',
left: json{'@type': 'Value', variable: 'action'},
right: json{'@type': 'Value', node: 'deleted'}
}
]
}
]
}
}
}
},
*/
% TODO: Use internal methods to query commit data without creating new transaction
% Extract layers from validation object to query changes directly
json_log_info_formatted("Extracting layers from validation object...", []),
% Get the instance layer from the validation object
( Validation_Object.instance_objects = [Instance_Object|_],
Layer = Instance_Object.read
-> json_log_info_formatted("Found instance layer: ~q", [Layer]),
% Query for document changes using proper TerminusDB patterns
catch(
(json_log_info_formatted("Starting document change detection...", []),
% Get rdf:type predicate ID
global_prefix_expand(rdf:type, Rdf_Type),
terminus_store:predicate_id(Layer, Rdf_Type, Rdf_Type_Id),
% Find added documents (new rdf:type triples)
findall(json{id: Subject_String, action: "added"},
(terminus_store:id_triple_addition(Layer, S_Id, Rdf_Type_Id, Type_Id),
terminus_store:subject_id(Layer, Subject_String, S_Id),
\+ terminus_store:id_triple_removal(Layer, S_Id, _, _)),
Added_Docs),
% Find deleted documents (removed rdf:type triples)
findall(json{id: Subject_String, action: "deleted"},
(terminus_store:id_triple_removal(Layer, S_Id, Rdf_Type_Id, Type_Id),
terminus_store:subject_id(Layer, Subject_String, S_Id),
\+ terminus_store:id_triple_addition(Layer, S_Id, _, _)),
Deleted_Docs),
% Find updated documents (both additions and removals for same subject)
findall(json{id: Subject_String, action: "updated"},
distinct(Subject_String,
(terminus_store:id_triple_addition(Layer, S_Id, _, _),
terminus_store:id_triple_removal(Layer, S_Id, _, _),
terminus_store:subject_id(Layer, Subject_String, S_Id))),
Updated_Docs),
json_log_info_formatted("Found ~w added, ~w deleted, ~w updated documents",
[length(Added_Docs), length(Deleted_Docs), length(Updated_Docs)])),
Layer_Error,
(json_log_error_formatted("Error detecting document changes: ~q", [Layer_Error]),
Added_Docs = [],
Deleted_Docs = [],
Updated_Docs = [])
),
% Combine all results
append([Added_Docs, Deleted_Docs, Updated_Docs], All_Changes),
% Remove duplicates and limit to 50
list_to_set(All_Changes, Unique_Changes),
length(Unique_Changes, Changes_Count),
(Changes_Count > 50 ->
append(First_50, _, Unique_Changes),
length(First_50, 50),
Limited_Changes = First_50
;
Limited_Changes = Unique_Changes
),
json_log_info_formatted("Found ~q document changes (limited to 50)", [Changes_Count]),
Query_Response = json{
bindings: Limited_Changes
}
; json_log_info_formatted("No instance layer found in validation object", []),
Query_Response = json{
bindings: [],
error: "No instance layer found"
}
),
% Get resource path
json_log_info_formatted("Getting resource path...", []),
Branch_Descriptor = Validation_Object.descriptor,
resolve_absolute_string_descriptor(Resource, Branch_Descriptor),
json_log_info_formatted("Resource path: ~q", [Resource]),
% Build complete webhook payload
Payload = json{
resource: Resource,
branch: Branch_Name,
commit: json{
id: Commit_Id,
author: Author,
message: Message,
timestamp: Timestamp
},
metadata: json{
inserts_count: Meta_Data.inserts,
deletes_count: Meta_Data.deletes
},
query_results: Query_Response
},
% Get webhook URL from environment variable or skip webhook
( getenv('TERMINUSDB_CHANGESET_WEBHOOK_URL', Webhook_URL),
Webhook_URL \= ''
-> % Webhook URL is configured, proceed with sending
json_log_info_formatted("Changeset webhook URL configured: ~q", [Webhook_URL]),
json_log_info_formatted("Payload size: ~q bytes", [Meta_Data.inserts + Meta_Data.deletes]),
catch(
(
json_log_info_formatted("Sending webhook POST request...", []),
http_post(Webhook_URL,
json(Payload),
_Response,
[]),
% Log success
json_log_info_formatted("Successfully sent change data for commit ~q to webhook", [Commit_Id])
),
Error,
json_log_error_formatted("Failed to send webhook for commit ~q: ~q", [Commit_Id, Error])
)
; % No webhook URL configured, log warning and skip
json_log_info_formatted("WARNING: TERMINUSDB_CHANGESET_WEBHOOK_URL environment variable not set - skipping webhook notification", [])
),
json_log_info_formatted("=== CHANGE TRACKER HOOK COMPLETED ===", []).root