@@ -5,6 +5,8 @@ defmodule MixDependencySubmission.CLI do
5
5
Used to configure and validate inputs for submitting a dependency snapshot.
6
6
"""
7
7
8
+ alias MixDependencySubmission.GitHub
9
+
8
10
@ app Mix.Project . config ( ) [ :app ]
9
11
@ description Mix.Project . config ( ) [ :description ]
10
12
@ version Mix.Project . config ( ) [ :version ]
@@ -25,10 +27,20 @@ defmodule MixDependencySubmission.CLI do
25
27
26
28
"""
27
29
@ spec parse! ( [ String . t ( ) ] ) :: Optimus.ParseResult . t ( )
28
- def parse! ( argv ) do
29
- cli_definition ( )
30
- |> Optimus . new! ( )
31
- |> Optimus . parse! ( argv )
30
+ case Mix . env ( ) do
31
+ :test ->
32
+ def parse! ( argv ) do
33
+ cli_definition ( )
34
+ |> Optimus . new! ( )
35
+ |> Optimus . parse! ( argv , & raise ( "Exit: #{ & 1 } " ) )
36
+ end
37
+
38
+ _other ->
39
+ def parse! ( argv ) do
40
+ cli_definition ( )
41
+ |> Optimus . new! ( )
42
+ |> Optimus . parse! ( argv )
43
+ end
32
44
end
33
45
34
46
@ spec cli_definition :: Optimus . spec ( )
@@ -59,41 +71,41 @@ defmodule MixDependencySubmission.CLI do
59
71
value_name: "GITHUB_API_URL" ,
60
72
long: "--github-api-url" ,
61
73
help: "GitHub API URL" ,
62
- parser: & parse_github_api_url / 1 ,
63
- default: System . get_env ( "GITHUB_API_URL" , "https://api.github.com" )
74
+ parser: & parse_uri / 1 ,
75
+ default: GitHub . get_api_url ( )
64
76
] ,
65
77
github_repository:
66
- optimus_options_with_env_default ( "GITHUB_REPOSITORY" ,
78
+ optimus_options_with_fun_default ( & GitHub . fetch_repository / 0 ,
67
79
value_name: "GITHUB_REPOSITORY" ,
68
80
long: "--github-repository" ,
69
81
help: ~S( GitHub repository name "owner/repository")
70
82
) ,
71
83
github_job_id:
72
- optimus_options_with_env_default ( "GITHUB_JOB" ,
84
+ optimus_options_with_fun_default ( & GitHub . fetch_job_id / 0 ,
73
85
value_name: "GITHUB_JOB" ,
74
86
long: "--github-job-id" ,
75
87
help: "GitHub Actions Job ID"
76
88
) ,
77
89
github_workflow:
78
- optimus_options_with_env_default ( "GITHUB_WORKFLOW" ,
90
+ optimus_options_with_fun_default ( & GitHub . fetch_workflow / 0 ,
79
91
value_name: "GITHUB_WORKFLOW" ,
80
92
long: "--github-workflow" ,
81
93
help: "GitHub Actions Workflow Name"
82
94
) ,
83
95
sha:
84
- sha_option (
96
+ optimus_options_with_fun_default ( & GitHub . fetch_head_sha / 0 ,
85
97
value_name: "SHA" ,
86
98
long: "--sha" ,
87
99
help: "Current Git SHA"
88
100
) ,
89
101
ref:
90
- optimus_options_with_env_default ( "GITHUB_REF" ,
102
+ optimus_options_with_fun_default ( & GitHub . fetch_ref / 0 ,
91
103
value_name: "REF" ,
92
104
long: "--ref" ,
93
105
help: "Current Git Ref"
94
106
) ,
95
107
github_token:
96
- optimus_options_with_env_default ( "GITHUB_TOKEN" ,
108
+ optimus_options_with_fun_default ( & GitHub . fetch_token / 0 ,
97
109
value_name: "GITHUB_TOKEN" ,
98
110
long: "--github-token" ,
99
111
help: "GitHub Token"
@@ -126,55 +138,22 @@ defmodule MixDependencySubmission.CLI do
126
138
end
127
139
end
128
140
129
- @ spec parse_github_api_url ( uri :: String . t ( ) ) :: Optimus . parser_result ( )
130
- defp parse_github_api_url ( uri ) do
141
+ @ spec parse_uri ( uri :: String . t ( ) ) :: Optimus . parser_result ( )
142
+ defp parse_uri ( uri ) do
131
143
with { :ok , % URI { } } <- URI . new ( uri ) do
132
- uri
144
+ { :ok , uri }
133
145
end
134
146
end
135
147
136
- @ spec optimus_options_with_env_default ( env :: String . t ( ) , details :: Keyword . t ( ) ) :: Keyword . t ( )
137
- defp optimus_options_with_env_default ( env , details ) do
138
- case System . fetch_env ( env ) do
148
+ @ spec optimus_options_with_fun_default (
149
+ fetch_fun :: ( -> { :ok , value } | :error ) ,
150
+ details :: Keyword . t ( )
151
+ ) :: Keyword . t ( )
152
+ when value: term ( )
153
+ defp optimus_options_with_fun_default ( fetch_fun , details ) when is_function ( fetch_fun , 0 ) do
154
+ case fetch_fun . ( ) do
139
155
{ :ok , value } -> [ default: value ]
140
156
:error -> [ required: true ]
141
157
end ++ details
142
158
end
143
-
144
- @ spec sha_option ( Keyword . t ( ) ) :: Keyword . t ( )
145
- defp sha_option ( base_opts ) do
146
- # If the GitHub event is a pull request, we need to use the head SHA of the PR
147
- # instead of the commit SHA of the workflow run.
148
- # This is because the workflow run is triggered by the base commit of the PR,
149
- # and we want to report the dependencies of the head commit.
150
- # See: https://github.com/github/dependency-submission-toolkit/blob/72f5e31325b5e1bcc91f1b12eb7abe68e75b2105/src/snapshot.ts#L36-L61
151
- case load_pr_head_sha ( ) do
152
- { :ok , sha } ->
153
- Keyword . put ( base_opts , :default , sha )
154
-
155
- :error ->
156
- # If we can't load the PR head SHA, we fall back to the default behavior
157
- # of using the GITHUB_SHA environment variable.
158
- optimus_options_with_env_default ( "GITHUB_SHA" , base_opts )
159
- end
160
- end
161
-
162
- # Note that pull_request_target is omitted here.
163
- # That event runs in the context of the base commit of the PR,
164
- # so the snapshot should not be associated with the head commit.
165
-
166
- @ pr_events ~w[ pull_request pull_request_comment pull_request_review pull_request_review_comment]
167
-
168
- @ spec load_pr_head_sha :: { :ok , << _ :: 320 >> } | :error
169
- defp load_pr_head_sha do
170
- with { :ok , event } when event in @ pr_events <- System . fetch_env ( "GITHUB_EVENT_NAME" ) ,
171
- { :ok , event_path } <- System . fetch_env ( "GITHUB_EVENT_PATH" ) do
172
- event_details_json = File . read! ( event_path )
173
-
174
- % { "pull_request" => % { "head" => % { "sha" => << _binary :: 320 >> = sha } } } =
175
- JSON . decode! ( event_details_json )
176
-
177
- { :ok , sha }
178
- end
179
- end
180
159
end
0 commit comments