Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on base and stdio #138

Merged
merged 17 commits into from
Mar 21, 2024

Conversation

phongulus
Copy link
Contributor

Description of the task

Removed and replaced all uses of the Base and Stdio libraries.

How to test

Should compile without issues and the tests should pass.

make test

@tysg
Copy link
Contributor

tysg commented Mar 11, 2024

You mentioned

 "ocaml" {>= "4.08.0"}

is too strict. Should we relax this then? What's the minimal required version to compile this library?

lib/rule.ml Outdated
match t with
| [] -> None
| v :: vs -> Some (List.fold_left (fun a b -> if compare b a > 0 then b else a) v vs) in
let is_prefix prefix = Stre.starts_with filename prefix in
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I noticed while doing this is that in monorobot.opam the minimum required version for ocaml is >=4.08.0. There are some API changes in later versions that would make life easier in some places, like String.starts_with (here) and String.fold_left (lib/mrkdwn.ml line 11). Right now they're replaced with equivalents from Extlib and Devkit. Should we bump the minimum version up to 4.14? I think that's the compiler installed by default when running opam switch create ./ anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's do that. Thanks!

@phongulus
Copy link
Contributor Author

You mentioned

 "ocaml" {>= "4.08.0"}

is too strict. Should we relax this then? What's the minimal required version to compile this library?

I think 4.14.0 is the default opam installs. I tried to see if it compiles with 4.08.0 but I couldn't get the compiler down to that version (M1 Mac only supported from 4.12 - it does compile there though).

lib/api_local.ml Outdated Show resolved Hide resolved
lib/common.ml Outdated Show resolved Hide resolved
lib/common.ml Outdated Show resolved Hide resolved
lib/common.ml Outdated Show resolved Hide resolved
lib/github.ml Outdated Show resolved Hide resolved
lib/github.ml Outdated Show resolved Hide resolved
lib/github.ml Outdated Show resolved Hide resolved
lib/github.ml Outdated Show resolved Hide resolved
@@ -12,38 +11,38 @@ let log = Log.from "action"

module Action (Github_api : Api.Github) (Slack_api : Api.Slack) = struct
let partition_push (cfg : Config_t.config) n =
let default = Option.to_list cfg.prefix_rules.default_channel in
let default = Stdlib.Option.to_list cfg.prefix_rules.default_channel in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stdlib is not required since open Base is removed. There are also a few more occurrences in the code base.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Extlib somehow overrides Stdlib here, it doesn't compile unless I add this.

lib/common.ml Outdated Show resolved Hide resolved
lib/common.ml Outdated Show resolved Hide resolved
lib/common.ml Outdated
let empty () = Hashtbl.create (module String)
let to_list (l : 'a t) : (string * 'a) list = Hashtbl.to_alist l
let of_list (m : (string * 'a) list) : 'a t = Hashtbl.of_alist_exn (module String) m
let empty () = Hashtbl.create 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's assign it a non-zero value:
from https://v2.ocaml.org/releases/4.14/api/Hashtbl.html

Hashtbl.create n creates a new, empty hash table, with initial size n. For best results, n should be on the order of the expected number of elements that will be in the table. The table grows as needed, so n is just an initial guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how big the initial hash table should be - it's being used to configs for different repos?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A small number, e.g. 10, will do. Although interestingly base uses 0 (ref).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand correctly the Stringtbl is used to store per monorepo configs. Unless it's expected that we monitor multiple monorepos.


type 'a t = 'a Hashtbl.M(String).t
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hashtbl also has a functorial interface like Map, so we can include Hashtbl.Make(String) too.

(also I'm not sure why we need Set, Hashtbl and Map in one repo. Functionally just Hashtbl will work, but that's out of the scope for this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hashtbl.Make requires defining a hash module for string I believe: https://v2.ocaml.org/api/Hashtbl.HashedType.html

lib/api_remote.ml Outdated Show resolved Hide resolved
lib/context.ml Outdated Show resolved Hide resolved
lib/action.ml Outdated Show resolved Hide resolved
lib/action.ml Outdated Show resolved Hide resolved
src/monorobot.ml Outdated Show resolved Hide resolved
lib/github.ml Outdated Show resolved Hide resolved
@phongulus phongulus requested a review from tysg March 11, 2024 09:06
lib/github.ml Outdated Show resolved Hide resolved
lib/mrkdwn.ml Outdated Show resolved Hide resolved
lib/mrkdwn.ml Outdated Show resolved Hide resolved
@@ -157,13 +159,13 @@ let populate_commit ?(include_changes = true) repository (api_commit : api_commi
but looks like slack doesn't provide such functionality
*)
try
match String.split commit.author.date ~on:'T' with
match String.split_on_char 'T' commit.author.date with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like Devkit.Time time format parsing, should be moved to devkit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is a change for a later PR?

lib/action.ml Outdated Show resolved Hide resolved
lib/action.ml Outdated Show resolved Hide resolved
lib/action.ml Outdated Show resolved Hide resolved
lib/api_remote.ml Outdated Show resolved Hide resolved
lib/rule.ml Outdated
match t with
| [] -> None
| v :: vs -> Some (List.fold_left (fun a b -> if compare b a > 0 then b else a) v vs) in
let is_prefix prefix = Stre.starts_with filename prefix in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's do that. Thanks!

Copy link
Contributor

@tysg tysg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be good to merge apart from 1 minor issue. Thanks!

lib/common.ml Outdated
with exn -> fmt_error "%s" (Exn.to_string exn)

let longest_common_prefix xs =
match xs with
| [] -> ""
| [ x ] -> x
| x :: _ -> String.sub x ~pos:0 ~len:(Stre.common_prefix x (List.sort xs ~compare:String.compare |> List.last_exn))
| x :: _ -> String.sub x 0 (Stre.common_prefix x (List.sort String.compare xs |> List.rev |> List.hd))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| x :: _ -> String.sub x 0 (Stre.common_prefix x (List.sort String.compare xs |> List.rev |> List.hd))
| x :: xs ->
let longest = List.sort (Fun.flip String.compare) xs |> List.hd in
String.sub x 0 (Stre.common_prefix x longest)

less code-golfing, more clarity :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a problem with the original code's logic here. Got the following

utop # longest_common_prefix ["/test/paths/not_here"; "/test/path/here"; "/test/paths/here"];;
- : string = "/test/paths/not_here"

I'll fix it in another PR.

@tysg tysg merged commit 6d67e34 into ahrefs:master Mar 21, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants