Skip to content

Commit aa2b5f3

Browse files
committed
feat: update commit processing to use new end point to get user orcids
1 parent 9511db4 commit aa2b5f3

File tree

6 files changed

+138
-64
lines changed

6 files changed

+138
-64
lines changed

.github/workflows/commit-context.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ jobs:
1818
uses: ./
1919
with:
2020
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21-
APICURON_ENDPOINT: 'https://www.apicuron.org/api/reports'
22-
APICURON_TOKEN: ${{ secrets.APICURON_TOKEN }}
21+
REPORT_API_ENDPOINT: https://apicuron.com/api/report
22+
REPORT_API_TOKEN: ${{ secrets.REPORT_API_TOKEN }}
23+
USER_INFO_SERVICE_ENDPOINT: ${{ secrets.USER_INFO_SERVICE_ENDPOINT }}
24+
USER_INFO_SERVICE_TOKEN: ${{ secrets.USER_INFO_SERVICE_TOKEN }}

action.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,18 @@ inputs:
77
GITHUB_TOKEN:
88
description: 'GitHub token'
99
required: true
10-
APICURON_ENDPOINT:
11-
description: 'API endpoint URL'
10+
REPORT_API_ENDPOINT:
11+
description: 'Endpoint for sending final reports'
1212
required: true
13-
APICURON_TOKEN:
14-
description: 'API authentication token'
13+
REPORT_API_TOKEN:
14+
description: 'Authentication token for report API'
15+
required: false
16+
USER_INFO_SERVICE_ENDPOINT:
17+
description: 'Endpoint for user information lookup'
1518
required: true
19+
USER_INFO_SERVICE_TOKEN:
20+
description: 'Authentication token for user info service'
21+
required: false
1622
# Define your outputs here.
1723
outputs:
1824
reports:

dist/index.js

Lines changed: 52 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.d.ts

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import * as core from '@actions/core'
22
import * 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

3374
async 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+
64100
export 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

Comments
 (0)