Skip to content

Conversation

@ReshmaRamaiah10
Copy link
Contributor

@ReshmaRamaiah10 ReshmaRamaiah10 commented Dec 30, 2025

  • Fetch dev tree from GitHub only in production.
  • Use ETag to skip downloading if the file is unchanged.
  • Overwrite the dev tree file when updated; no versioned copies.
  • Local/non-production environments use existing files without fetching.

func GetTreeFilepath(name string) string {
env := os.Getenv("ENV")

if name == DEV_TREE_IDENTIFIER+".json" && env == "production" {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bprize15 Please suggest if this is the best way to identify production deployment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting APP_ENV=production in the Dockerfile

func fetchDevTreeIfChanged(devTreePath string) error {
etagPath := devTreePath + ".etag"

req, err := http.NewRequest("GET", DEV_TREE_GITHUB_RAW_URL, nil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use http.MethodGet instead of the string literal "GET"

return err
}

if etag := resp.Header.Get("ETag"); etag != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we save the etag at a different path? I think we should maintain that everything of the trees dir can be expected to be a tree


if name == DEV_TREE_IDENTIFIER+".json" && appEnv == "production" {
devTreePath := filepath.Join(TREE_FILES_PATH, DEV_TREE_IDENTIFIER+".json")
_ = fetchDevTreeIfChanged(devTreePath)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should handle this error. Also, I think we should handle fetching the dev tree somewhere else. It feels confusing that this function also fetches the dev tree, and if we handle the error here, we force any call to GetTreeFilepath to handle this error.

I think a better spot might be ReadTreeFromFile. Since this already returns an error, you won't need to add extra error handling (probably), and reading the tree seems more likely to have the side effect of making sure the dev tree is up to date than getting a filepath. This is just one way of fixing it, so feel free to implement differently if you have a better solution.

return nil // already up-to-date

case http.StatusOK:
tmp := devTreePath + ".tmp"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do this work somewhere else than the root of the tree repo?

}

if etag := resp.Header.Get("ETag"); etag != "" {
_ = os.WriteFile(etagPath, []byte(etag), 0644)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should get in the habit of ignoring errors. Callers of this function would have no idea the etag writing can fail. This won't cause any errors in this case, but generally the caller should be aware of what can go wrong so it can decide how to handle it. Maybe we should add a lint rule to enforce this.

)

func ReadTreeFromFile(name string) (Tree, error) {
if err := maybeUpdateDevTree(name); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function name is a bit confusing. I would try to be more descriptive. My intuition is just to move all the logic from maybeUpdateDevTree to readTreeFromFile (since it is only used once and I find it easier to read without jumping around).

func ReadTreeFromFile(name string) (Tree, error) {
if err := maybeUpdateDevTree(name); err != nil {
return nil, err
appEnv := os.Getenv("APP_ENV")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is the same logic as ReadTreeRaw let's reuse that here

Copy link
Collaborator

@bprize15 bprize15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@ReshmaRamaiah10 ReshmaRamaiah10 merged commit 8314634 into master Jan 7, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants