Switch OrganizationGithubAppInstallation away from ja_resource and canary#921
Conversation
| def delete(%Conn{} = conn, %{} = params) do | ||
| with %User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
| {:ok, :authorized} <- current_user |> Policy.authorize(:delete, %OrganizationGithubAppInstallation{}, params), | ||
| {:ok, %OrganizationGithubAppInstallation{} = organization_installation} <- %OrganizationGithubAppInstallation{} |> OrganizationGithubAppInstallation.delete_changeset(params) |> Repo.insert do |
There was a problem hiding this comment.
Looks like the build is erroring because OrganizationGithubAppInstallation.delete_changeset/2 doesn't exist. I'm not sure this needs a changeset, just a delete operation.
There was a problem hiding this comment.
Thanks @joshsmith, the tests were running locally (3 failures) so I didn't notice the build and I've updated...
|
I get 2 failing tests locally now with message like: I wonder how I setup the authorization correctly? |
begedin
left a comment
There was a problem hiding this comment.
Some changes to be made, but otherwise, good first draft. 👍
| with %User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
| {:ok, :authorized} <- current_user |> Policy.authorize(:delete, %OrganizationGithubAppInstallation{}, params), | ||
| {:ok, %OrganizationGithubAppInstallation{} = organization_installation} <- %OrganizationGithubAppInstallation{} |> OrganizationGithubAppInstallation.changeset(params) |> Repo.insert do | ||
| conn |> put_status(:deleted) |> render("show.json-api", data: organization_installation) |
There was a problem hiding this comment.
The order of operations here should be
- load organization github app installation by id
{:ok, :authorized} <- current_user |> Policy.authorize(:delete, organization_github_app_installation),{:ok, %OrganizationGithubAppInstallation{}} <- organization_github_app_installation |> Repo.delete- render a 204 (basically,
conn |> send_resp(:no_content, "")
If you implement it that way, you do should
- not have to make the changeset function public
- implement the delete policy to accept a record, not params
| {:ok, %OrganizationGithubAppInstallation{} = organization_installation} <- organization_installation |> OrganizationGithubAppInstallation.changeset(params) |> Repo.update do | ||
| conn |> render("show.json-api", data: organization_installation) | ||
| end | ||
| end |
There was a problem hiding this comment.
We don't actually support an update in this controller, so this action should be completely removed.
A good way to determine which are supported is to look into the router.ex:
# in the non-authenticated scope, we have
resources "/organization-github-app-installations", OrganizationGithubAppInstallationController, only: [:index, :show]
# in the authenticated scope, we have
resources "/organization-github-app-installations", OrganizationGithubAppInstallationController, only: [:create, :delete]That makes the supported actions only [:index, :show, :create, :delete]
If you think about it, it makes sense. An organization_github_app_installation is a many-to-many link between an organization and a github-app-installation, so there is nothing to change about an existing record. It either exists, or it doesn't.
lib/code_corps/policy/policy.ex
Outdated
| defp can?(%User{} = user, :create, %Organization{}, %{}), do: Policy.Organization.create?(user) | ||
| defp can?(%User{} = user, :update, %Organization{} = organization, %{}), do: Policy.Organization.update?(user, organization) | ||
| defp can?(%User{} = current_user, :update, %User{} = user, %{}), do: Policy.User.update?(user, current_user) | ||
| defp can?(%User{} = user, :delete, %OrganizationGithubAppInstallation{}, %{} = params), do: Policy.OrganizationGithubAppInstallation.delete?(user, params) |
There was a problem hiding this comment.
If you fix the delete action below, this will become
defp can?(%User{} = user, :delete, %OrganizationGithubAppInstallation{} = organization_github_app_installation, %{}), do: Policy.OrganizationGithubAppInstallation.delete?(user, organization_github_app_installation)| def delete?(%User{} = user, %OrganizationGithubAppInstallation{} = github_app_installation), | ||
| do: github_app_installation |> get_organization |> owned_by?(user) | ||
| def delete?(%User{} = user, params), | ||
| do: params |> atomize_params |> get_organization |> owned_by?(user) |
There was a problem hiding this comment.
You can completely revert this change if you fix the controller action.
|
Hey @treble37 just a heads up that Let me know if you need any help at all. |
|
Hey @treble37 wanted to check in on this again and see if you need anything from me. |
|
Thanks @joshsmith, I just pulled down the latest dev branch changes, will rebase and get back to you...it's just been a busy week |
aaf9f12 to
0930663
Compare
|
@joshsmith - |
|
@treble37 I restarted the build and the tests passed. It must have been some parallelism issue with how bypass uses ports or something. We need you to squash your commits into one and rebase before we can merge. |
0930663 to
f710533
Compare
|
squashed @begedin , thanks! |
| plug CodeCorpsWeb.Plug.DataToAttributes | ||
|
|
||
| @spec model :: module | ||
| def model, do: CodeCorps.OrganizationGithubAppInstallation |
There was a problem hiding this comment.
Sorry I missed this on the first pass, but the method here is no longer necessary now, so you can remove it.
So I don't annoy you further, don't forget to squash again, as well as run a git pull develop followed by git rebase origin/develop at the end of it. Then I can merge.
There was a problem hiding this comment.
ok thanks for your patience @begedin with my first contribution, I rebased on the latest origin/develop and removed def model method....thanks!
There was a problem hiding this comment.
I'm the one who should be thankful @treble37
And now I have to apologize again and ask you to do yet another change. The filter method also isn't required anymore. Mind removing that one to?
Again, squash and rebase after.
I'm really sorry about this, but the "changes" view on github really sucks when it comes to seeing which code needs to be removed.
7abfa03 to
a3d36b6
Compare
Switch OrganizationGithubAppInstallationController away from ja_resource and canary Closes code-corps#893
a3d36b6 to
55ca3eb
Compare
|
@treble37 Excellent work! 👍 |
|
🙌 |
Switch OrganizationGithubAppInstallationController away from ja_resource and canary
Closes #893
What's in this PR?
Switch OrganizationGithubAppInstallationController away from ja_resource and canary
Make sure any changes to code include changes to documentation.
References
Fixes # 893
Progress on: # 864