liquid_feedback_frontend
view env/lf4rcs/update_references.lua @ 1729:ae45894bbf08
Check for member before checking supporter status
| author | bsw | 
|---|---|
| date | Tue Sep 28 15:01:54 2021 +0200 (2021-09-28) | 
| parents | bb2f388540cc | 
| children | 
 line source
     1 function lf4rcs.update_references(repository, path, unit_id)
     2   local function log(message)
     3     print(lf4rcs.log_prefix .. message)
     4   end
     5   if not config.lf4rcs[repository]
     6       or not config.lf4rcs[repository].get_remote_user
     7       or not config.lf4rcs[repository].get_branches
     8   then
     9     log("Unsupported repository type")
    10     os.exit(1)
    11   end
    12   log("inspecting changesets")
    13   local remote_user = config.lf4rcs[repository].get_remote_user()
    14   local function abort(message)
    15     log("TEST FAILED: " .. message)
    16     log("ABORTING and ROLLBACK due to failed test.")
    17     db:query("ROLLBACK")
    18     os.exit(1)
    19   end
    20   db:query("BEGIN")
    21   local member = Member:new_selector()
    22     :add_where{ "login = ?", remote_user }
    23     :optional_object_mode()
    24     :exec()
    25   if not member then
    26     abort(
    27       "internal error, member '" 
    28       .. remote_user .. "' not found in database"
    29     )
    30   end
    31   local function exec(...)
    32     local command, output, err_message, exit_code = lf4rcs.exec(...)
    33     if not output then
    34       log("Could not execute: " .. command)
    35       abort(err_message)
    36     end
    37     if exit_code ~= 0 then
    38       log("Could not execute: " .. command)
    39       abort("Exit code: " .. tostring(exit_code))
    40     end
    41     return output
    42   end
    43   if config.lf4rcs[repository].extra_checks then
    44     local success, err_message = config.lf4rcs[repository].extra_checks(path, exec)
    45     if not success then
    46       abort(err_message)
    47     end
    48   end
    49   local branches, err = config.lf4rcs[repository].get_branches(path, exec)
    50   if not branches then abort(err) end
    51   for branch, head_node_ids in pairs(branches) do
    52     log('checking branch ' .. branch)
    53     if branch ~= config.lf4rcs[repository].working_branch_name then
    54       local initiative_id = string.match(branch, "^i([0-9]+)$")
    55       if not initiative_id 
    56           or initiative_id ~= tostring(tonumber(initiative_id))
    57       then
    58         abort("this branch name is not allowed")
    59       end
    60       initiative_id = tonumber(initiative_id)
    61       if #head_node_ids > 1 then
    62         abort("number of heads found for branch is greater than 1: " .. #head_node_ids)
    63       end
    64       local initiative = Initiative:by_id(initiative_id)
    65       if not initiative then
    66         abort("initiative i" .. initiative_id .. " not found" )
    67       end
    68       if initiative.issue.area.unit_id ~= tonumber(unit_id) then
    69         abort("initiative belongs to another unit (unit ID " .. initiative.issue.area.unit_id .. ")")
    70       end
    71       if initiative.issue.state ~= "admission" and initiative.issue.state ~= "discussion" then
    72         if initiative.issue.state == "verification" then
    73           if config.lf4rcs.push_grace_period then
    74             local in_push_grace_period = (Initiative:new_selector()
    75               :reset_fields()
    76               :add_field({ "now() - initiative.created <= ?", config.lf4rcs.push_grace_period }, "in_push_grace_period")
    77               :add_where{ "id = ?", initiative.id }
    78               :single_object_mode()
    79               :exec()).in_push_grace_period
    80             if not in_push_grace_period then
    81               abort("issue is already frozen and the push grace period is expired")
    82             end
    83           else
    84             abort("issue is already frozen")
    85           end
    86         else
    87           abort("issue is already closed (" .. initiative.issue.state .. ")")
    88         end
    89       end
    90       if initiative.revoked then
    91         abort("initiative has been revoked")
    92       end
    93       local initiator = Initiator:by_pk(initiative.id, member.id)
    94       if not initiator then
    95         abort("member is not initiator of initiative i" .. initiative_id)
    96       end
    97       if not initiator.accepted then
    98         abort(
    99           "member has not accepted invitation to become initiator of initiative i" 
   100           .. initiative_id
   101         )
   102       end
   103       local node_id = head_node_ids[1] or false
   104       if node_id then
   105         log("adding node " .. node_id .. " to initiative i" .. initiative_id)
   106       else
   107         log("removing node reference from initiative i" .. initiative_id)
   108       end
   109       Draft:update_content(member.id, initiative_id, nil, nil, node_id)
   110     end
   111   end
   112   log("changes cleared. continue committing.")
   113   db:query("COMMIT")
   114   os.exit(0)
   115 end
