@@ -52,8 +52,17 @@ function insertIssueIntoDatabase($pdo, $issue, $projectData = null) {
5252 $ gh_id = $ issue ['number ' ] ?? 0 ;
5353 $ gh_node_id = $ issue ['node_id ' ] ?? '' ;
5454 $ gh_id_url = $ issue ['html_url ' ] ?? '' ;
55- $ repo = $ issue ['repository ' ]['name ' ] ?? '' ;
56- $ repo_url = $ issue ['repository ' ]['html_url ' ] ?? '' ;
55+
56+ // Extract repository name from repository_url (for search API response)
57+ $ repo = '' ;
58+ $ repo_url = '' ;
59+ if (isset ($ issue ['repository_url ' ])) {
60+ $ repo_url = $ issue ['repository_url ' ];
61+ $ repo = basename ($ repo_url );
62+ } elseif (isset ($ issue ['repository ' ]['name ' ])) {
63+ $ repo = $ issue ['repository ' ]['name ' ];
64+ $ repo_url = $ issue ['repository ' ]['html_url ' ] ?? '' ;
65+ }
5766 $ issue_text = $ issue ['title ' ] ?? '' ;
5867 $ client = '' ;
5968 $ assigned_date = isset ($ issue ['created_at ' ]) ? date ('Y-m-d ' , strtotime ($ issue ['created_at ' ])) : date ('Y-m-d ' );
@@ -73,19 +82,39 @@ function insertIssueIntoDatabase($pdo, $issue, $projectData = null) {
7382 $ gh_project_title = '' ;
7483 }
7584
76- // Insert issue into database
85+ // Set project URL
86+ $ gh_project_url = $ projectData && is_array ($ projectData ) ? ($ projectData ['url ' ] ?? '' ) : '' ;
87+
88+ // Insert issue into database with ON DUPLICATE KEY UPDATE
7789 $ stmt = $ pdo ->prepare (
7890 "INSERT INTO gh_issues (gh_id, gh_node_id, gh_id_url, repo, repo_url, gh_project_url, issue_text, client, assigned_date, target_date,
7991 gh_json, assignee, gh_project, gh_project_title, last_updated_at, closed_at, gh_state) VALUES
80- (:gh_id, :gh_node_id, :gh_id_url, :repo, :repo_url, :gh_project_url, :issue_text, :client, :assigned_date, :target_date, :gh_json, :assignee, :gh_project, :gh_project_title, :updated_at, :closed_at, :gh_state) "
92+ (:gh_id, :gh_node_id, :gh_id_url, :repo, :repo_url, :gh_project_url, :issue_text, :client, :assigned_date, :target_date, :gh_json, :assignee, :gh_project, :gh_project_title, :updated_at, :closed_at, :gh_state)
93+ ON DUPLICATE KEY UPDATE
94+ gh_id = VALUES(gh_id),
95+ gh_id_url = VALUES(gh_id_url),
96+ repo = VALUES(repo),
97+ repo_url = VALUES(repo_url),
98+ gh_project_url = VALUES(gh_project_url),
99+ issue_text = VALUES(issue_text),
100+ client = VALUES(client),
101+ assigned_date = VALUES(assigned_date),
102+ target_date = VALUES(target_date),
103+ gh_json = VALUES(gh_json),
104+ assignee = VALUES(assignee),
105+ gh_project = VALUES(gh_project),
106+ gh_project_title = VALUES(gh_project_title),
107+ last_updated_at = VALUES(last_updated_at),
108+ closed_at = VALUES(closed_at),
109+ gh_state = VALUES(gh_state) "
81110 );
82111
83112 $ stmt ->bindParam (':gh_id ' , $ gh_id );
84113 $ stmt ->bindParam (':gh_node_id ' , $ gh_node_id );
85114 $ stmt ->bindParam (':gh_id_url ' , $ gh_id_url );
86115 $ stmt ->bindParam (':repo ' , $ repo );
87116 $ stmt ->bindParam (':repo_url ' , $ repo_url );
88- $ stmt ->bindParam (':gh_project_url ' , '' );
117+ $ stmt ->bindParam (':gh_project_url ' , $ gh_project_url );
89118 $ stmt ->bindParam (':issue_text ' , $ issue_text );
90119 $ stmt ->bindParam (':client ' , $ client );
91120 $ stmt ->bindParam (':assigned_date ' , $ assigned_date );
@@ -100,7 +129,7 @@ function insertIssueIntoDatabase($pdo, $issue, $projectData = null) {
100129
101130 try {
102131 $ stmt ->execute ();
103- write_log ("Inserted issue # {$ gh_id }: {$ issue_text } - Project: {$ gh_project_title }" );
132+ write_log ("Inserted/Updated issue # {$ gh_id }: {$ issue_text } - Project: {$ gh_project_title }" );
104133 } catch (PDOException $ e ) {
105134 write_log ("Database Error inserting issue # {$ gh_id }: " . $ e ->getMessage ());
106135 return ;
@@ -113,7 +142,7 @@ function insertIssueIntoDatabase($pdo, $issue, $projectData = null) {
113142 if (is_array ($ label ) && isset ($ label ['name ' ]) && isset ($ label ['color ' ])) {
114143 $ tag = $ label ['name ' ];
115144 $ color = $ label ['color ' ];
116- $ stmt = $ pdo ->prepare ("INSERT INTO gh_issue_tags (gh_node_id, tag, color) VALUES (:gh_node_id, :tag, :color) " );
145+ $ stmt = $ pdo ->prepare ("INSERT INTO gh_issue_tags (gh_node_id, tag, color) VALUES (:gh_node_id, :tag, :color) ON DUPLICATE KEY UPDATE color = VALUES(color) " );
117146 $ stmt ->bindParam (':gh_node_id ' , $ gh_node_id );
118147 $ stmt ->bindParam (':tag ' , $ tag );
119148 $ stmt ->bindParam (':color ' , $ color );
@@ -136,7 +165,8 @@ function fetchAllIssuesWithProjects($githubOrg, $githubToken, $appName) {
136165 write_log ("Starting to fetch issues from GitHub API... " );
137166
138167 while (true ) {
139- $ apiUrl = "https://api.github.com/orgs/ {$ githubOrg }/issues?filter=all&state=open&per_page= {$ perPage }&page= {$ page }" ;
168+ // Use the correct GitHub API endpoint for organization issues
169+ $ apiUrl = "https://api.github.com/search/issues?q=org: {$ githubOrg }+is:issue+is:open&per_page= {$ perPage }&page= {$ page }" ;
140170
141171 $ curl = curl_init ();
142172 curl_setopt ($ curl , CURLOPT_URL , $ apiUrl );
@@ -152,26 +182,31 @@ function fetchAllIssuesWithProjects($githubOrg, $githubToken, $appName) {
152182 curl_close ($ curl );
153183
154184 if ($ httpCode !== 200 ) {
155- write_log ("GitHub API error: HTTP {$ httpCode } for page {$ page }" );
185+ write_log ("GitHub API error: HTTP {$ httpCode } for page {$ page }. Response: " . substr ( $ response , 0 , 200 ) );
156186 break ;
157187 }
158188
159- $ issues = json_decode ($ response , true );
189+ $ data = json_decode ($ response , true );
160190
161- if (empty ($ issues )) {
191+ if (! isset ( $ data [ ' items ' ]) || empty ($ data [ ' items ' ] )) {
162192 write_log ("No more issues found on page {$ page }" );
163193 break ;
164194 }
165195
196+ $ issues = $ data ['items ' ];
166197 write_log ("Fetched " . count ($ issues ) . " issues from page {$ page }" );
167198
168199 foreach ($ issues as $ issue ) {
169200 if (isset ($ issue ['pull_request ' ])) {
170201 continue ; // Skip pull requests
171202 }
172203
204+ // Extract repository name from the issue URL
205+ $ repoUrl = $ issue ['repository_url ' ];
206+ $ repoName = basename ($ repoUrl );
207+
173208 // For each issue, fetch its project assignments
174- $ projectData = fetchIssueProjects ($ githubOrg , $ githubToken , $ appName , $ issue [ ' repository ' ][ ' name ' ] , $ issue ['number ' ]);
209+ $ projectData = fetchIssueProjects ($ githubOrg , $ githubToken , $ appName , $ repoName , $ issue ['number ' ]);
175210 $ issue ['project_data ' ] = $ projectData ;
176211 $ allIssues [] = $ issue ;
177212 }
0 commit comments