forked from warestack/watchflow-landing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
972 lines (835 loc) · 30.3 KB
/
script.js
File metadata and controls
972 lines (835 loc) · 30.3 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
// DOM Elements - Rule Generation
const ruleInput = document.getElementById('ruleInput');
const analyzeBtn = document.getElementById('analyzeBtn');
const resultSection = document.getElementById('resultSection');
const loadingIndicator = document.getElementById('loadingIndicator');
const errorAlert = document.getElementById('errorAlert');
const errorMessage = document.getElementById('errorMessage');
const codeContent = document.getElementById('codeContent');
const downloadBtn = document.getElementById('downloadBtn');
const copyIconBtn = document.getElementById('copyIconBtn');
// DOM Elements - Repository Analysis
const repoUrlInput = document.getElementById('repoUrlInput');
const analyzeRepoBtn = document.getElementById('analyzeRepoBtn');
const repoAnalysisResult = document.getElementById('repoAnalysisResult');
const repoLoadingIndicator = document.getElementById('repoLoadingIndicator');
const repoErrorAlert = document.getElementById('repoErrorAlert');
const repoErrorMessage = document.getElementById('repoErrorMessage');
const repoCodeContent = document.getElementById('repoCodeContent');
const copyRepoCodeBtn = document.getElementById('copyRepoCodeBtn');
const proceedWithPrBtn = document.getElementById('proceedWithPrBtn');
const downloadRulesBtn = document.getElementById('downloadRulesBtn');
const recommendationsList = document.getElementById('recommendationsList');
const prPlanDetails = document.getElementById('prPlanDetails');
// showExamplesBtn removed - examples are now always visible
const examplesGrid = document.getElementById('examplesGrid');
// State management
let isLoading = false;
let currentRule = '';
let isRepoLoading = false;
let currentRepoAnalysis = null;
// API Configuration
const API_ENDPOINT = 'https://api.watchflow.dev/api/v1/rules/evaluate';
const REPO_ANALYZE_ENDPOINT = "https://api.watchflow.dev/api/v1/rules/recommend";
const PROCEED_WITH_PR_ENDPOINT = 'https://httpbin.org/post'; // Mock endpoint
const USE_MOCK_DATA = false; // Set to false when backend is available
// Initialize the application
document.addEventListener('DOMContentLoaded', function() {
initializeEventListeners();
hideAllStates();
// Only generate stars on non-mobile devices
if (window.innerWidth > 768) {
generateRandomStars();
}
});
// Event Listeners
function initializeEventListeners() {
// Rule Generation Events
analyzeBtn.addEventListener('click', handleAnalyzeClick);
ruleInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
handleAnalyzeClick();
}
});
downloadBtn.addEventListener('click', handleDownloadClick);
copyIconBtn.addEventListener('click', handleCopyClick);
ruleInput.addEventListener('input', handleInputChange);
// Repository Analysis Events
analyzeRepoBtn.addEventListener('click', handleRepoAnalyzeClick);
repoUrlInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter') {
e.preventDefault();
handleRepoAnalyzeClick();
}
});
copyRepoCodeBtn.addEventListener('click', handleRepoCopyClick);
proceedWithPrBtn.addEventListener('click', handleProceedWithPrClick);
downloadRulesBtn.addEventListener('click', handleRepoDownloadClick);
repoUrlInput.addEventListener('input', handleRepoInputChange);
// Examples are now always visible - no button needed
}
// Handle analyze button click
async function handleAnalyzeClick() {
const inputValue = ruleInput.value.trim();
if (!inputValue) {
showError('Please enter a rule description');
return;
}
if (isLoading) return;
try {
isLoading = true;
showLoading();
// Start the rule generation and minimum loading time in parallel
const [rule] = await Promise.all([
generateRule(inputValue),
new Promise(resolve => setTimeout(resolve, 3000)) // Always show loading for 3 seconds
]);
showSuccess(rule);
} catch (error) {
console.error('Error generating rule:', error);
// Even on error, wait for the minimum loading time
await new Promise(resolve => setTimeout(resolve, 3000));
showError(error.message || 'Failed to generate rule. Please try again.');
} finally {
isLoading = false;
}
}
// Generate rule via API
async function generateRule(description) {
const response = await fetch(API_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
rule_text: description,
event_data: null
}),
});
if (!response.ok) {
// Handle validation errors or other API errors
if (response.status === 422) {
const errorData = await response.json();
throw new Error(`Validation error: ${errorData.detail || 'Invalid rule description format'}`);
}
throw new Error(`API error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
if (!data) {
throw new Error('Empty response from API');
}
// Check if the rule is supported
if (!data.supported) {
const reason = data.feedback || 'This type of rule is not supported by Watchflow';
throw new Error(reason);
}
// Extract the YAML snippet from the response
if (data.snippet) {
// Remove the markdown code block wrapper if present
let yaml = data.snippet;
if (yaml.startsWith('```yaml\n')) {
yaml = yaml.replace(/^```yaml\n/, '').replace(/\n```$/, '');
}
return yaml;
}
throw new Error('No rule snippet found in API response');
}
// Analyze repository via API (with mock data for demo)
async function analyzeRepository(repoUrl) {
if (USE_MOCK_DATA) {
// Simulate API delay
await new Promise(resolve => setTimeout(resolve, 2000));
// Extract repo info from URL
const urlMatch = repoUrl.match(/github\.com\/([^\/]+)\/([^\/]+)/);
const repoName = urlMatch ? `${urlMatch[1]}/${urlMatch[2]}` : 'unknown/repo';
// Return mock analysis data
return {
repository_full_name: repoName,
rules_yaml: `rules:
- description: "Require tests when code changes"
enabled: true
severity: medium
event_types:
- pull_request
validators:
- type: diff_pattern
parameters:
file_patterns:
- "**/*.py"
- "**/*.ts"
- "**/*.tsx"
- "**/*.js"
- type: related_tests
parameters:
search_paths:
- "**/tests/**"
- "**/*_test.py"
actions:
- type: warn
parameters:
message: "Please include or update tests for code changes."
- description: "Ensure PRs include context"
enabled: true
severity: low
event_types:
- pull_request
validators:
- type: required_field_in_diff
parameters:
field: "body"
pattern: "(?i)(summary|context|issue)"
actions:
- type: warn
parameters:
message: "Add a short summary and linked issue in the PR body."`,
recommendations: [
{
yaml_rule: `description: "Require tests when code changes"
enabled: true
severity: medium
event_types:
- pull_request
validators:
- type: diff_pattern
parameters:
file_patterns:
- "**/*.py"
- "**/*.ts"
- "**/*.tsx"
- "**/*.js"
- type: related_tests
parameters:
search_paths:
- "**/tests/**"
- "**/*_test.py"
actions:
- type: warn
parameters:
message: "Please include or update tests for code changes."`,
confidence: 0.74,
reasoning: "Default guardrail for code changes without tests.",
strategy_used: "static"
},
{
yaml_rule: `description: "Ensure PRs include context"
enabled: true
severity: low
event_types:
- pull_request
validators:
- type: required_field_in_diff
parameters:
field: "body"
pattern: "(?i)(summary|context|issue)"
actions:
- type: warn
parameters:
message: "Add a short summary and linked issue in the PR body."`,
confidence: 0.68,
reasoning: "Encourages context for reviewers; lightweight default.",
strategy_used: "static"
}
],
pr_plan: {
branch_name: "watchflow/rules",
base_branch: "main",
commit_message: "chore: add Watchflow rules",
title: "Add Watchflow rules",
body: "This PR adds Watchflow rule recommendations generated by Watchflow.",
file_path: ".watchflow/rules.yaml"
},
analysis_summary: {
repository_features: {
has_contributing: true,
has_codeowners: false,
has_workflows: true,
workflow_count: 3,
language: "Python",
contributor_count: 12,
pr_count: 25
},
contributing: {
has_pr_template: true,
has_issue_template: true,
requires_tests: true,
requires_docs: false,
code_style_requirements: ["black", "flake8"],
review_requirements: ["review", "approval"]
}
}
};
}
// Real API call (when backend is available)
const response = await fetch(REPO_ANALYZE_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
repository_url: repoUrl,
max_prs: 10
}),
});
if (!response.ok) {
if (response.status === 422) {
const errorData = await response.json();
throw new Error(`Validation error: ${errorData.detail || 'Invalid repository URL'}`);
} else if (response.status === 404) {
throw new Error('Repository not found. Please check the URL and ensure it\'s a public repository.');
} else if (response.status === 403) {
throw new Error('Access denied. This repository may be private or require authentication.');
}
throw new Error(`API error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
if (!data) {
throw new Error('Empty response from API');
}
return data;
}
// Create PR with generated rules (with mock data for demo)
async function createPullRequest(analysisData) {
if (USE_MOCK_DATA) {
// Simulate API delay
await new Promise(resolve => setTimeout(resolve, 1500));
// Mock successful PR creation
return {
repository_full_name: analysisData.repository_full_name,
branch_name: analysisData.pr_plan?.branch_name || 'watchflow/rules',
base_branch: analysisData.pr_plan?.base_branch || 'main',
file_path: analysisData.pr_plan?.file_path || '.watchflow/rules.yaml',
pull_request_url: `https://github.com/${analysisData.repository_full_name}/pull/123`,
pull_request_number: 123,
commit_sha: 'abc123def456'
};
}
// Real API call (when backend is available)
const response = await fetch(PROCEED_WITH_PR_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
repository_full_name: analysisData.repository_full_name,
rules_yaml: analysisData.rules_yaml,
base_branch: analysisData.pr_plan?.base_branch || 'main',
branch_name: analysisData.pr_plan?.branch_name || 'watchflow/rules',
pr_title: analysisData.pr_plan?.title || 'Add Watchflow rules',
pr_body: analysisData.pr_plan?.body || 'Adds Watchflow rule recommendations.',
commit_message: analysisData.pr_plan?.commit_message || 'chore: add .watchflow/rules.yaml',
file_path: analysisData.pr_plan?.file_path || '.watchflow/rules.yaml'
}),
});
if (!response.ok) {
if (response.status === 422) {
const errorData = await response.json();
throw new Error(`Validation error: ${errorData.detail || 'Invalid PR creation request'}`);
} else if (response.status === 403) {
throw new Error('Authentication required. Please install the Watchflow GitHub App first.');
} else if (response.status === 404) {
throw new Error('Repository not found or access denied.');
}
throw new Error(`API error: ${response.status} ${response.statusText}`);
}
const data = await response.json();
if (!data) {
throw new Error('Empty response from API');
}
return data;
}
// Handle download button click
function handleDownloadClick() {
if (!currentRule) return;
// Create blob with YAML content
const blob = new Blob([currentRule], { type: 'text/yaml' });
const url = URL.createObjectURL(blob);
// Create temporary download link
const a = document.createElement('a');
a.href = url;
a.download = 'rules.yaml';
document.body.appendChild(a);
a.click();
// Cleanup
document.body.removeChild(a);
URL.revokeObjectURL(url);
// Show feedback
showDownloadFeedback();
}
// Handle copy icon button click
async function handleCopyClick() {
if (!currentRule) return;
try {
await navigator.clipboard.writeText(currentRule);
showCopyFeedback();
} catch (error) {
// Fallback for older browsers
fallbackCopyToClipboard(currentRule);
showCopyFeedback();
}
}
// Fallback copy method
function fallbackCopyToClipboard(text) {
const textArea = document.createElement('textarea');
textArea.value = text;
textArea.style.position = 'fixed';
textArea.style.top = '0';
textArea.style.left = '0';
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = '0';
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
// Note: document.execCommand is deprecated but kept for compatibility
const success = document.execCommand('copy');
if (!success) {
throw new Error('Copy command failed');
}
} catch (err) {
console.error('Fallback copy failed:', err);
}
document.body.removeChild(textArea);
}
// Show download feedback
function showDownloadFeedback() {
const originalText = downloadBtn.textContent;
downloadBtn.textContent = 'Downloaded!';
downloadBtn.style.background = '#4CAF50';
setTimeout(() => {
downloadBtn.textContent = originalText;
downloadBtn.style.background = '';
}, 1500);
}
// Show copy feedback
function showCopyFeedback() {
copyIconBtn.style.background = '#4CAF50';
copyIconBtn.style.color = '#FFFFFF';
setTimeout(() => {
copyIconBtn.style.background = '';
copyIconBtn.style.color = '';
}, 1500);
}
// Repository Analysis Event Handlers
async function handleRepoAnalyzeClick() {
const repoUrl = repoUrlInput.value.trim();
if (!repoUrl) {
showRepoError('Please enter a GitHub repository URL');
return;
}
// Basic URL validation
if (!repoUrl.match(/^https:\/\/github\.com\/[^\/]+\/[^\/]+/)) {
showRepoError('Please enter a valid GitHub repository URL (e.g., https://github.com/owner/repo)');
return;
}
if (isRepoLoading) return;
try {
isRepoLoading = true;
showRepoLoading();
const analysisResult = await analyzeRepository(repoUrl);
showRepoSuccess(analysisResult);
} catch (error) {
console.error('Error analyzing repository:', error);
showRepoError(error.message || 'Failed to analyze repository. Please try again.');
} finally {
isRepoLoading = false;
}
}
async function handleProceedWithPrClick() {
if (!currentRepoAnalysis) return;
try {
const prResult = await createPullRequest(currentRepoAnalysis);
// Show success message with PR link
showPrSuccess(prResult);
} catch (error) {
console.error('Error creating PR:', error);
showPrError(error.message || 'Failed to create pull request. Please try again.');
}
}
function handleRepoDownloadClick() {
if (!currentRepoAnalysis?.rules_yaml) return;
const blob = new Blob([currentRepoAnalysis.rules_yaml], { type: 'text/yaml' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'rules.yaml';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
showRepoDownloadFeedback();
}
async function handleRepoCopyClick() {
if (!currentRepoAnalysis?.rules_yaml) return;
try {
await navigator.clipboard.writeText(currentRepoAnalysis.rules_yaml);
showRepoCopyFeedback();
} catch (error) {
fallbackCopyToClipboard(currentRepoAnalysis.rules_yaml);
showRepoCopyFeedback();
}
}
function handleRepoInputChange() {
const inputValue = repoUrlInput.value.trim();
analyzeRepoBtn.disabled = !inputValue;
if (!inputValue) {
hideRepoAllStates();
}
}
// handleShowExamplesClick removed - examples are now always visible
// Handle input change
function handleInputChange() {
const inputValue = ruleInput.value.trim();
analyzeBtn.disabled = !inputValue;
if (!inputValue) {
hideAllStates();
}
}
// Show loading state
function showLoading() {
hideAllStates();
loadingIndicator.classList.remove('hidden');
requestAnimationFrame(() => {
loadingIndicator.classList.add('fade-in');
});
analyzeBtn.disabled = true;
analyzeBtn.textContent = 'Analyzing...';
}
// Show success state
function showSuccess(rule) {
hideAllStates();
currentRule = rule;
codeContent.textContent = rule;
// Show the success alert first
const codeBlock = resultSection.querySelector('.code-block');
// Ensure code block starts hidden
codeBlock.classList.remove('code-block-visible');
resultSection.classList.remove('hidden');
requestAnimationFrame(() => {
resultSection.classList.add('fade-in');
// Show code block after success alert is visible
setTimeout(() => {
codeBlock.classList.add('code-block-visible');
}, 600);
});
analyzeBtn.disabled = false;
analyzeBtn.textContent = 'Analyze';
}
// Show error state
function showError(message) {
hideAllStates();
errorMessage.textContent = message;
errorAlert.classList.remove('hidden');
requestAnimationFrame(() => {
errorAlert.classList.add('fade-in');
});
analyzeBtn.disabled = false;
analyzeBtn.textContent = 'Analyze';
}
// Repository Analysis UI Functions
function showRepoLoading() {
hideRepoAllStates();
repoLoadingIndicator.classList.remove('hidden');
requestAnimationFrame(() => {
repoLoadingIndicator.classList.add('fade-in');
});
analyzeRepoBtn.disabled = true;
analyzeRepoBtn.textContent = 'Analyzing...';
}
function showRepoSuccess(analysisResult) {
hideRepoAllStates();
currentRepoAnalysis = analysisResult;
// The new response has three top-level keys.
const { rules_yaml, pr_plan, analysis_summary } = analysisResult;
// Populate recommendations from the summary object
populateSummaryMetrics(analysis_summary);
// Populate PR plan from the markdown string
populatePrPlan(pr_plan);
// Show code content from the yaml string
repoCodeContent.textContent = rules_yaml;
// Make the entire result section visible
repoAnalysisResult.classList.remove('hidden');
requestAnimationFrame(() => {
repoAnalysisResult.classList.add('fade-in');
});
analyzeRepoBtn.disabled = false;
analyzeRepoBtn.textContent = 'Analyze Repository';
}
function showRepoError(message) {
hideRepoAllStates();
repoErrorMessage.textContent = message;
repoErrorAlert.classList.remove('hidden');
requestAnimationFrame(() => {
repoErrorAlert.classList.add('fade-in');
});
analyzeRepoBtn.disabled = false;
analyzeRepoBtn.textContent = 'Analyze Repository';
}
function showPrSuccess(prResult) {
// Update the proceed button to show success
const originalText = proceedWithPrBtn.textContent;
proceedWithPrBtn.textContent = 'PR Created!';
proceedWithPrBtn.style.background = '#4CAF50';
proceedWithPrBtn.disabled = true;
// Add link to PR if available
if (prResult.pull_request_url) {
setTimeout(() => {
window.open(prResult.pull_request_url, '_blank');
}, 1000);
}
setTimeout(() => {
proceedWithPrBtn.textContent = originalText;
proceedWithPrBtn.style.background = '';
proceedWithPrBtn.disabled = false;
}, 3000);
}
function showPrError(message) {
// Show error in a temporary alert
const errorDiv = document.createElement('div');
errorDiv.className = 'pr-error-alert';
errorDiv.innerHTML = `
<div class="error-header">
<svg class="error-icon" viewBox="0 0 24 24">
<path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm1 15h-2v-2h2v2zm0-4h-2V7h2v6z" fill="#F44336"/>
</svg>
<span class="error-title">PR Creation Failed</span>
</div>
<div class="error-content">
<p>${message}</p>
</div>
`;
const prActions = document.querySelector('.pr-actions');
prActions.appendChild(errorDiv);
setTimeout(() => {
errorDiv.remove();
}, 5000);
}
function showRepoDownloadFeedback() {
const originalText = downloadRulesBtn.textContent;
downloadRulesBtn.textContent = 'Downloaded!';
downloadRulesBtn.style.background = '#4CAF50';
setTimeout(() => {
downloadRulesBtn.textContent = originalText;
downloadRulesBtn.style.background = '';
}, 1500);
}
function showRepoCopyFeedback() {
copyRepoCodeBtn.style.background = '#4CAF50';
copyRepoCodeBtn.style.color = '#FFFFFF';
setTimeout(() => {
copyRepoCodeBtn.style.background = '';
copyRepoCodeBtn.style.color = '';
}, 1500);
}
function populateRecommendations(recommendations) {
const container = recommendationsList;
container.innerHTML = '';
if (!recommendations || recommendations.length === 0) {
container.innerHTML = '<p class="no-recommendations">No recommendations found for this repository.</p>';
return;
}
recommendations.forEach((rec, index) => {
const recElement = document.createElement('div');
recElement.className = 'recommendation-item';
const confidencePercent = Math.round(rec.confidence * 100);
const confidenceColor = confidencePercent >= 80 ? '#4CAF50' : confidencePercent >= 60 ? '#FF9800' : '#F44336';
recElement.innerHTML = `
<div class="recommendation-header">
<div class="recommendation-confidence" style="background: ${confidenceColor}">
${confidencePercent}%
</div>
<div class="recommendation-strategy">${rec.strategy_used || 'static'}</div>
</div>
<div class="recommendation-content">
<p class="recommendation-reasoning">${rec.reasoning}</p>
<details class="recommendation-details">
<summary>View YAML Rule</summary>
<pre class="recommendation-yaml">${rec.yaml_rule}</pre>
</details>
</div>
`;
container.appendChild(recElement);
});
}
function populateSummaryMetrics(analysisSummary) {
const container = recommendationsList;
container.innerHTML = '';
if (!analysisSummary) {
container.innerHTML = '<p class="no-recommendations">No analysis summary available.</p>';
return;
}
// Display key metrics from analysis_summary
const metricsHtml = Object.entries(analysisSummary)
.map(([key, value]) => {
const displayKey = key.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase());
const displayValue = typeof value === 'number' ? value.toFixed(2) : value;
return `
<div class="metric-item">
<div class="metric-label">${displayKey}</div>
<div class="metric-value">${displayValue}</div>
</div>
`;
}).join('');
container.innerHTML = `<div class="metrics-grid">${metricsHtml}</div>`;
}
function populatePrPlan(prPlan) {
const container = prPlanDetails;
container.innerHTML = '';
if (!prPlan) {
container.innerHTML = '<p class="no-pr-plan">No PR plan available.</p>';
return;
}
// prPlan is now a markdown string, not an object
if (typeof prPlan === 'string') {
container.innerHTML = `<div class="pr-plan-markdown">${prPlan.replace(/\n/g, '<br>')}</div>`;
} else {
// Legacy object format fallback
container.innerHTML = `
<div class="pr-plan-item">
<strong>Branch:</strong> ${prPlan.branch_name}
</div>
<div class="pr-plan-item">
<strong>Base:</strong> ${prPlan.base_branch}
</div>
<div class="pr-plan-item">
<strong>File:</strong> ${prPlan.file_path}
</div>
<div class="pr-plan-item">
<strong>Title:</strong> ${prPlan.title}
</div>
`;
}
}
function hideRepoAllStates() {
repoAnalysisResult.classList.remove('fade-in');
repoLoadingIndicator.classList.remove('fade-in');
repoErrorAlert.classList.remove('fade-in');
repoAnalysisResult.classList.add('hidden');
repoLoadingIndicator.classList.add('hidden');
repoErrorAlert.classList.add('hidden');
}
// Hide all states instantly without transition
function hideAllStates() {
resultSection.classList.remove('fade-in');
loadingIndicator.classList.remove('fade-in');
errorAlert.classList.remove('fade-in');
resultSection.classList.add('hidden');
loadingIndicator.classList.add('hidden');
errorAlert.classList.add('hidden');
// Reset code block visibility
const codeBlock = resultSection.querySelector('.code-block');
if (codeBlock) {
codeBlock.classList.remove('code-block-visible');
}
}
// Utility functions
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Add some interactive enhancements
document.addEventListener('DOMContentLoaded', function() {
// Add hover effects to example cards
const exampleCards = document.querySelectorAll('.example-card');
exampleCards.forEach(card => {
card.addEventListener('click', function() {
const description = this.querySelector('.example-description').textContent;
// Populate the input with the example description
ruleInput.value = description;
ruleInput.focus();
// Scroll to the input
ruleInput.scrollIntoView({ behavior: 'smooth', block: 'center' });
});
});
// Add ripple effect to buttons (only on non-touch devices)
const buttons = document.querySelectorAll('.btn');
buttons.forEach(button => {
button.addEventListener('click', function(e) {
// Only add ripple on devices that support hover (desktop)
if (window.matchMedia('(hover: hover)').matches) {
const ripple = document.createElement('span');
const rect = this.getBoundingClientRect();
const size = Math.max(rect.width, rect.height);
const x = e.clientX - rect.left - size / 2;
const y = e.clientY - rect.top - size / 2;
ripple.style.width = ripple.style.height = size + 'px';
ripple.style.left = x + 'px';
ripple.style.top = y + 'px';
ripple.classList.add('ripple');
this.appendChild(ripple);
setTimeout(() => {
ripple.remove();
}, 600);
}
});
});
});
// Add CSS for ripple effect
const style = document.createElement('style');
style.textContent = `
.btn {
position: relative;
overflow: hidden;
}
.ripple {
position: absolute;
border-radius: 50%;
background: rgba(255, 255, 255, 0.3);
transform: scale(0);
animation: ripple 0.6s linear;
pointer-events: none;
}
@keyframes ripple {
to {
transform: scale(4);
opacity: 0;
}
}
.metrics-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1rem;
margin-top: 1rem;
}
.metric-item {
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 8px;
padding: 1rem;
text-align: center;
}
.metric-label {
font-size: 0.9rem;
color: rgba(255, 255, 255, 0.7);
margin-bottom: 0.5rem;
}
.metric-value {
font-size: 1.2rem;
font-weight: 600;
color: #00d9ff;
}
.pr-plan-markdown {
background: rgba(255, 255, 255, 0.05);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 8px;
padding: 1rem;
line-height: 1.6;
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}
`;
document.head.appendChild(style);