Skip to content

Commit

Permalink
Parse opam-ci-check errors from the job log
Browse files Browse the repository at this point in the history
  • Loading branch information
punchagan committed Oct 1, 2024
1 parent d213677 commit 5891129
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
37 changes: 37 additions & 0 deletions lib/lint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ module Check = struct
let marshal () = Yojson.Safe.to_string `Null
let unmarshal _ = ()

let parse_errors_from_log job =
let job_id = Current.Job.id job in
let path = Current.Job.log_path job_id in
match path with
| Ok path ->
Lwt_io.with_file ~mode:Lwt_io.input (Fpath.to_string path) (fun ch ->
let rec aux acc =
(* We choose the relevant output lines by filtering lines starting
with Error/Warning. The other log lines in the file would start
with a timestamp (logged info), or not start with the
Error/Warning prefix (other text output from the CLI tool). *)
Lwt_io.read_line_opt ch >>= function
| Some line ->
if (String.starts_with ~prefix:"Error" line ||
String.starts_with ~prefix:"Warning" line)
then
aux (line :: acc)
else
aux acc
| None -> Lwt.return acc
in
aux [] >>= function
| [] -> Lwt.return (Error (`Msg "Could not find any Error/Warning lines in CLI output."))
| errors -> Lwt.return (Ok (String.concat "\n" errors)))
| Error (`Msg m) -> Lwt.return (Error (`Msg (Fmt.str "Could not find the log file for job <%s>: %s" job_id m)))

let of_dir ~master ~job ~packages cwd =
let master = Current_git.Commit.hash master in
exec ~cwd ~job [|"git"; "merge"; "-q"; "--"; master|] >>/= fun () ->
Expand All @@ -38,6 +64,17 @@ module Check = struct
in
let cmd = ["opam-ci-check"; "lint"; "--opam-repository"; "."] @ changed @ new_ in
exec ~cwd ~job (cmd |> Array.of_list)
>>= function
| Error (`Msg err) ->
(* The exec function doesn't capture stdout when the command fails; so we
parse the command output from the log file, instead. *)
parse_errors_from_log job >>= (function
| Ok errors -> Lwt_result.fail (`Msg errors)
| Error (`Msg msg) ->
let error_msg = (Fmt.str "internal error: %s - %s" err msg) in
Lwt_result.fail (`Msg error_msg))
| Ok () ->
Lwt_result.return ()
end

module Lint = struct
Expand Down
26 changes: 20 additions & 6 deletions test/lint.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ Tests the following:
* b-incorrect-opam (HEAD -> new-branch-1)
* a-1 (tag: initial-state, master)
$ opam-repo-ci-local --repo="." --branch=new-branch-1 --lint-only --no-web-server
Error "Command "opam-ci-check" "lint" "--opam-repository" "." "--newly-published"
"b.0.0.1,b.0.0.2,b.0.0.3,system-b.0.0.1" exited with status 1"
Error "Warning in system-b.0.0.1: package name has restricted prefix 'system-'
Error in system-b.0.0.1: package with prefix 'system-' requires conflict class 'ocaml-system'
Error in system-b.0.0.1: Maintainer email missing. Please add a maintainer email to the opam file. Maintainer: Maintainer
Error in b.0.0.3: package with conflict class 'ocaml-host-arch' requires name prefix 'host-arch-'
Error in b.0.0.3: pin-depends present. This is not allowed in the opam-repository.
Error in b.0.0.3: Maintainer email missing. Please add a maintainer email to the opam file. Maintainer: Maintainer
Error in b.0.0.2: Maintainer email missing. Please add a maintainer email to the opam file. Maintainer: Maintainer
Error in b.0.0.2: error 3: File format error in 'unknown-field' at line 11, column 0: Invalid field unknown-field
Error in b.0.0.1: Maintainer email missing. Please add a maintainer email to the opam file. Maintainer: Maintainer
Error in b.0.0.1: warning 25: Missing field 'authors'"

Reset commit and clear build cache

Expand All @@ -47,8 +55,8 @@ of a package [a_1] that conflicts with the existing [a-1] package
* a_1-name-collision (HEAD -> new-branch-1)
* a-1 (tag: initial-state, master)
$ opam-repo-ci-local --repo="." --branch=new-branch-1 --lint-only --no-web-server
Error "Command "opam-ci-check" "lint" "--opam-repository" "." "--newly-published"
"a_1.0.0.1" exited with status 1"
Error "Warning in a_1.0.0.1: Possible name collision with package 'a-1'
Error in a_1.0.0.1: Maintainer email missing. Please add a maintainer email to the opam file. Maintainer: Maintainer"

Delete OCurrent cache

Expand All @@ -72,5 +80,11 @@ test various positive and negative cases
* levenshtein-1 (master)
* a-1 (tag: initial-state)
$ opam-repo-ci-local --repo="." --branch=new-branch-2 --lint-only --no-web-server
Error "Command "opam-ci-check" "lint" "--opam-repository" "." "--changed-packages"
"field1.0.0.2" "--newly-published" "fieffind.0.0.1,fieffinder.0.0.1,fielf.0.0.1" exited with status 1"
Error "Error in field1.0.0.2: Maintainer email missing. Please add a maintainer email to the opam file. Maintainer: Maintainer
Warning in fielf.0.0.1: Possible name collision with package 'field1'
Error in fielf.0.0.1: Maintainer email missing. Please add a maintainer email to the opam file. Maintainer: Maintainer
Warning in fieffinder.0.0.1: Possible name collision with package 'fieffind'
Error in fieffinder.0.0.1: Maintainer email missing. Please add a maintainer email to the opam file. Maintainer: Maintainer
Warning in fieffind.0.0.1: Possible name collision with package 'fieldfind'
Warning in fieffind.0.0.1: Possible name collision with package 'fieffinder'
Error in fieffind.0.0.1: Maintainer email missing. Please add a maintainer email to the opam file. Maintainer: Maintainer"

0 comments on commit 5891129

Please sign in to comment.