bsw@1219: function lf4rcs.update_references(repository, path, unit_id) bsw@1219: local function log(message) bsw@1219: print(lf4rcs.log_prefix .. message) bsw@1219: end bsw@1219: if not config.lf4rcs[repository] bsw@1219: or not config.lf4rcs[repository].get_remote_user bsw@1219: or not config.lf4rcs[repository].get_branches bsw@1219: then bsw@1219: log("Unsupported repository type") bsw@1219: os.exit(1) bsw@1219: end bsw@1219: log("inspecting changesets") bsw@1219: local remote_user = config.lf4rcs[repository].get_remote_user() bsw@1219: local function abort(message) bsw@1219: log("TEST FAILED: " .. message) bsw@1219: log("ABORTING and ROLLBACK due to failed test.") bsw@1219: db:query("ROLLBACK") bsw@1219: os.exit(1) bsw@1219: end bsw@1219: db:query("BEGIN") bsw@1219: local member = Member:new_selector() bsw@1219: :add_where{ "login = ?", remote_user } bsw@1219: :optional_object_mode() bsw@1219: :exec() bsw@1219: if not member then bsw@1219: abort( bsw@1219: "internal error, member '" bsw@1219: .. remote_user .. "' not found in database" bsw@1219: ) bsw@1219: end bsw@1219: local function exec(...) bsw@1219: local command, output, err_message, exit_code = lf4rcs.exec(...) bsw@1219: if not output then bsw@1219: log("Could not execute: " .. command) bsw@1219: abort(err_message) bsw@1219: end bsw@1219: if exit_code ~= 0 then bsw@1219: log("Could not execute: " .. command) bsw@1219: abort("Exit code: " .. tostring(exit_code)) bsw@1219: end bsw@1219: return output bsw@1219: end bsw@1219: if config.lf4rcs[repository].extra_checks then bsw@1219: local success, err_message = config.lf4rcs[repository].extra_checks(path, exec) bsw@1219: if not success then bsw@1219: abort(err_message) bsw@1219: end bsw@1219: end bsw@1219: local branches, err = config.lf4rcs[repository].get_branches(path, exec) bsw@1219: if not branches then abort(err) end bsw@1219: for branch, head_node_ids in pairs(branches) do bsw@1219: log('checking branch ' .. branch) bsw@1219: if branch ~= config.lf4rcs[repository].working_branch_name then bsw@1219: local initiative_id = string.match(branch, "^i([0-9]+)$") bsw@1219: if not initiative_id bsw@1219: or initiative_id ~= tostring(tonumber(initiative_id)) bsw@1219: then bsw@1219: abort("this branch name is not allowed") bsw@1219: end bsw@1219: initiative_id = tonumber(initiative_id) bsw@1219: if #head_node_ids > 1 then bsw@1219: abort("number of heads found for branch is greater than 1: " .. #head_node_ids) bsw@1219: end bsw@1219: local initiative = Initiative:by_id(initiative_id) bsw@1219: if not initiative then bsw@1219: abort("initiative i" .. initiative_id .. " not found" ) bsw@1219: end bsw@1219: if initiative.issue.area.unit_id ~= tonumber(unit_id) then bsw@1219: abort("initiative belongs to another unit (unit ID " .. initiative.issue.area.unit_id .. ")") bsw@1219: end bsw@1219: if initiative.issue.state ~= "admission" and initiative.issue.state ~= "discussion" then bsw@1235: if initiative.issue.state == "verification" then bsw@1238: if config.lf4rcs.push_grace_period then bsw@1239: local in_push_grace_period = (Initiative:new_selector() bsw@1235: :reset_fields() bsw@1242: :add_field({ "now() - initiative.created <= ?", config.lf4rcs.push_grace_period }, "in_push_grace_period") bsw@1235: :add_where{ "id = ?", initiative.id } bsw@1235: :single_object_mode() bsw@1235: :exec()).in_push_grace_period bsw@1241: if not in_push_grace_period then bsw@1235: abort("issue is already frozen and the push grace period is expired") bsw@1235: end bsw@1235: else bsw@1235: abort("issue is already frozen") bsw@1235: end bsw@1235: else bsw@1235: abort("issue is already closed (" .. initiative.issue.state .. ")") bsw@1235: end bsw@1219: end bsw@1219: if initiative.revoked then bsw@1219: abort("initiative has been revoked") bsw@1219: end bsw@1219: local initiator = Initiator:by_pk(initiative.id, member.id) bsw@1219: if not initiator then bsw@1219: abort("member is not initiator of initiative i" .. initiative_id) bsw@1219: end bsw@1219: if not initiator.accepted then bsw@1219: abort( bsw@1219: "member has not accepted invitation to become initiator of initiative i" bsw@1219: .. initiative_id bsw@1219: ) bsw@1219: end bsw@1219: local node_id = head_node_ids[1] or false bsw@1219: if node_id then bsw@1219: log("adding node " .. node_id .. " to initiative i" .. initiative_id) bsw@1219: else bsw@1219: log("removing node reference from initiative i" .. initiative_id) bsw@1219: end bsw@1219: Draft:update_content(member.id, initiative_id, nil, nil, node_id) bsw@1219: end bsw@1219: end bsw@1219: log("changes cleared. continue committing.") bsw@1219: db:query("COMMIT") bsw@1219: os.exit(0) bsw@1219: end bsw@1219: