Skip to content

Commit

Permalink
Merge branch 'main' into compose-module
Browse files Browse the repository at this point in the history
* main:
  chore: generate dependabot configs for examples (testcontainers#654)
  chore: format dependabot using go-yaml indents (testcontainers#658)
  chore(deps): bump github.com/docker/compose/v2 from 2.12.2 to 2.14.0 (testcontainers#657)
  chore(deps): bump github.com/stretchr/testify from 1.8.0 to 1.8.1 (#583)
  chore(deps): bump github.com/cenkalti/backoff/v4 from 4.1.3 to 4.2.0 (testcontainers#656)
  chore: remove quotes from dependabot updates (testcontainers#655)
  fix: do not create garbage in examples nav bar (testcontainers#652)
  chore(deps): bump github.com/containerd/containerd from 1.6.8 to 1.6.10 (#621)
  chore(deps): bump golang.org/x/sys from 0.1.0 to 0.3.0 (testcontainers#651)
  • Loading branch information
mdelapenya committed Dec 5, 2022
2 parents 6a62857 + 65c6fd8 commit 0f906e5
Show file tree
Hide file tree
Showing 12 changed files with 440 additions and 108 deletions.
78 changes: 66 additions & 12 deletions .github/dependabot.yml
@@ -1,14 +1,68 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: "/e2e"
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /e2e
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/cockroachdb
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/datastore
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/firestore
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/nginx
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/pubsub
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/pulsar
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/redis
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/spanner
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
- package-ecosystem: gomod
directory: /examples/toxiproxy
schedule:
interval: daily
open-pull-requests-limit: 3
rebase-strategy: disabled
115 changes: 115 additions & 0 deletions examples/dependabot.go
@@ -0,0 +1,115 @@
package main

import (
"io/ioutil"
"path/filepath"

"gopkg.in/yaml.v3"
)

type Updates []Update

type DependabotConfig struct {
Version int `yaml:"version"`
Updates Updates `yaml:"updates"`
}

type Schedule struct {
Interval string `yaml:"interval"`
}

type Update struct {
PackageEcosystem string `yaml:"package-ecosystem"`
Directory string `yaml:"directory"`
Schedule Schedule `yaml:"schedule"`
OpenPullRequestsLimit int `yaml:"open-pull-requests-limit"`
RebaseStrategy string `yaml:"rebase-strategy"`
}

func NewUpdate(example string) Update {
return Update{
Directory: "/examples/" + example,
OpenPullRequestsLimit: 3,
PackageEcosystem: "gomod",
RebaseStrategy: "disabled",
Schedule: Schedule{
Interval: "daily",
},
}
}

// Len is the number of elements in the collection.
func (u Updates) Len() int {
return len(u)
}

// Less reports whether the element with index i
// must sort before the element with index j.
//
// If both Less(i, j) and Less(j, i) are false,
// then the elements at index i and j are considered equal.
// Sort may place equal elements in any order in the final result,
// while Stable preserves the original input order of equal elements.
//
// Less must describe a transitive ordering:
// - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well.
// - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.
//
// Note that floating-point comparison (the < operator on float32 or float64 values)
// is not a transitive ordering when not-a-number (NaN) values are involved.
// See Float64Slice.Less for a correct implementation for floating-point values.
func (u Updates) Less(i, j int) bool {
return u[i].Directory < u[j].Directory
}

// Swap swaps the elements with indexes i and j.
func (u Updates) Swap(i, j int) {
u[i], u[j] = u[j], u[i]
}

func getDependabotConfigFile(rootDir string) string {
return filepath.Join(rootDir, ".github", "dependabot.yml")
}

func getDependabotUpdates() ([]Update, error) {
parent, err := getRootDir()
if err != nil {
return nil, err
}

config, err := readDependabotConfig(parent)
if err != nil {
return nil, err
}

return config.Updates, nil
}

func readDependabotConfig(rootDir string) (*DependabotConfig, error) {
configFile := getDependabotConfigFile(rootDir)

file, err := ioutil.ReadFile(configFile)
if err != nil {
return nil, err
}

config := &DependabotConfig{}

err = yaml.Unmarshal(file, config)
if err != nil {
return nil, err
}

return config, nil
}

func writeDependabotConfig(rootDir string, config *DependabotConfig) error {
data, err := yaml.Marshal(config)
if err != nil {
return err
}

file := getDependabotConfigFile(rootDir)

return ioutil.WriteFile(file, data, 0777)
}
81 changes: 81 additions & 0 deletions examples/dependabot_test.go
@@ -0,0 +1,81 @@
package main

import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGetDependabotConfigFile(t *testing.T) {
tmp := t.TempDir()

rootDir := filepath.Join(tmp, "testcontainers-go")
githubDir := filepath.Join(rootDir, ".github")
cfgFile := filepath.Join(githubDir, "dependabot.yml")
err := os.MkdirAll(githubDir, 0777)
require.NoError(t, err)

err = os.WriteFile(cfgFile, []byte{}, 0777)
require.NoError(t, err)

file := getDependabotConfigFile(rootDir)
require.NotNil(t, file)

assert.True(t, strings.HasSuffix(file, filepath.Join("testcontainers-go", ".github", "dependabot.yml")))
}

func TestReadDependabotConfig(t *testing.T) {
tmp := t.TempDir()

rootDir := filepath.Join(tmp, "testcontainers-go")
githubDir := filepath.Join(rootDir, ".github")
err := os.MkdirAll(githubDir, 0777)
require.NoError(t, err)

err = copyInitialDependabotConfig(t, rootDir)
require.NoError(t, err)

config, err := readDependabotConfig(rootDir)
require.NoError(t, err)
require.NotNil(t, config)

assert.Greater(t, len(config.Updates), 0)
}

func TestExamplesHasDependabotEntry(t *testing.T) {
examples, err := getExamples()
require.NoError(t, err)
exampleUpdates, err := getDependabotUpdates()
require.NoError(t, err)

// we have to exclude the main and e2e modules from the examples updates
assert.Equal(t, len(exampleUpdates)-2, len(examples))

// all example modules exist in the dependabot updates
for _, example := range examples {
found := false
for _, exampleUpdate := range exampleUpdates {
dependabotDir := "/examples/" + strings.ToLower(example.Name())

if dependabotDir == exampleUpdate.Directory {
found = true
continue
}
}
assert.True(t, found, "example %s is not present in the dependabot updates", example.Name())
}
}

func copyInitialDependabotConfig(t *testing.T, tmpDir string) error {
projectDir, err := getRootDir()
require.NoError(t, err)

initialConfig, err := readDependabotConfig(projectDir)
require.NoError(t, err)

return writeDependabotConfig(tmpDir, initialConfig)
}
68 changes: 57 additions & 11 deletions examples/main.go
Expand Up @@ -135,6 +135,58 @@ func generate(example Example, rootDir string) error {
}
}

// update examples in mkdocs
err = generateMkdocs(rootDir, exampleLower)
if err != nil {
return err
}

// update examples in dependabot
err = generateDependabotUpdates(rootDir, exampleLower)
if err != nil {
return err
}

fmt.Println("Please go to", example.Lower(), "directory and execute 'go mod tidy' to synchronize the dependencies")
fmt.Println("Commit the modified files and submit a pull request to include them into the project")
fmt.Println("Thanks!")
return nil
}

func generateDependabotUpdates(rootDir string, exampleLower string) error {
// update examples in dependabot
dependabotConfig, err := readDependabotConfig(rootDir)
if err != nil {
return err
}

dependabotExampleUpdates := dependabotConfig.Updates

// make sure the main module is the first element in the list of examples
// and the e2e module is the second element
exampleUpdates := make(Updates, len(dependabotExampleUpdates)-2)
j := 0

for _, exampleUpdate := range dependabotExampleUpdates {
// filter out the index.md file
if exampleUpdate.Directory != "/" && exampleUpdate.Directory != "/e2e" {
exampleUpdates[j] = exampleUpdate
j++
}
}

exampleUpdates = append(exampleUpdates, NewUpdate(exampleLower))
sort.Sort(exampleUpdates)

// prepend the main and e2e modules
exampleUpdates = append([]Update{dependabotExampleUpdates[0], dependabotExampleUpdates[1]}, exampleUpdates...)

dependabotConfig.Updates = exampleUpdates

return writeDependabotConfig(rootDir, dependabotConfig)
}

func generateMkdocs(rootDir string, exampleLower string) error {
// update examples in mkdocs
mkdocsConfig, err := readMkdocsConfig(rootDir)
if err != nil {
Expand All @@ -145,11 +197,13 @@ func generate(example Example, rootDir string) error {

// make sure the index.md is the first element in the list of examples in the nav
examplesNav := make([]string, len(mkdocsExamplesNav)-1)
j := 0

for _, exampleNav := range mkdocsExamplesNav {
// filter out the index.md file
if !strings.HasSuffix("index.md", exampleNav) {
examplesNav = append(examplesNav, exampleNav)
if !strings.HasSuffix(exampleNav, "index.md") {
examplesNav[j] = exampleNav
j++
}
}

Expand All @@ -161,13 +215,5 @@ func generate(example Example, rootDir string) error {

mkdocsConfig.Nav[3].Examples = examplesNav

err = writeMkdocsConfig(rootDir, mkdocsConfig)
if err != nil {
return err
}

fmt.Println("Please go to", example.Lower(), "directory and execute 'go mod tidy' to synchronize the dependencies")
fmt.Println("Commit the modified files and submit a pull request to include them into the project")
fmt.Println("Thanks!")
return nil
return writeMkdocsConfig(rootDir, mkdocsConfig)
}

0 comments on commit 0f906e5

Please sign in to comment.