Skip to content

Commit

Permalink
Add light weight logic to detect newly published packages
Browse files Browse the repository at this point in the history
The API now makes the flag newly_published a bool option type. When the
flag is None, the library automatically tries to detect if the package
is a newly published one based on whether the repository has any other
versions of the package already in it.
  • Loading branch information
punchagan committed Nov 29, 2024
1 parent 3f0afab commit f481e5b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions opam-ci-check/bin/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ let lint (changed_pkgs, new_pkgs) local_repo_dir =
Lint.v ~pkg ~newly_published ~pkg_src_dir opam
in
let all_lint_packages =
List.map (process_package ~newly_published:true) new_pkgs
@ List.map (process_package ~newly_published:false) changed_pkgs
List.map (process_package ~newly_published:(Some true)) new_pkgs
@ List.map (process_package ~newly_published:(Some false)) changed_pkgs
in
let errors = Lint.lint_packages ~opam_repo_dir all_lint_packages in
match errors with
Expand Down
20 changes: 18 additions & 2 deletions opam-ci-check/lib/lint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -371,14 +371,25 @@ end

type t = {
pkg : OpamPackage.t;
newly_published : bool;
newly_published : bool option;
pkg_src_dir : string option;
opam : OpamFile.OPAM.t;
}

let v ~pkg ~newly_published ~pkg_src_dir opam =
let v ~pkg ?(newly_published = None) ~pkg_src_dir opam =
{ pkg; newly_published; pkg_src_dir; opam }

(** A package is considered newly published if the repository doesn't have any
other versions of the package, other than the current one.*)
let is_newly_published ~opam_repo_dir pkg =
let pkg_name = OpamPackage.(pkg |> name |> Name.to_string) in
let pkg_str = OpamPackage.to_string pkg in
match get_files (opam_repo_dir // "packages" // pkg_name) with
| exception Sys_error _ -> true
| [] -> true
| v :: [] when v = pkg_str -> true
| _ -> false

let get_package_names repo_dir =
get_files (repo_dir // "packages") |> List.sort String.compare

Expand All @@ -387,6 +398,11 @@ let lint_packages ~opam_repo_dir metas =
let repo_package_names = get_package_names opam_repo_dir in
metas
|> List.map (fun { pkg; newly_published; pkg_src_dir; opam } ->
let newly_published =
match newly_published with
| Some v -> v
| None -> is_newly_published ~opam_repo_dir pkg
in
Checks.lint_package ~opam_repo_dir ~pkg ~pkg_src_dir
~repo_package_names ~newly_published opam)
|> List.concat |> Result.ok
Expand Down
2 changes: 1 addition & 1 deletion opam-ci-check/lib/lint.mli
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type t

val v :
pkg:OpamPackage.t ->
newly_published:bool ->
?newly_published:bool option ->
pkg_src_dir:string option ->
OpamFile.OPAM.t ->
t
Expand Down

0 comments on commit f481e5b

Please sign in to comment.