Skip to content

Commit

Permalink
Create tool to pin locally replaced modules to latest trunk commit (#…
Browse files Browse the repository at this point in the history
…15290)

* Add separate require line for root module.

Separating it to avoid potential merge conflicts when surrounding lines change as we expect CI to update its psuedo version.

* Create tool to update certain module deps to pin to latest trunk commit

* Create new makefile target to use new tool to update module deps to pin

* Add support for additional local module replacements

* Add support for automatically updating locally replaced modules

* Update make target to auto update locally replaced modules

* Run go fmt

* Add ci workflow

* Add test for config sourcing

* Add build step to CI

* Make it gomods compatible and not a separate module

* Remove mocked sys op

* Only build during dedicated CI workflow.

Tests and linting should now be ran from the ci-core workflow.

* Refactor

* Remove unused function

* Use psuedo version from go lib

* Upate pseudo-versions for local replaces to latest time/sha

* Cleanup

* Update bin path

* Fix README

* Allow custom org/repo name and apply better validation to git cmds

* Address linting

* Address linting 2

* Ignore gosec lint for execs

* Update Make target name

* Avoid merge queue for build

* Rename tool

* Point to new binary

* Remove unused lint directive

* Ran goimports -local

* Cleanup and refactor

* Format

* Update local requires

* Add more refactoring

* Clarify readme

* Update comment

* Update README about usage and gomods

* Add gomods as a dep for new target

Co-authored-by: Jordan Krage <jmank88@gmail.com>

* Move pre-compiled regular expressions into vars

* Fix formatting

* Remove remnant from refactor

* Move validation of git input into config

* Run `make gomodslocalupdate`

* Appease linter

* Remove unnecessary inline comments

* Use a better name for git exec

* Remove unnecessary build workflow

---------

Co-authored-by: Jordan Krage <jmank88@gmail.com>
  • Loading branch information
chainchad and jmank88 authored Dec 10, 2024
1 parent 100d87d commit 5bdf5c2
Show file tree
Hide file tree
Showing 13 changed files with 985 additions and 8 deletions.
6 changes: 6 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ presubmit: ## Format go files and imports.
gomods: ## Install gomods
go install github.com/jmank88/gomods@v0.1.4

.PHONY: gomodslocalupdate
gomodslocalupdate: gomods ## Run gomod-local-update
go install ./tools/gomod-local-update/cmd/gomod-local-update
gomods -w gomod-local-update
gomods tidy

.PHONY: mockery
mockery: $(mockery) ## Install mockery.
go install github.com/vektra/mockery/v2@v2.46.3
Expand Down
9 changes: 7 additions & 2 deletions core/scripts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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-20241206210521-125d98cdaf66
github.com/smartcontractkit/chainlink/v2 v2.0.0-20241206210521-125d98cdaf66
)

require (
github.com/docker/docker v27.3.1+incompatible
github.com/docker/go-connections v0.5.0
Expand All @@ -27,8 +34,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.20241210192653-a9c706f99e83
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 @@ -7,6 +7,10 @@ toolchain go1.23.4
// 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-20241206210521-125d98cdaf66

require (
github.com/Khan/genqlient v0.7.0
github.com/Masterminds/semver/v3 v3.3.0
Expand All @@ -29,7 +33,6 @@ require (
github.com/smartcontractkit/chainlink-common v0.3.1-0.20241210192653-a9c706f99e83
github.com/smartcontractkit/chainlink-protos/job-distributor v0.6.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 @@ -9,6 +9,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-20241206210521-125d98cdaf66
github.com/smartcontractkit/chainlink/v2 v2.0.0-20241206210521-125d98cdaf66
)

require (
dario.cat/mergo v1.0.1
github.com/AlekSi/pointer v1.1.0
Expand Down Expand Up @@ -47,8 +54,6 @@ require (
github.com/smartcontractkit/chainlink-testing-framework/lib/grafana v1.50.0
github.com/smartcontractkit/chainlink-testing-framework/seth v1.50.9
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.20241120195829-bd7a1943ad07
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 @@ -11,6 +11,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-20241206210521-125d98cdaf66
github.com/smartcontractkit/chainlink/integration-tests v0.0.0-20241206210521-125d98cdaf66
github.com/smartcontractkit/chainlink/v2 v2.0.0-20241206210521-125d98cdaf66
)

require (
github.com/K-Phoen/grabana v0.22.2
github.com/ethereum/go-ethereum v1.14.11
Expand All @@ -23,9 +31,6 @@ require (
github.com/smartcontractkit/chainlink-testing-framework/lib v1.50.18
github.com/smartcontractkit/chainlink-testing-framework/seth v1.50.9
github.com/smartcontractkit/chainlink-testing-framework/wasp v1.50.2
github.com/smartcontractkit/chainlink/deployment v0.0.0-20241120141814-47da13e86197
github.com/smartcontractkit/chainlink/integration-tests v0.0.0-20241030133659-9ec788e78b4f
github.com/smartcontractkit/chainlink/v2 v2.14.0-mercury-20240807.0.20241120195829-bd7a1943ad07
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
41 changes: 41 additions & 0 deletions tools/gomod-local-update/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# gomod-local-update

Updates any module that is `replace`'d with a local path to have its required module version in `go.mod` to match the latest git SHA from a remote branch.

Is meant to run within each directory where a `go.mod` file is present.

## Configuration

Command Line Flags:

```shell
Optional:
-org-name Organization name (default: smartcontractkit)
-repo-name Repository name (default: chainlink)
-repo-remote Git remote to use (default: origin)
-branch-trunk Branch to get SHA from (default: develop)
-dry-run Preview changes without applying them (default: false)
```

## 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. From the root of this repository, run:

```shell
go install ./tools/gomod-local-update/cmd/gomod-local-update
```

## Usage Examples

Run from the root of a go module directory.

```shell
gomod-local-update
```

Was designed to be used with [gomods](https://github.com/jmank88/gomods) like:

```shell
gomods -w gomod-local-update
gomods tidy
```
48 changes: 48 additions & 0 deletions tools/gomod-local-update/cmd/gomod-local-update/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"log"
"os"

"github.com/smartcontractkit/chainlink/v2/tools/gomod-local-update/internal/updater"
)

const (
goBinaryName = "gomod-local-update"
)

var version = "dev"
var usage = fmt.Sprintf(`%s version %%s
Usage:
cd /path/to/go/module
%s [flags]
`, goBinaryName, goBinaryName)

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("%s version %s\n", goBinaryName, version)
os.Exit(0)
}

if err := cfg.Validate(); err != nil {
fmt.Fprintf(os.Stderr, usage, version)
log.Fatal(err)
}

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

if err := u.Run(); err != nil {
log.Fatal(err)
}
}
69 changes: 69 additions & 0 deletions tools/gomod-local-update/internal/updater/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package updater

import (
"flag"
"fmt"
)

const (
DefaultRepoRemote = "origin"
DefaultBranchTrunk = "develop"
DefaultOrgName = "smartcontractkit"
DefaultRepoName = "chainlink"
)

type Config struct {
RepoRemote string
BranchTrunk string
DryRun bool
ShowVersion bool
OrgName string
RepoName string
}

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

cfg := &Config{}

flags.StringVar(&cfg.RepoRemote, "repo-remote", DefaultRepoRemote, "Git remote to use")
flags.StringVar(&cfg.BranchTrunk, "branch-trunk", DefaultBranchTrunk, "Branch to get SHA from")
flags.BoolVar(&cfg.DryRun, "dry-run", false, "Preview changes without applying them")
flags.BoolVar(&cfg.ShowVersion, "version", false, "Show version information")
flags.StringVar(&cfg.OrgName, "org-name", DefaultOrgName, "GitHub organization name")
flags.StringVar(&cfg.RepoName, "repo-name", DefaultRepoName, "GitHub repository name")

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

return cfg, nil
}

func (c *Config) Validate() error {
if c.ShowVersion {
return nil
}

if c.OrgName == "" {
return fmt.Errorf("%w: org name must be provided", ErrInvalidConfig)
}
if c.RepoName == "" {
return fmt.Errorf("%w: repo name must be provided", ErrInvalidConfig)
}
if c.RepoRemote == "" {
return fmt.Errorf("%w: repo remote must be provided", ErrInvalidConfig)
}
if c.BranchTrunk == "" {
return fmt.Errorf("%w: trunk branch must be provided", ErrInvalidConfig)
}

if !gitRemoteRE.MatchString(c.RepoRemote) {
return fmt.Errorf("%w: git remote '%s' contains invalid characters", ErrInvalidConfig, c.RepoRemote)
}
if !gitBranchRE.MatchString(c.BranchTrunk) {
return fmt.Errorf("%w: git branch '%s' contains invalid characters", ErrInvalidConfig, c.BranchTrunk)
}

return nil
}
Loading

0 comments on commit 5bdf5c2

Please sign in to comment.