11import * as core from '@actions/core'
22import * as github from '@actions/github'
3- interface ApiConfig {
3+ interface ReportApiConfig {
4+ endpoint : string
5+ token : string
6+ }
7+ interface UserInfoServiceConfig {
48 endpoint : string
59 token : string
610}
@@ -12,27 +16,64 @@ interface Report {
1216 activity_term : string
1317 league : string
1418}
15- function processCommits ( ) : Report [ ] {
19+
20+ async function fetchUserInfo (
21+ username : string ,
22+ config : UserInfoServiceConfig
23+ ) : Promise < string > {
24+ try {
25+ const url = new URL ( config . endpoint )
26+ url . searchParams . append ( 'profileName' , username )
27+ const response = await fetch ( url . toString ( ) , {
28+ method : 'GET' ,
29+ headers : {
30+ Authorization : `tokenKey ${ config . token } ` ,
31+ 'Content-Type' : 'application/json'
32+ }
33+ } )
34+
35+ if ( ! response . ok ) {
36+ throw new Error ( `HTTP error! status: ${ response . status } ` )
37+ }
38+
39+ const data = await response . json ( ) as { orcid_id ?: string }
40+
41+ return data ?. orcid_id || 'unknown'
42+ } catch ( error ) {
43+ core . error ( `Failed to fetch user info for ${ username } : ${ error instanceof Error ? error . message : 'Unknown error' } ` )
44+ return 'unknown'
45+ }
46+ }
47+
48+ async function processCommits ( userInfoConfig : UserInfoServiceConfig ) : Promise < Report [ ] > {
1649 const { payload } = github . context
1750 const repo = payload . repository !
18- if ( ! payload . commits ) {
51+ if ( ! payload . commits ?. length ) {
1952 throw new Error ( 'No commits found in the payload' )
2053 }
21- return (
22- payload . commits ?. map ( ( commit : any ) => ( {
23- curator_orcid : commit . author ?. username || 'unknown' ,
24- entity_uri : `${ repo . html_url } /commit/${ commit . id } ` ,
25- resource_id : repo . full_name ?. toString ( ) ,
26- timestamp : commit . timestamp ,
27- activity_term : 'commit' ,
28- league : 'default'
29- } ) ) || [ ]
54+ const reports = await Promise . all (
55+ payload . commits . map ( async ( commit : any ) => {
56+ const username = commit . author ?. username || commit . committer ?. username
57+ if ( ! username ) {
58+ throw new Error ( 'No username found in the commit' )
59+ }
60+ const orcid = await fetchUserInfo ( username , userInfoConfig )
61+ return {
62+ curator_orcid : orcid ,
63+ entity_uri : `${ repo . html_url } /commit/${ commit . id } ` ,
64+ resource_id : repo . full_name ?. toString ( ) || 'unknown' ,
65+ timestamp : commit . timestamp ,
66+ activity_term : 'commit' ,
67+ league : 'default'
68+ }
69+ } )
3070 )
71+ return reports ;
3172}
3273
3374async function sendToApi (
3475 reports : Report [ ] ,
35- apiConfig : ApiConfig
76+ apiConfig : ReportApiConfig
3677) : Promise < void > {
3778 try {
3879 const response = await fetch ( apiConfig . endpoint , {
@@ -49,34 +90,33 @@ async function sendToApi(
4990 `API request failed: ${ response . status } ${ response . statusText } `
5091 )
5192 }
52-
53- core . info ( `Successfully sent ${ reports . length } reports to API` )
93+ core . info ( `Successfully sent ${ reports . length } reports` )
5494 } catch ( error ) {
55- core . error ( 'Failed to send reports to API ' )
95+ core . error ( 'Failed to send reports' )
5696 throw error
5797 }
5898}
59- /**
60- * The main function for the action.
61- *
62- * @returns Resolves when the action is complete.
63- */
99+
64100export async function run ( ) : Promise < void > {
65101 try {
66- const apiConfig : ApiConfig = {
67- endpoint : core . getInput ( 'APICURON_ENDPOINT ' , { required : true } ) ,
68- token : core . getInput ( 'APICURON_TOKEN' ) || ''
102+ const userInfoConfig : UserInfoServiceConfig = {
103+ endpoint : core . getInput ( 'USER_INFO_SERVICE_ENDPOINT ' , { required : true } ) ,
104+ token : core . getInput ( 'USER_INFO_SERVICE_TOKEN' )
69105 }
70- const reports = processCommits ( )
106+ const reportApiConfig : ReportApiConfig = {
107+ endpoint : core . getInput ( 'REPORT_API_ENDPOINT' , { required : true } ) ,
108+ token : core . getInput ( 'REPORT_API_TOKEN' )
109+ }
110+ const reports = await processCommits ( userInfoConfig )
111+
71112 if ( reports . length === 0 ) {
72- core . info ( 'No commits to process' )
113+ core . info ( 'No valid commits to process' )
73114 return
74115 }
75- console . log ( JSON . stringify ( reports ) )
76- console . log ( apiConfig )
77- await sendToApi ( reports , apiConfig )
78- core . setOutput ( 'reports' , JSON . stringify ( reports ) )
116+ console . log ( JSON . stringify ( reports , null , 2 ) )
117+ await sendToApi ( reports , reportApiConfig )
118+ core . setOutput ( 'reports sent:' , JSON . stringify ( reports ) )
79119 } catch ( error ) {
80120 if ( error instanceof Error ) core . setFailed ( error . message )
81121 }
82- }
122+ }
0 commit comments