liquid_feedback_frontend

diff env/lf4rcs/update_references.lua @ 1219:30523f31b186

Added lf4rcs module and example configuration
author bsw
date Mon Nov 30 19:40:38 2015 +0100 (2015-11-30)
parents
children 7f818548b7b8
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/env/lf4rcs/update_references.lua	Mon Nov 30 19:40:38 2015 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +function lf4rcs.update_references(repository, path, unit_id)
     1.5 +  local function log(message)
     1.6 +    print(lf4rcs.log_prefix .. message)
     1.7 +  end
     1.8 +  if not config.lf4rcs[repository]
     1.9 +      or not config.lf4rcs[repository].get_remote_user
    1.10 +      or not config.lf4rcs[repository].get_branches
    1.11 +  then
    1.12 +    log("Unsupported repository type")
    1.13 +    os.exit(1)
    1.14 +  end
    1.15 +  log("inspecting changesets")
    1.16 +  local remote_user = config.lf4rcs[repository].get_remote_user()
    1.17 +  local function abort(message)
    1.18 +    log("TEST FAILED: " .. message)
    1.19 +    log("ABORTING and ROLLBACK due to failed test.")
    1.20 +    db:query("ROLLBACK")
    1.21 +    os.exit(1)
    1.22 +  end
    1.23 +  db:query("BEGIN")
    1.24 +  local member = Member:new_selector()
    1.25 +    :add_where{ "login = ?", remote_user }
    1.26 +    :optional_object_mode()
    1.27 +    :exec()
    1.28 +  if not member then
    1.29 +    abort(
    1.30 +      "internal error, member '" 
    1.31 +      .. remote_user .. "' not found in database"
    1.32 +    )
    1.33 +  end
    1.34 +  local function exec(...)
    1.35 +    local command, output, err_message, exit_code = lf4rcs.exec(...)
    1.36 +    if not output then
    1.37 +      log("Could not execute: " .. command)
    1.38 +      abort(err_message)
    1.39 +    end
    1.40 +    if exit_code ~= 0 then
    1.41 +      log("Could not execute: " .. command)
    1.42 +      abort("Exit code: " .. tostring(exit_code))
    1.43 +    end
    1.44 +    return output
    1.45 +  end
    1.46 +  if config.lf4rcs[repository].extra_checks then
    1.47 +    local success, err_message = config.lf4rcs[repository].extra_checks(path, exec)
    1.48 +    if not success then
    1.49 +      abort(err_message)
    1.50 +    end
    1.51 +  end
    1.52 +  local branches, err = config.lf4rcs[repository].get_branches(path, exec)
    1.53 +  if not branches then abort(err) end
    1.54 +  for branch, head_node_ids in pairs(branches) do
    1.55 +    log('checking branch ' .. branch)
    1.56 +    if branch ~= config.lf4rcs[repository].working_branch_name then
    1.57 +      local initiative_id = string.match(branch, "^i([0-9]+)$")
    1.58 +      if not initiative_id 
    1.59 +          or initiative_id ~= tostring(tonumber(initiative_id))
    1.60 +      then
    1.61 +        abort("this branch name is not allowed")
    1.62 +      end
    1.63 +      initiative_id = tonumber(initiative_id)
    1.64 +      if #head_node_ids > 1 then
    1.65 +        abort("number of heads found for branch is greater than 1: " .. #head_node_ids)
    1.66 +      end
    1.67 +      local initiative = Initiative:by_id(initiative_id)
    1.68 +      if not initiative then
    1.69 +        abort("initiative i" .. initiative_id .. " not found" )
    1.70 +      end
    1.71 +      if initiative.issue.area.unit_id ~= tonumber(unit_id) then
    1.72 +        abort("initiative belongs to another unit (unit ID " .. initiative.issue.area.unit_id .. ")")
    1.73 +      end
    1.74 +      if initiative.issue.state ~= "admission" and initiative.issue.state ~= "discussion" then
    1.75 +        abort("issue is already frozen or closed (" .. initiative.issue.state .. ")")
    1.76 +      end
    1.77 +      if initiative.revoked then
    1.78 +        abort("initiative has been revoked")
    1.79 +      end
    1.80 +      local initiator = Initiator:by_pk(initiative.id, member.id)
    1.81 +      if not initiator then
    1.82 +        abort("member is not initiator of initiative i" .. initiative_id)
    1.83 +      end
    1.84 +      if not initiator.accepted then
    1.85 +        abort(
    1.86 +          "member has not accepted invitation to become initiator of initiative i" 
    1.87 +          .. initiative_id
    1.88 +        )
    1.89 +      end
    1.90 +      local node_id = head_node_ids[1] or false
    1.91 +      if node_id then
    1.92 +        log("adding node " .. node_id .. " to initiative i" .. initiative_id)
    1.93 +      else
    1.94 +        log("removing node reference from initiative i" .. initiative_id)
    1.95 +      end
    1.96 +      Draft:update_content(member.id, initiative_id, nil, nil, node_id)
    1.97 +    end
    1.98 +  end
    1.99 +  log("changes cleared. continue committing.")
   1.100 +  db:query("COMMIT")
   1.101 +  os.exit(0)
   1.102 +end
   1.103 +

Impressum / About Us