Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: generate countries.tsx from Go code #15274

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

Merged
merged 11 commits into from
Nov 15, 2024
Merged

Conversation

joobisb
Copy link
Contributor

@joobisb joobisb commented Oct 29, 2024

Closes #15074

We have a hard-coded list of countries at https://github.com/coder/coder/blob/main/site/src/pages/SetupPage/countries.tsx. This means Go code in coder/coder doesn't have an easy way of utilizing it.

Solution

Generate countries.tsx from Go code. Generated by scripts/apitypings

@cdr-bot cdr-bot bot added the community Pull Requests and issues created by the community. label Oct 29, 2024
@joobisb
Copy link
Contributor Author

joobisb commented Oct 29, 2024

@johnstcn @dannykopping please do have a look

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

Looks good from my end, some linter issues.

Could you add an exaple prompt comand (see cli/prompts.go)?

This will let us validate country selection more easily (e.g. go run ./cmd/coder exp prompt-example country)

cc @BrunoQuaresma for FE review

@@ -51,3 +52,36 @@ func TestGeneration(t *testing.T) {
})
}
}

func TestGenerateSliceOfObjectsTypeScript(t *testing.T) {

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
t.Parallel()

Comment on lines 62 to 69

countriesTS, err := generateSliceOfObjectsTypeScript("countries", codersdk.Countries)
if err != nil {
log.Fatal(ctx, fmt.Sprintf("generate countries typeScript: %s", err.Error()))
}
_, _ = fmt.Print("// The code below is generated from codersdk/countries.go.\n")
_, _ = fmt.Print(countriesTS, "\n")

Copy link
Member

Choose a reason for hiding this comment

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

To do this correctly, we should not have to specify codersdk.Countries. We should implement it here:

case *types.Var:
// TODO: Are any enums var declarations? This is also codersdk.Me.

I feel a bit apprehensive to import codersdk into this tool.

We could make it a new generator (:cry:) or actually auto-gen vars from the codersdk package (like codersdk.Me).

If we do the vars, we might want to have some opt-in comment. Idk how many var declarations we have

Copy link
Member

Choose a reason for hiding this comment

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

We should really have a general autogen tool and pattern for these. When the types are in Go and no package AST parsing is necessary.

rbacgen is an example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Emyrk

got it, will implement a general autogen tool. As suggested, I'm thinking of doing it similar to how rbacgen is done.

So the autogen tool will be under scripts/valuegen which contains main.go which is the script itself and typescript.tstmpl which will be the TS template to which we will convert the countries.go

var Countries = []Country{

If similar types conversion needs are needed in the future for other types in Go, it can be handled in the script to use the same typescriptconst.tstmpl template or a different one based on the need.

does that make sense? wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

@joobisb That all sounds reasonable. It'll probably throw into a different .ts file.

It would be awesome if rbacgen and valuegen could be merged into 1 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Emyrk I have updated the scripts and have merged both into a single typegen script, please do have a look

@dannykopping dannykopping removed their request for review November 5, 2024 08:02
@joobisb joobisb requested a review from Emyrk November 9, 2024 17:51
Copy link
Member

@Emyrk Emyrk left a comment

Choose a reason for hiding this comment

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

Perfect 👍

@matifali matifali enabled auto-merge (squash) November 13, 2024 07:47
auto-merge was automatically disabled November 13, 2024 14:16

Head branch was pushed to by a user without write access

@joobisb
Copy link
Contributor Author

joobisb commented Nov 13, 2024

@matifali could you please help to run the CI again and merge, I've fixed a formatting error

@Emyrk
Copy link
Member

Emyrk commented Nov 13, 2024

I am unsure why that premium e2e is failing. I'll take a quick look

@matifali
Copy link
Member

@Emyrk Because PRs from forks do not have access to secrets.

@matifali
Copy link
Member

matifali commented Nov 13, 2024

This could be solved by using,

on:
  pull_request_target:

The only downside is that the workflows are run from the target branch, but the code is checked out from the PR branch.

@Emyrk
Copy link
Member

Emyrk commented Nov 13, 2024

@Emyrk Because PRs from forks do not have access to secrets.

Ahh, how do we get around this? @matifali

@joobisb
Copy link
Contributor Author

joobisb commented Nov 15, 2024

@Emyrk Because PRs from forks do not have access to secrets.

Ahh, how do we get around this? @matifali

@matifali @Emyrk

Do we have any workaround for this?

@matifali
Copy link
Member

@joobisb Thanks for the reminder. I will try something like the one below and let you know if it works.

This could be solved by using,

on:
  pull_request_target:

The only downside is that the workflows are run from the target branch, but the code is checked out from the PR branch.

@ammario ammario merged commit 4cb8076 into coder:main Nov 15, 2024
27 of 29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Pull Requests and issues created by the community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

chore: generate countries.tsx from Go code
5 participants