@@ -15,6 +15,11 @@ const DEVICE_EXPIRATION_MAX_SECONDS: u64 = 86_400;
1515// Once an interval reaches the maximum lifetime, no further poll can occur.
1616const DEVICE_INTERVAL_MAX_SECONDS : u64 = DEVICE_EXPIRATION_MAX_SECONDS ;
1717const DEVICE_SLOW_DOWN_SECONDS : u64 = 5 ;
18+ // The changelog shows the newest releases; one bounded page keeps the
19+ // anonymous request small.
20+ const RELEASE_PAGE_SIZE : u64 = 20 ;
21+ // A Git ref name is bounded like a file name; GitHub refuses longer ones.
22+ const GIT_REF_MAX_BYTES : usize = 255 ;
1823
1924#[ derive( Debug , Deserialize ) ]
2025#[ serde( rename_all = "camelCase" ) ]
@@ -200,6 +205,9 @@ pub struct RequestPlanRequest {
200205 /// Full commit SHA required by commit-status operations.
201206 #[ serde( default ) ]
202207 pub commit : Option < String > ,
208+ /// Branch, tag or `HEAD` whose newest commit `latestCommit` looks up.
209+ #[ serde( default , rename = "ref" ) ]
210+ pub git_ref : Option < String > ,
203211 /// OAuth application's public client identifier.
204212 #[ serde( default ) ]
205213 pub client_id : Option < String > ,
@@ -452,6 +460,24 @@ pub struct GitHubComparison {
452460 pub files : Vec < GitHubPullRequestFile > ,
453461}
454462
463+ #[ derive( Debug , Clone , PartialEq , Eq , Deserialize , Serialize ) ]
464+ #[ serde( rename_all = "camelCase" ) ]
465+ /// Normalized published release of a repository, as a changelog lists it.
466+ pub struct GitHubRelease {
467+ /// Git tag the release was published from.
468+ pub tag_name : String ,
469+ /// Release title; null when the author left it empty.
470+ pub name : Option < String > ,
471+ /// Markdown release notes; empty when the author wrote none.
472+ pub body : String ,
473+ /// ISO-8601 publication timestamp.
474+ pub published_at : Option < String > ,
475+ /// Whether the author marked the release as a pre-release.
476+ pub prerelease : bool ,
477+ /// Browser URL of the release page.
478+ pub url : String ,
479+ }
480+
455481/// Parses one supported GitHub remote URL.
456482pub fn parse_remote ( request : ParseRemoteRequest ) -> Result < GitHubRepository , CoreError > {
457483 let value = request. remote_url . trim ( ) . trim_end_matches ( '/' ) ;
@@ -634,6 +660,32 @@ pub fn request_plan(request: RequestPlanRequest) -> Result<GitHubRequestPlan, Co
634660 true ,
635661 )
636662 }
663+ // Public reads: an anonymous request works on any public repository,
664+ // so a signed-out user gets the same answer.
665+ "latestCommit" => {
666+ let repository = repository_path ( & request) ?;
667+ let git_ref = git_ref ( & request) ?;
668+ query. insert ( "sha" . to_string ( ) , git_ref. to_string ( ) ) ;
669+ query. insert ( "per_page" . to_string ( ) , "1" . to_string ( ) ) ;
670+ (
671+ GitHubHost :: Api ,
672+ "GET" ,
673+ format ! ( "/repos/{repository}/commits" ) ,
674+ None ,
675+ false ,
676+ )
677+ }
678+ "listReleases" => {
679+ let repository = repository_path ( & request) ?;
680+ query. insert ( "per_page" . to_string ( ) , RELEASE_PAGE_SIZE . to_string ( ) ) ;
681+ (
682+ GitHubHost :: Api ,
683+ "GET" ,
684+ format ! ( "/repos/{repository}/releases" ) ,
685+ None ,
686+ false ,
687+ )
688+ }
637689 "listPullRequestComments" => {
638690 let repository = repository_path ( & request) ?;
639691 let number = pull_number ( & request) ?;
@@ -744,6 +796,8 @@ pub fn normalize_response(request: NormalizeResponseRequest) -> Result<Value, Co
744796 "listReviewComments" => normalize_review_comment_list ( value) ,
745797 "listCheckRuns" => normalize_check_run_list ( value) ,
746798 "getCombinedStatus" => normalize_combined_status ( value) ,
799+ "latestCommit" => normalize_latest_commit ( value) ,
800+ "listReleases" => normalize_release_list ( value) ,
747801 "createPullRequestComment" => {
748802 Ok ( serde_json:: to_value ( normalize_comment ( & value) ?) . expect ( "comment encodes" ) )
749803 }
@@ -766,6 +820,24 @@ fn commit_path(request: &RequestPlanRequest) -> Result<(String, String), CoreErr
766820 Ok ( ( repository, commit. to_string ( ) ) )
767821}
768822
823+ /// The ref travels as a query value, so it is only bounded here; GitHub
824+ /// answers an unknown ref with an error.
825+ fn git_ref ( request : & RequestPlanRequest ) -> Result < & str , CoreError > {
826+ let value = required_text ( request. git_ref . as_deref ( ) , "ref" ) ?;
827+ let valid = value. len ( ) <= GIT_REF_MAX_BYTES
828+ && !value. contains ( ".." )
829+ && !value. starts_with ( '-' )
830+ && value. chars ( ) . all ( |character| {
831+ character. is_ascii_graphic ( )
832+ && !matches ! ( character, '~' | '^' | ':' | '?' | '*' | '[' | '\\' )
833+ } ) ;
834+ if valid {
835+ Ok ( value)
836+ } else {
837+ Err ( invalid_field ( "ref" ) )
838+ }
839+ }
840+
769841fn repository_path ( request : & RequestPlanRequest ) -> Result < String , CoreError > {
770842 request
771843 . repository
@@ -1097,6 +1169,59 @@ fn normalize_combined_status(value: Value) -> Result<Value, CoreError> {
10971169 Ok ( serde_json:: to_value ( checks) . expect ( "combined status encodes" ) )
10981170}
10991171
1172+ /// The commit list is asked for one entry: the ref's newest commit. A full
1173+ /// SHA-1 or SHA-256 object name is required, because the caller builds an
1174+ /// immutable file URL from it.
1175+ fn normalize_latest_commit ( value : Value ) -> Result < Value , CoreError > {
1176+ let first = value
1177+ . as_array ( )
1178+ . and_then ( |commits| commits. first ( ) )
1179+ . ok_or_else ( parse_shape_error) ?;
1180+ let sha = text ( object ( first) ?, "sha" ) ?;
1181+ if !matches ! ( sha. len( ) , 40 | 64 ) || !sha. bytes ( ) . all ( |byte| byte. is_ascii_hexdigit ( ) ) {
1182+ return Err ( parse_shape_error ( ) ) ;
1183+ }
1184+ Ok ( json ! ( { "sha" : sha. to_ascii_lowercase( ) } ) )
1185+ }
1186+
1187+ /// Drafts are dropped (an anonymous reader never sees them). The newest
1188+ /// publication comes first; equal timestamps fall back to the tag so the
1189+ /// order does not depend on GitHub's.
1190+ fn normalize_release_list ( value : Value ) -> Result < Value , CoreError > {
1191+ let mut releases = Vec :: new ( ) ;
1192+ for value in value. as_array ( ) . ok_or_else ( parse_shape_error) ? {
1193+ let object = object ( value) ?;
1194+ if object. get ( "draft" ) . and_then ( Value :: as_bool) == Some ( true ) {
1195+ continue ;
1196+ }
1197+ let optional = |key : & str | {
1198+ object
1199+ . get ( key)
1200+ . and_then ( Value :: as_str)
1201+ . filter ( |value| !value. trim ( ) . is_empty ( ) )
1202+ . map ( str:: to_string)
1203+ } ;
1204+ releases. push ( GitHubRelease {
1205+ tag_name : text ( object, "tag_name" ) ?. to_string ( ) ,
1206+ name : optional ( "name" ) ,
1207+ body : optional ( "body" ) . unwrap_or_default ( ) ,
1208+ published_at : optional ( "published_at" ) ,
1209+ prerelease : object
1210+ . get ( "prerelease" )
1211+ . and_then ( Value :: as_bool)
1212+ . unwrap_or ( false ) ,
1213+ url : text ( object, "html_url" ) ?. to_string ( ) ,
1214+ } ) ;
1215+ }
1216+ releases. sort_by ( |left, right| {
1217+ right
1218+ . published_at
1219+ . cmp ( & left. published_at )
1220+ . then_with ( || right. tag_name . cmp ( & left. tag_name ) )
1221+ } ) ;
1222+ Ok ( serde_json:: to_value ( releases) . expect ( "release list encodes" ) )
1223+ }
1224+
11001225fn normalize_file_list ( value : Value ) -> Result < Value , CoreError > {
11011226 let array = value. as_array ( ) . ok_or_else ( parse_shape_error) ?;
11021227 let mut files = array
0 commit comments