-
Notifications
You must be signed in to change notification settings - Fork 574
feat(cosmosgen): fetch fallback buf token #4805
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3e62d6d
feat(cosmosgen): fetch fallback buf token
julienrbrt 55c6a7b
package refactor
julienrbrt 293a213
fix `CaptureException called with nil error`
julienrbrt f473f91
Merge branch 'main' into julien/endpoint
julienrbrt d29589d
cl
julienrbrt 4088343
updates
julienrbrt e1f5cc8
updates
julienrbrt 149a842
feedback
julienrbrt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package buf | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/ignite/cli/v29/ignite/pkg/errors" | ||
| ) | ||
|
|
||
| var BufTokenURL = "https://buf.ignite.com" //nolint:gosec // URL is hardcoded and not user-provided | ||
|
|
||
| // FetchToken fetches the buf token from the Ignite API. | ||
| func FetchToken() (string, error) { | ||
| client := &http.Client{ | ||
| Timeout: 5 * time.Second, | ||
| } | ||
|
|
||
| resp, err := client.Get(BufTokenURL) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| if resp.StatusCode != http.StatusOK { | ||
| return "", errors.Errorf("HTTP request failed with status code: %d", resp.StatusCode) | ||
| } | ||
|
|
||
| type tokenResponse struct { | ||
| Token string `json:"token"` | ||
| } | ||
| var tokenResp tokenResponse | ||
|
|
||
| if err := json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| return tokenResp.Token, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package buf_test | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/ignite/cli/v29/ignite/internal/buf" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestFetchToken(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| serverResponse string | ||
| statusCode int | ||
| expectedToken string | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "successful token fetch", | ||
| serverResponse: `{"token":"test_token_123"}`, | ||
| statusCode: http.StatusOK, | ||
| expectedToken: "test_token_123", | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "server error", | ||
| serverResponse: `{"error":"internal server error"}`, | ||
| statusCode: http.StatusInternalServerError, | ||
| expectedToken: "", | ||
| expectError: true, | ||
| }, | ||
| { | ||
| name: "invalid json response", | ||
| serverResponse: `invalid json`, | ||
| statusCode: http.StatusOK, | ||
| expectedToken: "", | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| // Create mock server | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(tt.statusCode) | ||
| w.Write([]byte(tt.serverResponse)) | ||
| })) | ||
| defer server.Close() | ||
|
|
||
| // Temporarily override the endpoint | ||
| originalEndpoint := buf.BufTokenURL | ||
| buf.BufTokenURL = server.URL | ||
| defer func() { | ||
| buf.BufTokenURL = originalEndpoint | ||
| }() | ||
|
|
||
| token, err := buf.FetchToken() | ||
| if tt.expectError { | ||
| require.Error(t, err) | ||
| require.Empty(t, token) | ||
| } else { | ||
| require.NoError(t, err) | ||
| require.Equal(t, tt.expectedToken, token) | ||
| } | ||
| }) | ||
| } | ||
| } |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
ignite/pkg/cosmosgen/testdata/testchain/proto/buf.gen.swagger.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.