@@ -3,116 +3,123 @@ defmodule Lightning.VersionControl.GithubClient do
33 Tesla github http client we use this to make any network requests
44 to github from Lightning
55 """
6- use Tesla
7-
86 alias Lightning.VersionControl.GithubError
97 alias Lightning.VersionControl.GithubToken
108
119 require Logger
1210
13- plug ( Tesla.Middleware.BaseUrl , "https://api.github.com" )
14- plug ( Tesla.Middleware.JSON )
11+ defp adapter do
12+ Application . get_env ( :tesla , __MODULE__ , [ ] ) [ :adapter ]
13+ end
1514
1615 def create_repo_dispatch_event ( client , repo_name , body ) do
17- client |> post ( "/repos/#{ repo_name } /dispatches" , body ) |> handle_resp ( [ 204 ] )
16+ client
17+ |> Tesla . post ( "/repos/#{ repo_name } /dispatches" , body )
18+ |> handle_resp ( [ 204 ] )
1819 end
1920
2021 def create_workflow_dispatch_event ( client , repo_name , workflow_id , body ) do
2122 client
22- |> post (
23+ |> Tesla . post (
2324 "repos/#{ repo_name } /actions/workflows/#{ workflow_id } /dispatches" ,
2425 body
2526 )
2627 |> handle_resp ( [ 204 ] )
2728 end
2829
2930 def get_installations ( client ) do
30- client |> get ( "/user/installations" ) |> handle_resp ( [ 200 ] )
31+ client |> Tesla . get ( "/user/installations" ) |> handle_resp ( [ 200 ] )
3132 end
3233
3334 def get_installation_repos ( client , query \\ [ page: 1 , per_page: 100 ] ) do
3435 client
35- |> get ( "/installation/repositories" , query: query )
36+ |> Tesla . get ( "/installation/repositories" , query: query )
3637 |> handle_resp ( [ 200 ] )
3738 end
3839
3940 def get_repo ( client , repo_name ) do
40- client |> get ( "/repos/#{ repo_name } " ) |> handle_resp ( [ 200 ] )
41+ client |> Tesla . get ( "/repos/#{ repo_name } " ) |> handle_resp ( [ 200 ] )
4142 end
4243
4344 def get_repo_branches ( client , repo_name ) do
44- client |> get ( "/repos/#{ repo_name } /branches" ) |> handle_resp ( [ 200 ] )
45+ client |> Tesla . get ( "/repos/#{ repo_name } /branches" ) |> handle_resp ( [ 200 ] )
4546 end
4647
4748 def get_repo_content ( client , repo , path , ref ) do
4849 client
49- |> get ( "/repos/#{ repo } /contents/#{ path } " , query: [ ref: ref ] )
50+ |> Tesla . get ( "/repos/#{ repo } /contents/#{ path } " , query: [ ref: ref ] )
5051 |> handle_resp ( [ 200 ] )
5152 end
5253
5354 def delete_repo_content ( client , repo , path , body ) do
5455 client
55- |> delete ( "/repos/#{ repo } /contents/#{ path } " , body: body )
56+ |> Tesla . delete ( "/repos/#{ repo } /contents/#{ path } " , body: body )
5657 |> handle_resp ( [ 200 ] )
5758 end
5859
5960 def create_blob ( client , repo , body ) do
60- client |> post ( "/repos/#{ repo } /git/blobs" , body ) |> handle_resp ( [ 201 ] )
61+ client |> Tesla . post ( "/repos/#{ repo } /git/blobs" , body ) |> handle_resp ( [ 201 ] )
6162 end
6263
6364 def create_tree ( client , repo , body ) do
64- client |> post ( "/repos/#{ repo } /git/trees" , body ) |> handle_resp ( [ 201 ] )
65+ client |> Tesla . post ( "/repos/#{ repo } /git/trees" , body ) |> handle_resp ( [ 201 ] )
6566 end
6667
6768 def get_commit ( client , repo , ref ) do
68- client |> get ( "/repos/#{ repo } /commits/#{ ref } " ) |> handle_resp ( [ 200 ] )
69+ client |> Tesla . get ( "/repos/#{ repo } /commits/#{ ref } " ) |> handle_resp ( [ 200 ] )
6970 end
7071
7172 def create_commit ( client , repo , body ) do
72- client |> post ( "/repos/#{ repo } /git/commits" , body ) |> handle_resp ( [ 201 ] )
73+ client
74+ |> Tesla . post ( "/repos/#{ repo } /git/commits" , body )
75+ |> handle_resp ( [ 201 ] )
7376 end
7477
7578 def create_ref ( client , repo , body ) do
76- client |> post ( "/repos/#{ repo } /git/refs" , body ) |> handle_resp ( [ 201 ] )
79+ client |> Tesla . post ( "/repos/#{ repo } /git/refs" , body ) |> handle_resp ( [ 201 ] )
7780 end
7881
7982 def update_ref ( client , repo , ref , body ) do
80- client |> post ( "/repos/#{ repo } /git/refs/#{ ref } " , body ) |> handle_resp ( [ 200 ] )
83+ client
84+ |> Tesla . post ( "/repos/#{ repo } /git/refs/#{ ref } " , body )
85+ |> handle_resp ( [ 200 ] )
8186 end
8287
8388 def delete_ref ( client , repo , ref ) do
84- client |> delete ( "/repos/#{ repo } /git/refs/#{ ref } " ) |> handle_resp ( [ 204 ] )
89+ client
90+ |> Tesla . delete ( "/repos/#{ repo } /git/refs/#{ ref } " )
91+ |> handle_resp ( [ 204 ] )
8592 end
8693
8794 def delete_app_grant ( client , app_client_id , token ) do
8895 client
89- |> delete ( "/applications/#{ app_client_id } /grant" ,
96+ |> Tesla . delete ( "/applications/#{ app_client_id } /grant" ,
9097 body: % { access_token: token }
9198 )
9299 |> handle_resp ( [ 204 ] )
93100 end
94101
95102 def get_repo_public_key ( client , repo ) do
96103 client
97- |> get ( "/repos/#{ repo } /actions/secrets/public-key" )
104+ |> Tesla . get ( "/repos/#{ repo } /actions/secrets/public-key" )
98105 |> handle_resp ( [ 200 ] )
99106 end
100107
101108 def get_repo_secret ( client , repo , secret_name ) do
102109 client
103- |> get ( "/repos/#{ repo } /actions/secrets/#{ secret_name } " )
110+ |> Tesla . get ( "/repos/#{ repo } /actions/secrets/#{ secret_name } " )
104111 |> handle_resp ( [ 200 ] )
105112 end
106113
107114 def create_repo_secret ( client , repo , secret_name , body ) do
108115 client
109- |> put ( "/repos/#{ repo } /actions/secrets/#{ secret_name } " , body )
116+ |> Tesla . put ( "/repos/#{ repo } /actions/secrets/#{ secret_name } " , body )
110117 |> handle_resp ( [ 201 , 204 ] )
111118 end
112119
113120 def delete_repo_secret ( client , repo , secret_name ) do
114121 client
115- |> delete ( "/repos/#{ repo } /actions/secrets/#{ secret_name } " )
122+ |> Tesla . delete ( "/repos/#{ repo } /actions/secrets/#{ secret_name } " )
116123 |> handle_resp ( [ 204 ] )
117124 end
118125
@@ -126,26 +133,30 @@ defmodule Lightning.VersionControl.GithubClient do
126133 ] }
127134 ]
128135
129- { :ok , Tesla . client ( middleware ) }
136+ { :ok , Tesla . client ( middleware , adapter ( ) ) }
130137 end
131138
132139 def build_bearer_client ( token ) do
133140 middleware = [
141+ { Tesla.Middleware.BaseUrl , "https://api.github.com" } ,
142+ Tesla.Middleware.JSON ,
134143 { Tesla.Middleware.Headers ,
135144 [
136145 { "Authorization" , "Bearer #{ token } " }
137146 ] }
138147 ]
139148
140- { :ok , Tesla . client ( middleware ) }
149+ { :ok , Tesla . client ( middleware , adapter ( ) ) }
141150 end
142151
143152 def build_basic_auth_client ( username , password ) do
144153 middleware = [
154+ { Tesla.Middleware.BaseUrl , "https://api.github.com" } ,
155+ Tesla.Middleware.JSON ,
145156 { Tesla.Middleware.BasicAuth , [ username: username , password: password ] }
146157 ]
147158
148- { :ok , Tesla . client ( middleware ) }
159+ { :ok , Tesla . client ( middleware , adapter ( ) ) }
149160 end
150161
151162 def build_installation_client ( installation_id ) do
@@ -155,7 +166,7 @@ defmodule Lightning.VersionControl.GithubClient do
155166
156167 with { :ok , auth_token , _ } <- GithubToken . build ( cert , app_id ) ,
157168 { :ok , client } <- build_bearer_client ( auth_token ) do
158- case post (
169+ case Tesla . post (
159170 client ,
160171 "/app/installations/#{ installation_id } /access_tokens" ,
161172 ""
0 commit comments