-
Notifications
You must be signed in to change notification settings - Fork 551
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
970956a
commit 56c3730
Showing
5 changed files
with
124 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,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) | ||
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) | ||
} |
This file contains 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,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) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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