1010//! whose file on the default branch uses `githubnext/projections`.
1111
1212use super :: Origin ;
13- use crate :: { artifact, runtime:: Runner } ;
13+ use crate :: {
14+ artifact,
15+ runtime:: { ProcessFailure , Runner } ,
16+ } ;
1417use anyhow:: { Context , Result } ;
1518use serde_json:: { Value , json} ;
1619use std:: {
@@ -89,37 +92,45 @@ impl<'a> GitHub<'a> {
8992
9093 /// The workflow file of `run` (an Actions run from GitHub's API) when its
9194 /// artifact is this repository's own CI output, with the basis it was
92- /// accepted on ([`Basis`]), else `None`.
93- pub fn accepted ( & self , run : & Value ) -> Option < ( String , Basis ) > {
94- let workflow = candidate ( run, & self . repository ) ?;
95+ /// accepted on ([`Basis`]), else `None`. It fails when GitHub can't say
96+ /// whether the workflow uses the Action, rather than calling it ineligible.
97+ pub fn accepted ( & self , run : & Value ) -> Result < Option < ( String , Basis ) > > {
98+ let Some ( workflow) = candidate ( run, & self . repository ) else {
99+ return Ok ( None ) ;
100+ } ;
95101 let basis = if self . workflows . is_empty ( ) {
96- self . runs_action ( & workflow) . then_some ( Basis :: UsesAction )
102+ self . runs_action ( & workflow) ? . then_some ( Basis :: UsesAction )
97103 } else {
98104 names ( self . workflows , & workflow) . then_some ( Basis :: Configured )
99105 } ;
100- basis. map ( |basis| ( workflow, basis) )
106+ Ok ( basis. map ( |basis| ( workflow, basis) ) )
101107 }
102108
103109 /// Whether the workflow file at `path` on the default branch uses the
104- /// Projections Action.
105- fn runs_action ( & self , path : & str ) -> bool {
110+ /// Projections Action: not when it's absent there (GitHub's 404), and an
111+ /// error when it can't be read (authentication, rate limits, transport).
112+ fn runs_action ( & self , path : & str ) -> Result < bool > {
106113 if let Some ( known) = self . uses_action . borrow ( ) . get ( path) {
107- return * known;
114+ return Ok ( * known) ;
108115 }
109116 let action = regex:: Regex :: new ( r#"(?m)^\s*(?:-\s+)?uses:\s*["']?githubnext/projections@"# ) . expect ( "pattern" ) ;
110117 // Without a ref, the contents API reads the default branch.
111- let uses = self
112- . gh ( & [
113- "api" ,
114- "--hostname" ,
115- & self . host ,
116- "-H" ,
117- "Accept: application/vnd.github.raw+json" ,
118- & format ! ( "repos/{}/contents/{path}" , self . repository) ,
119- ] )
120- . is_ok_and ( |text| action. is_match ( & text) ) ;
118+ let uses = match self . gh ( & [
119+ "api" ,
120+ "--hostname" ,
121+ & self . host ,
122+ "-H" ,
123+ "Accept: application/vnd.github.raw+json" ,
124+ & format ! ( "repos/{}/contents/{path}" , self . repository) ,
125+ ] ) {
126+ Ok ( text) => action. is_match ( & text) ,
127+ Err ( error) if not_found ( & error) => false ,
128+ Err ( error) => {
129+ return Err ( error. context ( format ! ( "couldn't read {path} on the default branch of {}" , self . repository) ) ) ;
130+ }
131+ } ;
121132 self . uses_action . borrow_mut ( ) . insert ( path. to_owned ( ) , uses) ;
122- uses
133+ Ok ( uses)
123134 }
124135
125136 /// Downloads into `base/<run>` the `name` artifact of the newest
@@ -134,7 +145,7 @@ impl<'a> GitHub<'a> {
134145 let Some ( id) = run[ "id" ] . as_u64 ( ) . filter ( |id| * id > 0 ) else {
135146 continue ;
136147 } ;
137- let Some ( ( workflow, basis) ) = self . accepted ( run) else {
148+ let Some ( ( workflow, basis) ) = self . accepted ( run) ? else {
138149 continue ;
139150 } ;
140151 let artifacts = self . api ( & format ! ( "repos/{}/actions/runs/{id}/artifacts?name=projections" , self . repository) ) ?;
@@ -223,7 +234,7 @@ impl<'a> GitHub<'a> {
223234 let run = self . api ( & format ! ( "repos/{}/actions/runs/{id}" , self . repository) ) ?;
224235 anyhow:: ensure!( run[ "conclusion" ] == "success" , "run {id} didn't succeed" ) ;
225236 let ( workflow, _) = self
226- . accepted ( & run)
237+ . accepted ( & run) ?
227238 . with_context ( || format ! ( "run {id} isn't this repository's own Projections workflow" ) ) ?;
228239 anyhow:: ensure!( signed. contains( & workflow) , "run {id}'s attestation names another workflow" ) ;
229240 Ok ( Origin {
@@ -261,6 +272,15 @@ impl Basis {
261272 }
262273}
263274
275+ /// Whether a failed `gh api` call was GitHub's 404: what it answers for a file
276+ /// absent on the branch it reads.
277+ fn not_found ( error : & anyhow:: Error ) -> bool {
278+ error
279+ . chain ( )
280+ . find_map ( |cause| cause. downcast_ref :: < ProcessFailure > ( ) )
281+ . is_some_and ( |failure| !failure. timed_out ( ) && failure. stderr ( ) . contains ( "(HTTP 404)" ) )
282+ }
283+
264284/// Whether `workflows` (paths or file names) names the workflow file `workflow`.
265285pub fn names ( workflows : & [ String ] , workflow : & str ) -> bool {
266286 workflows
0 commit comments