|
1 | 1 | package core |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "time" |
5 | 6 |
|
6 | 7 | "github.com/switcherapi/switcher-gitops/src/config" |
@@ -89,7 +90,7 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi |
89 | 90 | gitService.UpdateRepositorySettings(account.Repository, account.Token, account.Branch, account.Path) |
90 | 91 |
|
91 | 92 | // Fetch repository data |
92 | | - repositoryData, err := gitService.GetRepositoryData(account.Environment) |
| 93 | + repositoryData, err := c.getRepositoryData(gitService, account) |
93 | 94 |
|
94 | 95 | if err != nil { |
95 | 96 | c.updateDomainStatus(*account, model.StatusError, "Failed to fetch repository data - "+err.Error(), |
@@ -125,6 +126,16 @@ func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitServi |
125 | 126 | } |
126 | 127 | } |
127 | 128 |
|
| 129 | +func (c *CoreHandler) getRepositoryData(gitService IGitService, account *model.Account) (*model.RepositoryData, error) { |
| 130 | + repositoryData, err := gitService.GetRepositoryData(account.Environment) |
| 131 | + |
| 132 | + if err != nil && err.Error() == "file not found" { |
| 133 | + repositoryData, err = c.createRepositoryData(*account, gitService) |
| 134 | + } |
| 135 | + |
| 136 | + return repositoryData, err |
| 137 | +} |
| 138 | + |
128 | 139 | func (c *CoreHandler) syncUp(account model.Account, repositoryData *model.RepositoryData, gitService IGitService) { |
129 | 140 | // Update account status: Out of sync |
130 | 141 | account.Domain.LastCommit = repositoryData.CommitHash |
@@ -234,19 +245,32 @@ func (c *CoreHandler) pushChangesToRepository(account model.Account, snapshot mo |
234 | 245 | snapshotContent.Domain.Version = 0 |
235 | 246 |
|
236 | 247 | // Push changes to repository |
237 | | - lastCommit, err := gitService.PushChanges(account.Environment, utils.ToJsonFromObject(snapshotContent)) |
| 248 | + repositoryData, err := gitService.PushChanges(account.Environment, utils.ToJsonFromObject(snapshotContent), |
| 249 | + fmt.Sprintf("updated %s.json", account.Environment)) |
238 | 250 |
|
239 | 251 | if err != nil { |
240 | 252 | return account, err |
241 | 253 | } |
242 | 254 |
|
243 | 255 | // Update domain |
244 | 256 | account.Domain.Version = snapshot.Domain.Version |
245 | | - account.Domain.LastCommit = lastCommit |
| 257 | + account.Domain.LastCommit = repositoryData.CommitHash |
246 | 258 |
|
247 | 259 | return account, nil |
248 | 260 | } |
249 | 261 |
|
| 262 | +func (c *CoreHandler) createRepositoryData(account model.Account, gitService IGitService) (*model.RepositoryData, error) { |
| 263 | + utils.LogInfo("[%s - %s (%s)] Creating repository data", account.ID.Hex(), account.Domain.Name, account.Environment) |
| 264 | + |
| 265 | + snapshotJsonFromApi, err := c.apiService.FetchSnapshot(account.Domain.ID, account.Environment) |
| 266 | + |
| 267 | + if err != nil { |
| 268 | + return nil, err |
| 269 | + } |
| 270 | + |
| 271 | + return gitService.PushChanges(account.Environment, snapshotJsonFromApi, fmt.Sprintf("created %s.json", account.Environment)) |
| 272 | +} |
| 273 | + |
250 | 274 | func (c *CoreHandler) isOutSync(account model.Account, lastCommit string, snapshotVersionPayload string) bool { |
251 | 275 | snapshotVersion := c.apiService.NewDataFromJson([]byte(snapshotVersionPayload)).Snapshot.Domain.Version |
252 | 276 |
|
|
0 commit comments