@@ -16,7 +16,7 @@ let isLoading = false;
1616let currentRule = '' ;
1717
1818// API Configuration
19- const API_ENDPOINT = 'https://api.example.com/generate-rule ' ;
19+ const API_ENDPOINT = 'https://api.watchflow.dev/api/v1/rules/evaluate ' ;
2020
2121// Initialize the application
2222document . addEventListener ( 'DOMContentLoaded' , function ( ) {
@@ -89,102 +89,53 @@ async function handleAnalyzeClick() {
8989
9090// Generate rule via API
9191async function generateRule ( description ) {
92- try {
93- const response = await fetch ( API_ENDPOINT , {
94- method : 'POST' ,
95- headers : {
96- 'Content-Type' : 'application/json' ,
97- } ,
98- body : JSON . stringify ( { description } ) ,
99- } ) ;
100-
101- if ( ! response . ok ) {
102- throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
103- }
104-
105- const data = await response . json ( ) ;
106-
107- if ( ! data . rule ) {
108- throw new Error ( 'Invalid response format' ) ;
109- }
110-
111- return data . rule ;
112-
113- } catch ( error ) {
114- // For demo purposes, return a mock rule if API fails
115- if ( error . message . includes ( 'fetch' ) ) {
116- return generateMockRule ( description ) ;
92+ const response = await fetch ( API_ENDPOINT , {
93+ method : 'POST' ,
94+ headers : {
95+ 'Content-Type' : 'application/json' ,
96+ 'Accept' : 'application/json' ,
97+ } ,
98+ body : JSON . stringify ( {
99+ rule_text : description ,
100+ event_data : null
101+ } ) ,
102+ } ) ;
103+
104+ if ( ! response . ok ) {
105+ // Handle validation errors or other API errors
106+ if ( response . status === 422 ) {
107+ const errorData = await response . json ( ) ;
108+ throw new Error ( `Validation error: ${ errorData . detail || 'Invalid rule description format' } ` ) ;
117109 }
118- throw error ;
110+ throw new Error ( `API error: ${ response . status } ${ response . statusText } ` ) ;
119111 }
120- }
121-
122- // Generate mock rule for demonstration
123- function generateMockRule ( description ) {
124- const mockRules = {
125- 'pr title' : `name: PR Title Format
126- description: ${ description }
127- on:
128- pull_request:
129- types: [opened, edited, synchronize]
130- rules:
131- - name: Check PR title format
132- condition: |
133- pull_request.title matches /^[A-Z]+-\\d+: .+$/
134- message: "PR title must follow the pattern PROJ-123: Description"
135- severity: error` ,
136-
137- 'commit' : `name: Commit Message Format
138- description: ${ description }
139- on:
140- push:
141- branches: [main, develop]
142- rules:
143- - name: Check commit message format
144- condition: |
145- commit.message matches /^(feat|fix|docs|style|refactor|test|chore): .+$/
146- message: "Commit messages must follow conventional commits format"
147- severity: error` ,
148-
149- 'approval' : `name: Approval Requirements
150- description: ${ description }
151- on:
152- pull_request:
153- types: [opened, review_requested]
154- rules:
155- - name: Check approval requirements
156- condition: |
157- pull_request.author not in pull_request.reviewers
158- message: "Pull requests cannot be approved by the author"
159- severity: error` ,
160-
161- 'default' : `name: Custom Rule
162- description: ${ description }
163- on:
164- pull_request:
165- types: [opened, edited, synchronize]
166- rules:
167- - name: Custom validation
168- condition: |
169- # Add your custom validation logic here
170- true
171- message: "Custom validation message"
172- severity: warning`
173- } ;
174112
175- const lowerDescription = description . toLowerCase ( ) ;
113+ const data = await response . json ( ) ;
114+
115+ if ( ! data ) {
116+ throw new Error ( 'Empty response from API' ) ;
117+ }
118+
119+ // Check if the rule is supported
120+ if ( ! data . supported ) {
121+ const reason = data . feedback || 'This type of rule is not supported by Watchflow' ;
122+ throw new Error ( reason ) ;
123+ }
176124
177- if ( lowerDescription . includes ( 'title' ) || lowerDescription . includes ( 'pr' ) ) {
178- return mockRules [ 'pr title' ] ;
179- } else if ( lowerDescription . includes ( 'commit' ) ) {
180- return mockRules [ 'commit' ] ;
181- } else if ( lowerDescription . includes ( 'approval' ) || lowerDescription . includes ( 'review ') ) {
182- return mockRules [ 'approval' ] ;
183- } else {
184- return mockRules [ 'default' ] ;
125+ // Extract the YAML snippet from the response
126+ if ( data . snippet ) {
127+ // Remove the markdown code block wrapper if present
128+ let yaml = data . snippet ;
129+ if ( yaml . startsWith ( '```yaml\n ') ) {
130+ yaml = yaml . replace ( / ^ ` ` ` y a m l \n / , '' ) . replace ( / \n ` ` ` $ / , '' ) ;
131+ }
132+ return yaml ;
185133 }
134+
135+ throw new Error ( 'No rule snippet found in API response' ) ;
186136}
187137
138+
188139// Handle download button click
189140function handleDownloadClick ( ) {
190141 if ( ! currentRule ) return ;
0 commit comments