From d2dc0f963321805f098fbc82c9b7329eb69645b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:22:20 +0900 Subject: [PATCH 01/33] chore: empty string test --- build/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build/index.ts b/build/index.ts index e1cf0b87..1b0be7a5 100644 --- a/build/index.ts +++ b/build/index.ts @@ -2,3 +2,5 @@ import * as dotenv from "dotenv"; // load environment variables (if you have them) dotenv.config(); console.log("Welcome to ts-template"); + +const _EMPTY_STRING_TEST = ""; From 37d8bd4a06774d9a296eec9e013276b5af6213cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:27:02 +0900 Subject: [PATCH 02/33] chore: empty string test --- .github/workflows/no-empty-strings.yml | 12 ++++++++++++ .github/workflows/sync-template.yml | 4 +--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/.github/workflows/no-empty-strings.yml b/.github/workflows/no-empty-strings.yml index 41f1f1c3..d40b6008 100644 --- a/.github/workflows/no-empty-strings.yml +++ b/.github/workflows/no-empty-strings.yml @@ -1,5 +1,9 @@ name: Check for Empty Strings +permissions: + contents: read + id-token: write + on: pull_request: types: [opened, synchronize, reopened] @@ -8,6 +12,14 @@ jobs: check-empty-strings: runs-on: ubuntu-latest steps: + - name: Debug - Check if APP_ID is set + run: | + if [ -n "${{ secrets.APP_ID }}" ]; then + echo "APP_ID is set" + else + echo "APP_ID is not set" + fi + - name: Checkout code uses: actions/checkout@v4 diff --git a/.github/workflows/sync-template.yml b/.github/workflows/sync-template.yml index dc92ad59..274b2ea6 100644 --- a/.github/workflows/sync-template.yml +++ b/.github/workflows/sync-template.yml @@ -23,7 +23,7 @@ jobs: with: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.APP_PRIVATE_KEY }} - + - name: Sync branch to template env: GH_TOKEN: ${{ steps.get_installation_token.outputs.token }} @@ -45,5 +45,3 @@ jobs: git commit -m "chore: sync template" git push "$original_remote" "$pr_branch" gh pr create --title "Sync branch to template" --body "This pull request merges changes from the template repository." --head "$pr_branch" --base "$branch_name" - - From f156cbfb45dc5b5c0b6997af0cba5413eb4393d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:30:19 +0900 Subject: [PATCH 03/33] chore: empty string test --- build/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build/index.ts b/build/index.ts index 1b0be7a5..a7c0eb9c 100644 --- a/build/index.ts +++ b/build/index.ts @@ -3,4 +3,5 @@ import * as dotenv from "dotenv"; dotenv.config(); console.log("Welcome to ts-template"); -const _EMPTY_STRING_TEST = ""; +const _EMPTY_STRING_TEST_1 = ""; +const _EMPTY_STRING_TEST_2 = ""; From 3fd489511c9576262b8d0e9d6ba22b7ec08d7f0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:32:36 +0900 Subject: [PATCH 04/33] chore: empty string test --- .github/workflows/no-empty-strings.yml | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/.github/workflows/no-empty-strings.yml b/.github/workflows/no-empty-strings.yml index d40b6008..e15ae301 100644 --- a/.github/workflows/no-empty-strings.yml +++ b/.github/workflows/no-empty-strings.yml @@ -33,11 +33,20 @@ jobs: - name: Find empty strings id: find-empty-strings run: | + # Set PR number as an environment variable + PR_NUMBER="${{ github.event.pull_request.number }}" + + # Check if PR_NUMBER is set + if [ -z "$PR_NUMBER" ]; then + echo "Error: PR number is not set" + exit 1 + fi + # Get the base branch (usually main or master) - BASE_BRANCH=$(gh pr view ${{ github.event.pull_request.number }} --json baseRefName --jq .baseRefName) + BASE_BRANCH=$(gh pr view "$PR_NUMBER" --json baseRefName --jq .baseRefName) # Get the list of changed files in the PR - CHANGED_FILES=$(gh pr diff ${{ github.event.pull_request.number }} --name-only) + CHANGED_FILES=$(gh pr diff "$PR_NUMBER" --name-only) # Initialize empty string for results EMPTY_STRINGS="" @@ -46,7 +55,7 @@ jobs: for file in $CHANGED_FILES; do if [[ $file =~ \.(ts|tsx)$ ]]; then # Get the diff for this file - DIFF=$(gh pr diff ${{ github.event.pull_request.number }} --color never -- $file) + DIFF=$(gh pr diff "$PR_NUMBER" --color never -- "$file") # Find added lines with empty strings ADDED_EMPTY=$(echo "$DIFF" | grep -n "^+" | grep -E "(''{2}|\"\"{2})" | sed "s/^+//g" | sed "s/^/${file}:/") From 03d926be05d33545df114ea9220d988d81ee15a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:39:24 +0900 Subject: [PATCH 05/33] chore: empty string test --- .github/workflows/no-empty-strings.yml | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/.github/workflows/no-empty-strings.yml b/.github/workflows/no-empty-strings.yml index e15ae301..46a39d15 100644 --- a/.github/workflows/no-empty-strings.yml +++ b/.github/workflows/no-empty-strings.yml @@ -30,18 +30,20 @@ jobs: app_id: ${{ secrets.APP_ID }} private_key: ${{ secrets.APP_PRIVATE_KEY }} - - name: Find empty strings + - name: Check for empty strings in PR id: find-empty-strings run: | - # Set PR number as an environment variable - PR_NUMBER="${{ github.event.pull_request.number }}" + # Get PR number from GitHub event + PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") - # Check if PR_NUMBER is set - if [ -z "$PR_NUMBER" ]; then - echo "Error: PR number is not set" + # Check if PR_NUMBER is set and valid + if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then + echo "Error: PR number is not set or invalid" exit 1 fi + echo "Checking PR #$PR_NUMBER" + # Get the base branch (usually main or master) BASE_BRANCH=$(gh pr view "$PR_NUMBER" --json baseRefName --jq .baseRefName) @@ -55,7 +57,7 @@ jobs: for file in $CHANGED_FILES; do if [[ $file =~ \.(ts|tsx)$ ]]; then # Get the diff for this file - DIFF=$(gh pr diff "$PR_NUMBER" --color never -- "$file") + DIFF=$(gh pr diff "$PR_NUMBER" --color never "$file") # Find added lines with empty strings ADDED_EMPTY=$(echo "$DIFF" | grep -n "^+" | grep -E "(''{2}|\"\"{2})" | sed "s/^+//g" | sed "s/^/${file}:/") @@ -73,6 +75,7 @@ jobs: echo "EOF" >> $GITHUB_OUTPUT env: GH_TOKEN: ${{ steps.get_installation_token.outputs.token }} + shell: bash - name: Comment on PR uses: actions/github-script@v6 From 2ff231ccc8e7d3a68f9c724b256c6179165ec25a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:44:46 +0900 Subject: [PATCH 06/33] chore: empty string test --- .github/workflows/no-empty-strings.yml | 92 ++--------------- package.json | 1 + yarn.lock | 134 +++++++++++++++++++------ 3 files changed, 115 insertions(+), 112 deletions(-) diff --git a/.github/workflows/no-empty-strings.yml b/.github/workflows/no-empty-strings.yml index 46a39d15..5a7a4570 100644 --- a/.github/workflows/no-empty-strings.yml +++ b/.github/workflows/no-empty-strings.yml @@ -1,95 +1,19 @@ -name: Check for Empty Strings - -permissions: - contents: read - id-token: write +name: Empty String Check on: pull_request: types: [opened, synchronize, reopened] jobs: - check-empty-strings: + check-for-empty-strings: runs-on: ubuntu-latest steps: - - name: Debug - Check if APP_ID is set + - uses: actions/checkout@v4 + - name: Install Dependencies run: | - if [ -n "${{ secrets.APP_ID }}" ]; then - echo "APP_ID is set" - else - echo "APP_ID is not set" - fi - - - name: Checkout code - uses: actions/checkout@v4 - - - name: Get GitHub App token - uses: tibdex/github-app-token@v1.7.0 - id: get_installation_token - with: - app_id: ${{ secrets.APP_ID }} - private_key: ${{ secrets.APP_PRIVATE_KEY }} - - - name: Check for empty strings in PR - id: find-empty-strings + yarn install + - name: Check for Empty Strings run: | - # Get PR number from GitHub event - PR_NUMBER=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") - - # Check if PR_NUMBER is set and valid - if [ -z "$PR_NUMBER" ] || [ "$PR_NUMBER" = "null" ]; then - echo "Error: PR number is not set or invalid" - exit 1 - fi - - echo "Checking PR #$PR_NUMBER" - - # Get the base branch (usually main or master) - BASE_BRANCH=$(gh pr view "$PR_NUMBER" --json baseRefName --jq .baseRefName) - - # Get the list of changed files in the PR - CHANGED_FILES=$(gh pr diff "$PR_NUMBER" --name-only) - - # Initialize empty string for results - EMPTY_STRINGS="" - - # Loop through changed files - for file in $CHANGED_FILES; do - if [[ $file =~ \.(ts|tsx)$ ]]; then - # Get the diff for this file - DIFF=$(gh pr diff "$PR_NUMBER" --color never "$file") - - # Find added lines with empty strings - ADDED_EMPTY=$(echo "$DIFF" | grep -n "^+" | grep -E "(''{2}|\"\"{2})" | sed "s/^+//g" | sed "s/^/${file}:/") - - # Append results - if [ ! -z "$ADDED_EMPTY" ]; then - EMPTY_STRINGS+="$ADDED_EMPTY"$'\n' - fi - fi - done - - # Output results - echo "EMPTY_STRINGS<> $GITHUB_OUTPUT - echo "$EMPTY_STRINGS" >> $GITHUB_OUTPUT - echo "EOF" >> $GITHUB_OUTPUT + yarn tsx .github/empty-string-checker.ts env: - GH_TOKEN: ${{ steps.get_installation_token.outputs.token }} - shell: bash - - - name: Comment on PR - uses: actions/github-script@v6 - if: steps.find-empty-strings.outputs.EMPTY_STRINGS - with: - github-token: ${{ steps.get_installation_token.outputs.token }} - script: | - const emptyStrings = process.env.EMPTY_STRINGS.trim().split('\n'); - if (emptyStrings.length > 0) { - const body = `### Empty Strings Found\n\nPlease review the following lines containing empty strings:\n\n${emptyStrings.map(line => `- \`${line}\``).join('\n')}`; - github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.name, - body: body - }); - } + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/package.json b/package.json index d40382f9..87b10147 100644 --- a/package.json +++ b/package.json @@ -40,6 +40,7 @@ "@cspell/dict-typescript": "^3.1.2", "@jest/globals": "29.7.0", "@mswjs/data": "0.16.1", + "@octokit/rest": "^21.0.2", "@types/jest": "29.5.12", "@types/node": "^20.11.19", "@typescript-eslint/eslint-plugin": "^7.0.1", diff --git a/yarn.lock b/yarn.lock index c4f27c7c..61a0a002 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1580,6 +1580,99 @@ dependencies: which "^4.0.0" +"@octokit/auth-token@^5.0.0": + version "5.1.1" + resolved "https://registry.yarnpkg.com/@octokit/auth-token/-/auth-token-5.1.1.tgz#3bbfe905111332a17f72d80bd0b51a3e2fa2cf07" + integrity sha512-rh3G3wDO8J9wSjfI436JUKzHIxq8NaiL0tVeB2aXmG6p/9859aUOAjA9pmSPNGGZxfwmaJ9ozOJImuNVJdpvbA== + +"@octokit/core@^6.1.2": + version "6.1.2" + resolved "https://registry.yarnpkg.com/@octokit/core/-/core-6.1.2.tgz#20442d0a97c411612da206411e356014d1d1bd17" + integrity sha512-hEb7Ma4cGJGEUNOAVmyfdB/3WirWMg5hDuNFVejGEDFqupeOysLc2sG6HJxY2etBp5YQu5Wtxwi020jS9xlUwg== + dependencies: + "@octokit/auth-token" "^5.0.0" + "@octokit/graphql" "^8.0.0" + "@octokit/request" "^9.0.0" + "@octokit/request-error" "^6.0.1" + "@octokit/types" "^13.0.0" + before-after-hook "^3.0.2" + universal-user-agent "^7.0.0" + +"@octokit/endpoint@^10.0.0": + version "10.1.1" + resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-10.1.1.tgz#1a9694e7aef6aa9d854dc78dd062945945869bcc" + integrity sha512-JYjh5rMOwXMJyUpj028cu0Gbp7qe/ihxfJMLc8VZBMMqSwLgOxDI1911gV4Enl1QSavAQNJcwmwBF9M0VvLh6Q== + dependencies: + "@octokit/types" "^13.0.0" + universal-user-agent "^7.0.2" + +"@octokit/graphql@^8.0.0": + version "8.1.1" + resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-8.1.1.tgz#3cacab5f2e55d91c733e3bf481d3a3f8a5f639c4" + integrity sha512-ukiRmuHTi6ebQx/HFRCXKbDlOh/7xEV6QUXaE7MJEKGNAncGI/STSbOkl12qVXZrfZdpXctx5O9X1AIaebiDBg== + dependencies: + "@octokit/request" "^9.0.0" + "@octokit/types" "^13.0.0" + universal-user-agent "^7.0.0" + +"@octokit/openapi-types@^22.2.0": + version "22.2.0" + resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-22.2.0.tgz#75aa7dcd440821d99def6a60b5f014207ae4968e" + integrity sha512-QBhVjcUa9W7Wwhm6DBFu6ZZ+1/t/oYxqc2tp81Pi41YNuJinbFRx8B133qVOrAaBbF7D/m0Et6f9/pZt9Rc+tg== + +"@octokit/plugin-paginate-rest@^11.0.0": + version "11.3.5" + resolved "https://registry.yarnpkg.com/@octokit/plugin-paginate-rest/-/plugin-paginate-rest-11.3.5.tgz#a1929b3ba3dc7b63bc73bb6d3c7a3faf2a9c7649" + integrity sha512-cgwIRtKrpwhLoBi0CUNuY83DPGRMaWVjqVI/bGKsLJ4PzyWZNaEmhHroI2xlrVXkk6nFv0IsZpOp+ZWSWUS2AQ== + dependencies: + "@octokit/types" "^13.6.0" + +"@octokit/plugin-request-log@^5.3.1": + version "5.3.1" + resolved "https://registry.yarnpkg.com/@octokit/plugin-request-log/-/plugin-request-log-5.3.1.tgz#ccb75d9705de769b2aa82bcd105cc96eb0c00f69" + integrity sha512-n/lNeCtq+9ofhC15xzmJCNKP2BWTv8Ih2TTy+jatNCCq/gQP/V7rK3fjIfuz0pDWDALO/o/4QY4hyOF6TQQFUw== + +"@octokit/plugin-rest-endpoint-methods@^13.0.0": + version "13.2.6" + resolved "https://registry.yarnpkg.com/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-13.2.6.tgz#b9d343dbe88a6cb70cc7fa16faa98f0a29ffe654" + integrity sha512-wMsdyHMjSfKjGINkdGKki06VEkgdEldIGstIEyGX0wbYHGByOwN/KiM+hAAlUwAtPkP3gvXtVQA9L3ITdV2tVw== + dependencies: + "@octokit/types" "^13.6.1" + +"@octokit/request-error@^6.0.1": + version "6.1.5" + resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-6.1.5.tgz#907099e341c4e6179db623a0328d678024f54653" + integrity sha512-IlBTfGX8Yn/oFPMwSfvugfncK2EwRLjzbrpifNaMY8o/HTEAFqCA1FZxjD9cWvSKBHgrIhc4CSBIzMxiLsbzFQ== + dependencies: + "@octokit/types" "^13.0.0" + +"@octokit/request@^9.0.0": + version "9.1.3" + resolved "https://registry.yarnpkg.com/@octokit/request/-/request-9.1.3.tgz#42b693bc06238f43af3c037ebfd35621c6457838" + integrity sha512-V+TFhu5fdF3K58rs1pGUJIDH5RZLbZm5BI+MNF+6o/ssFNT4vWlCh/tVpF3NxGtP15HUxTTMUbsG5llAuU2CZA== + dependencies: + "@octokit/endpoint" "^10.0.0" + "@octokit/request-error" "^6.0.1" + "@octokit/types" "^13.1.0" + universal-user-agent "^7.0.2" + +"@octokit/rest@^21.0.2": + version "21.0.2" + resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-21.0.2.tgz#9b767dbc1098daea8310fd8b76bf7a97215d5972" + integrity sha512-+CiLisCoyWmYicH25y1cDfCrv41kRSvTq6pPWtRroRJzhsCZWZyCqGyI8foJT5LmScADSwRAnr/xo+eewL04wQ== + dependencies: + "@octokit/core" "^6.1.2" + "@octokit/plugin-paginate-rest" "^11.0.0" + "@octokit/plugin-request-log" "^5.3.1" + "@octokit/plugin-rest-endpoint-methods" "^13.0.0" + +"@octokit/types@^13.0.0", "@octokit/types@^13.1.0", "@octokit/types@^13.6.0", "@octokit/types@^13.6.1": + version "13.6.1" + resolved "https://registry.yarnpkg.com/@octokit/types/-/types-13.6.1.tgz#432fc6c0aaae54318e5b2d3e15c22ac97fc9b15f" + integrity sha512-PHZE9Z+kWXb23Ndik8MKPirBPziOc0D2/3KH1P+6jK5nGWe96kadZuE4jev2/Jq7FvIfTlT2Ltg8Fv2x1v0a5g== + dependencies: + "@octokit/openapi-types" "^22.2.0" + "@open-draft/deferred-promise@^2.2.0": version "2.2.0" resolved "https://registry.yarnpkg.com/@open-draft/deferred-promise/-/deferred-promise-2.2.0.tgz#4a822d10f6f0e316be4d67b4d4f8c9a124b073bd" @@ -2340,6 +2433,11 @@ bcrypt-pbkdf@^1.0.0: dependencies: tweetnacl "^0.14.3" +before-after-hook@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-3.0.2.tgz#d5665a5fa8b62294a5aa0a499f933f4a1016195d" + integrity sha512-Nik3Sc0ncrMK4UUdXQmAnRtzmNQTAAXmXIopizwZ1W1t8QmfJj+zL4OA2I7XPTPW5z5TDqv4hRo/JzouDJnX3A== + blob-util@^2.0.2: version "2.0.2" resolved "https://registry.yarnpkg.com/blob-util/-/blob-util-2.0.2.tgz#3b4e3c281111bb7f11128518006cdc60b403a1eb" @@ -6659,16 +6757,7 @@ string-length@^4.0.1: char-regex "^1.0.2" strip-ansi "^6.0.0" -"string-width-cjs@npm:string-width@^4.2.0": - version "4.2.3" - resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" - integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== - dependencies: - emoji-regex "^8.0.0" - is-fullwidth-code-point "^3.0.0" - strip-ansi "^6.0.1" - -string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: +"string-width-cjs@npm:string-width@^4.2.0", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3: version "4.2.3" resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010" integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g== @@ -6743,14 +6832,7 @@ string_decoder@^1.1.1: dependencies: safe-buffer "~5.2.0" -"strip-ansi-cjs@npm:strip-ansi@^6.0.1": - version "6.0.1" - resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" - integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== - dependencies: - ansi-regex "^5.0.1" - -strip-ansi@^6.0.0, strip-ansi@^6.0.1: +"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1: version "6.0.1" resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9" integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A== @@ -7117,6 +7199,11 @@ unique-string@^3.0.0: dependencies: crypto-random-string "^4.0.0" +universal-user-agent@^7.0.0, universal-user-agent@^7.0.2: + version "7.0.2" + resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-7.0.2.tgz#52e7d0e9b3dc4df06cc33cb2b9fd79041a54827e" + integrity sha512-0JCqzSKnStlRRQfCdowvqy3cy0Dvtlb8xecj/H8JFZuCze4rwjPZQOgvFvn0Ws/usCHQFGpyr+pB9adaGwXn4Q== + universalify@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.2.0.tgz#6451760566fa857534745ab1dde952d1b1761be0" @@ -7284,7 +7371,7 @@ which@^4.0.0: dependencies: isexe "^3.1.1" -"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0": +"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0: version "7.0.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== @@ -7302,15 +7389,6 @@ wrap-ansi@^6.2.0: string-width "^4.1.0" strip-ansi "^6.0.0" -wrap-ansi@^7.0.0: - version "7.0.0" - resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43" - integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q== - dependencies: - ansi-styles "^4.0.0" - string-width "^4.1.0" - strip-ansi "^6.0.0" - wrap-ansi@^8.1.0: version "8.1.0" resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-8.1.0.tgz#56dc22368ee570face1b49819975d9b9a5ead214" From 1a0963a4ef2f5f2886937f89251572424486bbfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:46:12 +0900 Subject: [PATCH 07/33] chore: empty string test --- .github/workflows/no-empty-strings.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/no-empty-strings.yml b/.github/workflows/no-empty-strings.yml index 5a7a4570..3d48e85d 100644 --- a/.github/workflows/no-empty-strings.yml +++ b/.github/workflows/no-empty-strings.yml @@ -9,11 +9,15 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20.10.0' - name: Install Dependencies run: | - yarn install + bun install - name: Check for Empty Strings run: | - yarn tsx .github/empty-string-checker.ts + bun run tsx .github/empty-string-checker.ts env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From cfee41214d1c04a622042f6b81f3f8a0ccc32046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:46:54 +0900 Subject: [PATCH 08/33] chore: empty string test --- .github/workflows/no-empty-strings.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/no-empty-strings.yml b/.github/workflows/no-empty-strings.yml index 3d48e85d..77cd1a76 100644 --- a/.github/workflows/no-empty-strings.yml +++ b/.github/workflows/no-empty-strings.yml @@ -15,9 +15,9 @@ jobs: node-version: '20.10.0' - name: Install Dependencies run: | - bun install + yarn add tsx - name: Check for Empty Strings run: | - bun run tsx .github/empty-string-checker.ts + yarn tsx .github/empty-string-checker.ts env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From b03f2989331ff0267361a8f460d83478e6e6523a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:47:11 +0900 Subject: [PATCH 09/33] chore: empty string test --- .github/empty-string-checker.ts | 79 +++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 .github/empty-string-checker.ts diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts new file mode 100644 index 00000000..a455e6c7 --- /dev/null +++ b/.github/empty-string-checker.ts @@ -0,0 +1,79 @@ +import { Octokit } from "@octokit/rest"; +import { execSync } from "child_process"; + +const token = process.env.GITHUB_TOKEN; +const [owner, repo] = process.env.GITHUB_REPOSITORY?.split("/") || []; +const pullNumber = process.env.GITHUB_PR_NUMBER || process.env.PULL_REQUEST_NUMBER || "0"; + +if (!token || !owner || !repo || pullNumber === "0") { + console.error("Missing required environment variables."); + process.exit(1); +} + +const octokit = new Octokit({ auth: token }); + +async function run() { + // Get the diff of the pull request + const diff = execSync(`git diff origin/${process.env.GITHUB_BASE_REF}...HEAD`, { + encoding: "utf8", + }); + + const violations = parseDiffForEmptyStrings(diff); + + if (violations.length > 0) { + await createReview(violations); + process.exit(1); // Exit with error to indicate failure + } else { + console.log("No empty strings found."); + } +} + +function parseDiffForEmptyStrings(diff: string) { + const violations: Array<{ file: string; line: number; content: string }> = []; + const diffLines = diff.split("\n"); + + let currentFile = ""; + let lineNumber = 0; + + diffLines.forEach((line) => { + if (line.startsWith("+++ b/")) { + currentFile = line.replace("+++ b/", ""); + lineNumber = 0; + } else if (line.startsWith("+") && !line.startsWith("+++")) { + lineNumber++; + if (line.includes('""')) { + violations.push({ + file: currentFile, + line: lineNumber, + content: line.substring(1), + }); + } + } else if (!line.startsWith("-")) { + lineNumber++; + } + }); + + return violations; +} + +async function createReview(violations: Array<{ file: string; line: number; content: string }>) { + const reviewComments = violations.map((v) => ({ + path: v.file, + line: v.line, + body: `Warning: Empty string found.\n\`\`\`\n${v.content}\n\`\`\``, + })); + + await octokit.pulls.createReview({ + owner, + repo, + pull_number: parseInt(pullNumber), + event: "REQUEST_CHANGES", + comments: reviewComments, + body: "Empty strings detected in the code. Please review.", + }); +} + +run().catch((error) => { + console.error("Error running empty string check:", error); + process.exit(1); +}); From c27d5e7671fe71a3ce9ac85dea7fe4064087264a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:49:08 +0900 Subject: [PATCH 10/33] chore: empty string test --- .github/workflows/no-empty-strings.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/no-empty-strings.yml b/.github/workflows/no-empty-strings.yml index 77cd1a76..6b1e0073 100644 --- a/.github/workflows/no-empty-strings.yml +++ b/.github/workflows/no-empty-strings.yml @@ -20,4 +20,7 @@ jobs: run: | yarn tsx .github/empty-string-checker.ts env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_REPOSITORY: ${{ github.repository }} + GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }} + GITHUB_BASE_REF: ${{ github.base_ref }} \ No newline at end of file From 6fb71320d6bbf5f5a9aed22b62a3e96c141dd648 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:50:57 +0900 Subject: [PATCH 11/33] chore: empty string test --- .github/empty-string-checker.ts | 9 ++++----- .github/workflows/no-empty-strings.yml | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index a455e6c7..b482f72f 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -1,5 +1,5 @@ import { Octokit } from "@octokit/rest"; -import { execSync } from "child_process"; +import simpleGit from "simple-git"; const token = process.env.GITHUB_TOKEN; const [owner, repo] = process.env.GITHUB_REPOSITORY?.split("/") || []; @@ -11,12 +11,11 @@ if (!token || !owner || !repo || pullNumber === "0") { } const octokit = new Octokit({ auth: token }); +const git = simpleGit(); async function run() { // Get the diff of the pull request - const diff = execSync(`git diff origin/${process.env.GITHUB_BASE_REF}...HEAD`, { - encoding: "utf8", - }); + const diff = await git.diff([`origin/${process.env.GITHUB_BASE_REF}...HEAD`]); const violations = parseDiffForEmptyStrings(diff); @@ -67,7 +66,7 @@ async function createReview(violations: Array<{ file: string; line: number; cont owner, repo, pull_number: parseInt(pullNumber), - event: "REQUEST_CHANGES", + event: "COMMENT", comments: reviewComments, body: "Empty strings detected in the code. Please review.", }); diff --git a/.github/workflows/no-empty-strings.yml b/.github/workflows/no-empty-strings.yml index 6b1e0073..02d71ef0 100644 --- a/.github/workflows/no-empty-strings.yml +++ b/.github/workflows/no-empty-strings.yml @@ -15,7 +15,7 @@ jobs: node-version: '20.10.0' - name: Install Dependencies run: | - yarn add tsx + yarn add tsx simple-git - name: Check for Empty Strings run: | yarn tsx .github/empty-string-checker.ts From 26e265b49b8ba98cd6736d68b9fa651ce3804638 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:52:23 +0900 Subject: [PATCH 12/33] chore: empty string test --- .github/empty-string-checker.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index b482f72f..a903f3e4 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -14,16 +14,31 @@ const octokit = new Octokit({ auth: token }); const git = simpleGit(); async function run() { - // Get the diff of the pull request - const diff = await git.diff([`origin/${process.env.GITHUB_BASE_REF}...HEAD`]); + try { + // Get the base and head SHAs for the pull request + const { data: pullRequest } = await octokit.pulls.get({ + owner, + repo, + pull_number: parseInt(pullNumber), + }); - const violations = parseDiffForEmptyStrings(diff); + const baseSha = pullRequest.base.sha; + const headSha = pullRequest.head.sha; - if (violations.length > 0) { - await createReview(violations); - process.exit(1); // Exit with error to indicate failure - } else { - console.log("No empty strings found."); + // Get the diff of the pull request using the SHAs + const diff = await git.diff([`${baseSha}...${headSha}`]); + + const violations = parseDiffForEmptyStrings(diff); + + if (violations.length > 0) { + await createReview(violations); + process.exit(1); // Exit with error to indicate failure + } else { + console.log("No empty strings found."); + } + } catch (error) { + console.error("Error running empty string check:", error); + process.exit(1); } } From 2c8fc075d14d390456e71ad86beed41d42264342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:56:26 +0900 Subject: [PATCH 13/33] chore: empty string test --- .github/empty-string-checker.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index a903f3e4..1c109a04 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -4,8 +4,9 @@ import simpleGit from "simple-git"; const token = process.env.GITHUB_TOKEN; const [owner, repo] = process.env.GITHUB_REPOSITORY?.split("/") || []; const pullNumber = process.env.GITHUB_PR_NUMBER || process.env.PULL_REQUEST_NUMBER || "0"; +const baseRef = process.env.GITHUB_BASE_REF; -if (!token || !owner || !repo || pullNumber === "0") { +if (!token || !owner || !repo || pullNumber === "0" || !baseRef) { console.error("Missing required environment variables."); process.exit(1); } @@ -25,8 +26,11 @@ async function run() { const baseSha = pullRequest.base.sha; const headSha = pullRequest.head.sha; + // Fetch the base branch + await git.fetch(["origin", baseRef]); + // Get the diff of the pull request using the SHAs - const diff = await git.diff([`${baseSha}...${headSha}`]); + const diff = await git.diff([`origin/${baseRef}...${headSha}`]); const violations = parseDiffForEmptyStrings(diff); From c372b6e33a9e5256f374ec78d2bb2be0bbb66973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 06:59:02 +0900 Subject: [PATCH 14/33] chore: empty string test --- .github/empty-string-checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 1c109a04..166dd081 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -26,8 +26,8 @@ async function run() { const baseSha = pullRequest.base.sha; const headSha = pullRequest.head.sha; - // Fetch the base branch - await git.fetch(["origin", baseRef]); + // Fetch all remote branches and tags + await git.fetch(["--all"]); // Get the diff of the pull request using the SHAs const diff = await git.diff([`origin/${baseRef}...${headSha}`]); From c8eaf0871c44438d3427dd308ba674733378377d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 07:02:18 +0900 Subject: [PATCH 15/33] chore: empty string test --- .github/empty-string-checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 166dd081..406cce71 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -27,10 +27,10 @@ async function run() { const headSha = pullRequest.head.sha; // Fetch all remote branches and tags - await git.fetch(["--all"]); + await git.fetch(["origin", baseSha, headSha]); // Get the diff of the pull request using the SHAs - const diff = await git.diff([`origin/${baseRef}...${headSha}`]); + const diff = await git.diff([`${baseSha}...${headSha}`]); const violations = parseDiffForEmptyStrings(diff); From 629f351f6ad26f5d4d7fad6c58f05c05b8be05c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 07:08:06 +0900 Subject: [PATCH 16/33] chore: empty string test --- .github/empty-string-checker.ts | 21 +++++++++++++-------- package.json | 1 + yarn.lock | 30 +++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 406cce71..61c4d268 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -14,7 +14,7 @@ if (!token || !owner || !repo || pullNumber === "0" || !baseRef) { const octokit = new Octokit({ auth: token }); const git = simpleGit(); -async function run() { +async function main() { try { // Get the base and head SHAs for the pull request const { data: pullRequest } = await octokit.pulls.get({ @@ -32,17 +32,22 @@ async function run() { // Get the diff of the pull request using the SHAs const diff = await git.diff([`${baseSha}...${headSha}`]); - const violations = parseDiffForEmptyStrings(diff); + console.log("Checking for empty strings..."); + const emptyStrings = parseDiffForEmptyStrings(diff); - if (violations.length > 0) { - await createReview(violations); - process.exit(1); // Exit with error to indicate failure + if (emptyStrings.length > 0) { + console.error("Empty strings found:"); + emptyStrings.forEach(({ file, line }) => { + console.error(`${file}:${line}`); + }); + await createReview(emptyStrings); + process.exit(1); // This line is causing the non-zero exit code } else { console.log("No empty strings found."); } } catch (error) { - console.error("Error running empty string check:", error); - process.exit(1); + console.error("An error occurred:", error); + process.exit(1); // This could also be causing the non-zero exit code } } @@ -91,7 +96,7 @@ async function createReview(violations: Array<{ file: string; line: number; cont }); } -run().catch((error) => { +main().catch((error) => { console.error("Error running empty string check:", error); process.exit(1); }); diff --git a/package.json b/package.json index 87b10147..66650346 100644 --- a/package.json +++ b/package.json @@ -61,6 +61,7 @@ "lint-staged": "^15.2.2", "npm-run-all": "^4.1.5", "prettier": "^3.2.5", + "simple-git": "^3.27.0", "ts-jest": "29.1.2", "tsx": "^4.7.1", "typescript": "^5.3.3" diff --git a/yarn.lock b/yarn.lock index 61a0a002..b4458249 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1451,6 +1451,18 @@ "@jridgewell/resolve-uri" "^3.1.0" "@jridgewell/sourcemap-codec" "^1.4.14" +"@kwsites/file-exists@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@kwsites/file-exists/-/file-exists-1.1.1.tgz#ad1efcac13e1987d8dbaf235ef3be5b0d96faa99" + integrity sha512-m9/5YGR18lIwxSFDwfE3oA7bWuq9kdau6ugN4H2rJeyhFQZcG9AgSHkQtSD15a8WvTgfz9aikZMrKPHvbpqFiw== + dependencies: + debug "^4.1.1" + +"@kwsites/promise-deferred@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@kwsites/promise-deferred/-/promise-deferred-1.1.1.tgz#8ace5259254426ccef57f3175bc64ed7095ed919" + integrity sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw== + "@mswjs/cookies@^1.1.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@mswjs/cookies/-/cookies-1.1.0.tgz#1528eb43630caf83a1d75d5332b30e75e9bb1b5b" @@ -3142,6 +3154,13 @@ debug@^3.1.0: dependencies: ms "^2.1.1" +debug@^4.3.5: + version "4.3.7" + resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.7.tgz#87945b4151a011d76d95a198d7111c865c360a52" + integrity sha512-Er2nc/H7RrMXZBFCEim6TCmMk02Z8vLC2Rbi1KEBggpo0fS6l0S1nnapwmIi3yW/+GOJap1Krg4w0Hg80oCqgQ== + dependencies: + ms "^2.1.3" + decamelize-keys@^1.1.0: version "1.1.1" resolved "https://registry.yarnpkg.com/decamelize-keys/-/decamelize-keys-1.1.1.tgz#04a2d523b2f18d80d0158a43b895d56dff8d19d8" @@ -5641,7 +5660,7 @@ ms@2.1.2: resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w== -ms@^2.1.1: +ms@^2.1.1, ms@^2.1.3: version "2.1.3" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2" integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA== @@ -6600,6 +6619,15 @@ simple-get@^4.0.1: once "^1.3.1" simple-concat "^1.0.0" +simple-git@^3.27.0: + version "3.27.0" + resolved "https://registry.yarnpkg.com/simple-git/-/simple-git-3.27.0.tgz#f4b09e807bda56a4a3968f635c0e4888d3decbd5" + integrity sha512-ivHoFS9Yi9GY49ogc6/YAi3Fl9ROnF4VyubNylgCkA+RVqLaKWnDSzXOVzya8csELIaWaYNutsEuAhZrtOjozA== + dependencies: + "@kwsites/file-exists" "^1.1.1" + "@kwsites/promise-deferred" "^1.1.1" + debug "^4.3.5" + sisteransi@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-1.0.5.tgz#134d681297756437cc05ca01370d3a7a571075ed" From 6f16e905ed4bc1542772ae3775d84ae166f17c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 07:19:39 +0900 Subject: [PATCH 17/33] chore: empty string test --- .github/empty-string-checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 61c4d268..066f0615 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -83,7 +83,7 @@ async function createReview(violations: Array<{ file: string; line: number; cont const reviewComments = violations.map((v) => ({ path: v.file, line: v.line, - body: `Warning: Empty string found.\n\`\`\`\n${v.content}\n\`\`\``, + body: `> [!WARNING]\n> Empty string found, consider another approach. [Read more](https://github.com/ubiquity/ts-template/issues/31).`, })); await octokit.pulls.createReview({ From f13fae7277a5ff5876c493198f1194565ac3e8e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 07:25:29 +0900 Subject: [PATCH 18/33] chore: empty string test --- .github/empty-string-checker.ts | 2 +- .github/workflows/no-empty-strings.yml | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 066f0615..49645828 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -92,7 +92,7 @@ async function createReview(violations: Array<{ file: string; line: number; cont pull_number: parseInt(pullNumber), event: "COMMENT", comments: reviewComments, - body: "Empty strings detected in the code. Please review.", + body: "> [!WARNING]\n> Empty strings detected in the code. Please review.", }); } diff --git a/.github/workflows/no-empty-strings.yml b/.github/workflows/no-empty-strings.yml index 02d71ef0..c303cc2b 100644 --- a/.github/workflows/no-empty-strings.yml +++ b/.github/workflows/no-empty-strings.yml @@ -12,7 +12,13 @@ jobs: - name: Setup Node.js uses: actions/setup-node@v4 with: - node-version: '20.10.0' + node-version: "20.10.0" + - name: Get GitHub App token + uses: tibdex/github-app-token@v1.7.0 + id: get_installation_token + with: + app_id: ${{ secrets.APP_ID }} + private_key: ${{ secrets.APP_PRIVATE_KEY }} - name: Install Dependencies run: | yarn add tsx simple-git @@ -20,7 +26,7 @@ jobs: run: | yarn tsx .github/empty-string-checker.ts env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ steps.get_installation_token.outputs.token }} GITHUB_REPOSITORY: ${{ github.repository }} GITHUB_PR_NUMBER: ${{ github.event.pull_request.number }} - GITHUB_BASE_REF: ${{ github.base_ref }} \ No newline at end of file + GITHUB_BASE_REF: ${{ github.base_ref }} From 9c5da5e6766a45621f150069e042e3058a7172a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 07:26:46 +0900 Subject: [PATCH 19/33] fix: ensure no linkback --- .github/empty-string-checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 49645828..bfe5dbf1 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -83,7 +83,7 @@ async function createReview(violations: Array<{ file: string; line: number; cont const reviewComments = violations.map((v) => ({ path: v.file, line: v.line, - body: `> [!WARNING]\n> Empty string found, consider another approach. [Read more](https://github.com/ubiquity/ts-template/issues/31).`, + body: `> [!WARNING]\n> Empty string found, consider another approach. [Read more](https://www.github.com/ubiquity/ts-template/issues/31).`, })); await octokit.pulls.createReview({ From 5813aef825b6030e6b9f14259250ac7ec0c33a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 07:47:35 +0900 Subject: [PATCH 20/33] fix: knip --- .github/knip.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/knip.ts b/.github/knip.ts index 4c402560..c9f21e8a 100644 --- a/.github/knip.ts +++ b/.github/knip.ts @@ -1,7 +1,7 @@ import type { KnipConfig } from "knip"; const config: KnipConfig = { - entry: ["build/index.ts"], + entry: ["build/index.ts", ".github/empty-string-checker.ts"], project: ["src/**/*.ts"], ignore: ["src/types/config.ts", "**/__mocks__/**", "**/__fixtures__/**"], ignoreExportsUsedInFile: true, From 19b73b2fb490f99fa7164fc0649e90cf91537581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 07:54:09 +0900 Subject: [PATCH 21/33] chore: annotations --- .github/empty-string-checker.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index bfe5dbf1..e41ddac5 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -80,19 +80,16 @@ function parseDiffForEmptyStrings(diff: string) { } async function createReview(violations: Array<{ file: string; line: number; content: string }>) { - const reviewComments = violations.map((v) => ({ - path: v.file, - line: v.line, - body: `> [!WARNING]\n> Empty string found, consider another approach. [Read more](https://www.github.com/ubiquity/ts-template/issues/31).`, - })); + const annotationsBody = violations + .map((v) => `${v.file}#L${v.line}\n\`\`\`suggestion\n${v.content.trim().replace('""', "/* TODO: Replace empty string */")}\n\`\`\``) + .join("\n\n"); await octokit.pulls.createReview({ owner, repo, pull_number: parseInt(pullNumber), event: "COMMENT", - comments: reviewComments, - body: "> [!WARNING]\n> Empty strings detected in the code. Please review.", + body: `> [!WARNING]\n> ${violations.length} empty string${violations.length > 1 ? "s" : ""} detected in the code.\n\n${annotationsBody}\n\n[Read more about empty string issues](https://www.github.com/ubiquity/ts-template/issues/31).`, }); } From 57077f60b1af1e3fae990ab5101edb9b1a797a48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:06:18 +0900 Subject: [PATCH 22/33] chore: annotations --- .github/empty-string-checker.ts | 41 +++++++++--------------------- package.json | 1 + yarn.lock | 45 +++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index e41ddac5..5cbcc5a9 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -1,5 +1,6 @@ import { Octokit } from "@octokit/rest"; import simpleGit from "simple-git"; +import * as core from "@actions/core"; const token = process.env.GITHUB_TOKEN; const [owner, repo] = process.env.GITHUB_REPOSITORY?.split("/") || []; @@ -7,7 +8,7 @@ const pullNumber = process.env.GITHUB_PR_NUMBER || process.env.PULL_REQUEST_NUMB const baseRef = process.env.GITHUB_BASE_REF; if (!token || !owner || !repo || pullNumber === "0" || !baseRef) { - console.error("Missing required environment variables."); + core.setFailed("Missing required environment variables."); process.exit(1); } @@ -16,7 +17,6 @@ const git = simpleGit(); async function main() { try { - // Get the base and head SHAs for the pull request const { data: pullRequest } = await octokit.pulls.get({ owner, repo, @@ -26,28 +26,26 @@ async function main() { const baseSha = pullRequest.base.sha; const headSha = pullRequest.head.sha; - // Fetch all remote branches and tags await git.fetch(["origin", baseSha, headSha]); - // Get the diff of the pull request using the SHAs const diff = await git.diff([`${baseSha}...${headSha}`]); - console.log("Checking for empty strings..."); + core.info("Checking for empty strings..."); const emptyStrings = parseDiffForEmptyStrings(diff); if (emptyStrings.length > 0) { - console.error("Empty strings found:"); - emptyStrings.forEach(({ file, line }) => { - console.error(`${file}:${line}`); + emptyStrings.forEach(({ file, line, content }) => { + core.warning(`Empty string found: ${content}`, { + file, + startLine: parseInt(line.toString()), + }); }); - await createReview(emptyStrings); - process.exit(1); // This line is causing the non-zero exit code + core.setFailed(`${emptyStrings.length} empty string${emptyStrings.length > 1 ? "s" : ""} detected in the code.`); } else { - console.log("No empty strings found."); + core.info("No empty strings found."); } } catch (error) { - console.error("An error occurred:", error); - process.exit(1); // This could also be causing the non-zero exit code + core.setFailed(`An error occurred: ${error instanceof Error ? error.message : String(error)}`); } } @@ -79,21 +77,6 @@ function parseDiffForEmptyStrings(diff: string) { return violations; } -async function createReview(violations: Array<{ file: string; line: number; content: string }>) { - const annotationsBody = violations - .map((v) => `${v.file}#L${v.line}\n\`\`\`suggestion\n${v.content.trim().replace('""', "/* TODO: Replace empty string */")}\n\`\`\``) - .join("\n\n"); - - await octokit.pulls.createReview({ - owner, - repo, - pull_number: parseInt(pullNumber), - event: "COMMENT", - body: `> [!WARNING]\n> ${violations.length} empty string${violations.length > 1 ? "s" : ""} detected in the code.\n\n${annotationsBody}\n\n[Read more about empty string issues](https://www.github.com/ubiquity/ts-template/issues/31).`, - }); -} - main().catch((error) => { - console.error("Error running empty string check:", error); - process.exit(1); + core.setFailed(`Error running empty string check: ${error instanceof Error ? error.message : String(error)}`); }); diff --git a/package.json b/package.json index 66650346..82632f6e 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "dotenv": "^16.4.4" }, "devDependencies": { + "@actions/core": "^1.11.1", "@commitlint/cli": "^18.6.1", "@commitlint/config-conventional": "^18.6.2", "@cspell/dict-node": "^4.0.3", diff --git a/yarn.lock b/yarn.lock index b4458249..f6967bb0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7,6 +7,34 @@ resolved "https://registry.yarnpkg.com/@aashutoshrathi/word-wrap/-/word-wrap-1.2.6.tgz#bd9154aec9983f77b3a034ecaa015c2e4201f6cf" integrity sha512-1Yjs2SvM8TflER/OD3cOjhWWOZb58A2t7wpE2S9XfBYTiIl+XFhQG2bjy4Pu1I+EAlCNUzRDYDdFwFYUKvXcIA== +"@actions/core@^1.11.1": + version "1.11.1" + resolved "https://registry.yarnpkg.com/@actions/core/-/core-1.11.1.tgz#ae683aac5112438021588030efb53b1adb86f172" + integrity sha512-hXJCSrkwfA46Vd9Z3q4cpEpHB1rL5NG04+/rbqW9d3+CSvtB1tYe8UTpAlixa1vj0m/ULglfEK2UKxMGxCxv5A== + dependencies: + "@actions/exec" "^1.1.1" + "@actions/http-client" "^2.0.1" + +"@actions/exec@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@actions/exec/-/exec-1.1.1.tgz#2e43f28c54022537172819a7cf886c844221a611" + integrity sha512-+sCcHHbVdk93a0XT19ECtO/gIXoxvdsgQLzb2fE2/5sIZmWQuluYyjPQtrtTHdU1YzTZ7bAPN4sITq2xi1679w== + dependencies: + "@actions/io" "^1.0.1" + +"@actions/http-client@^2.0.1": + version "2.2.3" + resolved "https://registry.yarnpkg.com/@actions/http-client/-/http-client-2.2.3.tgz#31fc0b25c0e665754ed39a9f19a8611fc6dab674" + integrity sha512-mx8hyJi/hjFvbPokCg4uRd4ZX78t+YyRPtnKWwIl+RzNaVuFpQHfmlGVfsKEJN8LwTCvL+DfVgAM04XaHkm6bA== + dependencies: + tunnel "^0.0.6" + undici "^5.25.4" + +"@actions/io@^1.0.1": + version "1.1.3" + resolved "https://registry.yarnpkg.com/@actions/io/-/io-1.1.3.tgz#4cdb6254da7962b07473ff5c335f3da485d94d71" + integrity sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q== + "@ampproject/remapping@^2.2.0": version "2.3.0" resolved "https://registry.yarnpkg.com/@ampproject/remapping/-/remapping-2.3.0.tgz#ed441b6fa600072520ce18b43d2c8cc8caecc7f4" @@ -1147,6 +1175,11 @@ resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.56.0.tgz#ef20350fec605a7f7035a01764731b2de0f3782b" integrity sha512-gMsVel9D7f2HLkBma9VbtzZRehRogVRfbr++f06nL2vnCGCNlzOD+/MUov/F4p8myyAHspEhVobgjpX64q5m6A== +"@fastify/busboy@^2.0.0": + version "2.1.1" + resolved "https://registry.yarnpkg.com/@fastify/busboy/-/busboy-2.1.1.tgz#b9da6a878a371829a0502c9b6c1c143ef6663f4d" + integrity sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA== + "@humanwhocodes/config-array@^0.11.13": version "0.11.14" resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.11.14.tgz#d78e481a039f7566ecc9660b4ea7fe6b1fec442b" @@ -7083,6 +7116,11 @@ tunnel-agent@^0.6.0: dependencies: safe-buffer "^5.0.1" +tunnel@^0.0.6: + version "0.0.6" + resolved "https://registry.yarnpkg.com/tunnel/-/tunnel-0.0.6.tgz#72f1314b34a5b192db012324df2cc587ca47f92c" + integrity sha512-1h/Lnq9yajKY2PEbBadPXj3VxsDDu844OnaAo52UVmIzIvwwtBPIuNvkjuzBlTWpfJyUbG3ez0KSBibQkj4ojg== + tweetnacl@^0.14.3, tweetnacl@~0.14.0: version "0.14.5" resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-0.14.5.tgz#5ae68177f192d4456269d108afa93ff8743f4f64" @@ -7206,6 +7244,13 @@ undici-types@~5.26.4: resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-5.26.5.tgz#bcd539893d00b56e964fd2657a4866b221a65617" integrity sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA== +undici@^5.25.4: + version "5.28.4" + resolved "https://registry.yarnpkg.com/undici/-/undici-5.28.4.tgz#6b280408edb6a1a604a9b20340f45b422e373068" + integrity sha512-72RFADWFqKmUb2hmmvNODKL3p9hcB6Gt2DOQMis1SEBaV6a4MH8soBvzg+95CYhCKPFedut2JY9bMfrDl9D23g== + dependencies: + "@fastify/busboy" "^2.0.0" + unescape-js@^1.0.5: version "1.1.4" resolved "https://registry.yarnpkg.com/unescape-js/-/unescape-js-1.1.4.tgz#4bc6389c499cb055a98364a0b3094e1c3d5da395" From b003b80e5e3fcb998dc073145677be741d4d4315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:12:16 +0900 Subject: [PATCH 23/33] chore: annotations --- .github/empty-string-checker.ts | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 5cbcc5a9..f611c805 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -31,16 +31,37 @@ async function main() { const diff = await git.diff([`${baseSha}...${headSha}`]); core.info("Checking for empty strings..."); - const emptyStrings = parseDiffForEmptyStrings(diff); + const violations = parseDiffForEmptyStrings(diff); - if (emptyStrings.length > 0) { - emptyStrings.forEach(({ file, line, content }) => { + if (violations.length > 0) { + violations.forEach(({ file, line, content }) => { core.warning(`Empty string found: ${content}`, { file, startLine: parseInt(line.toString()), }); }); - core.setFailed(`${emptyStrings.length} empty string${emptyStrings.length > 1 ? "s" : ""} detected in the code.`); + // core.setFailed(`${emptyStrings.length} empty string${emptyStrings.length > 1 ? "s" : ""} detected in the code.`); + + await octokit.rest.checks.create({ + owner, + repo, + name: "Empty String Check", + head_sha: headSha, + status: "completed", + conclusion: violations.length > 0 ? "failure" : "success", + output: { + title: "Empty String Check Results", + summary: `Found ${violations.length} violations`, + annotations: violations.map((v) => ({ + path: v.file, + start_line: v.line, + end_line: v.line, + annotation_level: "warning", + message: "Empty string found", + raw_details: v.content, + })), + }, + }); } else { core.info("No empty strings found."); } @@ -61,11 +82,10 @@ function parseDiffForEmptyStrings(diff: string) { currentFile = line.replace("+++ b/", ""); lineNumber = 0; } else if (line.startsWith("+") && !line.startsWith("+++")) { - lineNumber++; if (line.includes('""')) { violations.push({ file: currentFile, - line: lineNumber, + line: lineNumber++, content: line.substring(1), }); } From 61261e41d6fadf3f282f7c7d286c8c602d32d90f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:17:56 +0900 Subject: [PATCH 24/33] chore: annotations --- .github/empty-string-checker.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 5cbcc5a9..10da8338 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -1,6 +1,6 @@ +import * as core from "@actions/core"; import { Octokit } from "@octokit/rest"; import simpleGit from "simple-git"; -import * as core from "@actions/core"; const token = process.env.GITHUB_TOKEN; const [owner, repo] = process.env.GITHUB_REPOSITORY?.split("/") || []; @@ -61,11 +61,11 @@ function parseDiffForEmptyStrings(diff: string) { currentFile = line.replace("+++ b/", ""); lineNumber = 0; } else if (line.startsWith("+") && !line.startsWith("+++")) { - lineNumber++; + ++lineNumber; if (line.includes('""')) { violations.push({ file: currentFile, - line: lineNumber, + line: ++lineNumber, content: line.substring(1), }); } From a9b93e5d8e427c4ee38d1344632bbcd826443239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:33:11 +0900 Subject: [PATCH 25/33] chore: fix empty string checker to correctly count violations --- .github/empty-string-checker.ts | 50 ++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 74a07ac3..67b08945 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -20,7 +20,7 @@ async function main() { const { data: pullRequest } = await octokit.pulls.get({ owner, repo, - pull_number: parseInt(pullNumber), + pull_number: parseInt(pullNumber, 10), }); const baseSha = pullRequest.base.sha; @@ -37,10 +37,11 @@ async function main() { violations.forEach(({ file, line, content }) => { core.warning(`Empty string found: ${content}`, { file, - startLine: parseInt(line.toString()), + startLine: line, }); }); - // core.setFailed(`${emptyStrings.length} empty string${emptyStrings.length > 1 ? "s" : ""} detected in the code.`); + + // core.setFailed(`${violations.length} empty string${violations.length > 1 ? "s" : ""} detected in the code.`); await octokit.rest.checks.create({ owner, @@ -51,7 +52,7 @@ async function main() { conclusion: violations.length > 0 ? "failure" : "success", output: { title: "Empty String Check Results", - summary: `Found ${violations.length} violations`, + summary: `Found ${violations.length} violation${violations.length !== 1 ? "s" : ""}`, annotations: violations.map((v) => ({ path: v.file, start_line: v.line, @@ -75,22 +76,37 @@ function parseDiffForEmptyStrings(diff: string) { const diffLines = diff.split("\n"); let currentFile = ""; - let lineNumber = 0; + let headLine = 0; + let inHunk = false; diffLines.forEach((line) => { - if (line.startsWith("+++ b/")) { - currentFile = line.replace("+++ b/", ""); - lineNumber = 0; - } else if (line.startsWith("+") && !line.startsWith("+++")) { - if (line.includes('""')) { - violations.push({ - file: currentFile, - line: lineNumber++, - content: line.substring(1), - }); + const hunkHeaderMatch = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/.exec(line); + if (hunkHeaderMatch) { + headLine = parseInt(hunkHeaderMatch[1], 10); + inHunk = true; + return; + } + + if (inHunk) { + if (line.startsWith("+++ b/")) { + currentFile = line.replace("+++ b/", ""); + return; + } + + if (line.startsWith("+") && !line.startsWith("+++")) { + if (line.includes('""')) { + violations.push({ + file: currentFile, + line: headLine, + content: line.substring(1), + }); + } + headLine++; + } else if (line.startsWith("-")) { + // Removed line; do not increment headLine + } else { + headLine++; } - } else if (!line.startsWith("-")) { - lineNumber++; } }); From 1f2e6d4ce98ab8367fcd3a6637e044a822a31c39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:36:33 +0900 Subject: [PATCH 26/33] chore: fix empty string checker to correctly count violations --- .github/empty-string-checker.ts | 35 +++++++++++++++------------------ 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 67b08945..5327f0e4 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -87,32 +87,29 @@ function parseDiffForEmptyStrings(diff: string) { return; } - if (inHunk) { - if (line.startsWith("+++ b/")) { - currentFile = line.replace("+++ b/", ""); - return; - } + if (line.startsWith("--- a/") || line.startsWith("+++ b/")) { + currentFile = line.slice(6); + inHunk = false; + return; + } - if (line.startsWith("+") && !line.startsWith("+++")) { - if (line.includes('""')) { - violations.push({ - file: currentFile, - line: headLine, - content: line.substring(1), - }); - } - headLine++; - } else if (line.startsWith("-")) { - // Removed line; do not increment headLine - } else { - headLine++; + if (inHunk && line.startsWith("+")) { + // Check for various forms of empty strings, including at the start of the line + if (/^\+.*?(?:=\s*["'`]{2}|["'`]\s*:\s*["'`]|:\s*["'`]{2})/.test(line)) { + violations.push({ + file: currentFile, + line: headLine, + content: line.substring(1).trim(), + }); } + headLine++; + } else if (!line.startsWith("-")) { + headLine++; } }); return violations; } - main().catch((error) => { core.setFailed(`Error running empty string check: ${error instanceof Error ? error.message : String(error)}`); }); From bbfb46fbbd8ef21cd9b6fdf9b991e4fdcc359bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:39:14 +0900 Subject: [PATCH 27/33] chore: fix empty string checker to correctly count violations --- .github/empty-string-checker.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 5327f0e4..f158cbfc 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -94,8 +94,14 @@ function parseDiffForEmptyStrings(diff: string) { } if (inHunk && line.startsWith("+")) { - // Check for various forms of empty strings, including at the start of the line - if (/^\+.*?(?:=\s*["'`]{2}|["'`]\s*:\s*["'`]|:\s*["'`]{2})/.test(line)) { + // Ignore package.json version numbers and ternary expressions + if (currentFile.endsWith("package.json") || /\? .+ : .+/.test(line)) { + headLine++; + return; + } + + // Check for various forms of empty strings, excluding version numbers and valid use cases + if (/^\+.*?(?:=\s*["'`]{2}(?!\s*[,;])|["'`]\s*:\s*["'`](?!\s*[,;])|:\s*["'`]{2}(?!\s*[,;]))/.test(line)) { violations.push({ file: currentFile, line: headLine, From e7149c396a26031850fa202497dce7cbe1fb14bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:41:59 +0900 Subject: [PATCH 28/33] chore: fix empty string checker to correctly count violations --- .github/empty-string-checker.ts | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index f158cbfc..a560d738 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -93,20 +93,25 @@ function parseDiffForEmptyStrings(diff: string) { return; } - if (inHunk && line.startsWith("+")) { - // Ignore package.json version numbers and ternary expressions - if (currentFile.endsWith("package.json") || /\? .+ : .+/.test(line)) { - headLine++; - return; - } + // Only process TypeScript files + if (!currentFile.endsWith(".ts")) { + return; + } - // Check for various forms of empty strings, excluding version numbers and valid use cases - if (/^\+.*?(?:=\s*["'`]{2}(?!\s*[,;])|["'`]\s*:\s*["'`](?!\s*[,;])|:\s*["'`]{2}(?!\s*[,;]))/.test(line)) { - violations.push({ - file: currentFile, - line: headLine, - content: line.substring(1).trim(), - }); + if (inHunk && line.startsWith("+")) { + // Check for empty strings in TypeScript syntax + if (/^\+.*?(?:=\s*["'`]{2}(?!\s*[,;])|:\s*["'`]{2}(?!\s*[,;]))/.test(line)) { + // Ignore empty strings in comments + if (!line.trim().startsWith("//") && !line.trim().startsWith("*")) { + // Ignore empty strings in template literals + if (!/`[^`]*\$\{[^}]*\}[^`]*`/.test(line)) { + violations.push({ + file: currentFile, + line: headLine, + content: line.substring(1).trim(), + }); + } + } } headLine++; } else if (!line.startsWith("-")) { From d7312bf8f0c102fa97783e03d0f26641abd090f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:47:41 +0900 Subject: [PATCH 29/33] chore: fix empty string checker to correctly count violations --- .github/empty-string-checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index a560d738..f9e65b2c 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -100,7 +100,7 @@ function parseDiffForEmptyStrings(diff: string) { if (inHunk && line.startsWith("+")) { // Check for empty strings in TypeScript syntax - if (/^\+.*?(?:=\s*["'`]{2}(?!\s*[,;])|:\s*["'`]{2}(?!\s*[,;]))/.test(line)) { + if (/^\+.*""/.test(line)) { // Ignore empty strings in comments if (!line.trim().startsWith("//") && !line.trim().startsWith("*")) { // Ignore empty strings in template literals From cdc89caf5195d36f65def7d1edc7f7b6bbfaa3e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:51:23 +0900 Subject: [PATCH 30/33] chore: fix empty string checker to correctly count violations --- .github/empty-string-checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index f9e65b2c..2ae6fa15 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -35,7 +35,7 @@ async function main() { if (violations.length > 0) { violations.forEach(({ file, line, content }) => { - core.warning(`Empty string found: ${content}`, { + core.warning("⚠️ EMPTY STRING DETECTED ⚠️", { file, startLine: line, }); @@ -75,7 +75,7 @@ function parseDiffForEmptyStrings(diff: string) { const violations: Array<{ file: string; line: number; content: string }> = []; const diffLines = diff.split("\n"); - let currentFile = ""; + let currentFile: string; let headLine = 0; let inHunk = false; From ea798ab4cf9a31116c5e66686386ca3e614ea4e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:53:43 +0900 Subject: [PATCH 31/33] chore: fix empty string checker to correctly count violations --- .github/empty-string-checker.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index 2ae6fa15..c7e9c3b5 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -94,7 +94,7 @@ function parseDiffForEmptyStrings(diff: string) { } // Only process TypeScript files - if (!currentFile.endsWith(".ts")) { + if (!currentFile?.endsWith(".ts")) { return; } From 9e2370b435f6a17eb6f53380fa5cf91e6c5fa198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 08:59:00 +0900 Subject: [PATCH 32/33] chore: formatting --- .github/empty-string-checker.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/.github/empty-string-checker.ts b/.github/empty-string-checker.ts index c7e9c3b5..ff94e813 100644 --- a/.github/empty-string-checker.ts +++ b/.github/empty-string-checker.ts @@ -35,10 +35,13 @@ async function main() { if (violations.length > 0) { violations.forEach(({ file, line, content }) => { - core.warning("⚠️ EMPTY STRING DETECTED ⚠️", { - file, - startLine: line, - }); + core.warning( + "Detected an empty string.\n\nIf this is during variable initialization, consider using a different approach.\nFor more information, visit: https://www.github.com/ubiquity/ts-template/issues/31", + { + file, + startLine: line, + } + ); }); // core.setFailed(`${violations.length} empty string${violations.length > 1 ? "s" : ""} detected in the code.`); From e956bec9c5f827f90503c2196cfec020862b5372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= Date: Wed, 9 Oct 2024 09:04:33 +0900 Subject: [PATCH 33/33] fix: remove dummy empty strings --- build/index.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/build/index.ts b/build/index.ts index a7c0eb9c..e1cf0b87 100644 --- a/build/index.ts +++ b/build/index.ts @@ -2,6 +2,3 @@ import * as dotenv from "dotenv"; // load environment variables (if you have them) dotenv.config(); console.log("Welcome to ts-template"); - -const _EMPTY_STRING_TEST_1 = ""; -const _EMPTY_STRING_TEST_2 = "";