11defmodule CodeCorps.GitHub.API.Repository do
22 @ moduledoc ~S"""
3- Functions for working with issues on GitHub.
3+ Functions for retrieving a GitHub repository's issues, pull requests, and
4+ comments from the GitHub API.
45 """
56
67 alias CodeCorps . {
@@ -10,22 +11,65 @@ defmodule CodeCorps.GitHub.API.Repository do
1011 GithubRepo ,
1112 }
1213
14+ @ doc ~S"""
15+ Retrieves issues for a repository
16+
17+ All pages of records are retrieved.
18+ Closed issues are included.
19+ """
1320 @ spec issues ( GithubRepo . t ) :: { :ok , list ( map ) } | { :error , GitHub . api_error_struct }
14- def issues ( % GithubRepo { github_app_installation: % GithubAppInstallation { } = installation } = github_repo ) do
15- with { :ok , access_token } <- API.Installation . get_access_token ( installation ) ,
16- issues <- fetch_issues ( github_repo , access_token )
17- do
18- { :ok , issues }
21+ def issues ( % GithubRepo {
22+ github_app_installation: % GithubAppInstallation {
23+ github_account_login: owner
24+ } = installation ,
25+ name: repo
26+ } ) do
27+ with { :ok , access_token } <- API.Installation . get_access_token ( installation ) do
28+ "repos/#{ owner } /#{ repo } /issues"
29+ |> GitHub . get_all ( % { } , [ access_token: access_token , params: [ per_page: 100 , state: "all" ] ] )
30+ |> ( & { :ok , & 1 } ) . ( )
1931 else
2032 { :error , error } -> { :error , error }
2133 end
2234 end
2335
24- defp fetch_issues ( % GithubRepo { github_app_installation: % GithubAppInstallation { github_account_login: owner } , name: repo } , access_token ) do
25- per_page = 100
26- path = "repos/#{ owner } /#{ repo } /issues"
27- params = [ per_page: per_page , state: "all" ]
28- opts = [ access_token: access_token , params: params ]
29- GitHub . get_all ( path , % { } , opts )
36+ @ doc ~S"""
37+ Retrieves pull requests for a repository.
38+
39+ All pages of records are retrieved.
40+ """
41+ @ spec pulls ( GithubRepo . t ) :: { :ok , list ( map ) } | { :error , GitHub . api_error_struct }
42+ def pulls ( % GithubRepo {
43+ github_app_installation: % GithubAppInstallation {
44+ github_account_login: owner
45+ } = installation ,
46+ name: repo
47+ } ) do
48+ with { :ok , access_token } <- API.Installation . get_access_token ( installation ) do
49+ "repos/#{ owner } /#{ repo } /pulls"
50+ |> GitHub . get_all ( % { } , [ access_token: access_token , params: [ per_page: 100 , state: "all" ] ] )
51+ |> ( & { :ok , & 1 } ) . ( )
52+ else
53+ { :error , error } -> { :error , error }
54+ end
55+ end
56+
57+ @ doc ~S"""
58+ Retrieves comments from all issues in a github repository.
59+ """
60+ @ spec issue_comments ( GithubRepo . t ) :: { :ok , list ( map ) } | { :error , GitHub . api_error_struct }
61+ def issue_comments ( % GithubRepo {
62+ github_app_installation: % GithubAppInstallation {
63+ github_account_login: owner
64+ } = installation ,
65+ name: repo
66+ } ) do
67+ with { :ok , access_token } <- API.Installation . get_access_token ( installation ) do
68+ "repos/#{ owner } /#{ repo } /issues/comments"
69+ |> GitHub . get_all ( % { } , [ access_token: access_token , params: [ per_page: 100 ] ] )
70+ |> ( & { :ok , & 1 } ) . ( )
71+ else
72+ { :error , error } -> { :error , error }
73+ end
3074 end
3175end
0 commit comments