Skip to content

Commit

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

* 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

(cherry picked from commit 56c3730)

# Conflicts:
#	ignite/cmd/model/chain_serve.go

* updates

* updates

* updates

* import

---------

Co-authored-by: Ashish Khuraishy <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
3 people authored Sep 10, 2024
1 parent 92dac0d commit 10938ae
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 7 deletions.
6 changes: 3 additions & 3 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

## Unreleased

## [`v28.5.2`](https://github.com/ignite/cli/releases/tag/v28.5.2)

### Features

- [#4297](https://github.com/ignite/cli/pull/4297) Add in-place testnet creation command for apps.

### Changes

- [#4292](https://github.com/ignite/cli/pull/4292) Bump Cosmos SDK to `v0.50.9`
- [#4345](https://github.com/ignite/cli/pull/4345) Added survey link

### Fixes

- [#4319](https://github.com/ignite/cli/pull/4319) Remove fee abstraction module from open api code generation
- [#4309](https://github.com/ignite/cli/pull/4309) Fix chain id for chain simulations
- [#4322](https://github.com/ignite/cli/pull/4322) Create a message for authenticate buf for generate ts-client

### Fixes

- [#4323](https://github.com/ignite/cli/pull/4323) Add missing `--config` handling in the `chain` commands

## [`v28.5.1`](https://github.com/ignite/cli/releases/tag/v28.5.1)
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/v28/ignite/pkg/announcements"
"github.com/ignite/cli/v28/ignite/pkg/cliui/colors"
"github.com/ignite/cli/v28/ignite/pkg/cliui/icons"
cliuimodel "github.com/ignite/cli/v28/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/v28/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)
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/v28/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 10938ae

Please sign in to comment.