diff --git a/.github/workflows/cr.yml b/.github/workflows/cr.yml index c346c6b..d6a9696 100644 --- a/.github/workflows/cr.yml +++ b/.github/workflows/cr.yml @@ -1,21 +1,28 @@ -name: Automated Ollama Code Review +name: Code Review + +permissions: + contents: read + pull-requests: write on: pull_request: - branches: - - main + types: [opened, reopened, synchronize] jobs: - ollama_review: + test: + # if: ${{ contains(github.event.*.labels.*.name, 'gpt review') }} # Optional; to run only when a label is attached runs-on: ubuntu-latest - name: Ollama Code Review Job steps: - - name: Checkout Repository - uses: actions/checkout@v2 - - - name: Ollama Code Review - uses: ./.github/actions/ollama-code-review - with: - llm-model: 'codegemma' + - uses: anc95/ChatGPT-CodeReview@main env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file + 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 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); + ``` 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); 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();