Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_current_history(app: App, label: LabelRef) -> History:
"""Get a label's last history entry."""
history_entry = app.session.query(History).filter_by(
label_name=label.name,
label_state_machine=label.state_machine,
).order_by(
# Our model type stubs define the `id` attribute as `int`, yet
# sqlalchemy actually allows the attribute to be used for ordering like
# this; ignore the type check here specifically rather than complicate
# our type definitions.
History.id.desc(), # type: ignore
).first()
if history_entry is None:
raise UnknownLabel(label)
return history_entry
)
if result != WebhookResult.SUCCESS:
return False
context = context_for_label(
label,
metadata,
state_machine,
action,
latest_history,
app.logger,
)
next_state = choose_next_state(state_machine, action, context)
app.session.add(History(
label_state_machine=state_machine.name,
label_name=label.name,
created=func.now(),
old_state=action.name,
new_state=next_state.name,
))
return True
return
if row is None or row.deleted:
return
# Record the label as having been deleted and remove its metadata
row.metadata = {}
row.deleted = True
# Add a history entry for the deletion
current_state = get_current_state(app, label, state_machine)
if current_state is None:
raise AssertionError(f"Active label {label} has no current state!")
row.history.append(History(
old_state=current_state.name,
new_state=None,
))
context = context_for_label(
label,
metadata,
state_machine,
gate,
history_entry,
app.logger,
)
can_exit = gate.exit_condition.run(context)
if not can_exit:
return False
destination = choose_next_state(state_machine, gate, context)
app.session.add(History(
label_state_machine=state_machine.name,
label_name=label.name,
created=func.now(),
old_state=gate.name,
new_state=destination.name,
))
app.session.query(Label).filter_by(
name=label.name,
state_machine=label.state_machine,
).update({
'metadata_triggers_processed': True,
})
return True
def _validate_no_labels_in_nonexistent_states(state_machine, app):
states = [x.name for x in state_machine.states]
states_by_rank = app.session.query(
History.label_name,
History.new_state,
func.row_number().over(
order_by=History.id.desc(),
partition_by=History.label_name,
).label('rank'),
).filter_by(
label_state_machine=state_machine.name,
).subquery()
invalid_labels_and_states = app.session.query(
states_by_rank.c.label_name,
states_by_rank.c.new_state,
).filter(
states_by_rank.c.rank == 1,
~(
states_by_rank.c.new_state.in_(states) |
def _labels_in_state(
app: App,
state_machine: StateMachine,
state: State,
filter_: Any,
) -> List[str]:
"""Util to get all the labels in an action state that need retrying."""
states_by_rank = app.session.query(
History.label_name,
History.new_state,
func.row_number().over(
# Our model type stubs define the `id` attribute as `int`, yet
# sqlalchemy actually allows the attribute to be used for ordering
# like this; ignore the type check here specifically rather than
# complicate our type definitions.
order_by=History.id.desc(), # type: ignore
partition_by=History.label_name,
).label('rank'),
).filter_by(
label_state_machine=state_machine.name,
).subquery()
ranked_transitions = app.session.query(
states_by_rank.c.label_name,
).filter(
states_by_rank.c.rank == 1,
def _validate_no_labels_in_nonexistent_states(state_machine, app):
states = [x.name for x in state_machine.states]
states_by_rank = app.session.query(
History.label_name,
History.new_state,
func.row_number().over(
order_by=History.id.desc(),
partition_by=History.label_name,
).label('rank'),
).filter_by(
label_state_machine=state_machine.name,
).subquery()
invalid_labels_and_states = app.session.query(
states_by_rank.c.label_name,
states_by_rank.c.new_state,
).filter(
states_by_rank.c.rank == 1,
~(
states_by_rank.c.new_state.in_(states) |
states_by_rank.c.new_state.is_(None)
),
).all()
if app.session.query(
app.session.query(Label).filter_by(
name=label.name,
state_machine=label.state_machine,
).exists(),
).scalar():
raise LabelAlreadyExists(label)
app.session.add(
Label(
name=label.name,
state_machine=state_machine.name,
metadata=metadata,
history=[
History(
old_state=None,
new_state=state_machine.states[0].name,
),
],
),
)
process_transitions(app, label)
return metadata