From d48f23c541c29602a2ad83853c8a807f5944b340 Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Mon, 23 Sep 2024 14:29:35 +0800 Subject: [PATCH 01/11] chore: update readme --- packages/core/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/core/README.md b/packages/core/README.md index 68fe204..ffe25ce 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -70,6 +70,8 @@ message MyRequest { const ts = parseProto(source, { // Options + outputType, + mode, }); ``` @@ -101,4 +103,5 @@ message MyRequest { `; const ts = parseProto(source); + ``` From 94de3ba1299035663e09032bbb182607819ccf0e Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Mon, 23 Sep 2024 14:34:02 +0800 Subject: [PATCH 02/11] chore: update cr yml --- .github/workflows/{cr.yml => ollama_review.yml} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename .github/workflows/{cr.yml => ollama_review.yml} (88%) diff --git a/.github/workflows/cr.yml b/.github/workflows/ollama_review.yml similarity index 88% rename from .github/workflows/cr.yml rename to .github/workflows/ollama_review.yml index c346c6b..470c4d8 100644 --- a/.github/workflows/cr.yml +++ b/.github/workflows/ollama_review.yml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v2 - name: Ollama Code Review - uses: ./.github/actions/ollama-code-review + uses: zakerby/gh-code-review with: llm-model: 'codegemma' env: From e44a5599d2b2419beb44e8ee57b2d0dd68ad9162 Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Mon, 23 Sep 2024 15:19:33 +0800 Subject: [PATCH 03/11] chore: update version --- .github/workflows/ollama_review.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ollama_review.yml b/.github/workflows/ollama_review.yml index 470c4d8..fc66fe5 100644 --- a/.github/workflows/ollama_review.yml +++ b/.github/workflows/ollama_review.yml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v2 - name: Ollama Code Review - uses: zakerby/gh-code-review + uses: zakerby/gh-code-review@v0 with: llm-model: 'codegemma' env: From edee6afadd3870c89bca58f2c4cb0c0b0757951d Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Mon, 23 Sep 2024 15:27:32 +0800 Subject: [PATCH 04/11] chore: update version --- .github/workflows/ollama_review.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ollama_review.yml b/.github/workflows/ollama_review.yml index fc66fe5..2fd4629 100644 --- a/.github/workflows/ollama_review.yml +++ b/.github/workflows/ollama_review.yml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v2 - name: Ollama Code Review - uses: zakerby/gh-code-review@v0 + uses: zakerby/gh-code-review@v0.0.3 with: llm-model: 'codegemma' env: From eb11a4f7d6d2bc1681afebc3f695c501b86a6cd4 Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Mon, 23 Sep 2024 16:01:47 +0800 Subject: [PATCH 05/11] debug --- .github/workflows/ollama-code-review.yml | 37 ++++++++++++++++++++++++ .github/workflows/ollama_review.yml | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/ollama-code-review.yml diff --git a/.github/workflows/ollama-code-review.yml b/.github/workflows/ollama-code-review.yml new file mode 100644 index 0000000..3c78170 --- /dev/null +++ b/.github/workflows/ollama-code-review.yml @@ -0,0 +1,37 @@ +name: 'Code Review using Ollama' +description: 'Perform a code review on code modified using Ollama' +inputs: + llm-model: + description: 'Name of the LLM model to use for code review' + required: true + default: 'codegemma' + runs: + using: 'composite' + steps: + - name: Checkout code + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Install Ollama + run: | + curl -fsSL https://ollama.com/install.sh | bash + ollama pull ${{ inputs.llm-model }} + shell: bash + + - name: Get modified files + id: get-modified-files + use: tj-actions/changed-files@v43 + + - name: Review modified files + env: + GITHUB_TOKEN: ${{ github.token }} + run: | + for file in ${{ steps.get-modified-files.outputs.all_changed_files }}; do + modified_file_review=$(curl -s http://127.0.0.1:11434/api/generate -d '{"model": "${{ inputs.llm-model }}", "prompt": "Review the following file:\n\n```\n$(cat $file)\n```", "stream": false}' | jq -r '.response') + file_comment="Ollama Code Review for \`$file\`:\n\n$modified_file_review" + echo "$file_comment" >> ollama_review.txt + done + + gh pr comment ${{ github.event.pull_request.number }} --body "$(cat ollama_review.txt)" + shell: bash \ No newline at end of file diff --git a/.github/workflows/ollama_review.yml b/.github/workflows/ollama_review.yml index 2fd4629..c346c6b 100644 --- a/.github/workflows/ollama_review.yml +++ b/.github/workflows/ollama_review.yml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v2 - name: Ollama Code Review - uses: zakerby/gh-code-review@v0.0.3 + uses: ./.github/actions/ollama-code-review with: llm-model: 'codegemma' env: From 361410b94b66894385f202dd9a7ce5c9f47ac0ed Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Mon, 23 Sep 2024 16:03:39 +0800 Subject: [PATCH 06/11] debug --- .github/workflows/ollama_review.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ollama_review.yml b/.github/workflows/ollama_review.yml index c346c6b..6b1304e 100644 --- a/.github/workflows/ollama_review.yml +++ b/.github/workflows/ollama_review.yml @@ -14,7 +14,7 @@ jobs: uses: actions/checkout@v2 - name: Ollama Code Review - uses: ./.github/actions/ollama-code-review + uses: zakerby/gh-code-review@v0.0.2 with: llm-model: 'codegemma' env: From 4f0cc265a1fac46f66a9c6d755a53ed3483ba5a2 Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Mon, 23 Sep 2024 16:04:49 +0800 Subject: [PATCH 07/11] debug --- .github/workflows/ollama-code-review.yml | 37 ------------------------ 1 file changed, 37 deletions(-) delete mode 100644 .github/workflows/ollama-code-review.yml diff --git a/.github/workflows/ollama-code-review.yml b/.github/workflows/ollama-code-review.yml deleted file mode 100644 index 3c78170..0000000 --- a/.github/workflows/ollama-code-review.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: 'Code Review using Ollama' -description: 'Perform a code review on code modified using Ollama' -inputs: - llm-model: - description: 'Name of the LLM model to use for code review' - required: true - default: 'codegemma' - runs: - using: 'composite' - steps: - - name: Checkout code - uses: actions/checkout@v2 - with: - fetch-depth: 0 - - - name: Install Ollama - run: | - curl -fsSL https://ollama.com/install.sh | bash - ollama pull ${{ inputs.llm-model }} - shell: bash - - - name: Get modified files - id: get-modified-files - use: tj-actions/changed-files@v43 - - - name: Review modified files - env: - GITHUB_TOKEN: ${{ github.token }} - run: | - for file in ${{ steps.get-modified-files.outputs.all_changed_files }}; do - modified_file_review=$(curl -s http://127.0.0.1:11434/api/generate -d '{"model": "${{ inputs.llm-model }}", "prompt": "Review the following file:\n\n```\n$(cat $file)\n```", "stream": false}' | jq -r '.response') - file_comment="Ollama Code Review for \`$file\`:\n\n$modified_file_review" - echo "$file_comment" >> ollama_review.txt - done - - gh pr comment ${{ github.event.pull_request.number }} --body "$(cat ollama_review.txt)" - shell: bash \ No newline at end of file From ebf5e68e13f7bbdd63acb866f15f314a410eb418 Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Mon, 23 Sep 2024 16:17:00 +0800 Subject: [PATCH 08/11] debug --- .github/workflows/ollama_review.yml | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 .github/workflows/ollama_review.yml diff --git a/.github/workflows/ollama_review.yml b/.github/workflows/ollama_review.yml deleted file mode 100644 index 6b1304e..0000000 --- a/.github/workflows/ollama_review.yml +++ /dev/null @@ -1,21 +0,0 @@ -name: Automated Ollama Code Review - -on: - pull_request: - branches: - - main - -jobs: - ollama_review: - runs-on: ubuntu-latest - name: Ollama Code Review Job - steps: - - name: Checkout Repository - uses: actions/checkout@v2 - - - name: Ollama Code Review - uses: zakerby/gh-code-review@v0.0.2 - with: - llm-model: 'codegemma' - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file From f781c7449b90016e9ec1d7304c912af5e3ffd2da Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Mon, 23 Sep 2024 16:18:19 +0800 Subject: [PATCH 09/11] debug --- .github/workflows/cr.yml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/cr.yml diff --git a/.github/workflows/cr.yml b/.github/workflows/cr.yml new file mode 100644 index 0000000..d6a9696 --- /dev/null +++ b/.github/workflows/cr.yml @@ -0,0 +1,28 @@ +name: Code Review + +permissions: + contents: read + pull-requests: write + +on: + pull_request: + types: [opened, reopened, synchronize] + +jobs: + test: + # if: ${{ contains(github.event.*.labels.*.name, 'gpt review') }} # Optional; to run only when a label is attached + runs-on: ubuntu-latest + steps: + - uses: anc95/ChatGPT-CodeReview@main + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} + # Optional + LANGUAGE: Chinese + OPENAI_API_ENDPOINT: https://api.openai.com/v1 + MODEL: gpt-3.5-turbo # https://platform.openai.com/docs/models + PROMPT: # example: Please check if there are any confusions or irregularities in the following code diff: + top_p: 1 # https://platform.openai.com/docs/api-reference/chat/create#chat/create-top_p + temperature: 1 # https://platform.openai.com/docs/api-reference/chat/create#chat/create-temperature + max_tokens: 10000 + MAX_PATCH_LENGTH: 10000 # if the patch/diff length is large than MAX_PATCH_LENGTH, will be ignored and won't review. By default, with no MAX_PATCH_LENGTH set, there is also no limit for the patch/diff length. \ No newline at end of file From a285c4da9d42fe6d562174fdc6db82a5b7a45cb2 Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Fri, 27 Sep 2024 14:33:53 +0800 Subject: [PATCH 10/11] chore: fix #26 --- packages/core/src/print/gen-enum.js | 18 +++++++++--- packages/core/src/print/gen-service.js | 4 ++- packages/core/tests/comment/index.spec.js | 35 +++++++++++++++++++++-- packages/core/tests/print/nest.spec.js | 22 ++++++++++++++ 4 files changed, 72 insertions(+), 7 deletions(-) diff --git a/packages/core/src/print/gen-enum.js b/packages/core/src/print/gen-enum.js index 0284600..9f9f91b 100644 --- a/packages/core/src/print/gen-enum.js +++ b/packages/core/src/print/gen-enum.js @@ -1,5 +1,8 @@ import MagicString from 'magic-string'; import { indentPrefix, OUTPUT_TYPE } from '../constant.js'; +import protobuf from 'protobufjs'; + +const { Type } = protobuf; /** * generate typescript files from proto info @@ -8,7 +11,7 @@ import { indentPrefix, OUTPUT_TYPE } from '../constant.js'; * @returns */ export function genEnum(proto, options) { - const { values, comments, name } = proto; + const { values, comments, name, parent } = proto; const items = Object.keys(values) .map((key) => ({ @@ -21,7 +24,7 @@ export function genEnum(proto, options) { const result = new MagicString(''); items.forEach((s) => { - if(s.comment) { + if (s.comment) { result.append(`//${s.comment}\n`); } result.append(`${s.name} = ${s.id},\n`); @@ -29,9 +32,16 @@ export function genEnum(proto, options) { const prefix = options.outputType === OUTPUT_TYPE.definition ? '' : 'export '; + let interfaceName = name; + + // deal with nested type conflict problem + if (parent && parent instanceof Type && parent.name) { + interfaceName = parent.name + interfaceName; + } + result .indent(indentPrefix) - .prepend(`${prefix}enum ${name} {\n`) + .prepend(`${prefix}enum ${interfaceName} {\n`) .append('}\n\n'); return { @@ -60,7 +70,7 @@ export function getJsdocEnum(proto, options) { const result = new MagicString(''); items.forEach((s) => { - if(s.comment) { + if (s.comment) { result.append(`//${s.comment}\n`); } result.append(`${s.name} = ${s.id},\n`); diff --git a/packages/core/src/print/gen-service.js b/packages/core/src/print/gen-service.js index 9b76561..3e400d6 100644 --- a/packages/core/src/print/gen-service.js +++ b/packages/core/src/print/gen-service.js @@ -52,7 +52,9 @@ export function genService(proto, options) { const prefix = options.outputType === OUTPUT_TYPE.definition ? '' : 'export '; if (item.comment) { - result.append(`//${item.comment}\n`); + item.comment.split('\n').forEach((line) => { + result.append(`//${line} \n`); + }) } result.append( diff --git a/packages/core/tests/comment/index.spec.js b/packages/core/tests/comment/index.spec.js index af97f4a..f2f4541 100644 --- a/packages/core/tests/comment/index.spec.js +++ b/packages/core/tests/comment/index.spec.js @@ -4,7 +4,7 @@ import { test } from 'uvu'; import * as assert from 'uvu/assert'; -const source = ` +const sample1 = ` message UpdateHostBankAccWalletInfoResp { // 0:成功  非0:错误码    @@ -19,12 +19,43 @@ message UpdateHostBankAccWalletInfoResp { `; test('Comment of Field type should be converted', () => { - const ts = parseProto(source); + const ts = parseProto(sample1); assert.match(ts, '//50010 bank account already exist'); assert.match(ts, '//50011: bank account number already exist'); assert.match(ts, '//50012 bank account mismatch the one on'); }); +const sample2 = ` + syntax = "proto3"; + + service Greeter { + //hello + //hello1 + rpc SayHello (HelloRequest) returns (HelloReply) {} + } + + message HelloRequest { + string name = 1; + } + + message Teacher { + string name = 1; + } + + message HelloReply { + string message = 1; + int32 test = 2; + Teacher teacher = 3; + } +`; + +test('Comment of function type should be converted', () => { + const ts = parseProto(sample2); + + assert.match(ts, '//hello'); + assert.match(ts, '//hello1'); +}); + test.run(); \ No newline at end of file diff --git a/packages/core/tests/print/nest.spec.js b/packages/core/tests/print/nest.spec.js index 06ff7ce..78fbcc3 100644 --- a/packages/core/tests/print/nest.spec.js +++ b/packages/core/tests/print/nest.spec.js @@ -140,5 +140,27 @@ test('nest type6 should be converted', () => { assert.match(ts, '@typedef {Object} Message1'); }); + +test('nest type7 should be converted', () => { + const source = ` + syntax = "proto3"; + message UpdateUserRoleReq { + enum Type { + PLATFORM = 0; + PROJECT = 1; + MODULE = 2; + PLUGIN = 3; + } + int32 user_id = 1; + int32 role_id = 2; + Type type = 3; + } + `; + + const ts = parseProto(source); + assert.match(ts, 'type?: UpdateUserRoleReqType;'); + assert.match(ts, 'enum UpdateUserRoleReqType'); +}) + test.run(); From b329b5be18ad734d35acb9321c899dddaf29b12d Mon Sep 17 00:00:00 2001 From: "brandon.xiang" <1542453460@qq.com> Date: Fri, 27 Sep 2024 14:46:59 +0800 Subject: [PATCH 11/11] fix: #26 --- packages/core/src/node/io.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/core/src/node/io.js b/packages/core/src/node/io.js index 92570bf..2a06b3a 100644 --- a/packages/core/src/node/io.js +++ b/packages/core/src/node/io.js @@ -2,6 +2,7 @@ import fs from 'node:fs'; import path from 'node:path'; import { handleError } from '../utils/log.js'; import { parseProtoFiles } from '../core.js'; +import { defaultFilename } from '../constant.js'; const { mkdirSync, readdirSync, writeFileSync } = fs; @@ -28,6 +29,10 @@ export function transformProtoFiles(options) { if (!res) return; mkdirSync(outputDir, { recursive: true }); + + if(res.has(defaultFilename)) { + res.delete(defaultFilename); + } res.forEach((value, key) => { const outputFile = path.join(outputDir, key);