Skip to content

Commit

Permalink
Restore the last seen mainCommitId
Browse files Browse the repository at this point in the history
  • Loading branch information
nlewo committed Jun 19, 2024
1 parent 67bcac9 commit 6363269
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
20 changes: 13 additions & 7 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,6 @@ var runCmd = &cobra.Command{
}
gitConfig := config.MkGitConfig(cfg)

repositoryStatus := repository.RepositoryStatus{}
repository, err := repository.New(gitConfig, repositoryStatus)
if err != nil {
logrus.Errorf("Failed to initialize the repository: %s", err)
os.Exit(1)
}

machineId, err := utils.ReadMachineId()
if err != nil {
logrus.Error(err)
Expand All @@ -49,6 +42,19 @@ var runCmd = &cobra.Command{
logrus.Errorf("Ignoring the state file %s because of the loading error: %s", storeFilename, err)
}
metrics.SetBuildInfo(cmd.Version)

// We get the last mainCommitId to avoid useless
// redeployment as well as non fast forward checkouts
var mainCommitId string
if ok, lastDeployment := store.LastDeployment(); ok {
mainCommitId = lastDeployment.Generation.MainCommitId
}
repository, err := repository.New(gitConfig, mainCommitId)
if err != nil {
logrus.Errorf("Failed to initialize the repository: %s", err)
os.Exit(1)
}

manager := manager.New(repository, store, metrics, gitConfig.Path, cfg.Hostname, machineId)
go poller.Poller(manager, cfg.Remotes)
http.Serve(manager,
Expand Down
23 changes: 16 additions & 7 deletions internal/generation/generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type Generation struct {
SelectedCommitMsg string `json:"commit-msg"`
SelectedBranchIsTesting bool `json:"branch-is-testing"`

MainCommitId string `json:"main-commit-id"`
MainRemoteName string `json:"main-remote-name"`
MainBranchName string `json:"main-branch-name"`

EvalStartedAt time.Time `json:"eval-started-at"`
evalTimeout time.Duration
evalFunc EvalFunc
Expand Down Expand Up @@ -128,13 +132,18 @@ func New(repositoryStatus repository.RepositoryStatus, flakeUrl, hostname, machi
SelectedCommitId: repositoryStatus.SelectedCommitId,
SelectedCommitMsg: repositoryStatus.SelectedCommitMsg,
SelectedBranchIsTesting: repositoryStatus.SelectedBranchIsTesting,
evalTimeout: 6 * time.Second,
evalFunc: evalFunc,
buildFunc: buildFunc,
FlakeUrl: flakeUrl,
Hostname: hostname,
MachineId: machineId,
Status: Init,

MainRemoteName: repositoryStatus.MainRemoteName,
MainBranchName: repositoryStatus.MainBranchName,
MainCommitId: repositoryStatus.MainCommitId,

evalTimeout: 6 * time.Second,
evalFunc: evalFunc,
buildFunc: buildFunc,
FlakeUrl: flakeUrl,
Hostname: hostname,
MachineId: machineId,
Status: Init,
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Repository interface {
}

// repositoryStatus is the last saved repositoryStatus
func New(config types.GitConfig, repositoryStatus RepositoryStatus) (r *repository, err error) {
func New(config types.GitConfig, mainCommitId string) (r *repository, err error) {
r = &repository{}
r.GitConfig = config
r.Repository, err = repositoryOpen(config)
Expand All @@ -33,7 +33,7 @@ func New(config types.GitConfig, repositoryStatus RepositoryStatus) (r *reposito
if err != nil {
return
}
r.RepositoryStatus = NewRepositoryStatus(config, repositoryStatus)
r.RepositoryStatus = NewRepositoryStatus(config, mainCommitId)
return
}

Expand Down
4 changes: 2 additions & 2 deletions internal/repository/repository_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ type RepositoryStatus struct {
ErrorMsg string `json:"error_msg"`
}

func NewRepositoryStatus(config types.GitConfig, repositoryStatus RepositoryStatus) RepositoryStatus {
func NewRepositoryStatus(config types.GitConfig, mainCommitId string) RepositoryStatus {
r := RepositoryStatus{
MainCommitId: repositoryStatus.MainCommitId,
MainCommitId: mainCommitId,
}
r.Remotes = make([]*Remote, len(config.Remotes))
for i, remote := range config.Remotes {
Expand Down
27 changes: 12 additions & 15 deletions internal/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestNew(t *testing.T) {
},
},
}
r, err := New(gitConfig, RepositoryStatus{})
r, err := New(gitConfig, "")
assert.Nil(t, err)
assert.Equal(t, "r1", r.RepositoryStatus.Remotes[0].Name)
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestPreferMain(t *testing.T) {
},
},
}
r, err := New(gitConfig, RepositoryStatus{})
r, err := New(gitConfig, "")
assert.Nil(t, err)
// r1/main: c1 - c2 - *c3
// r1/testing: c1 - c2 - c3
Expand Down Expand Up @@ -118,9 +118,7 @@ func TestMainCommitId(t *testing.T) {
},
},
}
r, _ := New(gitConfig, RepositoryStatus{
MainCommitId: cMain,
})
r, _ := New(gitConfig, cMain)

// r1/main: c1 - c2 - c3 - c4
// r1/testing: c1 - c2 - c3 - c4 - c5
Expand Down Expand Up @@ -173,9 +171,8 @@ func TestContinueIfHardReset(t *testing.T) {
},
},
}
r, _ := New(gitConfig, RepositoryStatus{
MainCommitId: cMain,
})
r, _ := New(gitConfig, cMain)

r.Fetch([]string{"r1", "r2"})
r.Update()

Expand Down Expand Up @@ -242,7 +239,7 @@ func TestMultipleRemote(t *testing.T) {
},
},
}
r, err := New(gitConfig, RepositoryStatus{})
r, err := New(gitConfig, "")
assert.Nil(t, err)
// r1/main: c1 - c2 - *c3
// r2/main: c1 - c2 - c3
Expand Down Expand Up @@ -384,7 +381,7 @@ func TestTestingSwitch(t *testing.T) {
},
},
}
r, _ := New(gitConfig, RepositoryStatus{})
r, _ := New(gitConfig, "")

// r1/main: c1 - c2 - *c3
// r1/testing: c1 - c2 - c3
Expand Down Expand Up @@ -453,7 +450,7 @@ func TestWithoutTesting(t *testing.T) {
},
},
}
r, _ := New(gitConfig, RepositoryStatus{})
r, _ := New(gitConfig, "")

_ = r.Fetch([]string{"r1"})
_ = r.Update()
Expand Down Expand Up @@ -486,7 +483,7 @@ func TestRepositoryUpdateMain(t *testing.T) {
},
},
}
r, _ := New(gitConfig, RepositoryStatus{})
r, _ := New(gitConfig, "")

// The remote repository is initially checkouted
_ = r.Fetch([]string{"origin"})
Expand Down Expand Up @@ -545,7 +542,7 @@ func TestRepositoryUpdateHardResetMain(t *testing.T) {
},
},
}
r, _ := New(gitConfig, RepositoryStatus{})
r, _ := New(gitConfig, "")

// The remote repository is initially checkouted
_ = r.Fetch([]string{"origin"})
Expand Down Expand Up @@ -604,7 +601,7 @@ func TestRepositoryUpdateTesting(t *testing.T) {
},
},
}
r, _ := New(gitConfig, RepositoryStatus{})
r, _ := New(gitConfig, "")

// The remote repository is initially checkouted on main
_ = r.Fetch([]string{"origin"})
Expand Down Expand Up @@ -672,7 +669,7 @@ func TestTestingHardReset(t *testing.T) {
},
},
}
r, err := New(gitConfig, RepositoryStatus{})
r, err := New(gitConfig, "")
assert.Nil(t, err)
// r1/main: c1 - c2 - *c3
// r1/testing: c1 - c2 - c3
Expand Down

0 comments on commit 6363269

Please sign in to comment.