Skip to content

Commit d4e06a7

Browse files
authored
Merge pull request #1 from hyperonecom/fix-go
Minor tweaks for Go & Python build
2 parents 7948e12 + 30de302 commit d4e06a7

File tree

11 files changed

+108
-86
lines changed

11 files changed

+108
-86
lines changed

.github/workflows/generate.yaml

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,29 @@ on:
77

88
jobs:
99
generate:
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
include:
14+
- language: ts
15+
copy: "/bin/cp -rf ./clients/ts/. h1-client-ts"
16+
directory: "h1-client-ts"
17+
repository: "hyperonecom/h1-client-js"
18+
build: "yarn"
19+
- language: go
20+
copy: "/bin/cp -rf ./clients/go/. h1-client-go"
21+
directory: "h1-client-go"
22+
repository: "hyperonecom/h1-client-go"
23+
build: "go build -o temporary_build && rm temporary_build"
24+
- language: python
25+
copy: "/bin/cp -rf ./clients/python/. h1-client-python"
26+
directory: "h1-client-python"
27+
repository: "hyperonecom/h1-client-python"
28+
build: "pip install -e ."
1029
runs-on: ubuntu-latest
30+
env:
31+
GO_POST_PROCESS_FILE: "/usr/local/bin/gofmt -w"
32+
LANGUAGE: "${{ matrix.language }}"
1133
steps:
1234
- name: Checkout generator repository
1335
uses: actions/checkout@v1
@@ -23,6 +45,10 @@ jobs:
2345
uses: actions/setup-go@v2
2446
with:
2547
go-version: 1.14.x
48+
- name: Set up Python 3.6
49+
uses: actions/setup-python@v2
50+
with:
51+
python-version: 3.6
2652
- name: Configure github credentials
2753
run: |
2854
git config --global user.email "action@github.com"
@@ -35,76 +61,29 @@ jobs:
3561
PASSPORT_FILE: ${{ secrets.PASSPORT_FILE }}
3662
- name: Install dependencies
3763
run: yarn
38-
- name: Generate and test TS client
39-
run: yarn start ts
40-
- name: Checkout TS Client repo
41-
uses: actions/checkout@v2
42-
with:
43-
persist-credentials: false
44-
fetch-depth: 0
45-
repository: hyperonecom/h1-client-js
46-
path: h1-client-ts
47-
- name: Copy TS client
48-
run: /bin/cp -rf ./clients/ts/. h1-client-ts
49-
- name: Commit TS client
50-
run: |
51-
git add .
52-
git diff-index --quiet HEAD || git commit -m "Update $(date)"
53-
working-directory: h1-client-ts
54-
- name: Push TS client
55-
uses: ad-m/github-push-action@master
56-
with:
57-
github_token: ${{ secrets.PUSH_TOKEN }}
58-
directory: h1-client-ts
59-
repository: hyperonecom/h1-client-js
60-
- name: Generate and test Go client
61-
run: yarn start go
62-
- name: Test building Go client
63-
run: go build -o temporary_build && rm temporary_build
64-
working-directory: ./clients/go/
65-
- name: Checkout Go Client repo
66-
uses: actions/checkout@v2
67-
with:
68-
persist-credentials: false
69-
fetch-depth: 0
70-
repository: hyperonecom/h1-client-go
71-
path: h1-client-go
72-
- name: Copy Go client
73-
run: /bin/cp -rf ./clients/go/. h1-client-go
74-
- name: Commit Go client
75-
run: |
76-
git add .
77-
git diff-index --quiet HEAD || git commit -m "Update $(date)"
78-
working-directory: h1-client-go
79-
- name: Push Go client
80-
uses: ad-m/github-push-action@master
81-
with:
82-
github_token: ${{ secrets.PUSH_TOKEN }}
83-
directory: h1-client-go
84-
repository: hyperonecom/h1-client-go
85-
- name: Set up Python 3.6
86-
uses: actions/setup-python@v2
87-
with:
88-
python-version: 3.6
89-
- name: Generate and test Python client
90-
run: yarn start python
91-
- name: Checkout Python Client repo
64+
- name: Generate and test client
65+
run: yarn start "$LANGUAGE"
66+
- name: Test building client
67+
run: "${{ matrix.build }}"
68+
working-directory: "./clients/${{ matrix.language }}"
69+
- name: Checkout Client repo
9270
uses: actions/checkout@v2
9371
with:
9472
persist-credentials: false
9573
fetch-depth: 0
96-
repository: hyperonecom/h1-client-python
97-
path: h1-client-python
98-
- name: Copy Python client
99-
run: /bin/cp -rf ./clients/python/. h1-client-python
100-
- name: Commit Python client
74+
repository: "${{ matrix.repository }}"
75+
path: "${{ matrix.directory }}"
76+
- name: Copy client
77+
run: "${{ matrix.copy }}"
78+
- name: Commit client
10179
run: |
10280
git add .
10381
git diff-index --quiet HEAD || git commit -m "Update $(date)"
104-
working-directory: h1-client-python
105-
- name: Push Python client
82+
working-directory: "${{ matrix.directory }}"
83+
- name: Push client
10684
uses: ad-m/github-push-action@master
85+
if: "${{ success() && github.ref == 'refs/heads/master' }}"
10786
with:
10887
github_token: ${{ secrets.PUSH_TOKEN }}
109-
directory: h1-client-python
110-
repository: hyperonecom/h1-client-python
88+
directory: "${{ matrix.directory }}"
89+
repository: "${{ matrix.repository }}"

src/generators/go/_README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func main() {
2424
if err != nil {
2525
// handle error with getting passport provider
2626
}
27-
projects, response, err := c.IamProjectApi.IamProjectList(p.Ctx(), nil) // getting IamProjectList using IamProjectApi struct
27+
projects, response, err := c.IamProjectApi.IamProjectList(p.Ctx()).Execute() // getting IamProjectList using IamProjectApi struct
2828

2929
if err != nil {
3030
// handle error with request
@@ -70,7 +70,7 @@ Then use `Ctx`, or `CtxWithError` method.
7070
`Ctx` method usage:
7171

7272
```go
73-
projects, response, err := c.IamProjectApi.IamProjectList(p.Ctx(), nil)
73+
projects, response, err := c.IamProjectApi.IamProjectList(p.Ctx()).Execute()
7474
// [...]
7575
```
7676

@@ -82,7 +82,7 @@ if err != nil {
8282
log.panic(err)
8383
}
8484

85-
projects, response, err := c.IamProjectApi.IamProjectList(p.Ctx(), nil)
85+
projects, response, err := c.IamProjectApi.IamProjectList(p.Ctx()).Execute()
8686
```
8787

8888
The difference between `Ctx` method and `CtxWithError` is that `Ctx` does not
@@ -105,7 +105,7 @@ func main() {
105105
for {
106106
time.Sleep(5 * time.Minute)
107107
// this will not work, since token will be invalid after 5 minutes
108-
projects, _, err := client.IamProjectApi.IamProjectList(ctx, nil)
108+
projects, _, err := client.IamProjectApi.IamProjectList(ctx).Execute()
109109
// [...]
110110
}
111111
}
@@ -115,7 +115,7 @@ func main() {
115115

116116
```go
117117
// [...]
118-
projects, _, err := client.IamProjectApi.IamProjectList(provider.Ctx(), nil)
118+
projects, _, err := client.IamProjectApi.IamProjectList(provider.Ctx()).Execute()
119119
```
120120

121121
### "Prefer" header

src/generators/go/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const generateGoClient = async (
1515
const generator = "go";
1616

1717
await execute(
18-
`yarn openapi-generator-cli generate -i ${openapiFile} -c ${config} -g ${generator} -o ${outputDir}`
18+
`yarn openapi-generator-cli generate --git-user-id "hyperonecom" --git-repo-id "h1-client-go" -i ${openapiFile} -c ${config} -g ${generator} -o ${outputDir}`
1919
);
2020

2121
const contextFileLocation = join(__dirname, "_passport_context.go");

src/generators/python/_README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ library installation description
2525
Then import it and use it as a token provider in your code:
2626

2727
```python
28-
from <credentials library name> import get_passport_credentials_helper
29-
from <client library name> import ApiClient, Configuration
28+
from credentials import get_passport_credentials_helper
29+
from h1 import ApiClient, Configuration
3030

3131
provider = get_passport_credentials_helper() # you can optionally pass passport file location
3232
cfg = Configuration()
@@ -42,8 +42,9 @@ Configuration object allows you to use choosen API client.
4242
Example:
4343

4444
```python
45-
from <credentials library name> import get_passport_credentials_helper
46-
from <client library name> import Configuration, ApiClient, IamProjectApi
45+
from credentials import get_passport_credentials_helper
46+
from h1 import Configuration, ApiClient
47+
from h1.api.iam_project_api import IamProjectApi
4748

4849
provider = get_passport_credentials_helper()
4950
cfg = Configuration()
@@ -73,7 +74,8 @@ api_client = ApiClient(cfg, header_name="prefer", header_value="respond-async,wa
7374
Full example:
7475

7576
```python
76-
from <client library name> import Configuration, ApiClient, IamProjectApi
77+
from h1 import Configuration, ApiClient
78+
from h1.api.iam_project_api import IamProjectApi
7779

7880
cfg = Configuration()
7981
api_client = ApiClient(cfg, header_name="prefer", header_value="respond-async,wait=86400")

src/generators/python/config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
packageName: h1-client
2-
projectName: h1
1+
packageName: h1
2+
projectName: h1-client

src/generators/python/index.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,52 @@
11
import { join } from "path";
2+
import { readFile, writeFile } from "fs/promises";
3+
24
import { execute } from "../../utils/shellUtils";
35
import { copyLicense } from "../../utils/licenseUtils";
46
import { fixPathsInReplacedReadme } from "../../utils/fileUtils";
57

8+
const dropObjectPattern = (s: any) => {
9+
if (!s.properties) return;
10+
for (const property of Object.values(s.properties)) {
11+
const p: any = property;
12+
delete p.pattern;
13+
if (p.items) {
14+
dropObjectPattern(p.items)
15+
}
16+
}
17+
}
18+
619
export const generatePythonClient = async (
720
openapiFile: string,
821
outputDir: string
922
): Promise<void> => {
1023
const config = join(__dirname, "config.yaml");
1124
const generator = "python";
12-
25+
const specification = JSON.parse(await readFile(openapiFile, {
26+
encoding: 'utf-8'
27+
}));
28+
// python does not pass validation our regexp
29+
// example value: '.components.schemas.iam_project_policy_create.properties.resource'
30+
for (const schema of Object.values(specification.components.schemas)) {
31+
const s: any = schema;
32+
dropObjectPattern(s);
33+
}
34+
// example value: .paths["/iam/project/{projectId}/policy"].get.parameters
35+
for (const endpoint of Object.values(specification.paths)) {
36+
const e: any = endpoint;
37+
for (const operation of Object.values(e)) {
38+
const o: any = operation;
39+
if (!o.parameters) continue;
40+
for (const parameter of o.parameters) {
41+
const p: any = parameter;
42+
if (!p.schema) continue;
43+
delete p.schema.pattern;
44+
}
45+
}
46+
}
47+
await writeFile(openapiFile, JSON.stringify(specification, null, 4));
1348
await execute(
14-
`yarn openapi-generator-cli generate -i ${openapiFile} -c ${config} -g ${generator} -o ${outputDir}`
49+
`yarn openapi-generator-cli generate --git-user-id "hyperonecom" --git-repo-id "h1-client-python" -i ${openapiFile} -c ${config} -g ${generator} -o ${outputDir}`
1550
);
1651

1752
await execute("mv README.md docs/README.md", outputDir);
@@ -32,7 +67,7 @@ export const generatePythonClient = async (
3267
const replacementReadmeLocation = join(__dirname, "_README.md");
3368
await execute(`cp ${replacementReadmeLocation} README.md`, outputDir);
3469

35-
await execute("pip install .", outputDir);
70+
await execute("python -m pip install .", outputDir);
3671

3772
await copyLicense("MIT", outputDir);
3873
};

src/generators/ts/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const generateTSClient = async (
1111
const generator = "typescript-axios";
1212

1313
await execute(
14-
`yarn openapi-generator-cli generate -i ${openapiFile} -c ${config} -g ${generator} -o ${outputDir}`
14+
`yarn openapi-generator-cli generate --git-user-id "hyperonecom" --git-repo-id "h1-client-js" -i ${openapiFile} -c ${config} -g ${generator} -o ${outputDir}`
1515
);
1616
await execute(
1717
"yarn add -D typedoc@0.19.2 typedoc-plugin-markdown@3.0.3",

src/utils/shellUtils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { exec, ExecOptions } from "child_process";
22

3+
const TEN_MEGA_BYTE = 1024 * 1024 * 10;
4+
35
export const execute = async (cmd: string, cwd?: string) => {
46
const options: ExecOptions = {};
57
options.cwd = cwd;
8+
options.maxBuffer = TEN_MEGA_BYTE;
69

710
return new Promise((resolve, reject) => {
811
exec(cmd, options, (err, stdout, stderr) => {
912
if (err) {
13+
console.log(`Command '${cmd}' execution failed`)
1014
reject(err);
1115
} else {
1216
console.log(stdout);

tests/go/_api_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestPassportContext(t *testing.T) {
1313
p, err := GetPassportContextProvider("", context.Background())
1414
require.NoError(t, err)
1515

16-
_, response, err := c.IamProjectApi.IamProjectList(p.Ctx(), nil)
16+
_, response, err := c.IamProjectApi.IamProjectList(p.Ctx()).Execute()
1717
require.NoError(t, err)
1818

1919
if response.StatusCode != 200 {
@@ -30,7 +30,7 @@ func TestPassportContextWithError(t *testing.T) {
3030
ctx, err := p.CtxWithError()
3131
require.NoError(t, err)
3232

33-
_, response, err := c.IamProjectApi.IamProjectList(ctx, nil)
33+
_, response, err := c.IamProjectApi.IamProjectList(ctx).Execute()
3434
require.NoError(t, err)
3535

3636
if response.StatusCode != 200 {

tests/python/execute.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
pip install -r ../../clients/python/requirements.txt
2-
pip install git+git://github.com/kuskoman/h1-credentials-helper-python.git
1+
#!/bin/sh
2+
set -eux
3+
python -m pip install -r ../../clients/python/requirements.txt
4+
python -m pip install git+git://github.com/hyperonecom/h1-credentials-helper-python.git
35

46
python test.py

0 commit comments

Comments
 (0)