Skip to content

Commit

Permalink
Merge branch 'next' into feat/sk-locale
Browse files Browse the repository at this point in the history
  • Loading branch information
xDivisionByZerox authored Aug 15, 2024
2 parents 19c698d + 466c8ba commit 86f41f3
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 99 deletions.
12 changes: 7 additions & 5 deletions .github/renovate.json5
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@
},
{
"groupName": "eslint",
"matchPackagePrefixes": [
"@typescript-eslint/",
"@eslint-types/",
"eslint"
"matchPackageNames": [
"@eslint-types/**",
"@eslint/**",
"eslint",
"eslint-**",
"typescript-eslint"
]
},
{
"groupName": "vitest",
"matchPackagePrefixes": ["@vitest/", "vitest"]
"matchPackageNames": ["@vitest/**", "vitest"]
},
{
"groupName": "prettier",
Expand Down
64 changes: 41 additions & 23 deletions .github/workflows/commentCodeGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,34 @@ import type { context as ctx, GitHub } from '@actions/github/lib/utils';
* https://github.com/actions/github-script
*
* @param github A pre-authenticated octokit/rest.js client with pagination plugins
* @param context An object containing the context of the workflow run
* @param isSuccess A boolean indicating whether the workflow was successful
* @param context An object containing the context of the workflow run.
*/
export async function script(
github: InstanceType<typeof GitHub>,
context: typeof ctx,
isSuccess: boolean
context: typeof ctx
): Promise<void> {
const repoArgs = { owner: context.repo.owner, repo: context.repo.repo };

// Identify the PR that triggered the workflow
const head_branch: string = context.payload.workflow_run.head_branch;
const { data: prs } = await github.rest.pulls.list({
...repoArgs,
state: 'open',
head: head_branch,
});

if (prs.length === 0) {
console.log(`No PRs found for branch ${head_branch}`);
return;
}

const pr_number = prs[0].number;

// Check if the PR already has a comment from the bot

const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
...repoArgs,
issue_number: pr_number,
});

const body = `GitHub Actions has found some problems running the preflight checks.
Expand All @@ -34,22 +50,24 @@ Please make sure to:
(comment) => comment.user?.type === 'Bot' && comment.body?.includes(body)
);

if (isSuccess) {
if (!botComment) return;
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
});
return;
}
const isSuccess = context.payload.workflow_run.conclusion === 'success';

if (!botComment) {
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body,
});
if (isSuccess) {
// Delete the bot comment if present
if (botComment != null) {
await github.rest.issues.deleteComment({
...repoArgs,
comment_id: botComment.id,
});
}
} else {
// Create the comment if missing
if (botComment == null) {
await github.rest.issues.createComment({
...repoArgs,
issue_number: pr_number,
body,
});
}
}
}
13 changes: 1 addition & 12 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 10
name: 'Check Code Generation: node-22, ubuntu-latest'
permissions:
pull-requests: write

steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
Expand Down Expand Up @@ -52,16 +51,6 @@ jobs:
git diff --cached --name-only --exit-code
continue-on-error: true

- name: Transpile ts
run: pnpm tsup-node .github/workflows/commentCodeGeneration.ts --format cjs --clean false --out-dir .github/workflows

- name: Comment
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const { script } = require('${{ github.workspace }}/.github/workflows/commentCodeGeneration.cjs')
await script(github, context, ${{ steps.generate.outcome == 'success' && steps.diff.outcome == 'success' }})
- name: Status
if: ${{ steps.generate.outcome == 'failure' || steps.diff.outcome == 'failure' }}
run: exit 1
45 changes: 45 additions & 0 deletions .github/workflows/preflight-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Preflight Comment

on:
workflow_run:
workflows: 'PR'
types:
- completed

permissions:
pull-requests: write

jobs:
comment-code-generation:
runs-on: ubuntu-latest
timeout-minutes: 5
name: 'Comment Code Generation'
steps:
- name: Checkout
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
ref: 'next'

- name: Install pnpm
uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2 # v4.0.0

- name: Set node version to 22
uses: actions/setup-node@1e60f620b9541d16bece96c5465dc8ee9832be0b # v4.0.3
with:
node-version: 22
cache: 'pnpm'

- name: Install deps
run: pnpm install
env:
CYPRESS_INSTALL_BINARY: 0

- name: Transpile ts
run: pnpm tsup-node .github/workflows/commentCodeGeneration.ts --format cjs --clean false --out-dir .github/workflows

- name: Comment
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const { script } = require('${{ github.workspace }}/.github/workflows/commentCodeGeneration.cjs')
await script(github, context)
82 changes: 82 additions & 0 deletions docs/guide/localization.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,85 @@ for (let key of Object.keys(allFakers)) {
}
}
```

## Handling Missing Data Errors

```txt
[Error]: The locale data for 'category.entry' are missing in this locale.
Please contribute the missing data to the project or use a locale/Faker instance that has these data.
For more information see https://fakerjs.dev/guide/localization.html
```

If you receive this error, this means you are using a locale (`Faker` instance) that does not have the relevant data for that method yet.
Please consider contributing the missing data, so that others can use them in the future as well.

As a workaround, you can provide additional fallbacks to your `Faker` instance:

```ts
import { Faker, el } from '@faker-js/faker'; // [!code --]
import { Faker, el, en } from '@faker-js/faker'; // [!code ++]

const faker = new Faker({
locale: [el], // [!code --]
locale: [el, en], // [!code ++]
});
console.log(faker.location.country()); // 'Belgium'
```

::: tip Note
Of course, you can use [Custom Locales and Fallbacks](#custom-locales-and-fallbacks) for this as well.
:::

## Handling Not-Applicable Data Errors

```txt
[Error]: The locale data for 'category.entry' aren't applicable to this locale.
If you think this is a bug, please report it at: https://github.com/faker-js/faker
```

If you receive this error, this means the current locale is unable to provide reasonable values for that method.
For example, there are no zip codes in Hongkong, so for that reason the `en_HK` locale is unable to provide these data.
The same applies to other locales and methods.

```ts
import { fakerEN_HK } from '@faker-js/faker';

console.log(fakerEN_HK.location.zipCode()); // Error // [!code error]
```

For these cases, we explicitly set the data to `null` to clarify, that we have thought about it, but there are no valid values to put there.
We could have used an empty array `[]`, but some locale data are stored as objects `{}`,
so `null` works for both of them without custom downstream handling of missing data.

::: tip Note
We are by far no experts in all provided languages/countries/locales,
so if you think this is an error for your locale, please create an issue and consider contributing the relevant data.
:::

If you want to use other fallback data instead, you can define them like this:

```ts{4}
import { Faker, en, en_HK } from '@faker-js/faker';
const faker = new Faker({
locale: [{ location: { postcode: en.location.postcode } }, en_HK],
});
console.log(faker.location.zipCode()); // '17551-0348'
```

::: warning Warning
Since `null` is considered present data, it will not use any fallbacks for that.
So the following code does **not** work:

```ts
import { Faker, en, en_HK } from '@faker-js/faker';

const faker = new Faker({
locale: [en_HK, { location: { postcode: en.location.postcode } }], // [!code warning]
});
console.log(faker.location.zipCode()); // Error // [!code error]
```

:::

See also: [Custom Locales and Fallbacks](#custom-locales-and-fallbacks)
Loading

0 comments on commit 86f41f3

Please sign in to comment.