11import  logging 
22import  re 
3+ from  typing  import  Literal 
34
4- from  github  import  Github 
5+ from  github  import  Auth ,  Github 
56from  pydantic  import  BaseModel , SecretStr 
67from  pydantic_settings  import  BaseSettings 
78
@@ -14,7 +15,7 @@ class Settings(BaseSettings):
1415    deploy_url : str  |  None  =  None 
1516    commit_sha : str 
1617    run_id : int 
17-     is_done :  bool   =   False 
18+     state :  Literal [ "pending" ,  "success" ,  "error" ]  =   "pending" 
1819
1920
2021class  LinkData (BaseModel ):
@@ -27,7 +28,7 @@ def main() -> None:
2728    settings  =  Settings ()
2829
2930    logging .info (f"Using config: { settings .model_dump_json ()}  )
30-     g  =  Github (settings .github_token .get_secret_value ())
31+     g  =  Github (auth = Auth . Token ( settings .github_token .get_secret_value () ))
3132    repo  =  g .get_repo (settings .github_repository )
3233    use_pr  =  next (
3334        (pr  for  pr  in  repo .get_pulls () if  pr .head .sha  ==  settings .commit_sha ), None 
@@ -38,24 +39,35 @@ def main() -> None:
3839    commits  =  list (use_pr .get_commits ())
3940    current_commit  =  [c  for  c  in  commits  if  c .sha  ==  settings .commit_sha ][0 ]
4041    run_url  =  f"https://github.com/{ settings .github_repository } { settings .run_id }  
41-     if  settings .is_done   and   not   settings . deploy_url :
42+     if  settings .state   ==   "pending" :
4243        current_commit .create_status (
43-             state = "success " ,
44-             description = "No  Docs Changes " ,
44+             state = "pending " ,
45+             description = "Deploying  Docs" ,
4546            context = "deploy-docs" ,
4647            target_url = run_url ,
4748        )
48-         logging .info ("No docs changes found " )
49+         logging .info ("No deploy URL available yet " )
4950        return 
51+     if  settings .state  ==  "error" :
52+         current_commit .create_status (
53+             state = "error" ,
54+             description = "Error Deploying Docs" ,
55+             context = "deploy-docs" ,
56+             target_url = run_url ,
57+         )
58+         logging .info ("Error deploying docs" )
59+         return 
60+     assert  settings .state  ==  "success" 
5061    if  not  settings .deploy_url :
5162        current_commit .create_status (
52-             state = "pending " ,
53-             description = "Deploying  Docs" ,
63+             state = "success " ,
64+             description = "No  Docs Changes " ,
5465            context = "deploy-docs" ,
5566            target_url = run_url ,
5667        )
57-         logging .info ("No deploy URL available yet " )
68+         logging .info ("No docs changes found " )
5869        return 
70+     assert  settings .deploy_url 
5971    current_commit .create_status (
6072        state = "success" ,
6173        description = "Docs Deployed" ,
@@ -84,7 +96,9 @@ def main() -> None:
8496        links .append (link )
8597        links .sort (key = lambda  x : x .preview_link )
8698
87-     message  =  f"📝 Docs preview for commit { settings .commit_sha } { deploy_url }  
99+     header  =  "## 📝 Docs preview" 
100+     message  =  header 
101+     message  +=  f"\n \n Last commit { settings .commit_sha } { deploy_url }  
88102
89103    if  links :
90104        message  +=  "\n \n ### Modified Pages\n \n " 
@@ -94,7 +108,17 @@ def main() -> None:
94108            message  +=  "\n " 
95109
96110    print (message )
97-     use_pr .as_issue ().create_comment (message )
111+     issue  =  use_pr .as_issue ()
112+     comments  =  list (issue .get_comments ())
113+     for  comment  in  comments :
114+         if  (
115+             comment .body .startswith (header )
116+             and  comment .user .login  ==  "github-actions[bot]" 
117+         ):
118+             comment .edit (message )
119+             break 
120+     else :
121+         issue .create_comment (message )
98122
99123    logging .info ("Finished" )
100124
0 commit comments