Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
branches: [main]

env:
GOLANGCI_LINT_VERSION: v2.9.0
GOLANGCI_LINT_VERSION: v2.11.4

permissions:
contents: read
Expand Down
119 changes: 119 additions & 0 deletions .github/workflows/release-code-samples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
name: Release Code Samples

on:
release:
types:
- published

concurrency:
group: release-code-samples-${{ github.event.release.tag_name }}
cancel-in-progress: true

permissions:
contents: read

jobs:
sync-go-code-samples:
name: Sync Go code samples
runs-on: ubuntu-latest
env:
TARGET_REPOSITORY: sumup/sumup-developer
TARGET_BRANCH: automation/go-code-samples
TARGET_FILE: src/codesamples/go.json
steps:
- name: Checkout source code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: refs/tags/${{ github.event.release.tag_name }}
persist-credentials: false

- name: Install Go
uses: actions/setup-go@4a3601121dd01d1626a1e23e37211e3254c1c06c # v6.4.0
with:
go-version-file: internal/cmd/codegen/go.mod

- name: Create GitHub App token
id: app-token
uses: actions/create-github-app-token@1b10c78c7865c340bc4f6099eb2f838309f1e8c3 # v3.1.1
with:
app-id: ${{ secrets.SUMUP_BOT_APP_ID }}
private-key: ${{ secrets.SUMUP_BOT_PRIVATE_KEY }}
owner: sumup
repositories: sumup-developer

- name: Checkout target repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
repository: ${{ env.TARGET_REPOSITORY }}
ref: main
token: ${{ steps.app-token.outputs.token }}
path: sumup-developer
persist-credentials: true

- name: Get GitHub App User ID
id: get-user-id
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"

- name: Configure git
run: |
git config --global user.name '${{ steps.app-token.outputs.app-slug }}[bot]'
git config --global user.email '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com'

- name: Prepare target branch
working-directory: sumup-developer
run: git checkout -B "${{ env.TARGET_BRANCH }}" origin/main

- name: Generate Go code samples
working-directory: internal/cmd/codegen
run: |
mkdir -p "../../../sumup-developer/$(dirname "${{ env.TARGET_FILE }}")"
go run . samples ../../../openapi.json > "../../../sumup-developer/${{ env.TARGET_FILE }}"

- name: Commit generated samples
id: commit
working-directory: sumup-developer
run: |
git add "${{ env.TARGET_FILE }}"
if git diff --cached --quiet; then
echo "changed=false" >> "$GITHUB_OUTPUT"
exit 0
fi

git commit -m "chore: update Go code samples for ${{ github.event.release.tag_name }}"
echo "changed=true" >> "$GITHUB_OUTPUT"

- name: Push branch
if: steps.commit.outputs.changed == 'true'
working-directory: sumup-developer
run: git push --force-with-lease origin "${{ env.TARGET_BRANCH }}"

- name: Create or update pull request
if: steps.commit.outputs.changed == 'true'
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
run: |
head_ref="sumup:${{ env.TARGET_BRANCH }}"
pr_url="$(gh pr list \
--repo "${{ env.TARGET_REPOSITORY }}" \
--head "$head_ref" \
--base main \
--state open \
--json url \
--jq '.[0].url')"

if [ -n "$pr_url" ]; then
gh pr edit "$pr_url" \
--repo "${{ env.TARGET_REPOSITORY }}" \
--title "chore: update Go code samples" \
--body "Updates \`${{ env.TARGET_FILE }}\` from \`${{ github.repository }}\` release \`${{ github.event.release.tag_name }}\`."
exit 0
fi

gh pr create \
--repo "${{ env.TARGET_REPOSITORY }}" \
--base main \
--head "${{ env.TARGET_BRANCH }}" \
--title "chore: update Go code samples" \
--body "Updates \`${{ env.TARGET_FILE }}\` from \`${{ github.repository }}\` release \`${{ github.event.release.tag_name }}\`."
27 changes: 2 additions & 25 deletions internal/cmd/codegen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"os"
"os/exec"

"github.com/pb33f/libopenapi"
"github.com/urfave/cli/v2"

"github.com/sumup/sumup-go/internal/cmd/codegen/pkg/builder"
)

func Generate() *cli.Command {
Expand All @@ -23,33 +20,13 @@ func Generate() *cli.Command {
return fmt.Errorf("empty argument, path to openapi specs expected")
}

specs := c.Args().First()

if err := os.MkdirAll(out, os.ModePerm); err != nil {
return fmt.Errorf("create output directory %q: %w", out, err)
}

spec, err := os.ReadFile(specs)
if err != nil {
return fmt.Errorf("read specs: %w", err)
}

doc, err := libopenapi.NewDocument(spec)
builder, err := loadBuilder(c.Args().First(), out)
if err != nil {
return fmt.Errorf("load openapi document: %w", err)
}

model, err := doc.BuildV3Model()
if err != nil {
return fmt.Errorf("build openapi v3 model: %w", err)
}

builder := builder.New(builder.Config{
Out: out,
})

if err := builder.Load(&model.Model); err != nil {
return fmt.Errorf("load spec: %w", err)
return err
}

if err := builder.Build(); err != nil {
Expand Down
37 changes: 37 additions & 0 deletions internal/cmd/codegen/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"fmt"
"os"

"github.com/pb33f/libopenapi"

"github.com/sumup/sumup-go/internal/cmd/codegen/pkg/builder"
)

func loadBuilder(specs, out string) (*builder.Builder, error) {
spec, err := os.ReadFile(specs)
if err != nil {
return nil, fmt.Errorf("read specs: %w", err)
}

doc, err := libopenapi.NewDocument(spec)
if err != nil {
return nil, fmt.Errorf("load openapi document: %w", err)
}

model, err := doc.BuildV3Model()
if err != nil {
return nil, fmt.Errorf("build openapi v3 model: %w", err)
}

b := builder.New(builder.Config{
Out: out,
})

if err := b.Load(&model.Model); err != nil {
return nil, fmt.Errorf("load spec: %w", err)
}

return b, nil
}
1 change: 1 addition & 0 deletions internal/cmd/codegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func App() *cli.App {
},
Commands: []*cli.Command{
Generate(),
Samples(),
},
}
}
Loading
Loading