Skip to content

Commit ebcba15

Browse files
committed
feat: implement commit processing and push to APICURON API
1 parent 5040c1c commit ebcba15

File tree

1 file changed

+67
-21
lines changed

1 file changed

+67
-21
lines changed

src/main.ts

Lines changed: 67 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,80 @@
11
import * as core from '@actions/core'
22
import * as github from '@actions/github'
3+
interface ApiConfig {
4+
endpoint: string
5+
token: string
6+
}
7+
interface Report {
8+
curator_orcid: string
9+
entity_uri: string
10+
resource_id: string
11+
timestamp: string
12+
activity_term: string
13+
league: string
14+
}
15+
function processCommits(): Report[] {
16+
const { payload } = github.context
17+
const repo = payload.repository!
18+
if (!payload.commits) {
19+
throw new Error('No commits found in the payload')
20+
}
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.id.toString(),
26+
timestamp: commit.timestamp,
27+
activity_term: 'commit',
28+
league: 'default'
29+
})) || []
30+
)
31+
}
32+
33+
async function sendToApi(
34+
reports: Report[],
35+
apiConfig: ApiConfig
36+
): Promise<void> {
37+
try {
38+
const response = await fetch(apiConfig.endpoint, {
39+
method: 'POST',
40+
headers: {
41+
'Content-Type': 'application/json',
42+
Authorization: `Bearer ${apiConfig.token}`
43+
},
44+
body: JSON.stringify({ reports })
45+
})
346

47+
if (!response.ok) {
48+
throw new Error(
49+
`API request failed: ${response.status} ${response.statusText}`
50+
)
51+
}
52+
53+
core.info(`Successfully sent ${reports.length} reports to API`)
54+
} catch (error) {
55+
core.error('Failed to send reports to API')
56+
throw error
57+
}
58+
}
459
/**
560
* The main function for the action.
661
*
762
* @returns Resolves when the action is complete.
863
*/
964
export async function run(): Promise<void> {
1065
try {
11-
const event = core.getInput('event')
12-
console.log(JSON.stringify(event, null, 2))
13-
// const repository: string = core.getInput('sourceRepo')
14-
// const token = core.getInput('github_token', { required: true })
15-
// const apicuron_token = core.getInput('apicuron_token', { required: true })
16-
// const octokit = github.getOctokit(token)
17-
// const sha = github.context.sha
18-
// const { owner, repo } = github.context.repo
19-
// const data = await octokit.rest.repos.getCommit({
20-
// owner,
21-
// repo,
22-
// ref: sha
23-
// })
24-
// add orcid
25-
// send to apicuron
26-
// console.log(JSON.stringify(data, null, 2))
27-
// console.log(`Commit Message: ${commit.commit.message}`)
28-
// console.log(`Author Name: ${commit.commit.author?.name}`)
29-
// console.log(`Author Email: ${commit.commit.author?.email}`)
30-
// console.log(`Committer Name: ${commit.commit.committer?.name}`)
31-
// console.log(`Committer Email: ${commit.commit.committer?.email}`)
66+
const apiConfig: ApiConfig = {
67+
endpoint: core.getInput('API_ENDPOINT', { required: true }),
68+
token: core.getInput('API_TOKEN', { required: true })
69+
}
70+
const reports = processCommits()
71+
if (reports.length === 0) {
72+
core.info('No commits to process')
73+
return
74+
}
75+
76+
await sendToApi(reports, apiConfig)
77+
core.setOutput('reports', JSON.stringify(reports))
3278
} catch (error) {
3379
if (error instanceof Error) core.setFailed(error.message)
3480
}

0 commit comments

Comments
 (0)