@@ -63,9 +63,10 @@ pub enum ForgeEvent {
6363 repository : Repository ,
6464 private : bool ,
6565 } ,
66- /// Renamed or transferred: the same repository (by ID) at a new URL.
66+ /// Renamed or transferred: the same repository (by ID) at a new URL, for the installation that sent it .
6767 Moved {
6868 repository : Repository ,
69+ installation : String ,
6970 } ,
7071 /// The account an installation is on was renamed (an `installation_target` delivery, action `renamed`): its
7172 /// repositories' URLs follow. GitHub sends no per-repository `renamed` delivery for an organization's rename.
@@ -204,7 +205,10 @@ pub fn read_event(kind: &str, event: &Value) -> Result<ForgeEvent, String> {
204205 } ,
205206 // The same repository at a new URL; links follow by its ID
206207 ( "repository" , "renamed" | "transferred" ) => match repository ( event) {
207- Some ( repository) if repository. id . is_some ( ) => ForgeEvent :: Moved { repository } ,
208+ Some ( repository) if repository. id . is_some ( ) => match installation {
209+ Some ( installation) => ForgeEvent :: Moved { repository, installation } ,
210+ None => ForgeEvent :: Ignored ,
211+ } ,
208212 _ => ForgeEvent :: Ignored ,
209213 } ,
210214 // The account the App is installed on was renamed: its repositories have new URLs
@@ -927,11 +931,14 @@ pub async fn handle(app: &GithubApp, event: ForgeEvent, claimed: bool) -> larave
927931 }
928932 json ! ( { "dispatched" : projects. len( ) } )
929933 }
930- ForgeEvent :: Moved { repository } => {
934+ ForgeEvent :: Moved { repository, installation } => {
935+ // Only links through the installation that sent it, as the hourly sync: another installation's link to
936+ // the same repository ID may not cover its new owner
931937 let ( id, url) = ( repository. id . clone ( ) , repository. url . clone ( ) ) ;
932938 let moved = db:: query!(
933939 "UPDATE projects SET repository_url = {url}, updated_at = now() \
934- WHERE forge = 'github' AND repository_id = {id} AND repository_url IS DISTINCT FROM {url}"
940+ WHERE forge = 'github' AND repository_id = {id} AND forge_installation = {installation} \
941+ AND repository_url IS DISTINCT FROM {url}"
935942 )
936943 . update ( )
937944 . await ?;
@@ -1019,10 +1026,10 @@ pub async fn handle(app: &GithubApp, event: ForgeEvent, claimed: bool) -> larave
10191026///
10201027/// A delivery can arrive late (the recovery job redelivers missed ones), after a newer rename: a late `acme→beta` after
10211028/// `beta→acme` would move everything back to `beta`, and its logins can't tell it from a new rename. So GitHub is asked
1022- /// what the account is called now (one call), and only a rename to that name is applied. Then the installation's login
1023- /// changes only from `from` (any case), and URLs move only when it did, when it already reads `to` (a redelivery of the
1024- /// rename just applied), or when VisualCI has no row for the installation ( GitHub has just confirmed the new name, and
1025- /// only URLs under `from` move) .
1029+ /// what the account is called now (one call), and only a rename to that name is applied. Once GitHub has confirmed it,
1030+ /// the installation's login becomes GitHub's, whatever VisualCI had stored, and URLs move from both `from` and the
1031+ /// login VisualCI had: after quick renames (`acme→beta→gamma`, both delivered once GitHub says `gamma`, so the first is
1032+ /// ignored as stale) or a lost delivery, VisualCI still reads `acme` while this delivery says `beta` .
10261033///
10271034/// # Errors
10281035/// When GitHub can't be asked (the delivery fails, so it's redelivered), or on a database error.
@@ -1033,37 +1040,42 @@ pub async fn account_renamed(
10331040 to : & str ,
10341041) -> laravel:: Result < Option < u64 > > {
10351042 let now = github_app:: installation ( app, installation) . await ?;
1036- let current = now. as_ref ( ) . and_then ( |i| i[ "account" ] [ "login" ] . as_str ( ) ) ;
1037- if !current. is_some_and ( |c| c. eq_ignore_ascii_case ( to) ) {
1038- tracing:: info!( installation, from, to, current, "a stale installation_target rename: ignored" ) ;
1043+ let Some ( current) = now
1044+ . as_ref ( )
1045+ . and_then ( |i| i[ "account" ] [ "login" ] . as_str ( ) )
1046+ . filter ( |c| c. eq_ignore_ascii_case ( to) && login ( c) )
1047+ . map ( str:: to_string)
1048+ else {
1049+ tracing:: info!( installation, from, to, "a stale installation_target rename: ignored" ) ;
10391050 return Ok ( None ) ;
1040- }
1041- let ( id, from, to) = ( installation. to_string ( ) , from. to_lowercase ( ) , to. to_string ( ) ) ;
1042- let renamed = db:: query!(
1043- "UPDATE github_installations SET account_login = {to}, updated_at = now() \
1044- WHERE installation_id = {id} AND lower(account_login) = {from}"
1051+ } ;
1052+ let id = installation. to_string ( ) ;
1053+ let from = from. to_lowercase ( ) ;
1054+ let stored: Option < String > =
1055+ db:: query!( "SELECT account_login FROM github_installations WHERE installation_id = {id}" ) . scalar ( ) . await ?;
1056+ // The login VisualCI had, when there's a row: its URLs are under it if a delivery before this one was missed
1057+ let old = stored. map_or_else ( || from. clone ( ) , |l| l. to_lowercase ( ) ) ;
1058+ db:: query!(
1059+ "UPDATE github_installations SET account_login = {current}, updated_at = now() \
1060+ WHERE installation_id = {id} AND account_login IS DISTINCT FROM {current}"
10451061 )
10461062 . update ( )
10471063 . await ?;
1048- if renamed == 0 {
1049- let login: Option < String > =
1050- db:: query!( "SELECT account_login FROM github_installations WHERE installation_id = {id}" ) . scalar ( ) . await ?;
1051- if login. is_some_and ( |l| !l. eq_ignore_ascii_case ( & to) ) {
1052- tracing:: info!( installation, from, to, "the installation's login isn't the rename's: ignored" ) ;
1053- return Ok ( None ) ;
1054- }
1055- }
10561064 let moved = db:: query!(
1057- "UPDATE projects SET repository_url = 'https://github.com/' || {to} || '/' || split_part(repository_url, '/', 5), \
1065+ "UPDATE projects \
1066+ SET repository_url = 'https://github.com/' || {current} || '/' || split_part(repository_url, '/', 5), \
10581067 updated_at = now() \
10591068 WHERE forge = 'github' AND forge_installation = {id} AND deleted_at IS NULL \
1060- AND split_part(repository_url, '/', 3) = 'github.com' AND lower(split_part(repository_url, '/', 4)) = {from}"
1069+ AND split_part(repository_url, '/', 3) = 'github.com' \
1070+ AND lower(split_part(repository_url, '/', 4)) IN ({from}, {old}) \
1071+ AND split_part(repository_url, '/', 4) <> {current}"
10611072 )
10621073 . update ( )
10631074 . await ?;
10641075 db:: query!(
1065- "UPDATE user_repositories SET full_name = {to} || '/' || split_part(full_name, '/', 2) \
1066- WHERE installation_id = {id} AND lower(split_part(full_name, '/', 1)) = {from}"
1076+ "UPDATE user_repositories SET full_name = {current} || '/' || split_part(full_name, '/', 2) \
1077+ WHERE installation_id = {id} AND lower(split_part(full_name, '/', 1)) IN ({from}, {old}) \
1078+ AND split_part(full_name, '/', 1) <> {current}"
10671079 )
10681080 . update ( )
10691081 . await ?;
@@ -1151,6 +1163,17 @@ pub struct Reach {
11511163 /// (GitHub repository ID, installation) -> its `owner/name` now, which the hourly sync gives that installation's
11521164 /// links whose URL went stale. Only repositories whose collaborators the installation could list.
11531165 pub paths : HashMap < ( String , String ) , String > ,
1166+ /// Every linked project as it was read before GitHub was asked.
1167+ pub links : Vec < Link > ,
1168+ }
1169+
1170+ /// A linked project's repository, installation and URL when [`reach`] read them.
1171+ #[ derive( Debug ) ]
1172+ pub struct Link {
1173+ pub project_id : i64 ,
1174+ pub repository_id : String ,
1175+ pub installation : String ,
1176+ pub url : Option < String > ,
11541177}
11551178
11561179/// List every linked repository's collaborators once: one installation token per installation, each repository's
@@ -1161,17 +1184,29 @@ pub struct Reach {
11611184/// When GitHub can't answer for some repository (a partial answer would drop people), when its rate limit is low, or
11621185/// on a database error.
11631186pub async fn reach ( app : & GithubApp ) -> laravel:: Result < Reach > {
1187+ // Each link's URL as it is before GitHub is asked, so the sync only replaces a URL nothing changed meanwhile
11641188 let rows = db:: query!(
1165- "SELECT DISTINCT repository_id, forge_installation FROM projects \
1166- WHERE forge = 'github' AND repository_id IS NOT NULL AND forge_installation IS NOT NULL AND deleted_at IS NULL"
1189+ "SELECT id, repository_id, forge_installation, repository_url FROM projects \
1190+ WHERE forge = 'github' AND repository_id IS NOT NULL AND forge_installation IS NOT NULL AND deleted_at IS NULL \
1191+ ORDER BY repository_id, forge_installation, id"
11671192 )
11681193 . select ( )
11691194 . await ?;
11701195 let mut tokens: HashMap < String , String > = HashMap :: new ( ) ;
11711196 let mut reach = Reach :: default ( ) ;
1197+ let mut asked = HashSet :: new ( ) ;
11721198 for r in rows {
11731199 let ( repository_id, installation) : ( String , String ) =
11741200 ( r. try_get ( "repository_id" ) ?, r. try_get ( "forge_installation" ) ?) ;
1201+ reach. links . push ( Link {
1202+ project_id : r. try_get ( "id" ) ?,
1203+ repository_id : repository_id. clone ( ) ,
1204+ installation : installation. clone ( ) ,
1205+ url : r. try_get ( "repository_url" ) ?,
1206+ } ) ;
1207+ if !asked. insert ( ( repository_id. clone ( ) , installation. clone ( ) ) ) {
1208+ continue ;
1209+ }
11751210 let token = match tokens. get ( & installation) {
11761211 Some ( t) => t. clone ( ) ,
11771212 None => {
0 commit comments