Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create tool to pin locally replaced modules to latest trunk commit #15290

Merged
merged 55 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
8192867
Add separate require line for root module.
chainchad Nov 15, 2024
8f5b382
Create tool to update certain module deps to pin to latest trunk commit
chainchad Nov 18, 2024
81968a8
Create new makefile target to use new tool to update module deps to pin
chainchad Nov 18, 2024
1a1d9d3
Add support for additional local module replacements
chainchad Nov 19, 2024
1e3811c
Add support for automatically updating locally replaced modules
chainchad Nov 19, 2024
cb70261
Update make target to auto update locally replaced modules
chainchad Nov 19, 2024
87e940f
Run go fmt
chainchad Nov 19, 2024
5b269ca
Add ci workflow
chainchad Nov 19, 2024
7165514
Add test for config sourcing
chainchad Nov 19, 2024
979b0c1
Add build step to CI
chainchad Nov 21, 2024
99ad50b
Make it gomods compatible and not a separate module
chainchad Nov 21, 2024
b5dc9fb
Remove mocked sys op
chainchad Nov 21, 2024
683ae99
Only build during dedicated CI workflow.
chainchad Nov 21, 2024
7358367
Refactor
chainchad Nov 22, 2024
e47bdd1
Remove unused function
chainchad Nov 22, 2024
eb28c01
Use psuedo version from go lib
chainchad Nov 22, 2024
9c22ee2
Upate pseudo-versions for local replaces to latest time/sha
chainchad Nov 22, 2024
dd2390c
Cleanup
chainchad Nov 22, 2024
b9dc91a
Update bin path
chainchad Nov 22, 2024
f8fbc9b
Fix README
chainchad Nov 22, 2024
bf12443
Allow custom org/repo name and apply better validation to git cmds
chainchad Nov 23, 2024
94a6f0f
Merge branch 'develop' into re-3199/updater-root-module-go.mod-ref
chainchad Nov 23, 2024
da8130b
Address linting
chainchad Nov 24, 2024
c37c8f5
Address linting 2
chainchad Nov 24, 2024
5f60e09
Ignore gosec lint for execs
chainchad Nov 24, 2024
f17e56d
Update Make target name
chainchad Nov 25, 2024
b40b4f6
Avoid merge queue for build
chainchad Nov 25, 2024
6c6c8be
Rename tool
chainchad Nov 25, 2024
9ec395a
Point to new binary
chainchad Nov 25, 2024
1b44e1c
Merge branch 'develop' into re-3199/updater-root-module-go.mod-ref
chainchad Nov 27, 2024
72c98a4
Remove unused lint directive
chainchad Nov 27, 2024
938baea
Ran goimports -local
chainchad Nov 27, 2024
a2b1834
Cleanup and refactor
chainchad Nov 29, 2024
daa12bc
Format
chainchad Nov 29, 2024
98cbd75
Update local requires
chainchad Nov 29, 2024
224281a
Merge branch 'develop' into re-3199/updater-root-module-go.mod-ref
chainchad Nov 29, 2024
df62848
Add more refactoring
chainchad Nov 29, 2024
08dfe5d
Merge branch 're-3199/updater-root-module-go.mod-ref' of https://gith…
chainchad Nov 29, 2024
9af0d0c
Clarify readme
chainchad Nov 29, 2024
2f48978
Update comment
chainchad Nov 29, 2024
63638a7
Update README about usage and gomods
chainchad Dec 6, 2024
88c1b73
Add gomods as a dep for new target
chainchad Dec 6, 2024
941e003
Move pre-compiled regular expressions into vars
chainchad Dec 6, 2024
833427a
Fix formatting
chainchad Dec 6, 2024
1d9497a
Remove remnant from refactor
chainchad Dec 6, 2024
cbda2d1
Move validation of git input into config
chainchad Dec 6, 2024
b53cec3
Merge branch 'develop' into re-3199/updater-root-module-go.mod-ref
chainchad Dec 6, 2024
b80bcfa
Run `make gomodslocalupdate`
chainchad Dec 6, 2024
89dd71b
Appease linter
chainchad Dec 7, 2024
25864d1
Remove unnecessary inline comments
chainchad Dec 7, 2024
4be3d94
Use a better name for git exec
chainchad Dec 9, 2024
d0740ce
Merge branch 'develop' into re-3199/updater-root-module-go.mod-ref
chainchad Dec 10, 2024
ccf2842
Remove unnecessary build workflow
chainchad Dec 10, 2024
737c8db
Merge branch 're-3199/updater-root-module-go.mod-ref' of https://gith…
chainchad Dec 10, 2024
45efc1a
Merge branch 'develop' into re-3199/updater-root-module-go.mod-ref
chainchad Dec 10, 2024
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
4 changes: 4 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ gomod: ## Ensure chainlink's go dependencies are installed.
gomodtidy: gomods ## Run go mod tidy on all modules.
gomods tidy

.PHONY: gomodrequiredupdater
gomodrequiredupdater: ## Update go.mod files containing certain required dependencies to use latest psuedo-versions from trunk.
cd tools/gomod-required-updater && go run cmd/gomod-required-updater/main.go -update-org-modules -root ../..
chainchad marked this conversation as resolved.
Show resolved Hide resolved

.PHONY: docs
docs: ## Install and run pkgsite to view Go docs
go install golang.org/x/pkgsite/cmd/pkgsite@latest
Expand Down
9 changes: 7 additions & 2 deletions core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ replace github.com/smartcontractkit/chainlink/v2 => ../../

replace github.com/smartcontractkit/chainlink/deployment => ../../deployment

// Using a separate `require` here to avoid surrounding line changes
// creating potential merge conflicts.
require (
github.com/smartcontractkit/chainlink/deployment v0.0.0-20241119120536-03115e80382d
github.com/smartcontractkit/chainlink/v2 v2.0.0-20241119120536-03115e80382d
)

require (
github.com/docker/docker v27.3.1+incompatible
github.com/docker/go-connections v0.5.0
Expand All @@ -25,8 +32,6 @@ require (
github.com/shopspring/decimal v1.4.0
github.com/smartcontractkit/chainlink-automation v0.8.1
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241114134822-aadff98ef068
github.com/smartcontractkit/chainlink/deployment v0.0.0-00010101000000-000000000000
github.com/smartcontractkit/chainlink/v2 v2.14.0-mercury-20240807.0.20241106193309-5560cd76211a
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
Expand Down
5 changes: 4 additions & 1 deletion deployment/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ go 1.22.8
// Make sure we're working with the latest chainlink libs
replace github.com/smartcontractkit/chainlink/v2 => ../

// Using a separate inline `require` here to avoid surrounding line changes
// creating potential merge conflicts.
require github.com/smartcontractkit/chainlink/v2 v2.0.0-20241119120536-03115e80382d

require (
github.com/Khan/genqlient v0.7.0
github.com/Masterminds/semver/v3 v3.3.0
Expand All @@ -26,7 +30,6 @@ require (
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241114134822-aadff98ef068
github.com/smartcontractkit/chainlink-protos/job-distributor v0.4.0
github.com/smartcontractkit/chainlink-testing-framework/lib v1.50.13
github.com/smartcontractkit/chainlink/v2 v2.0.0-00010101000000-000000000000
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
github.com/stretchr/testify v1.9.0
github.com/test-go/testify v1.1.4
Expand Down
9 changes: 7 additions & 2 deletions integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ replace github.com/smartcontractkit/chainlink/v2 => ../

replace github.com/smartcontractkit/chainlink/deployment => ../deployment

// Using a separate `require` here to avoid surrounding line changes
// creating potential merge conflicts.
require (
github.com/smartcontractkit/chainlink/deployment v0.0.0-20241119120536-03115e80382d
github.com/smartcontractkit/chainlink/v2 v2.0.0-20241119120536-03115e80382d
)

require (
dario.cat/mergo v1.0.1
github.com/AlekSi/pointer v1.1.0
Expand Down Expand Up @@ -44,8 +51,6 @@ require (
github.com/smartcontractkit/chainlink-testing-framework/lib/grafana v1.50.0
github.com/smartcontractkit/chainlink-testing-framework/seth v1.50.5
github.com/smartcontractkit/chainlink-testing-framework/wasp v1.50.2
github.com/smartcontractkit/chainlink/deployment v0.0.0-00010101000000-000000000000
github.com/smartcontractkit/chainlink/v2 v2.14.0-mercury-20240807.0.20241106193309-5560cd76211a
github.com/smartcontractkit/libocr v0.0.0-20241007185508-adbe57025f12
github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.9.0
Expand Down
11 changes: 8 additions & 3 deletions integration-tests/load/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ replace github.com/smartcontractkit/chainlink/deployment => ../../deployment

replace github.com/smartcontractkit/chainlink/integration-tests => ../

// Using a separate `require` here to avoid surrounding line changes
// creating potential merge conflicts.
require (
github.com/smartcontractkit/chainlink/deployment v0.0.0-20241119120536-03115e80382d
github.com/smartcontractkit/chainlink/integration-tests v0.0.0-20241119120536-03115e80382d
github.com/smartcontractkit/chainlink/v2 v2.0.0-20241119120536-03115e80382d
)

require (
github.com/K-Phoen/grabana v0.22.2
github.com/ethereum/go-ethereum v1.14.11
Expand All @@ -21,9 +29,6 @@ require (
github.com/smartcontractkit/chainlink-testing-framework/lib v1.50.13
github.com/smartcontractkit/chainlink-testing-framework/seth v1.50.5
github.com/smartcontractkit/chainlink-testing-framework/wasp v1.50.2
github.com/smartcontractkit/chainlink/deployment v0.0.0-00010101000000-000000000000
github.com/smartcontractkit/chainlink/integration-tests v0.0.0-20241030133659-9ec788e78b4f
github.com/smartcontractkit/chainlink/v2 v2.14.0-mercury-20240807.0.20241106193309-5560cd76211a
github.com/smartcontractkit/tdh2/go/tdh2 v0.0.0-20241009055228-33d0c0bf38de
github.com/stretchr/testify v1.9.0
github.com/wiremock/go-wiremock v1.9.0
Expand Down
82 changes: 82 additions & 0 deletions tools/gomod-required-updater/README.md
chainchad marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# gomod-required-updater

Updates required module versions in go.mod files to match the latest git SHA from a remote branch.

## Features

- Update modules to latest SHA from specified branch
- Auto-detect and update modules with local replace directives
- Update multiple modules at once
- Preview changes with dry run mode
- Configure via TOML or command line flags

## Configuration

Optional TOML config.

```toml
# List of modules to update
modules = [
"github.com/smartcontractkit/chainlink/v2"
chainchad marked this conversation as resolved.
Show resolved Hide resolved
]
```

Command Line Flags:

```shell
Required (one of):
-module Module to update (can be specified multiple times)
-config Path to TOML config file
-update-org-modules Auto-detect and update modules with local replaces
chainchad marked this conversation as resolved.
Show resolved Hide resolved

Optional:
-repo-remote Git remote to use (default: origin)
-branch-trunk Branch to get SHA from (default: develop)
-root Root path for searching go.mod files
-dry-run Preview changes without applying them
```

## Installation

The installed binary will be placed in your `$GOPATH/bin` directory. Make sure this directory is in your system's PATH to run the command from anywhere.

```shell
go install github.com/smartcontractkit/chainlink/tools/gomod-required-updater
```

## Usage Examples

Update Specific Modules:

```shell
# Update single module
gomod-required-updater -module github.com/org/repo`

# Update multiple modules
gomod-required-updater -module github.com/org/repo1 -module github.com/org/repo2

# Update using config file
gomod-required-updater -config modules.toml

# Using different remote/branch
gomod-required-updater -module github.com/org/repo -repo-remote upstream -branch-trunk main
```

Auto-detect and Update Local Modules:

```shell
# Update all local modules that have replace directives
gomod-required-updater -update-org-modules

# Preview changes first
gomod-required-updater -update-org-modules -dry-run
```

## Notes

- When using multiple module sources, precedence is:
1. Command line `-module` flags
2. Config file modules
3. Auto-detected modules via `-update-org-modules` flag
chainchad marked this conversation as resolved.
Show resolved Hide resolved
- Use the `-dry-run` flag to safely preview changes
- Local replace directives are preserved during updates
49 changes: 49 additions & 0 deletions tools/gomod-required-updater/cmd/gomod-required-updater/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"log"
"os"

"github.com/smartcontractkit/chainlink/tools/gomod-required-updater/internal/updater"
)

var version = "dev"
var usage = `gomod-required-updater version %s

Usage:
gomod-required-updater [flags]

Examples:
# Update modules specified in config file
gomod-required-updater -config modules.toml

# Update specific modules
gomod-required-updater -module github.com/org/repo1 -module github.com/org/repo2

# Dry run with specific modules
gomod-required-updater -dry-run -module github.com/org/repo1
`

func main() {
cfg, err := updater.ParseFlags(os.Args[1:], version)
if err != nil {
fmt.Fprintf(os.Stderr, usage, version)
log.Fatal(err)
}

if cfg.ShowVersion {
fmt.Printf("gomod-required-updater version %s\n", version)
os.Exit(0)
}

u := updater.New(
updater.NewGitOperator(),
updater.NewSystemOperator(),
cfg,
)

if err := u.Run(); err != nil {
log.Fatal(err)
}
}
9 changes: 9 additions & 0 deletions tools/gomod-required-updater/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/smartcontractkit/chainlink/tools/gomod-required-updater

go 1.23

require (
github.com/BurntSushi/toml v1.4.0
golang.org/x/mod v0.22.0

)
4 changes: 4 additions & 0 deletions tools/gomod-required-updater/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
113 changes: 113 additions & 0 deletions tools/gomod-required-updater/internal/updater/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package updater

import (
"flag"
"fmt"
"strings"

"github.com/BurntSushi/toml"
)

type TOMLConfig struct {
Modules []string `toml:"modules"`
}

type Config struct {
chainchad marked this conversation as resolved.
Show resolved Hide resolved
ModulesToUpdate []string
RepoRemote string
BranchTrunk string
DryRun bool
ConfigFile string
RootPath string
ShowVersion bool
modulesSource string
UpdateOrgModules bool // Update modules from same org/repo with local replaces
OrgName string // GitHub organization name
RepoName string // Repository name
}

func (c *Config) Validate() error {
if !c.ShowVersion {
// Skip module validation if using UpdateOrgModules
if !c.UpdateOrgModules && len(c.ModulesToUpdate) == 0 {
return fmt.Errorf("%w: no modules specified to update (use -module flag or config file)", ErrInvalidConfig)
}
if c.RepoRemote == "" {
return fmt.Errorf("%w: repo remote cannot be empty", ErrInvalidConfig)
}
if c.BranchTrunk == "" {
return fmt.Errorf("%w: branch trunk cannot be empty", ErrInvalidConfig)
}
}
return nil
}

func ParseFlags(args []string, version string) (*Config, error) {
flags := flag.NewFlagSet("gomod-required-updater", flag.ContinueOnError)

cfg := &Config{}
var cliModules arrayFlags // Define custom flag type for multiple -module flags

flags.StringVar(&cfg.RepoRemote, "repo-remote", "origin", "The name of the repo remote")
flags.StringVar(&cfg.BranchTrunk, "branch-trunk", "develop", "The name of the trunk branch")
flags.BoolVar(&cfg.DryRun, "dry-run", false, "Print what would be done without making changes")
flags.StringVar(&cfg.ConfigFile, "config", "", "Path to TOML config file (optional if using -module)")
flags.StringVar(&cfg.RootPath, "root", ".", "Root path to start scanning for go.mod files")
flags.BoolVar(&cfg.ShowVersion, "version", false, "Show version information")
flags.Var(&cliModules, "module", "Module to update (can be specified multiple times)")
flags.BoolVar(&cfg.UpdateOrgModules, "update-org-modules", false, "Update modules from same org/repo that have local replace directives")

if err := flags.Parse(args); err != nil {
return nil, err
}

// CLI modules take precedence
if len(cliModules) > 0 {
cfg.ModulesToUpdate = cliModules
cfg.modulesSource = "command line"
} else if cfg.ConfigFile != "" {
// Only load config file if no CLI modules specified
modules, err := loadTOMLConfig(cfg.ConfigFile)
if err != nil {
return nil, fmt.Errorf("failed to load config: %w", err)
}
cfg.ModulesToUpdate = modules
cfg.modulesSource = "config file"
}

if cfg.UpdateOrgModules {
gitOp := NewGitOperator()
org, repo, err := gitOp.GetRepoInfo(cfg.RepoRemote)
if err != nil {
return nil, fmt.Errorf("failed to get repo info: %w", err)
}
cfg.OrgName = org
cfg.RepoName = repo
}

if err := cfg.Validate(); err != nil {
return nil, err
}

return cfg, nil
}

// arrayFlags allows for repeated flag values
type arrayFlags []string

func (i *arrayFlags) String() string {
return strings.Join(*i, ", ")
}

func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}

func loadTOMLConfig(path string) ([]string, error) {
var cfg TOMLConfig
if _, err := toml.DecodeFile(path, &cfg); err != nil {
return nil, fmt.Errorf("failed to decode TOML: %w", err)
}
return cfg.Modules, nil
}
Loading
Loading