@@ -37,17 +37,43 @@ export default async function({login, data, rest, q, account, imports}, {enabled
3737 }
3838 console . debug ( `metrics/compute/${ login } /plugins > activity > ${ events . length } events loaded` )
3939
40+ const payloadTypesToCustomTypes = {
41+ CommitCommentEvent :"comment" ,
42+ CreateEvent :"ref/create" ,
43+ DeleteEvent :"ref/delete" ,
44+ ForkEvent :"fork" ,
45+ GollumEvent :"wiki" ,
46+ IssueCommentEvent :"comment" ,
47+ IssuesEvent :"issue" ,
48+ MemberEvent :"member" ,
49+ PublicEvent :"public" ,
50+ PullRequestEvent :"pr" ,
51+ PullRequestReviewEvent :"review" ,
52+ PullRequestReviewCommentEvent :"comment" ,
53+ PushEvent :"push" ,
54+ ReleaseEvent :"release" ,
55+ WatchEvent :"star" ,
56+ }
57+
4058 //Extract activity events
4159 const activity = ( await Promise . all (
4260 events
4361 . filter ( ( { actor} ) => account === "organization" ? true : actor . login ?. toLocaleLowerCase ( ) === login . toLocaleLowerCase ( ) )
4462 . filter ( ( { created_at} ) => Number . isFinite ( days ) ? new Date ( created_at ) > new Date ( Date . now ( ) - days * 24 * 60 * 60 * 1000 ) : true )
4563 . filter ( event => visibility === "public" ? event . public : true )
46- . map ( async ( { type, payload, actor : { login : actor } , repo : { name : repo } , created_at} ) => {
47- //See https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/github-event-types
64+ . map ( event => ( { event, customType :payloadTypesToCustomTypes [ event . type ] } ) )
65+ . filter ( ( { customType} ) => ! ! customType ) //Ignore events with an unknown type
66+ . filter ( ( { customType} ) => filter . includes ( "all" ) || filter . includes ( customType ) ) //Filter events based on user preference
67+ . map ( ( { event} ) => event ) //Discard customType, it will be re-assigned
68+ . map ( async ( { type, payload, actor :{ login :actor } , repo :{ name :repo } , created_at} ) => { //See https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/github-event-types
4869 const timestamp = new Date ( created_at )
4970 if ( ! imports . filters . repo ( repo , skipped ) )
5071 return null
72+
73+ //Get custom type from the previously declared mapping, so that it acts as a single source of truth
74+ const customType = payloadTypesToCustomTypes [ type ]
75+ if ( ! customType ) throw new Error ( `Missing event mapping for type: ${ type } ` )
76+
5177 switch ( type ) {
5278 //Commented on a commit
5379 case "CommitCommentEvent" : {
@@ -56,27 +82,27 @@ export default async function({login, data, rest, q, account, imports}, {enabled
5682 const { comment : { user : { login : user } , commit_id : sha , body : content } } = payload
5783 if ( ! imports . filters . text ( user , ignored ) )
5884 return null
59- return { type : "comment" , on : "commit" , actor, timestamp, repo, content : await imports . markdown ( content , { mode : markdown , codelines} ) , user, mobile : null , number : sha . substring ( 0 , 7 ) , title : "" }
85+ return { type : customType , on : "commit" , actor, timestamp, repo, content : await imports . markdown ( content , { mode : markdown , codelines} ) , user, mobile : null , number : sha . substring ( 0 , 7 ) , title : "" }
6086 }
6187 //Created a git branch or tag
6288 case "CreateEvent" : {
6389 const { ref : name , ref_type : type } = payload
64- return { type : "ref/create" , actor, timestamp, repo, ref : { name, type} }
90+ return { type : customType , actor, timestamp, repo, ref : { name, type} }
6591 }
6692 //Deleted a git branch or tag
6793 case "DeleteEvent" : {
6894 const { ref : name , ref_type : type } = payload
69- return { type : "ref/delete" , actor, timestamp, repo, ref : { name, type} }
95+ return { type : customType , actor, timestamp, repo, ref : { name, type} }
7096 }
7197 //Forked repository
7298 case "ForkEvent" : {
7399 const { forkee : { full_name : forked } } = payload
74- return { type : "fork" , actor, timestamp, repo, forked}
100+ return { type : customType , actor, timestamp, repo, forked}
75101 }
76102 //Wiki changes
77103 case "GollumEvent" : {
78104 const { pages} = payload
79- return { type : "wiki" , actor, timestamp, repo, pages : pages . map ( ( { title} ) => title ) }
105+ return { type : customType , actor, timestamp, repo, pages : pages . map ( ( { title} ) => title ) }
80106 }
81107 //Commented on an issue
82108 case "IssueCommentEvent" : {
@@ -85,7 +111,7 @@ export default async function({login, data, rest, q, account, imports}, {enabled
85111 const { issue : { user : { login : user } , title, number} , comment : { body : content , performed_via_github_app : mobile } } = payload
86112 if ( ! imports . filters . text ( user , ignored ) )
87113 return null
88- return { type : "comment" , on : "issue" , actor, timestamp, repo, content : await imports . markdown ( content , { mode : markdown , codelines} ) , user, mobile, number, title}
114+ return { type : customType , on : "issue" , actor, timestamp, repo, content : await imports . markdown ( content , { mode : markdown , codelines} ) , user, mobile, number, title}
89115 }
90116 //Issue event
91117 case "IssuesEvent" : {
@@ -94,7 +120,7 @@ export default async function({login, data, rest, q, account, imports}, {enabled
94120 const { action, issue : { user : { login : user } , title, number, body : content } } = payload
95121 if ( ! imports . filters . text ( user , ignored ) )
96122 return null
97- return { type : "issue" , actor, timestamp, repo, action, user, number, title, content : await imports . markdown ( content , { mode : markdown , codelines} ) }
123+ return { type : customType , actor, timestamp, repo, action, user, number, title, content : await imports . markdown ( content , { mode : markdown , codelines} ) }
98124 }
99125 //Activity from repository collaborators
100126 case "MemberEvent" : {
@@ -103,11 +129,11 @@ export default async function({login, data, rest, q, account, imports}, {enabled
103129 const { member : { login : user } } = payload
104130 if ( ! imports . filters . text ( user , ignored ) )
105131 return null
106- return { type : "member" , actor, timestamp, repo, user}
132+ return { type : customType , actor, timestamp, repo, user}
107133 }
108134 //Made repository public
109135 case "PublicEvent" : {
110- return { type : "public" , actor, timestamp, repo}
136+ return { type : customType , actor, timestamp, repo}
111137 }
112138 //Pull requests events
113139 case "PullRequestEvent" : {
@@ -116,14 +142,14 @@ export default async function({login, data, rest, q, account, imports}, {enabled
116142 const { action, pull_request : { user : { login : user } , title, number, body : content , additions : added , deletions : deleted , changed_files : changed , merged} } = payload
117143 if ( ! imports . filters . text ( user , ignored ) )
118144 return null
119- return { type : "pr" , actor, timestamp, repo, action : ( action === "closed" ) && ( merged ) ? "merged" : action , user, title, number, content : await imports . markdown ( content , { mode : markdown , codelines} ) , lines : { added, deleted} , files : { changed} }
145+ return { type : customType , actor, timestamp, repo, action : ( action === "closed" ) && ( merged ) ? "merged" : action , user, title, number, content : await imports . markdown ( content , { mode : markdown , codelines} ) , lines : { added, deleted} , files : { changed} }
120146 }
121147 //Reviewed a pull request
122148 case "PullRequestReviewEvent" : {
123149 const { review : { state : review } , pull_request : { user : { login : user } , number, title} } = payload
124150 if ( ! imports . filters . text ( user , ignored ) )
125151 return null
126- return { type : "review" , actor, timestamp, repo, review, user, number, title}
152+ return { type : customType , actor, timestamp, repo, review, user, number, title}
127153 }
128154 //Commented on a pull request
129155 case "PullRequestReviewCommentEvent" : {
@@ -132,31 +158,36 @@ export default async function({login, data, rest, q, account, imports}, {enabled
132158 const { pull_request : { user : { login : user } , title, number} , comment : { body : content , performed_via_github_app : mobile } } = payload
133159 if ( ! imports . filters . text ( user , ignored ) )
134160 return null
135- return { type : "comment" , on : "pr" , actor, timestamp, repo, content : await imports . markdown ( content , { mode : markdown , codelines} ) , user, mobile, number, title}
161+ return { type : customType , on : "pr" , actor, timestamp, repo, content : await imports . markdown ( content , { mode : markdown , codelines} ) , user, mobile, number, title}
136162 }
137163 //Pushed commits
138164 case "PushEvent" : {
139- let { size, commits, ref} = payload
140- commits = commits . filter ( ( { author : { email} } ) => imports . filters . text ( email , ignored ) )
165+ let { size, ref, head, before} = payload
166+ const [ owner , repoName ] = repo . split ( "/" )
167+
168+ const res = await rest . repos . compareCommitsWithBasehead ( { owner, repo :repoName , basehead :`${ before } ...${ head } ` } )
169+ let { commits} = res . data
170+
171+ commits = commits . filter ( ( { author :{ email} } ) => imports . filters . text ( email , ignored ) )
141172 if ( ! commits . length )
142173 return null
143- if ( commits . slice ( - 1 ) . pop ( ) ?. message . startsWith ( "Merge branch " ) )
174+ if ( commits . slice ( - 1 ) . pop ( ) ?. commit . message . startsWith ( "Merge branch " ) )
144175 commits = commits . slice ( - 1 )
145- return { type : "push" , actor, timestamp, repo, size, branch : ref . match ( / r e f s .h e a d s .(?< branch > .* ) / ) ?. groups ?. branch ?? null , commits : commits . reverse ( ) . map ( ( { sha, message} ) => ( { sha : sha . substring ( 0 , 7 ) , message} ) ) }
176+ return { type :customType , actor, timestamp, repo, size, branch : ref . match ( / r e f s .h e a d s .(?< branch > .* ) / ) ?. groups ?. branch ?? null , commits : commits . reverse ( ) . map ( ( { sha, message} ) => ( { sha : sha . substring ( 0 , 7 ) , message} ) ) }
146177 }
147178 //Released
148179 case "ReleaseEvent" : {
149180 if ( ! [ "published" ] . includes ( payload . action ) )
150181 return null
151182 const { action, release : { name, tag_name, prerelease, draft, body : content } } = payload
152- return { type : "release" , actor, timestamp, repo, action, name : name || tag_name , prerelease, draft, content : await imports . markdown ( content , { mode : markdown , codelines} ) }
183+ return { type : customType , actor, timestamp, repo, action, name : name || tag_name , prerelease, draft, content : await imports . markdown ( content , { mode : markdown , codelines} ) }
153184 }
154185 //Starred a repository
155186 case "WatchEvent" : {
156187 if ( ! [ "started" ] . includes ( payload . action ) )
157188 return null
158189 const { action} = payload
159- return { type : "star" , actor, timestamp, repo, action}
190+ return { type : customType , actor, timestamp, repo, action}
160191 }
161192 //Unknown event
162193 default : {
@@ -166,7 +197,6 @@ export default async function({login, data, rest, q, account, imports}, {enabled
166197 } ) ,
167198 ) )
168199 . filter ( event => event )
169- . filter ( event => filter . includes ( "all" ) || filter . includes ( event . type ) )
170200 . slice ( 0 , limit )
171201
172202 //Results
0 commit comments