Skip to content

Commit 4db4a23

Browse files
committed
feat: add GitHub installation repository listing functionality
1 parent d660e93 commit 4db4a23

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

lib/algora/integrations/github/behaviour.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ defmodule Algora.Github.Behaviour do
1515
@callback list_installations(token(), integer()) :: {:ok, map()} | {:error, String.t()}
1616
@callback find_installation(token(), integer(), integer()) :: {:ok, map()} | {:error, String.t()}
1717
@callback get_installation_token(integer()) :: {:ok, map()} | {:error, String.t()}
18+
@callback list_installation_repos(token()) :: {:ok, [map()]} | {:error, String.t()}
1819
@callback create_issue_comment(token(), String.t(), String.t(), integer(), String.t()) ::
1920
{:ok, map()} | {:error, String.t()}
2021
@callback update_issue_comment(token(), String.t(), String.t(), integer(), String.t()) ::

lib/algora/integrations/github/client.ex

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ defmodule Algora.Github.Client do
195195
end
196196
end
197197

198+
@impl true
199+
def list_installation_repos(access_token) do
200+
with {:ok, %{"repositories" => repos}} <- fetch(access_token, "/installation/repositories", "GET") do
201+
{:ok, repos}
202+
end
203+
end
204+
198205
@impl true
199206
def create_issue_comment(access_token, owner, repo, number, body) do
200207
fetch(

lib/algora/integrations/github/github.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ defmodule Algora.Github do
103103
@impl true
104104
def get_installation_token(installation_id), do: client().get_installation_token(installation_id)
105105

106+
@impl true
107+
def list_installation_repos(token), do: client().list_installation_repos(token)
108+
106109
@impl true
107110
def create_issue_comment(token, owner, repo, number, body),
108111
do: client().create_issue_comment(token, owner, repo, number, body)

lib/algora/workspace/workspace.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ defmodule Algora.Workspace do
114114
end
115115
end
116116

117+
def list_installation_repos_by(clauses) do
118+
with {:ok, user} <- Repo.fetch_by(User, clauses),
119+
{:ok, installation} <- Repo.fetch_by(Installation, connected_user_id: user.id),
120+
{:ok, token} <- Github.get_installation_token(installation.provider_id),
121+
{:ok, repos} <- Github.list_installation_repos(token) do
122+
Enum.map(repos, & &1["full_name"])
123+
end
124+
end
125+
117126
def create_installation(user, provider_user, org, data) do
118127
%Installation{}
119128
|> Installation.github_changeset(user, provider_user, org, data)

test/support/github_mock.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ defmodule Algora.Support.GithubMock do
9191
{:ok, %{"token" => "token_#{random_id()}"}}
9292
end
9393

94+
@impl true
95+
def list_installation_repos(_access_token) do
96+
{:ok, []}
97+
end
98+
9499
@impl true
95100
def create_issue_comment(_access_token, _owner, _repo, _number, _body) do
96101
{:ok, %{"id" => random_id()}}

0 commit comments

Comments
 (0)