@@ -65,25 +65,44 @@ def fetch_github_issues(repo: str, token: str, since: str | None = None) -> list
6565 return issues
6666
6767
68- def check_netbox (project_name : str , netbox_url : str , token : str ) -> tuple [bool , str | None ]:
69- """Look up a VM by project name in Netbox. Checks VMs then physical devices."""
70- if not project_name :
71- return False , None
68+ def check_netbox (project_name : str , netbox_url : str , token : str , issue_number : int ) -> tuple [bool , str | None ]:
69+ """Look up a VM by project name in Netbox, falling back to issue number in description."""
7270 headers = {"Authorization" : f"Token { token } " }
73- for endpoint in ("virtualization/virtual-machines" , "dcim/devices" ):
71+ endpoints = ("virtualization/virtual-machines" , "dcim/devices" )
72+
73+ # Pass 1: exact name match
74+ if project_name :
75+ for endpoint in endpoints :
76+ try :
77+ resp = requests .get (
78+ f"{ netbox_url } /api/{ endpoint } /" ,
79+ headers = headers ,
80+ params = {"name" : project_name , "limit" : 1 },
81+ timeout = 30 ,
82+ )
83+ if resp .status_code == 200 :
84+ results = resp .json ().get ("results" , [])
85+ if results :
86+ return True , results [0 ]["name" ]
87+ except requests .exceptions .RequestException as e :
88+ log .warning (f"Netbox name search failed for '{ project_name } ' on { endpoint } : { e } " )
89+
90+ # Pass 2: search for issue number in description field
91+ for endpoint in endpoints :
7492 try :
7593 resp = requests .get (
7694 f"{ netbox_url } /api/{ endpoint } /" ,
7795 headers = headers ,
78- params = {"name " : project_name , "limit" : 1 },
96+ params = {"description__icontains " : f"# { issue_number } " , "limit" : 10 },
7997 timeout = 30 ,
8098 )
8199 if resp .status_code == 200 :
82100 results = resp .json ().get ("results" , [])
83101 if results :
84102 return True , results [0 ]["name" ]
85103 except requests .exceptions .RequestException as e :
86- log .warning (f"Netbox request failed for '{ project_name } ' on { endpoint } : { e } " )
104+ log .warning (f"Netbox description search failed for issue #{ issue_number } on { endpoint } : { e } " )
105+
87106 return False , None
88107
89108
@@ -186,7 +205,7 @@ def main() -> None:
186205 expired , no_expiry , not_found = [], [], []
187206
188207 for entry in cache ["issues" ].values ():
189- netbox_match , netbox_vm = check_netbox (entry ["project_name" ], netbox_url , netbox_token )
208+ netbox_match , netbox_vm = check_netbox (entry ["project_name" ], netbox_url , netbox_token , entry [ "issue_number" ] )
190209 entry ["netbox_match" ] = netbox_match
191210 entry ["netbox_vm" ] = netbox_vm
192211 entry ["last_checked" ] = now .isoformat ()
0 commit comments