Skip to content

Commit a59cee9

Browse files
Order workflows alphabetically in credential revoke dialog (#4955)
* Order workflows alphabetically in credential revoke dialog The query behind the "this credential is in use by the following workflows" confirmation had no explicit ordering, so with a distinct select the workflow names came back in an arbitrary, unstable order. Add an order-by so the dialog lists them alphabetically and predictably. * Add contract tests and honest framing for workflow ordering Add unit tests for project_workflows_using_credentials/1, which had no coverage, exercising grouping, credential filtering, and the sorted result. Keep the credential dialog test order-insensitive, since the ordering guarantee belongs to the query, not the rendered view. Move the changelog note to Changed: the previous order was left to the database rather than being broken, so this makes the alphabetical sort explicit. --------- Co-authored-by: Frank Midigo <midigofrank@gmail.com>
1 parent f49e552 commit a59cee9

3 files changed

Lines changed: 65 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ and this project adheres to
3939
advisories from the `mix deps.audit` ignore list.
4040
[#4846](https://github.com/OpenFn/lightning/issues/4846)
4141
- Bump worker to 1.27.0
42+
- The credential revoke-access dialog now sorts the affected workflows
43+
alphabetically. The order was previously left to the database and not
44+
guaranteed. [#4954](https://github.com/OpenFn/lightning/issues/4954)
4245
- Updated Phoenix to 1.7.24 to address vulnerabilities in 1.7.23. This
4346
implicitly introduces a limit of 100 concurrent channels per Websocket
4447
connection (transport). If worker instances are set with a concurrency higher

lib/lightning/workflows.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ defmodule Lightning.Workflows do
7474
join: j in assoc(w, :jobs),
7575
where: j.project_credential_id in ^project_credential_ids,
7676
select: %{name: w.name, project_id: w.project_id},
77-
distinct: true
77+
distinct: true,
78+
order_by: w.name
7879

7980
query
8081
|> Repo.all()

test/lightning/workflows_test.exs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,66 @@ defmodule Lightning.WorkflowsTest do
17731773
end
17741774
end
17751775

1776+
describe "project_workflows_using_credentials/1" do
1777+
test "returns each project's workflow names sorted alphabetically" do
1778+
project = insert(:project)
1779+
1780+
project_credential =
1781+
insert(:project_credential,
1782+
project: project,
1783+
credential: insert(:credential)
1784+
)
1785+
1786+
# inserted in reverse-alphabetical order so a query that dropped its
1787+
# ordering would return them out of order
1788+
for name <- ["ccc-workflow", "bbb-workflow", "aaa-workflow"] do
1789+
insert(:simple_workflow, project: project, name: name)
1790+
|> then(fn %{jobs: [job | _]} ->
1791+
job
1792+
|> Ecto.Changeset.change(%{
1793+
project_credential_id: project_credential.id
1794+
})
1795+
|> Repo.update!()
1796+
end)
1797+
end
1798+
1799+
assert Workflows.project_workflows_using_credentials([
1800+
project_credential.id
1801+
]) ==
1802+
%{project.id => ["aaa-workflow", "bbb-workflow", "ccc-workflow"]}
1803+
end
1804+
1805+
test "only returns workflows whose jobs use the given credentials" do
1806+
project = insert(:project)
1807+
1808+
used =
1809+
insert(:project_credential,
1810+
project: project,
1811+
credential: insert(:credential)
1812+
)
1813+
1814+
unused =
1815+
insert(:project_credential,
1816+
project: project,
1817+
credential: insert(:credential)
1818+
)
1819+
1820+
insert(:simple_workflow, project: project, name: "uses-credential")
1821+
|> then(fn %{jobs: [job | _]} ->
1822+
job
1823+
|> Ecto.Changeset.change(%{project_credential_id: used.id})
1824+
|> Repo.update!()
1825+
end)
1826+
1827+
insert(:simple_workflow, project: project, name: "no-credential")
1828+
1829+
result = Workflows.project_workflows_using_credentials([used.id])
1830+
1831+
assert result == %{project.id => ["uses-credential"]}
1832+
assert Workflows.project_workflows_using_credentials([unused.id]) == %{}
1833+
end
1834+
end
1835+
17761836
defp assert_trigger_state_audit(
17771837
workflow_id,
17781838
user_id,

0 commit comments

Comments
 (0)