Skip to content

Commit

Permalink
Add lint for the x-reason-for-archiving field
Browse files Browse the repository at this point in the history
  • Loading branch information
shonfeder committed Jan 1, 2025
1 parent 3b67589 commit f9f566b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 5 deletions.
21 changes: 19 additions & 2 deletions opam-ci-check/lib/lint.ml
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,25 @@ module Checks = struct
)
[]

let check_x_reason_for_archival ~pkg:_ _opam =
[] (* TODO *)
let check_x_reason_for_archival ~pkg opam =
let is_valid_reason (item : OpamParserTypes.FullPos.value) =
match item.pelem with
| String reason -> List.mem reason x_reason_for_archiving_valid_reasons
| _ -> false (* Must be a string *)
in
opam
|> OpamFile.OPAM.extensions
|> OpamStd.String.Map.find_opt x_reason_for_archiving_field
|> function
| None ->
[(pkg, InvalidReasonForArchiving)] (* Field must be present *)
| Some field ->
match field.pelem with
(* Must be a non-empty list of valid reasons *)
| List {pelem = (_::_ as reasons); _} when List.for_all is_valid_reason reasons -> []
| _ -> [(pkg, InvalidReasonForArchiving)]


let x_opam_repository_commit_hash_at_time_of_archival ~pkg:_ _opam =
[] (* TODO *)

Expand Down
12 changes: 12 additions & 0 deletions opam-ci-check/lib/lint_error.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ type error =
| PrefixConflictClassMismatch of prefix_conflict_class_mismatch
| DefaultTagsPresent of string list
| MissingUpperBound of string
| InvalidReasonForArchiving

(**/**)

let x_reason_for_archiving_field = "x-reason-for-archiving"
(* Used in the opam repo archive *)
let x_reason_for_archiving_valid_reasons =
["ocaml-version"; "source-unavailable"; "maintenance-intent"; "uninstallable" ]

let msg_of_prefix_conflict_class_mismatch ~pkg = function
| WrongPrefix { conflict_class; required_prefix } ->
Printf.sprintf
Expand Down Expand Up @@ -168,3 +174,9 @@ let msg_of_error (package, (err : error)) =
Printf.sprintf
"Error in %s: An upper bound constraint is missing on dependency '%s'"
pkg dep_name
| InvalidReasonForArchiving ->
Printf.sprintf
"Error in %s: The field '%s' must be present and hold a nonempty list \
of one or more of the valid reasons %s"
pkg x_reason_for_archiving_field
(String.concat ", " x_reason_for_archiving_valid_reasons)
71 changes: 68 additions & 3 deletions opam-ci-check/test/lint.t
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,36 @@ passes linting:
# Opam Archive linting tests
$ git reset -q --hard initial-state
These linting checks automate enforcement of the opam archiving policy found at
https://github.com/ocaml/opam-repository/blob/master/governance/policies/archiving.md#archiving-a-package
Test that we do not report an error when a wellformed package has no deps:
$ git reset -q --hard initial-state
Test that we do not report an error on a minimal well-formed package:
$ echo 'x-reason-for-archiving: [ "source-unavailable" ]' >> packages/a-1/a-1.0.0.1/opam
$ cat packages/a-1/a-1.0.0.1/opam
opam-version: "2.0"
synopsis: "Synopsis"
description: "Description"
maintainer: "Maintainer <me@example.com>"
author: "Author"
license: "Apache-2.0"
homepage: "https://github.com/ocurrent/opam-repo-ci"
bug-reports: "https://github.com/ocurrent/opam-repo-ci/issues"
dev-repo: "git+https://github.com/ocurrent/opam-repo-ci.git"
doc: "https://ocurrent.github.io/ocurrent/"
build: []
depends: []
x-reason-for-archiving: [ "source-unavailable" ]
$ opam-ci-check lint -r . --check=archive-repo a-1.0.0.1
Linting opam-repository at $TESTCASE_ROOT/. ...
No errors
## Upper bounds checks
All dependencies
Test that we report errors when a package has dependandies without an upper bound:
$ sed \
Expand All @@ -387,7 +409,7 @@ Test that we do NOT report errors when all a packages dependandies have an upper
Linting opam-repository at $TESTCASE_ROOT/. ...
No errors
Test that we do NOT report errors when the ocaml dependency has no upper bound:
Test that we do NOT report errors when the compiler dependency has no upper bound:
$ sed \
> -e 's/depends.*/depends: ["ocaml"]/' \
Expand All @@ -396,3 +418,46 @@ Test that we do NOT report errors when the ocaml dependency has no upper bound:
$ opam-ci-check lint -r . --check=archive-repo a-1.0.0.1
Linting opam-repository at $TESTCASE_ROOT/. ...
No errors
## x-reason-for-archiving checks
Test we report an error when the x-reason-for-archiving is missing:
$ sed \
> -e '/x-reason-for-archiving/d' \
> packages/a-1/a-1.0.0.1/opam > opam.new
$ mv opam.new packages/a-1/a-1.0.0.1/opam
$ opam-ci-check lint -r . --check=archive-repo a-1.0.0.1
Linting opam-repository at $TESTCASE_ROOT/. ...
Error in a-1.0.0.1: The field 'x-reason-for-archiving' must be present and hold a nonempty list of one or more of the valid reasons ocaml-version, source-unavailable, maintenance-intent, uninstallable
[1]
Test we report an error when the x-reason-for-archiving has an invalid value:
$ echo 'x-reason-for-archiving: "not a list"' >> packages/a-1/a-1.0.0.1/opam
$ opam-ci-check lint -r . --check=archive-repo a-1.0.0.1
Linting opam-repository at $TESTCASE_ROOT/. ...
Error in a-1.0.0.1: The field 'x-reason-for-archiving' must be present and hold a nonempty list of one or more of the valid reasons ocaml-version, source-unavailable, maintenance-intent, uninstallable
[1]
Test we report an error when the x-reason-for-archiving has an empty list:
$ sed \
> -e 's/x-reason-for-archiving:.*/x-reason-for-archiving: []/' \
> packages/a-1/a-1.0.0.1/opam > opam.new
$ mv opam.new packages/a-1/a-1.0.0.1/opam
$ opam-ci-check lint -r . --check=archive-repo a-1.0.0.1
Linting opam-repository at $TESTCASE_ROOT/. ...
Error in a-1.0.0.1: The field 'x-reason-for-archiving' must be present and hold a nonempty list of one or more of the valid reasons ocaml-version, source-unavailable, maintenance-intent, uninstallable
[1]
Test we report an error when the x-reason-for-archiving has an invalid reason:
$ sed \
> -e 's/x-reason-for-archiving:.*/x-reason-for-archiving: ["an indalid reason"]/' \
> packages/a-1/a-1.0.0.1/opam > opam.new
$ mv opam.new packages/a-1/a-1.0.0.1/opam
$ opam-ci-check lint -r . --check=archive-repo a-1.0.0.1
Linting opam-repository at $TESTCASE_ROOT/. ...
Error in a-1.0.0.1: The field 'x-reason-for-archiving' must be present and hold a nonempty list of one or more of the valid reasons ocaml-version, source-unavailable, maintenance-intent, uninstallable
[1]

0 comments on commit f9f566b

Please sign in to comment.