@@ -26,59 +26,66 @@ const slackWrapper = (token, channel) => {
26
26
27
27
const delay = ( ms ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
28
28
29
- const fetchIssuesWithinTimeFrame = async ( gitToken , owner , repo , daysAgo , hoursAgo ) => {
30
- const apiUrl = `https://api.github.com/repos/${ owner } /${ repo } /issues?state=open` ;
31
- const now = new Date ( ) ;
32
- const sinceDate = new Date ( now . getTime ( ) - daysAgo * 24 * 60 * 60 * 1000 ) ;
33
- const cutoffDate = new Date ( now . getTime ( ) - hoursAgo * 60 * 60 * 1000 ) ;
34
-
35
- let issuesWithinTimeFrame = [ ] ;
36
- let page = 1 ;
37
- let olderIssueFound = false ;
38
-
39
- try {
40
- while ( true ) {
41
- const response = await axios . get ( apiUrl , {
42
- headers : { Authorization : `token ${ gitToken } ` } ,
43
- params : {
44
- page,
45
- per_page : 100 ,
46
- since : sinceDate . toISOString ( ) ,
47
- } ,
48
- } ) ;
49
-
50
- const issues = response . data . filter ( ( issue ) => ! issue . pull_request ) ;
51
-
52
- const recentIssues = issues . filter ( ( issue ) => {
53
- const createdAt = new Date ( issue . created_at ) ;
54
- if ( createdAt < cutoffDate ) {
55
- olderIssueFound = true ;
56
- return false ;
57
- }
58
- return createdAt <= now ;
59
- } ) ;
60
-
61
- issuesWithinTimeFrame . push (
62
- ...recentIssues . map ( ( issue ) => ( {
63
- title : issue . title ,
64
- url : issue . html_url ,
65
- createdAt : issue . created_at ,
66
- labels : issue . labels . map ( ( label ) => label . name ) ,
67
- comments : issue . comments ,
68
- } ) )
69
- ) ;
70
-
71
- if ( olderIssueFound || ! response . headers [ "link" ] ?. includes ( 'rel="next"' ) ) break ;
72
-
73
- page ++ ;
29
+ /**
30
+ * Fetch new issues created within a configurable timeframe.
31
+ *
32
+ * @param {string } gitToken - GitHub API token for authentication.
33
+ * @param {string } owner - Repository owner.
34
+ * @param {string } repo - Repository name.
35
+ * @param {number } hoursAgo - Timeframe in hours to fetch issues created since then.
36
+ * @returns {Promise<Array> } - List of new issues created within the specified timeframe.
37
+ */
38
+ const fetchNewIssues = async ( gitToken , owner , repo , hoursAgo ) => {
39
+ const apiUrl = `https://api.github.com/repos/${ owner } /${ repo } /issues` ;
40
+ const sinceDate = new Date ( new Date ( ) . getTime ( ) - hoursAgo * 60 * 60 * 1000 ) . toISOString ( ) ;
41
+
42
+ let newIssues = [ ] ;
43
+ let page = 1 ;
44
+
45
+ try {
46
+ while ( true ) {
47
+ const response = await axios . get ( apiUrl , {
48
+ headers : { Authorization : `token ${ gitToken } ` } ,
49
+ params : {
50
+ state : "open" , // Only fetch open issues
51
+ sort : "created" , // Sort by creation date
52
+ direction : "desc" , // Fetch the most recent issues first
53
+ since : sinceDate , // Fetch issues created or updated after this timestamp
54
+ per_page : 100 , // Maximum results per page
55
+ page, // Current page of the results
56
+ } ,
57
+ } ) ;
58
+
59
+ const issues = response . data . filter ( ( issue ) => {
60
+ // Exclude pull requests and ensure the issue is newly created
61
+ const createdAt = new Date ( issue . created_at ) ;
62
+ return ! issue . pull_request && createdAt >= new Date ( sinceDate ) ;
63
+ } ) ;
64
+
65
+ // Map issues to include required fields
66
+ newIssues . push (
67
+ ...issues . map ( ( issue ) => ( {
68
+ title : issue . title ,
69
+ url : issue . html_url ,
70
+ createdAt : issue . created_at ,
71
+ labels : issue . labels . map ( ( label ) => label . name ) ,
72
+ comments : issue . comments ,
73
+ } ) )
74
+ ) ;
75
+
76
+ // Exit loop if no more pages to fetch
77
+ if ( ! response . headers [ "link" ] ?. includes ( 'rel="next"' ) ) break ;
78
+
79
+ page ++ ; // Move to the next page
80
+ }
81
+
82
+ return newIssues ;
83
+ } catch ( error ) {
84
+ console . error ( "Error fetching issues:" , error . message ) ;
85
+ return [ ] ;
74
86
}
87
+ } ;
75
88
76
- return issuesWithinTimeFrame ;
77
- } catch ( error ) {
78
- console . error ( "Error fetching issues:" , error . message ) ;
79
- return [ ] ;
80
- }
81
- } ;
82
89
83
90
const sendSlackNotification = async ( slackToken , slackChannel , slackIDType , slackID , issues , repo ) => {
84
91
if ( ! issues . length ) {
@@ -119,10 +126,9 @@ const sendSlackNotification = async (slackToken, slackChannel, slackIDType, slac
119
126
} ;
120
127
121
128
async function monitorIssues ( gitToken , slackToken , slackChannel , owner , repo , slackIDType , slackID ) {
122
- const fetchDaysAgo = 1 ;
123
- const filterHoursAgo = 6 ;
129
+ const hoursAgo = 6 ;
124
130
125
- const issues = await fetchIssuesWithinTimeFrame ( gitToken , owner , repo , fetchDaysAgo , filterHoursAgo ) ;
131
+ const issues = await fetchNewIssues ( gitToken , owner , repo , hoursAgo ) ;
126
132
console . log ( "Issues to be notified via Slack:" , issues . map ( ( issue ) => issue . title ) ) ;
127
133
128
134
await sendSlackNotification ( slackToken , slackChannel , slackIDType , slackID , issues , repo ) ;
0 commit comments