-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: basic executor info support (#18)
- Loading branch information
Showing
9 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import fetch from 'node-fetch'; | ||
import snakeCase from 'lodash.snakecase'; | ||
import type { Plugin, PluginConstructor } from 'jest-allure2-reporter'; | ||
|
||
export const githubPlugin: PluginConstructor = () => { | ||
const plugin: Plugin = { | ||
name: 'jest-allure2-reporter/plugins/github', | ||
async globalContext() { | ||
const { | ||
GITHUB_ACTIONS, | ||
GITHUB_JOB, | ||
GITHUB_REPOSITORY, | ||
GITHUB_RUN_ATTEMPT, | ||
GITHUB_RUN_ID, | ||
GITHUB_SERVER_URL, | ||
GITHUB_TOKEN, | ||
} = process.env; | ||
|
||
if (!GITHUB_ACTIONS) { | ||
return; | ||
} | ||
|
||
const apiUrl = `https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}/attempts/${GITHUB_RUN_ATTEMPT}/jobs`; | ||
const headers: HeadersInit = { | ||
Accept: 'application/vnd.github.v3+json', | ||
}; | ||
if (GITHUB_TOKEN) { | ||
headers.Authorization = `token ${GITHUB_TOKEN}`; | ||
} | ||
|
||
try { | ||
const response = await fetch(apiUrl, { headers }); | ||
if (!response.ok) { | ||
// convert to http error | ||
throw new Error(response.statusText); | ||
} | ||
|
||
const data = await response.json(); | ||
const job = | ||
data.jobs.length === 1 | ||
? data.jobs[0] | ||
: data.jobs.find((job: any) => snakeCase(job.name) === GITHUB_JOB); | ||
|
||
if (job) { | ||
process.env.ALLURE_GITHUB_JOB_ID = job.id; | ||
process.env.ALLURE_GITHUB_URL = job.html_url; | ||
} | ||
} catch (error: unknown) { | ||
// TODO: migrate to bunyamin | ||
console.error('Failed to fetch job ID due to error:', error); | ||
} | ||
|
||
process.env.ALLURE_GITHUB_URL ||= `${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}`; | ||
}, | ||
}; | ||
|
||
return plugin; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export { detectPlugin as detect } from './detect'; | ||
export { docblockPlugin as docblock } from './docblock'; | ||
export { githubPlugin as github } from './github'; | ||
export { manifestPlugin as manifest } from './manifest'; | ||
export { prettierPlugin as prettier } from './prettier'; | ||
export { remarkPlugin as remark } from './remark'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os from 'node:os'; | ||
|
||
import type { ExecutorCustomizer } from 'jest-allure2-reporter'; | ||
import type { ExecutorInfo } from '@noomorph/allure-js-commons'; | ||
|
||
export function executor(): ExecutorCustomizer { | ||
if (process.env.GITHUB_ACTIONS) return githubActions; | ||
if (process.env.BUILDKITE) return buildkite; | ||
|
||
return local; | ||
} | ||
|
||
function githubActions(): ExecutorInfo { | ||
const { | ||
ALLURE_GITHUB_URL, | ||
GITHUB_RUN_ATTEMPT, | ||
GITHUB_RUN_ID, | ||
GITHUB_RUN_NUMBER, | ||
GITHUB_JOB, | ||
RUNNER_NAME, | ||
} = process.env; | ||
|
||
const runnerName = RUNNER_NAME || 'GitHub Actions'; | ||
|
||
return { | ||
name: `${runnerName} (${getOsDetails()})`, | ||
type: 'github', | ||
buildUrl: ALLURE_GITHUB_URL, | ||
buildOrder: Number(GITHUB_RUN_NUMBER) * 10 + Number(GITHUB_RUN_ATTEMPT), | ||
buildName: `${GITHUB_RUN_ID}#${GITHUB_JOB}`, | ||
}; | ||
} | ||
|
||
function buildkite(): ExecutorInfo { | ||
const { BUILDKITE_AGENT_NAME, BUILDKITE_BUILD_URL, BUILDKITE_BUILD_NUMBER } = | ||
process.env; | ||
const agentName = BUILDKITE_AGENT_NAME || 'Buildkite'; | ||
|
||
return { | ||
name: `${agentName} (${getOsDetails()})`, | ||
type: 'buildkite', | ||
buildUrl: BUILDKITE_BUILD_URL, | ||
buildOrder: Number(BUILDKITE_BUILD_NUMBER), | ||
buildName: `#${BUILDKITE_BUILD_NUMBER}`, | ||
}; | ||
} | ||
|
||
function local(): ExecutorInfo { | ||
return { | ||
name: `${os.hostname()} (${getOsDetails()})`, | ||
type: `${os.platform()}-${os.arch()}`, | ||
}; | ||
} | ||
|
||
function getOsDetails(): string { | ||
return `${os.type()} ${os.release()}/${os.arch()}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters