-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2668 GitHub connection auditing (#2742)
* Trivial doc changes * Audit repo creation * Audit repo removals * tidy up * Update CHANGELOG * Fix typo * Update CHANGELOG * Move auditing to VersionControl.Audit * Fix
- Loading branch information
1 parent
8e082b2
commit a278d52
Showing
7 changed files
with
376 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
defmodule Lightning.VersionControl.Audit do | ||
@moduledoc """ | ||
Generate Audit changesets for changes related to VersionControl. | ||
""" | ||
use Lightning.Auditing.Audit, | ||
repo: Lightning.Repo, | ||
item: "project", | ||
events: [ | ||
"repo_connection_created", | ||
"repo_connection_removed" | ||
] | ||
|
||
@spec repo_connection( | ||
Lightning.VersionControl.ProjectRepoConnection.t(), | ||
:created | :removed, | ||
Lightning.Accounts.User.t() | ||
| Lightning.VersionControl.ProjectRepoConnection.t() | ||
| Lightning.Workflows.Trigger.t() | ||
) :: Ecto.Changeset.t() | ||
def repo_connection(repo_connection, action, actor) do | ||
%{ | ||
branch: branch, | ||
config_path: config_path, | ||
project_id: project_id, | ||
repo: repo | ||
} = repo_connection | ||
|
||
changes = | ||
%{ | ||
branch: branch, | ||
repo: repo | ||
} | ||
|> then(fn connection_properties -> | ||
if config_path do | ||
Map.put(connection_properties, :config_path, config_path) | ||
else | ||
connection_properties | ||
end | ||
end) | ||
|> then(fn connection_properties -> | ||
if action == :created do | ||
%{ | ||
after: | ||
Map.put( | ||
connection_properties, | ||
:sync_direction, | ||
repo_connection.sync_direction | ||
) | ||
} | ||
else | ||
%{before: connection_properties} | ||
end | ||
end) | ||
|
||
event("repo_connection_#{action}", project_id, actor, changes) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
defmodule Lightning.VersionControl.AuditTest do | ||
use Lightning.DataCase, async: true | ||
|
||
alias Lightning.VersionControl.Audit | ||
|
||
describe "repo_connection/3 - created" do | ||
test "returns a changeset including the config_path" do | ||
%{ | ||
branch: branch, | ||
config_path: config_path, | ||
project_id: project_id, | ||
repo: repo, | ||
sync_direction: sync_direction | ||
} = | ||
repo_connection = | ||
insert(:project_repo_connection, config_path: "config_path") | ||
|
||
%{id: user_id} = user = insert(:user) | ||
|
||
changeset = | ||
Audit.repo_connection(repo_connection, :created, user) | ||
|
||
assert %{ | ||
changes: %{ | ||
event: "repo_connection_created", | ||
item_type: "project", | ||
item_id: ^project_id, | ||
actor_id: ^user_id, | ||
changes: %{ | ||
changes: audit_changes | ||
} | ||
}, | ||
valid?: true | ||
} = changeset | ||
|
||
assert %{ | ||
after: %{ | ||
branch: branch, | ||
config_path: config_path, | ||
repo: repo, | ||
sync_direction: sync_direction | ||
} | ||
} == audit_changes | ||
end | ||
|
||
test "excludes the config_path from the changeset if it is nil" do | ||
%{ | ||
branch: branch, | ||
repo: repo, | ||
sync_direction: sync_direction | ||
} = | ||
repo_connection = | ||
insert(:project_repo_connection, config_path: nil) | ||
|
||
user = insert(:user) | ||
|
||
changeset = | ||
Audit.repo_connection(repo_connection, :created, user) | ||
|
||
%{changes: %{changes: %{changes: audit_changes}}} = changeset | ||
|
||
assert %{ | ||
after: %{ | ||
branch: branch, | ||
repo: repo, | ||
sync_direction: sync_direction | ||
} | ||
} == audit_changes | ||
end | ||
end | ||
|
||
describe "repo_connection/3 - :removed" do | ||
test "returns a changeset including the config_path" do | ||
%{ | ||
branch: branch, | ||
config_path: config_path, | ||
project_id: project_id, | ||
repo: repo | ||
} = | ||
repo_connection = | ||
insert(:project_repo_connection, config_path: "config_path") | ||
|
||
%{id: user_id} = user = insert(:user) | ||
|
||
changeset = | ||
Audit.repo_connection(repo_connection, :removed, user) | ||
|
||
assert %{ | ||
changes: %{ | ||
event: "repo_connection_removed", | ||
item_type: "project", | ||
item_id: ^project_id, | ||
actor_id: ^user_id, | ||
changes: %{ | ||
changes: audit_changes | ||
} | ||
}, | ||
valid?: true | ||
} = changeset | ||
|
||
assert %{ | ||
before: %{ | ||
branch: branch, | ||
config_path: config_path, | ||
repo: repo | ||
} | ||
} == audit_changes | ||
end | ||
|
||
test "excludes the config_path if it is nil" do | ||
%{ | ||
branch: branch, | ||
repo: repo | ||
} = | ||
repo_connection = | ||
insert(:project_repo_connection, config_path: nil) | ||
|
||
user = insert(:user) | ||
|
||
changeset = | ||
Audit.repo_connection(repo_connection, :removed, user) | ||
|
||
assert %{ | ||
changes: %{ | ||
changes: %{ | ||
changes: audit_changes | ||
} | ||
}, | ||
valid?: true | ||
} = changeset | ||
|
||
assert %{ | ||
before: %{ | ||
branch: branch, | ||
repo: repo | ||
} | ||
} == audit_changes | ||
end | ||
end | ||
end |
Oops, something went wrong.