forked from Mehdi-SK/apicuron-on-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
173 lines (158 loc) · 5.06 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import * as core from '@actions/core'
import * as github from '@actions/github'
interface ReportApiConfig {
endpoint: string
token: string
}
interface UserInfoServiceConfig {
endpoint: string
token: string
}
interface Report {
curator_orcid: string
entity_uri: string
resource_id: string
timestamp: string
activity_term: string
league: string
}
async function fetchUserInfo(
username: string,
config: UserInfoServiceConfig
): Promise<string> {
try {
const url = new URL(config.endpoint)
url.searchParams.append('profileName', username)
const response = await fetch(url.toString(), {
method: 'GET',
headers: {
Authorization: `tokenKey ${config.token}`,
'Content-Type': 'application/json'
}
})
if (!response.ok) {
if (response.status >= 400) {
core.warning(`User '${username}' won't be credited - no ORCID associated with their github account`)
} else {
throw new Error(`HTTP error! status: ${response.status}`)
}
return 'unknown'
}
const data = (await response.json()) as { orcid_id?: string }
if (!data?.orcid_id) {
core.notice(`No ORCID found for user '${username}' in API response`)
return 'unknown'
}
return data.orcid_id
} catch (error) {
core.error(
`Failed to fetch user info for ${username}: ${error instanceof Error ? error.message : 'Unknown error'}`
)
return 'unknown'
}
}
async function processCommits(
userInfoConfig: UserInfoServiceConfig,
resourceId: string,
activityName: string,
league: string,
resourceUrl?: string
): Promise<Report[]> {
const { payload } = github.context
const repo = payload.repository!
if (!payload.commits?.length) {
throw new Error('No commits found in the payload')
}
const reports = await Promise.all(
payload.commits.map(async (commit: any) => {
const username = commit.author?.username || commit.committer?.username
if (!username) {
core.warning(`Skipping commit ${commit.id} - no username associated`)
return null
}
const orcid = await fetchUserInfo(username, userInfoConfig)
if (orcid === 'unknown') {
core.info(`Skipping report for ${username} - no ORCID available`)
return null
}
const resUrl = resourceUrl? `${resourceUrl}/${repo.html_url}` : `${repo.html_url}`
return {
curator_orcid: orcid,
entity_uri: `${resUrl}/commit/${commit.id}`,
resource_id: resourceId,
timestamp: commit.timestamp,
activity_term: activityName,
league: league
}
})
)
return reports.filter((report): report is Report => report !== null)
}
async function sendToApi(
reports: Report[],
apiConfig: ReportApiConfig
): Promise<void> {
try {
core.info(`Sending ${reports.length} reports to ${apiConfig.endpoint}`);
const requestBody = {
reports: reports,
};
const response = await fetch(apiConfig.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiConfig.token}`,
'version': '2'
},
body: JSON.stringify(requestBody)
});
const contentType = response.headers.get('content-type') || '';
const isJson = contentType.includes('application/json');
const responseBody = isJson ? await response.json() : await response.text();
if (!response.ok) {
core.error(`API Error: ${response.status} ${response.statusText}`);
core.error(`Response body: ${JSON.stringify(responseBody)}`);
throw new Error(`API request failed: ${response.status} ${response.statusText}`);
}
core.info(`Successfully sent ${reports.length} reports`);
core.debug(`API Response: ${JSON.stringify(responseBody)}`);
} catch (error) {
core.error('Failed to send reports');
if (error instanceof Error) {
core.error(error.stack || error.message);
}
throw error;
}
}
export async function run(): Promise<void> {
try {
const userInfoConfig: UserInfoServiceConfig = {
endpoint: core.getInput('USER_INFO_SERVICE_ENDPOINT', { required: true }),
token: core.getInput('USER_INFO_SERVICE_TOKEN')
}
const reportApiConfig: ReportApiConfig = {
endpoint: core.getInput('REPORT_API_ENDPOINT', { required: true }),
token: core.getInput('REPORT_API_TOKEN')
}
const resourceId = core.getInput('RESOURCE_ID', { required: true })
const activityName = core.getInput('ACTIVITY_NAME', { required: true })
const league = core.getInput('LEAGUE', { required: true })
const resourceUrl = core.getInput('RESOURCE_URL')
const reports = await processCommits(
userInfoConfig,
resourceId,
activityName,
league,
resourceUrl
)
if (reports.length === 0) {
core.info('No valid commits to process')
return
}
console.log(JSON.stringify(reports, null, 2))
await sendToApi(reports, reportApiConfig)
core.setOutput('reports sent:', JSON.stringify(reports))
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
}
}