Skip to content

Commit

Permalink
feat: basic executor info support (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph authored Nov 24, 2023
1 parent a654264 commit d932406
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 9 deletions.
10 changes: 6 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ jobs:
with:
useLockFile: false
working-directory: e2e
- name: Run E2E Tests (Fallback)
run: npm test -- --testEnvironment=node
working-directory: e2e
- name: Run E2E Tests (Default)
run: npm test
working-directory: e2e
- name: Upload Allure results
uses: actions/upload-artifact@v3
with:
name: allure-results
path: e2e/allure-results

publish:
name: Publish
Expand All @@ -56,7 +58,7 @@ jobs:
with:
useLockFile: false
- name: Semantic release
run: npx --no-install semantic-release --debug
run: npx --no-install semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
5 changes: 3 additions & 2 deletions docs/docs/config/05-executor.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ module.exports = {
</TabItem>
</Tabs>

The `defaultValue` is based on [`ci-info`] npm package, but you can override any of its properties,
as shown above in the example.
The default `value` is provided by `jest-allure2-reporter` for Github Actions and Buildkite
CI/CD environments. You can override it with your own value, or modify it as shown
in the example above.

## Overrides

Expand Down
2 changes: 0 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ declare module 'jest-allure2-reporter' {
environment?: Record<string, string> | EnvironmentCustomizer;
/**
* Configures the executor information that will be reported.
* By default, the executor information is inferred from `ci-info` package.
* Local runs won't have any executor information unless you customize this.
*/
executor?: ExecutorInfo | ExecutorCustomizer;
/**
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@
"@types/glob": "^8.0.0",
"@types/jest": "^29.0.0",
"@types/lodash": "^4.14.186",
"@types/lodash.snakecase": "^4.0.0",
"@types/node": "^14.18.46",
"@types/node-fetch": "^2.6.9",
"@types/rimraf": "^4.0.5",
"@typescript-eslint/eslint-plugin": "^5.28.0",
"@typescript-eslint/parser": "^5.28.0",
Expand Down Expand Up @@ -102,6 +104,8 @@
"@noomorph/allure-js-commons": "^2.3.0",
"ci-info": "^3.8.0",
"jest-metadata": "^1.2.1",
"lodash.snakecase": "^4.1.1",
"node-fetch": "^2.6.7",
"pkg-up": "^3.1.0",
"prettier": "^3.0.3",
"rehype-highlight": "^7.0.0",
Expand Down
58 changes: 58 additions & 0 deletions src/builtin-plugins/github.ts
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);

Check warning on line 42 in src/builtin-plugins/github.ts

View workflow job for this annotation

GitHub Actions / Sanity

Unexpected any. Specify a different type

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;
};
1 change: 1 addition & 0 deletions src/builtin-plugins/index.ts
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';
57 changes: 57 additions & 0 deletions src/options/default-options/executor.ts
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()}`;
}
3 changes: 2 additions & 1 deletion src/options/default-options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { defaultPlugins } from './plugins';
import { testCase } from './testCase';
import { testFile } from './testFile';
import { testStep } from './testStep';
import { executor } from './executor';

const identity = <T>(context: ExtractorContext<T>) => context.value;

Expand All @@ -25,7 +26,7 @@ export function defaultOptions(context: PluginContext): ReporterConfig {
testStep,
categories,
environment: identity,
executor: identity,
executor: executor(),
plugins: defaultPlugins(context),
};
}
1 change: 1 addition & 0 deletions src/options/default-options/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export async function defaultPlugins(context: PluginContext) {
return [
plugins.detect({}, context),
plugins.docblock({}, context),
plugins.github({}, context),
plugins.manifest({}, context),
plugins.prettier({}, context),
plugins.remark({}, context),
Expand Down

0 comments on commit d932406

Please sign in to comment.