Skip to content

Commit c970144

Browse files
Copilotjackfirth
andauthored
Add resyntax fix --output-as-json flag (#675)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: jackfirth <[email protected]>
1 parent 8b9e10b commit c970144

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

cli.rkt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
racket/logging
99
racket/match
1010
racket/path
11+
racket/port
1112
rebellion/base/comparator
1213
rebellion/base/range
1314
rebellion/collection/entry
@@ -33,7 +34,7 @@
3334
;@----------------------------------------------------------------------------------------------------
3435

3536

36-
(define-enum-type resyntax-output-format (plain-text github-pull-request-review git-commit-message))
37+
(define-enum-type resyntax-output-format (plain-text github-pull-request-review git-commit-message json))
3738
(define-enum-type resyntax-fix-method (modify-files create-multiple-git-commits))
3839
(define-record-type resyntax-analyze-options (targets suite output-format output-destination))
3940

@@ -160,6 +161,10 @@ changed relative to baseref are analyzed and fixed."
160161
"Report results in the form of a Git commit message printed to stdout."
161162
(set! output-format git-commit-message))
162163

164+
("--output-as-json"
165+
"Report results in the form of a JSON object printed to stdout."
166+
(set! output-format json))
167+
163168
("--refactoring-suite"
164169
modpath
165170
suite-name
@@ -293,6 +298,8 @@ For help on these, use 'analyze --help' or 'fix --help'."
293298
(match output-format
294299
[(== git-commit-message)
295300
(resyntax-fix-print-git-commit-message analysis)]
301+
[(== json)
302+
(resyntax-fix-print-json analysis)]
296303
[(== plain-text)
297304
(resyntax-fix-print-plain-text-summary analysis)]))
298305

@@ -344,5 +351,37 @@ For help on these, use 'analyze --help' or 'fix --help'."
344351
(newline)))
345352

346353

354+
(define (resyntax-fix-print-json analysis)
355+
(define total-fixes (resyntax-analysis-total-fixes analysis))
356+
(define total-files (resyntax-analysis-total-sources-modified analysis))
357+
(define fix-counts-by-rule
358+
(transduce (in-hash-entries (multiset-frequencies (resyntax-analysis-rules-applied analysis)))
359+
(sorting #:key entry-value #:descending? #true)
360+
#:into into-list))
361+
362+
;; Build commit message
363+
(define commit-message
364+
(with-output-to-string
365+
(λ ()
366+
(define issue-string (if (> total-fixes 1) "issues" "issue"))
367+
(define file-string (if (> total-files 1) "files" "file"))
368+
(if (zero? total-fixes)
369+
(printf "Resyntax found no issues.")
370+
(printf "Automated Resyntax fixes\n\nResyntax fixed ~a ~a in ~a ~a."
371+
total-fixes issue-string total-files file-string))
372+
(unless (zero? total-fixes)
373+
(printf "\n")
374+
(for ([rule+count (in-list fix-counts-by-rule)])
375+
(match-define (entry rule count) rule+count)
376+
(define occurrence-string (if (> count 1) "occurrences" "occurrence"))
377+
(printf "\n * Fixed ~a ~a of `~a`" count occurrence-string rule))))))
378+
379+
;; Output JSON
380+
(write-json
381+
(hasheq 'commit_message commit-message
382+
'fix_count total-fixes))
383+
(newline))
384+
385+
347386
(module+ main
348387
(resyntax-run))

0 commit comments

Comments
 (0)