summaryrefslogtreecommitdiffstats
path: root/maintainers/scripts/update-ruby-packages.checks.nix
blob: d08c695f0e1db462f73b01c05d4ab4c1c046e696 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{
  old,
  new,
  specifiedGems,
  withData ? false,
  # Use for `lib`.
  pkgs ? import ../.. { },
}:

# Rename inputs to re-use those names.
let
  old' = old;
  new' = new;
  specifiedGems' = specifiedGems;
in

let
  inherit (builtins)
    attrNames
    filter
    isNull
    length
    toJSON
    ;
  inherit (pkgs.lib)
    concatMapStringsSep
    concatStringsSep
    intersectLists
    splitString
    subtractLists
    versionOlder
    ;

  # Keeps non-nulls in a list.
  # Mirroring Ruby's `Array#compact`.
  compact = filter (v: !(isNull v));

  # The full gemsets attribute sets.
  old = import old';
  new = import new';

  # All gem names.
  allGems = attrNames (old // new);

  # Gems found in both old and new.
  keptGems = intersectLists (attrNames old) (attrNames new);

  # Gems added or removed.
  addedOrRemovedGems = subtractLists keptGems allGems;

  # Gems specified in Gemfile.
  specifiedGems = splitString "\n" specifiedGems';

  # Gems that were not specified.
  nonSpecifiedGems = subtractLists specifiedGems keptGems;

  # Generates data for the summary tables
  # This is also used for `failedChecks`.
  versionChangeDataFor =
    gems:
    let
      results = map (
        name:
        let
          oldv = old.${name}.version or null;
          newv = new.${name}.version or null;
        in
        if newv == oldv then
          # Nothing changed. This will be filtered out.
          null
        else
          {
            inherit
              name
              ;
            old = oldv;
            new = newv;
          }
      ) gems;
    in
    compact results;

  checkRegression =
    entry: message:
    let
      isRemoval = isNull entry.new;
      isAddition = isNull entry.old;
      isRegression = versionOlder entry.new entry.old;
    in
    if
      # Gems being added or gems being removed won't cause failures.
      !isRemoval
      && !isAddition
      # A version being regressed is a failure.
      && isRegression
    then
      message
    else
      null;

  # This is a list of error messages to float up to the user.
  # An empty list means no error.
  failedChecks = compact (
    [ ]
    ++ (map (
      entry:
      checkRegression entry "Version regression for specified gem ${toJSON entry.name}, from ${toJSON entry.old} to ${toJSON entry.new}"
    ) (versionChangeDataFor specifiedGems))
    ++ (map (
      entry:
      checkRegression entry "Version regression for non-specified gem ${toJSON entry.name}, from ${toJSON entry.old} to ${toJSON entry.new}"
    ) (versionChangeDataFor nonSpecifiedGems))
  );

  # Formats a version number (or null) as markdown.
  gemVersionToMD = version: if isNull version then "*N/A*" else "`${version}`";

  # Formats a `versionChangeDataFor` output as markdown.
  versionChangeDataMD =
    gems:
    let
      result = versionChangeDataFor gems;
    in
    map (
      row:
      [ row.name ]
      ++ (map gemVersionToMD [
        row.old
        row.new
      ])
    ) result;

  # Given a list of columns, and a list of list of column data,
  # generates the markup for markdown table.
  mkTable =
    columns: entries:
    let
      entryToMarkdown = columns: "| ${concatStringsSep " | " columns} |";
      sep = entryToMarkdown (map (_: "---") columns);
    in
    if length entries == 0 then
      "> *No data...*"
    else
      ''
        ${entryToMarkdown columns}
        ${sep}
        ${concatMapStringsSep "\n" entryToMarkdown entries}
      '';

  # The markdown report is built as this string.
  report = ''
    <!--
    ----------------------------------------------
    NOTE: You must copy this whole report section
          to your pull request!
    ----------------------------------------------
    -->

    #### Nixpkgs Ruby packages update report

    **Specified gems changed:**

    ${mkTable [ "Name" "old" "new" ] (versionChangeDataMD specifiedGems)}

    **Gems added or removed:**

    ${mkTable [ "Name" "old" "new" ] (versionChangeDataMD addedOrRemovedGems)}

    <details>

    <summary><strong>(Non-specified gem changes)</strong></summary>

    ${mkTable [ "Name" "old" "new" ] (versionChangeDataMD nonSpecifiedGems)}

    </details>

    <!-- --------------- End ----------------- -->
  '';
in
if (length failedChecks) > 0 then
  # Fail the update script via `abort` on checks failure.
  builtins.abort ''
    ${"\n"}Gem upgrade aborted with the following failures:

    ${concatMapStringsSep "\n" (msg: " - ${msg}") failedChecks}
  ''
else
  # Output the report.
  builtins.trace "(Report follows...)\n\n${report}" (
    # And if `withData` is true, expose the data for REPL usage.
    if withData then
      {
        inherit
          # The gemsets used
          old
          new
          # The lists of gems
          allGems
          specifiedGems
          nonSpecifiedGems
          addedOrRemovedGems
          keptGems
          ;
      }
    else
      null
  )