Skip to content

Commit

Permalink
desc
Browse files Browse the repository at this point in the history
- GetCommunities() now returns the results directly
- ValidateIcon now takes optional file name param
- Add github action for discord release notif
- Implemented CheckSemVer util method.
  • Loading branch information
Owen3H committed Apr 11, 2024
1 parent 3dbd747 commit ae15a78
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 24 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/github-releases-to-discord.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
release:
types: [published]

jobs:
github-releases-to-discord:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Github Releases To Discord
uses: SethCohen/[email protected]
with:
webhook_url: ${{ secrets.WEBHOOK_URL }}
color: "16766874"
username: "ThunderGo"
footer_title: "Changelog"
footer_timestamp: true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
# Go workspace file
go.work

test_icon.png
test_icon.png
test_manifest.json
16 changes: 13 additions & 3 deletions experimental/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ func (b Base64String) String() string {
}

func GetCommunities() (CommunityList, error) {
return util.JsonGetRequest[CommunityList]("api/experimental/community")
res, err := util.JsonGetRequest[CommunitiesResponse]("api/experimental/community")

if err != nil {
return CommunityList{}, err
}

return res.Results, nil
}

// Get a specific [Community] by it's identifier or short name.
Expand All @@ -30,11 +36,15 @@ func GetCommunity(nameOrId string) (*Community, bool, error) {
return nil, false, err
}

comm, found := lo.Find(communities.Results, func(c *Community) bool {
comm, found := lo.Find(communities, func(c Community) bool {
return strings.EqualFold(c.Name, nameOrId) || strings.EqualFold(c.Identifier, nameOrId)
})

return comm, found, nil
if !found {
return nil, false, nil
}

return &comm, true, nil
}

// Get a single [Package] given it's owner and package short name.
Expand Down
2 changes: 1 addition & 1 deletion experimental/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type AuthResponse struct {
SessionID string `json:"session_id"`
}

// Not yet implemented.
// TODO: Implement this.
func LoginWithGithub(auth AuthOptions) (AuthResponse, error) {
return AuthResponse{}, nil
}
10 changes: 6 additions & 4 deletions experimental/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (

type Category = common.PackageCategory

type CommunityList struct {
Next string `json:"next"`
Previous string `json:"previous"`
Results []*Community `json:"results"`
type CommunityList []Community

type CommunitiesResponse struct {
Next string `json:"next"`
Previous string `json:"previous"`
Results CommunityList `json:"results"`
}

type CommunityCategories struct {
Expand Down
60 changes: 49 additions & 11 deletions experimental/submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,77 @@ package experimental

import (
"bytes"
"encoding/json"
"errors"
"image"
_ "image/png"

"github.com/the-egg-corp/thundergo/util"
)

type PackageSubmissionMetadata struct {
UUID string `json:"upload_uuid"`
Author string `json:"author_name"`
Categories []string `json:"categories"`
Communities []string `json:"communities"`
HasNsfwContent bool `json:"has_nsfw_content"`
UUID string `json:"upload_uuid"`
CommunityCategories []string `json:"community_categories"`
Categories []string `json:"categories"`
HasNsfwContent bool `json:"has_nsfw_content"`
}

type ManifestMetadata struct {
Name string `json:"name"`
VersionNumber string `json:"version_number"`
WebsiteURL string `json:"website_url"`
Description string `json:"description"`
Dependencies []string `json:"dependencies"`
}

type IconValidatorParams struct {
FileName string
ImageData []byte
}

// Not yet implemented.
func SubmitPackage() (bool, error) {
// TODO: Implement this
func SubmitPackage(data []byte) (bool, error) {
return false, nil
}

// Not yet implemented.
// TODO: Implement this
func ValidateReadme(data []byte) (bool, error) {
return false, nil
}

// Not yet implemented.
func ValidateManifest() (bool, error) {
// TODO: Implement this
func ValidateManifest(data []byte) (bool, error) {
var manifest ManifestMetadata
err := json.Unmarshal(data, &manifest)

if err != nil {
return false, err
}

if manifest.Name == "" {
return false, errors.New("required manifest property 'name' is empty")
}

isSV, _ := util.CheckSemVer(manifest.VersionNumber)
if !isSV {
return false, errors.New("manifest version does not follow semantic versioning (major.minor.patch)")
}

return false, nil
}

// Decodes image data and validates that the image is a PNG and the dimensions are 256x256.
func ValidateIcon(data []byte) (bool, error) {
// Decode the image
img, _, err := image.Decode(bytes.NewReader(data))
//
// Additionally, if the file name is specified, it will validate that it is named correctly.
func ValidateIcon(params IconValidatorParams) (bool, error) {
if params.FileName != "" && params.FileName != "icon.png" {
return false, errors.New("image name provided did not match: icon.png")
}

// Decode data into the Image type.
img, _, err := image.Decode(bytes.NewReader(params.ImageData))
if err != nil {
return false, err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/community_test_exp.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestCommunitiesExp(t *testing.T) {
t.Error("Could not get list of communities!")
}

util.PrettyPrint(comms.Results)
util.PrettyPrint(comms)
}

//endregion
10 changes: 7 additions & 3 deletions tests/submission_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package tests

import (
TSGO "github.com/the-egg-corp/thundergo/experimental"
"github.com/the-egg-corp/thundergo/util"
"os"
"testing"

TSGO "github.com/the-egg-corp/thundergo/experimental"
"github.com/the-egg-corp/thundergo/util"
)

func TestValidateIcon(t *testing.T) {
Expand All @@ -15,7 +16,10 @@ func TestValidateIcon(t *testing.T) {
t.Fatalf(err.Error())
}

valid, err := TSGO.ValidateIcon(icon)
valid, err := TSGO.ValidateIcon(TSGO.IconValidatorParams{
ImageData: icon,
})

if err != nil {
t.Fatalf(err.Error())
}
Expand Down
10 changes: 10 additions & 0 deletions util/funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -43,3 +44,12 @@ func TryFind[T any](arr []T, pred func(pkg T) bool) *T {
pkg, found := lo.Find(arr, pred)
return lo.Ternary(found, &pkg, nil)
}

func CheckSemVer(version string) (bool, error) {
matched, err := regexp.MatchString(
`^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`,
version,
)

return lo.Ternary(err == nil, matched, false), lo.Ternary(err == nil, nil, err)
}

0 comments on commit ae15a78

Please sign in to comment.