Parcourir la source

feat: add date to release info

Zaiming (Stone) Shi il y a 4 ans
Parent
commit
052336699b
2 fichiers modifiés avec 32 ajouts et 5 suppressions
  1. 2 0
      .github/workflows/erlang.yml
  2. 30 5
      src/emqx_plugrel.erl

+ 2 - 0
.github/workflows/erlang.yml

@@ -19,3 +19,5 @@ jobs:
     - uses: actions/checkout@v2
     - name: Compile
       run: rebar3 compile
+    - name: Eunit
+      run: rebar3 eunit

+ 30 - 5
src/emqx_plugrel.erl

@@ -47,18 +47,36 @@ collect_info(PluginInfo, Name, Version, Apps, State) ->
                 , rel_vsn => bin(Version)
                 , rel_apps => AppsWithVsn
                 , git_ref => git_ref()
+                , date => get_date()
                 , metadata_vsn => ?METADATA_VSN
                 , built_on_otp_release => bin(erlang:system_info(otp_release))
                 },
     maps:merge(Info, MoreInfo).
 
+%% best-effort deterministic time, read git commit time otherwise now
+get_date() ->
+    case cmd_oneline_output("git log -1 --pretty=format:'%cd' --date=format:'%Y-%m-%d'") of
+        error -> today();
+        Date -> Date
+    end.
+
+today() ->
+    {Y, M, D} = erlang:date(),
+    bin(io_lib:format("~4..0b-~2..0b-~2..0b", [Y, M, D])).
+
 git_ref() ->
-    case rebar_utils:sh("git rev-parse HEAD", [{use_stdout, false}, return_on_error]) of
-        {ok, Ref} ->
-            bin(rebar_string:trim(Ref, trailing, "\n"));
+    case cmd_oneline_output("git rev-parse HEAD") of
+        error -> <<"unknown">>;
+        Ref -> Ref
+    end.
+
+cmd_oneline_output(Cmd) ->
+    case rebar_utils:sh(Cmd, [{use_stdout, false}, return_on_error]) of
+        {ok, Line} ->
+            bin(rebar_string:trim(Line, trailing, "\n"));
         {error, {Rc, Output}} ->
-            ?LOG(debug, "failed_to_get_git_ref ~p:~n~ts~n", [Rc, Output]),
-            <<"unknown">>
+            ?LOG(debug, "failed_run_cmd ~s~n, error=~p~noutput:~n~ts~n", [Rc, Output]),
+            error
     end.
 
 %% Find app vsn from compiled .app files
@@ -136,3 +154,10 @@ bin2hexstr(B) when is_binary(B) ->
 
 int2hexchar(I) when I >= 0 andalso I < 10 -> I + $0;
 int2hexchar(I) -> I - 10 + $a.
+
+-ifdef(TEST).
+-include_lib("eunit/include/eunit.hrl").
+date_test() ->
+    ?assertMatch([$2, $0, _, _, $-, _, _, $-, _, _], binary_to_list(today())),
+    ?assertMatch([$2, $0, _, _, $-, _, _, $-, _, _], binary_to_list(get_date())).
+-endif.