Skip to content

Commit

Permalink
feat: added survey link (#4345)
Browse files Browse the repository at this point in the history
* feat: added survey link on exit

* chore: added change log

* feat(view): added announcement api support

* added api support to dynamically show announcements
* fallbacks to a survey link
* unit tests for the same
  • Loading branch information
ashishkhuraishy committed Sep 10, 2024
1 parent 970956a commit 56c3730
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
- [#4322](https://github.com/ignite/cli/pull/4322) Create a message for authenticate buf for generate ts-client
- [#4319](https://github.com/ignite/cli/pull/4319) Remove fee abstraction module from open api code generation
- [#4317](https://github.com/ignite/cli/pull/4317) Remove xchisel dependency
- [#4345](https://github.com/ignite/cli/pull/4345) Added survey link

### Fixes

Expand Down
2 changes: 2 additions & 0 deletions ignite/cmd/model/chain_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

tea "github.com/charmbracelet/bubbletea"

"github.com/ignite/cli/v29/ignite/pkg/announcements"
"github.com/ignite/cli/v29/ignite/pkg/cliui/colors"
"github.com/ignite/cli/v29/ignite/pkg/cliui/icons"
cliuimodel "github.com/ignite/cli/v29/ignite/pkg/cliui/model"
Expand Down Expand Up @@ -260,6 +261,7 @@ func (m ChainServe) renderQuitView() string {
}

fmt.Fprintf(&view, "%s %s\n", icons.Info, colors.Info("Stopped"))
view.WriteString(announcements.GetAnnouncements())

return view.String()
}
Expand Down
50 changes: 50 additions & 0 deletions ignite/pkg/announcements/announcement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package announcements

import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/ignite/cli/v29/ignite/pkg/cliui/icons"
)

var (
SurveyLink = "https://bit.ly/3WZS2uS"
AnnouncementAPI = "http://api.ignite.com/announcements"
)

type announcement struct {
Announcements []string `json:"announcements"`
}

func GetAnnouncements() string {
resp, err := http.Get(AnnouncementAPI)

Check failure on line 22 in ignite/pkg/announcements/announcement.go

View workflow job for this annotation

GitHub Actions / Lint Go code

G107: Potential HTTP request made with variable url (gosec)
if err != nil || resp.StatusCode != 200 {
return fallbackData()
}
defer resp.Body.Close()

var data announcement
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return fallbackData()
}

// is this needed? or if its empty we don't want to show anything?
if len(data.Announcements) == 0 {
return fallbackData()
}

var out strings.Builder
fmt.Fprintf(&out, "\n%s %s\n", icons.Announcement, "Announcements")

for _, announcement := range data.Announcements {
fmt.Fprintf(&out, "%s %s\n", icons.Bullet, announcement)
}

return out.String()
}

func fallbackData() string {
return fmt.Sprintf("\n%s Survey: %s\n", icons.Survey, SurveyLink)
}
65 changes: 65 additions & 0 deletions ignite/pkg/announcements/announcement_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package announcements_test

import (
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/ignite/cli/v29/ignite/pkg/announcements"
)

func TestGetAnnouncements(t *testing.T) {
fallbackData := fmt.Sprintf("\n💬 Survey: %s\n", announcements.SurveyLink)

tests := []struct {
name string
mockResponse string
statusCode int
expected string
}{
{
name: "successful retrieval",
mockResponse: `{"announcements":["Announcement 1","Announcement 2"]}`,
statusCode: http.StatusOK,
expected: "\n🗣️ Announcements\n⋆ Announcement 1\n⋆ Announcement 2\n",
},
{
name: "empty announcements",
mockResponse: `{"announcements":[]}`,
statusCode: http.StatusOK,
expected: fallbackData,
},
{
name: "invalid JSON response",
mockResponse: `invalid json`,
statusCode: http.StatusOK,
expected: fallbackData,
},
{
name: "non-200 HTTP response",
mockResponse: ``,
statusCode: http.StatusInternalServerError,
expected: fallbackData,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(tt.statusCode)
w.Write([]byte(tt.mockResponse))
}))
defer server.Close()

originalAPI := announcements.AnnouncementAPI
announcements.AnnouncementAPI = server.URL
defer func() { announcements.AnnouncementAPI = originalAPI }()

result := announcements.GetAnnouncements()
if result != tt.expected {
t.Errorf("expected %q, got %q", tt.expected, result)
}
})
}
}
10 changes: 6 additions & 4 deletions ignite/pkg/cliui/icons/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
)

var (
Earth = "🌍"
CD = "💿"
User = "👤"
Tada = "🎉"
Earth = "🌍"
CD = "💿"
User = "👤"
Tada = "🎉"
Survey = "💬"
Announcement = "🗣️"

// OK is an OK mark.
OK = colors.SprintFunc(colors.Green)("✔")
Expand Down

0 comments on commit 56c3730

Please sign in to comment.