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

feat(CLI): Support branch in pull config #STRINGS-538 #701

Merged
merged 5 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
20 changes: 10 additions & 10 deletions clients/cli/cmd/internal/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ func (cmd *PullCommand) Run(config *phrase.Config) error {
}
cmd.Branch = branchName

projectIdToLocales, err := LocalesForProjects(client, targets, cmd.Branch)
localesCache, err := GetLocalesCache(client, targets, cmd.Branch)
if err != nil {
return err
}

for _, target := range targets {
val, ok := projectIdToLocales[LocaleCacheKey{target.ProjectID, cmd.Branch}]
if cmd.Branch != "" {
target.Params.Branch = optional.NewString(cmd.Branch)
}

val, ok := localesCache[LocalesCacheKey{target.ProjectID, target.Params.Branch.Value()}]
if !ok || len(val) == 0 {
if cmd.Branch != "" {
continue
Expand All @@ -75,7 +79,7 @@ func (cmd *PullCommand) Run(config *phrase.Config) error {
}

for _, target := range targets {
err := target.Pull(client, cmd.Branch, cmd.Async)
err := target.Pull(client, cmd.Async)
if err != nil {
return err
}
Expand Down Expand Up @@ -103,7 +107,7 @@ type PullParams struct {
LocaleID string `json:"locale_id"`
}

func (target *Target) Pull(client *phrase.APIClient, branch string, async bool) error {
func (target *Target) Pull(client *phrase.APIClient, async bool) error {
if err := target.CheckPreconditions(); err != nil {
return err
}
Expand All @@ -124,7 +128,7 @@ func (target *Target) Pull(client *phrase.APIClient, branch string, async bool)
return err
}

err = target.DownloadAndWriteToFile(client, localeFile, branch, async)
err = target.DownloadAndWriteToFile(client, localeFile, async)
if err != nil {
if openapiError, ok := err.(phrase.GenericOpenAPIError); ok {
print.Warn("API response: %s", openapiError.Body())
Expand All @@ -139,7 +143,7 @@ func (target *Target) Pull(client *phrase.APIClient, branch string, async bool)
return nil
}

func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFile *LocaleFile, branch string, async bool) error {
func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFile *LocaleFile, async bool) error {
localVarOptionals := phrase.LocaleDownloadOpts{}

if target.Params != nil {
Expand All @@ -155,10 +159,6 @@ func (target *Target) DownloadAndWriteToFile(client *phrase.APIClient, localeFil
localVarOptionals.FileFormat = optional.NewString(localeFile.FileFormat)
}

if branch != "" {
localVarOptionals.Branch = optional.NewString(branch)
}

if localeFile.Tag != "" {
localVarOptionals.Tags = optional.NewString(localeFile.Tag)
localVarOptionals.Tag = optional.EmptyString()
Expand Down
8 changes: 4 additions & 4 deletions clients/cli/cmd/internal/pull_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (

type Targets []*Target

func (targets Targets) ProjectIds() []string {
projectIds := []string{}
func (targets Targets) GetAllLocalesCacheKeys() []LocalesCacheKey {
localesCacheKeys := []LocalesCacheKey{}
for _, target := range targets {
projectIds = append(projectIds, target.ProjectID)
localesCacheKeys = append(localesCacheKeys, LocalesCacheKey{target.ProjectID, target.Params.Branch.Value()})
}
return projectIds
return localesCacheKeys
}

type Target struct {
Expand Down
4 changes: 2 additions & 2 deletions clients/cli/cmd/internal/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ func (cmd *PushCommand) Run() error {
}
}

projectIdToLocales, err := LocalesForProjects(client, sources, cmd.Branch)
localesCache, err := GetLocalesCache(client, sources, cmd.Branch)
if err != nil {
return err
}
for _, source := range sources {
val, ok := projectIdToLocales[LocaleCacheKey{source.ProjectID, cmd.Branch}]
val, ok := localesCache[LocalesCacheKey{source.ProjectID, cmd.Branch}]
if ok {
source.RemoteLocales = val
}
Expand Down
8 changes: 4 additions & 4 deletions clients/cli/cmd/internal/push_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,12 @@ func (source *Source) CheckPreconditions() error {
return nil
}

func (sources Sources) ProjectIds() []string {
projectIds := []string{}
func (sources Sources) GetAllLocalesCacheKeys() []LocalesCacheKey {
projectIdsBranches := []LocalesCacheKey{}
for _, source := range sources {
projectIds = append(projectIds, source.ProjectID)
projectIdsBranches = append(projectIdsBranches, LocalesCacheKey{source.ProjectID, source.Branch})
}
return projectIds
return projectIdsBranches
}
func (source *Source) uploadFile(client *phrase.APIClient, localeFile *LocaleFile, branch string, tag string) (*phrase.Upload, error) {
if Debug {
Expand Down
36 changes: 21 additions & 15 deletions clients/cli/cmd/internal/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,51 @@ import (

var Debug bool

type ProjectLocales interface {
ProjectIds() []string
type SourcesOrTargets interface {
// returns a list of LocalesCacheKeys (ProjectId, Branch) for all targets
GetAllLocalesCacheKeys() []LocalesCacheKey
}

type LocaleCacheKey struct {
type LocalesCacheKey struct {
ProjectID string
Branch string
}

type LocaleCache map[LocaleCacheKey][]*phrase.Locale
type LocaleCache map[LocalesCacheKey][]*phrase.Locale

func LocalesForProjects(client *phrase.APIClient, projectLocales ProjectLocales, branch string) (LocaleCache, error) {
projectIdToLocales := LocaleCache{}
// for every source or target, retrieves and caches the list of locales
func GetLocalesCache(client *phrase.APIClient, sourcesOrTargets SourcesOrTargets, branch string) (LocaleCache, error) {
localesCache := LocaleCache{}

for _, pid := range projectLocales.ProjectIds() {
key := LocaleCacheKey{
ProjectID: pid,
Branch: branch,
for _, localesCacheKey := range sourcesOrTargets.GetAllLocalesCacheKeys() {
branchToUse := localesCacheKey.Branch
if branch != "" {
branchToUse = branch
}
key := LocalesCacheKey{
ProjectID: localesCacheKey.ProjectID,
Branch: branchToUse,
}

if _, ok := projectIdToLocales[key]; !ok {
if _, ok := localesCache[key]; !ok {

remoteLocales, http_response, err := RemoteLocales(client, key)
if err != nil {
if http_response != nil && http_response.StatusCode == 404 && branch != "" {
if http_response != nil && http_response.StatusCode == 404 && branchToUse != "" {
// skip this key if we targeted a branch in
// a project which does not exist
continue
}
return nil, err
}

projectIdToLocales[key] = remoteLocales
localesCache[key] = remoteLocales
}
}
return projectIdToLocales, nil
return localesCache, nil
}

func RemoteLocales(client *phrase.APIClient, key LocaleCacheKey) ([]*phrase.Locale, *phrase.APIResponse, error) {
func RemoteLocales(client *phrase.APIClient, key LocalesCacheKey) ([]*phrase.Locale, *phrase.APIResponse, error) {
page := 1

localVarOptionals := phrase.LocalesListOpts{
Expand Down
Loading