github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/examples/hooks/commit_metadata_validator.lua (about)

     1  --[[
     2  Commit Metadata Validator
     3  
     4  Args:
     5   - Key (string) is the metadata field name to check
     6   - Value (map<string,string>)  is optional parameters.
     7       Currently supported: "pattern" whose value is a regexp pattern to match the metadata field value against
     8  
     9  Example hook declaration: (_lakefs_actions/pre-commit-metadata-validation.yaml):
    10  
    11  name: pre commit metadata field check
    12  on:
    13  pre-merge:
    14      branches:
    15      - main
    16      - stage
    17  hooks:
    18    - id: check_commit_metadata
    19      type: lua
    20      properties:
    21        script_path: scripts/commit_metadata_validator.lua # location of this script in the repository!
    22        args:
    23          notebook_url: {"pattern": "my-jupyter.example.com/.*"}
    24          spark_version:  {}
    25  ]]
    26  
    27  regexp = require("regexp")
    28  
    29  for k, props in pairs(args) do
    30      -- let's see that we indeed have this key in out metadata
    31      local current_value = action.commit.metadata[k]
    32      if current_value == nil then
    33          error("missing mandatory metadata field: " .. k)
    34      end
    35      if props.pattern and not regexp.match(props.pattern, current_value) then
    36          error("current value for commit metadata field " .. k .. " does not match pattern: " .. props.pattern .. " - got: " .. current_value)
    37      end
    38  end