1+ //Setup
2+ export default async function ( { login, q, imports, data, graphql, queries, account} , { enabled = false } = { } ) {
3+ //Plugin execution
4+ try {
5+ //Check if plugin is enabled and requirements are met
6+ if ( ( ! enabled ) || ( ! q . reactions ) )
7+ return null
8+
9+ //Load inputs
10+ let { limit, days, details} = imports . metadata . plugins . reactions . inputs ( { data, account, q} )
11+
12+ //Load issue comments
13+ let cursor = null , pushed = 0
14+ const comments = [ ]
15+ for ( const type of [ "issues" , "issueComments" ] ) {
16+ do {
17+ //Load issue comments
18+ console . debug ( `metrics/compute/${ login } /plugins > reactions > retrieving ${ type } after ${ cursor } ` )
19+ const { user :{ [ type ] :{ edges} } } = await graphql ( queries . reactions ( { login, type, after :cursor ? `after: "${ cursor } "` : "" } ) )
20+ cursor = edges ?. [ edges ?. length - 1 ] ?. cursor
21+ //Save issue comments
22+ const filtered = edges . flatMap ( ( { node :{ createdAt :created , reactions :{ nodes :reactions } } } ) => ( { created :new Date ( created ) , reactions :reactions . map ( ( { content} ) => content ) } ) ) . filter ( comment => Number . isFinite ( days ) ? comment . created < new Date ( Date . now ( ) - days * 24 * 60 * 60 * 1000 ) : true )
23+ pushed = filtered . length
24+ comments . push ( ...filtered )
25+ console . debug ( `metrics/compute/${ login } /plugins > reactions > currently at ${ comments . length } comments` )
26+ //Early break
27+ if ( ( comments . length >= limit ) || ( filtered . length < edges . length ) )
28+ break
29+ } while ( ( cursor ) && ( pushed ) && ( comments . length < limit ) )
30+ }
31+
32+ //Applying limit
33+ if ( limit ) {
34+ comments . splice ( limit )
35+ console . debug ( `metrics/compute/${ login } /plugins > reactions > keeping only ${ comments . length } comments` )
36+ }
37+
38+ //Format reactions list
39+ const list = { }
40+ const reactions = comments . flatMap ( ( { reactions} ) => reactions )
41+ for ( const reaction of reactions )
42+ list [ reaction ] = ( list [ reaction ] ?? 0 ) + 1
43+ for ( const [ key , value ] of Object . entries ( list ) )
44+ list [ key ] = { value, score :value / reactions . length }
45+
46+ //Results
47+ return { list, comments :comments . length , details, days}
48+ }
49+ //Handle errors
50+ catch ( error ) {
51+ throw { error :{ message :"An error occured" , instance :error } }
52+ }
53+ }
0 commit comments