Compare commits

..

1 Commits

Author SHA1 Message Date
e52e4b2e19 Merge pull request #30 from unistack-org/master
merge master
2022-02-02 02:38:03 +03:00
12 changed files with 100 additions and 232 deletions

View File

@@ -1,20 +0,0 @@
name: "autoapprove"
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened]
permissions:
pull-requests: write
contents: write
jobs:
autoapprove:
runs-on: ubuntu-latest
steps:
- name: approve
uses: hmarr/auto-approve-action@v2
if: github.actor == 'vtolstov' || github.actor == 'dependabot[bot]'
id: approve
with:
github-token: ${{ secrets.GITHUB_TOKEN }}

View File

@@ -1,21 +0,0 @@
name: "automerge"
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened]
permissions:
pull-requests: write
contents: write
jobs:
automerge:
runs-on: ubuntu-latest
if: github.actor == 'vtolstov'
steps:
- name: merge
id: merge
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.TOKEN}}

View File

@@ -3,7 +3,6 @@ on:
push:
branches:
- master
- v3
jobs:
test:
name: test
@@ -14,7 +13,7 @@ jobs:
with:
go-version: 1.16
- name: checkout
uses: actions/checkout@v3
uses: actions/checkout@v2
- name: cache
uses: actions/cache@v2
with:
@@ -32,9 +31,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
uses: actions/checkout@v2
- name: lint
uses: golangci/golangci-lint-action@v3.1.0
uses: golangci/golangci-lint-action@v2
continue-on-error: true
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.

View File

@@ -9,7 +9,7 @@
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "codeql"
name: "CodeQL"
on:
workflow_run:
@@ -17,16 +17,16 @@ on:
types:
- completed
push:
branches: [ master, v3 ]
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master, v3 ]
branches: [ master ]
schedule:
- cron: '34 1 * * 0'
jobs:
analyze:
name: analyze
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
@@ -42,14 +42,11 @@ jobs:
# https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
steps:
- name: checkout
uses: actions/checkout@v3
- name: setup
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Checkout repository
uses: actions/checkout@v2
# Initializes the CodeQL tools for scanning.
- name: init
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
with:
languages: ${{ matrix.language }}
@@ -60,7 +57,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: autobuild
- name: Autobuild
uses: github/codeql-action/autobuild@v1
# Command-line programs to run using the OS shell.
@@ -74,5 +71,5 @@ jobs:
# make bootstrap
# make release
- name: analyze
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1

View File

@@ -1,27 +1,66 @@
name: "dependabot-automerge"
name: "prautomerge"
on:
pull_request_target:
types: [assigned, opened, synchronize, reopened]
workflow_run:
workflows: ["prbuild"]
types:
- completed
permissions:
pull-requests: write
contents: write
pull-requests: write
jobs:
automerge:
Dependabot-Automerge:
runs-on: ubuntu-latest
if: github.actor == 'dependabot[bot]'
# Contains workaround to execute if dependabot updates the PR by checking for the base branch in the linked PR
# The the github.event.workflow_run.event value is 'push' and not 'pull_request'
# dont work with multiple workflows when last returns success
if: >-
github.event.workflow_run.conclusion == 'success'
&& github.actor == 'dependabot[bot]'
&& github.event.sender.login == 'dependabot[bot]'
&& github.event.sender.type == 'Bot'
&& (github.event.workflow_run.event == 'pull_request'
|| (github.event.workflow_run.event == 'push' && github.event.workflow_run.pull_requests[0].base.ref == github.event.repository.default_branch ))
steps:
- name: metadata
id: metadata
uses: dependabot/fetch-metadata@v1.3.0
- name: Approve Changes and Merge changes if label 'dependencies' is set
uses: actions/github-script@v5
with:
github-token: "${{ secrets.TOKEN }}"
- name: merge
id: merge
if: ${{contains(steps.metadata.outputs.dependency-names, 'go.unistack.org')}}
run: gh pr merge --auto --merge "$PR_URL"
env:
PR_URL: ${{github.event.pull_request.html_url}}
GITHUB_TOKEN: ${{secrets.TOKEN}}
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
console.log(context.payload.workflow_run);
var labelNames = await github.paginate(
github.issues.listLabelsOnIssue,
{
repo: context.repo.repo,
owner: context.repo.owner,
issue_number: context.payload.workflow_run.pull_requests[0].number,
},
(response) => response.data.map(
(label) => label.name
)
);
console.log(labelNames);
if (labelNames.includes('dependencies')) {
console.log('Found label');
await github.pulls.createReview({
repo: context.repo.repo,
owner: context.repo.owner,
pull_number: context.payload.workflow_run.pull_requests[0].number,
event: 'APPROVE'
});
console.log('Approved PR');
await github.pulls.merge({
repo: context.repo.repo,
owner: context.repo.owner,
pull_number: context.payload.workflow_run.pull_requests[0].number,
});
console.log('Merged PR');
}

View File

@@ -3,7 +3,6 @@ on:
pull_request:
branches:
- master
- v3
jobs:
test:
name: test
@@ -14,7 +13,7 @@ jobs:
with:
go-version: 1.16
- name: checkout
uses: actions/checkout@v3
uses: actions/checkout@v2
- name: cache
uses: actions/cache@v2
with:
@@ -32,9 +31,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3
uses: actions/checkout@v2
- name: lint
uses: golangci/golangci-lint-action@v3.1.0
uses: golangci/golangci-lint-action@v2
continue-on-error: true
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.

2
go.mod
View File

@@ -4,7 +4,7 @@ go 1.16
require (
github.com/fatih/structtag v1.2.0
go.unistack.org/micro-proto/v3 v3.2.7
go.unistack.org/micro-proto/v3 v3.2.3
golang.org/x/tools v0.1.9
google.golang.org/protobuf v1.27.1
)

4
go.sum
View File

@@ -68,8 +68,8 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.unistack.org/micro-proto/v3 v3.2.7 h1:zG6d69kHc+oij2lwQ3AfrCgdjiEVRG2A7TlsxjusWs4=
go.unistack.org/micro-proto/v3 v3.2.7/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM=
go.unistack.org/micro-proto/v3 v3.2.3 h1:vSRI6VoZrlv0pUdo69irHv6HbbnD+oZOGEUE/TS5XBQ=
go.unistack.org/micro-proto/v3 v3.2.3/go.mod h1:ZltVWNECD5yK+40+OCONzGw4OtmSdTpVi8/KFgo9dqM=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=

View File

@@ -22,14 +22,13 @@ import (
"regexp"
"sort"
"strings"
"sync"
"go.unistack.org/micro-proto/v3/api"
v2 "go.unistack.org/micro-proto/v3/openapiv2"
v3 "go.unistack.org/micro-proto/v3/openapiv3"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
)
const (
@@ -48,19 +47,6 @@ type openapiv3Generator struct {
namedPathPattern *regexp.Regexp
}
var (
once sync.Once
protofiles = &protoregistry.Files{}
)
func protofilesAdd(plugin *protogen.Plugin) {
for path, f := range plugin.FilesByPath {
if _, err := protofiles.FindFileByPath(path); err != nil {
protofiles.RegisterFile(f.Desc)
}
}
}
// openapiv3Generate creates a new generator for a protoc plugin invocation.
func (g *Generator) openapiv3Generate(component string, plugin *protogen.Plugin) error {
og := &openapiv3Generator{
@@ -75,8 +61,6 @@ func (g *Generator) openapiv3Generate(component string, plugin *protogen.Plugin)
namedPathPattern: regexp.MustCompile("{(.+)=(.+)}"),
}
protofilesAdd(plugin)
d := og.buildDocumentV3(plugin)
bytes, err := d.YAMLValue("Generated with protoc-gen-go-micro\n")
if err != nil {
@@ -261,6 +245,12 @@ func (g *openapiv3Generator) addPathsToDocumentV3(d *v3.Document, file *protogen
outputMessage := method.Output
operationID := service.GoName + "_" + method.GoName
e2opt := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation)
if e2opt != nil && e2opt != v2.E_Openapiv2Operation.InterfaceOf(v2.E_Openapiv2Operation.Zero()) {
if opt, ok := e2opt.(*v2.Operation); ok && opt.OperationId != "" {
operationID = opt.OperationId
}
}
e3opt := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
if e3opt != nil && e3opt != v3.E_Openapiv3Operation.InterfaceOf(v3.E_Openapiv3Operation.Zero()) {
if opt, ok := e3opt.(*v3.Operation); ok && opt.OperationId != "" {
@@ -530,21 +520,6 @@ func (g *openapiv3Generator) buildOperationV3(
sparameters[parameter.Name] = struct{}{}
}
if u, err := url.Parse(path); err == nil {
mp := u.Query()
path = u.Path
if mp != nil {
for _, field := range inputMessage.Fields {
fieldName := string(field.Desc.Name())
if _, ok := mp[fieldName]; ok && fieldName != bodyField {
fieldParams := g.buildQueryParamsV3(field)
parameters = append(parameters, fieldParams...)
coveredParameters = append(coveredParameters, fieldName)
}
}
}
}
// Find simple path parameters like {id}
if allMatches := g.pathPattern.FindAllStringSubmatch(path, -1); allMatches != nil {
for _, matches := range allMatches {
@@ -648,71 +623,22 @@ func (g *openapiv3Generator) buildOperationV3(
}
}
var responses *v3.Responses
if eopt != nil && eopt != v3.E_Openapiv3Operation.InterfaceOf(v3.E_Openapiv3Operation.Zero()) {
opt := eopt.(*v3.Operation)
if r := opt.Responses; r != nil {
responses = r
}
if ref := responses.Default.GetReference(); ref != nil && ref.GetXRef() != "" {
xref := strings.TrimPrefix(ref.GetXRef(), ".")
description := "Default"
if strings.Contains(xref, "micro.errors.Error") {
description += " Error"
}
desc, err := protofiles.FindDescriptorByName(protoreflect.FullName(xref))
if err != nil {
log.Printf("unknown ref type %s err %v", xref, err)
} else {
responses.Default.Oneof = &v3.ResponseOrReference_Response{
Response: &v3.Response{
Description: description,
Content: g.responseContentForMessage(&protogen.Message{
Desc: desc.(protoreflect.MessageDescriptor),
}),
},
}
}
}
for _, rref := range responses.GetResponseOrReference() {
if ref := rref.Value.GetReference(); ref != nil && ref.GetXRef() != "" {
xref := strings.TrimPrefix(ref.GetXRef(), ".")
description := "Default"
if strings.Contains(xref, "micro.errors.Error") {
description += " Error"
}
desc, err := protofiles.FindDescriptorByName(protoreflect.FullName(xref))
if err != nil {
log.Printf("unknown ref type %s err %v", xref, err)
} else {
responses.Default.Oneof = &v3.ResponseOrReference_Response{
Response: &v3.Response{
Description: description,
Content: g.responseContentForMessage(&protogen.Message{
Desc: desc.(protoreflect.MessageDescriptor),
}),
},
}
}
}
}
} else {
responses = &v3.Responses{}
}
// Create the response.
responses.ResponseOrReference = append(responses.ResponseOrReference, &v3.NamedResponseOrReference{
Name: "200",
Value: &v3.ResponseOrReference{
Oneof: &v3.ResponseOrReference_Response{
Response: &v3.Response{
Description: "OK",
Content: g.responseContentForMessage(outputMessage),
responses := &v3.Responses{
ResponseOrReference: []*v3.NamedResponseOrReference{
{
Name: "200",
Value: &v3.ResponseOrReference{
Oneof: &v3.ResponseOrReference_Response{
Response: &v3.Response{
Description: "OK",
Content: g.responseContentForMessage(outputMessage),
},
},
},
},
},
})
}
// Create the operation.
op := &v3.Operation{
@@ -739,6 +665,7 @@ func (g *openapiv3Generator) buildOperationV3(
// Pass the entire request message as the request body.
typeName := g.fullMessageTypeName(inputMessage.Desc)
requestSchema = g.schemaOrReferenceForType(typeName)
} else {
// If body refers to a message field, use that type.
for _, field := range inputMessage.Fields {
@@ -765,8 +692,6 @@ func (g *openapiv3Generator) buildOperationV3(
}
}
ctype := getMediaType(eopt)
op.RequestBody = &v3.RequestBodyOrReference{
Oneof: &v3.RequestBodyOrReference_RequestBody{
RequestBody: &v3.RequestBody{
@@ -774,7 +699,7 @@ func (g *openapiv3Generator) buildOperationV3(
Content: &v3.MediaTypes{
AdditionalProperties: []*v3.NamedMediaType{
{
Name: ctype,
Name: "application/json",
Value: &v3.MediaType{
Schema: requestSchema,
},

View File

@@ -1,39 +0,0 @@
package main
import v3 "go.unistack.org/micro-proto/v3/openapiv3"
func getMediaType(eopt interface{}) string {
ctype := "application/json"
if eopt == nil {
return ctype
}
if eopt == v3.E_Openapiv3Operation.InterfaceOf(v3.E_Openapiv3Operation.Zero()) {
return ctype
}
opt, ok := eopt.(*v3.Operation)
if !ok || opt.RequestBody == nil {
return ctype
}
if opt.GetRequestBody() == nil {
return ctype
}
if opt.GetRequestBody().GetRequestBody() == nil {
return ctype
}
c := opt.GetRequestBody().GetRequestBody().GetContent()
if c == nil {
return ctype
}
for _, prop := range c.GetAdditionalProperties() {
ctype = prop.Name
}
return ctype
}

15
util.go
View File

@@ -55,9 +55,6 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
opts := proto.GetExtension(method.Desc.Options(), v2.E_Openapiv2Operation)
if opts != nil {
r := opts.(*v2.Operation)
if r.Responses == nil {
goto labelMethod
}
gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses.ResponseCode), ")")
for _, rsp := range r.Responses.ResponseCode {
if schema := rsp.Value.GetJsonReference(); schema != nil {
@@ -87,15 +84,8 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
opts := proto.GetExtension(method.Desc.Options(), v3.E_Openapiv3Operation)
if opts != nil {
r := opts.(*v3.Operation)
if r.Responses == nil {
goto labelMethod
}
resps := r.Responses.ResponseOrReference
if r.Responses.GetDefault() != nil {
resps = append(resps, &v3.NamedResponseOrReference{Name: "default", Value: r.Responses.GetDefault()})
}
gfile.P("errmap := make(map[string]interface{}, ", len(resps), ")")
for _, rsp := range resps {
gfile.P("errmap := make(map[string]interface{}, ", len(r.Responses.ResponseOrReference), ")")
for _, rsp := range r.Responses.ResponseOrReference {
if schema := rsp.Value.GetReference(); schema != nil {
ref := schema.XRef
if strings.HasPrefix(ref, "."+string(service.Desc.ParentFile().Package())+".") {
@@ -120,7 +110,6 @@ func generateServiceClientMethods(gfile *protogen.GeneratedFile, service *protog
gfile.P(")")
}
labelMethod:
if proto.HasExtension(method.Desc.Options(), api_options.E_Http) {
gfile.P("opts = append(opts,")
endpoints, _ := generateEndpoints(method)

View File

@@ -16,8 +16,8 @@ var (
microServerPackage = protogen.GoImportPath("go.unistack.org/micro/v3/server")
microClientHttpPackage = protogen.GoImportPath("go.unistack.org/micro-client-http/v3")
microServerHttpPackage = protogen.GoImportPath("go.unistack.org/micro-server-http/v3")
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro-proto/v3/codec")
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro-proto/v3/errors")
microCodecPackage = protogen.GoImportPath("go.unistack.org/micro/v3/codec")
microErrorsPackage = protogen.GoImportPath("go.unistack.org/micro/v3/errors")
timePackage = protogen.GoImportPath("time")
deprecationComment = "// Deprecated: Do not use."
versionComment = "v3.5.3"