Added support for Gitea

main
Nichole Mattera 1 year ago
parent a7eddec8da
commit 2cbb77f28a
  1. 1
      assets/modules/01-atmosphere.json
  2. 1
      assets/modules/02-hekate.json
  3. 1
      assets/modules/03-fusee.json
  4. 5
      assets/modules/04-kosmos.json
  5. 5
      assets/modules/05-kosmos-reborn-setup.json
  6. 120
      internal/gitea.go
  7. 60
      internal/github.go
  8. 45
      internal/modules.go

@ -1,4 +1,5 @@
{
"Source": "GitHub",
"Name": "Atmosphère",
"Org": "Atmosphere-NX",
"Repo": "Atmosphere",

@ -1,4 +1,5 @@
{
"Source": "GitHub",
"Name": "hekate - CTCaer mod",
"Org": "CTCaer",
"Repo": "hekate",

@ -1,4 +1,5 @@
{
"Source": "GitHub",
"Name": "Fusée",
"Org": "Atmosphere-NX",
"Repo": "Atmosphere",

@ -1,6 +1,7 @@
{
"Source": "NicholeMattera",
"Name": "Kosmos Reborn Assets",
"Org": "TeamLibra",
"Org": "NicholeMattera",
"Repo": "Kosmos-Reborn-Assets",
"AssetPattern": "kosmos_reborn_assets\\.zip",
"Instructions": [
@ -18,4 +19,4 @@
"Destination": "./"
}
]
}
}

@ -1,7 +1,8 @@
{
"Source": "NicholeMattera",
"Name": "Kosmos Reborn Setup",
"Org": "TeamLibra",
"Org": "NicholeMattera",
"Repo": "Kosmos-Reborn-Setup",
"AssetPattern": "",
"Instructions": []
}
}

@ -0,0 +1,120 @@
// Kosmos Reborn Builder
// Copyright (C) 2022 Nichole Mattera
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.
package internal
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"regexp"
"strconv"
)
type GiteaUser struct {
Active bool `json:"active"`
AvatarUrl string `json:"avatar_url"`
Created string `json:"created"`
Description string `json:"description"`
Email string `json:"email"`
FullName string `json:"full_name"`
FollowersCount int `json:"followers_count"`
FollowingCount int `json:"following_count"`
Id int `json:"id"`
IsAdmin bool `json:"is_admin"`
Language string `json:"language"`
LastLogin string `json:"last_login"`
Location string `json:"location"`
Login string `json:"login"`
ProhibitLogin bool `json:"prohibit_login"`
Restricted bool `json:"restricted"`
StarredReposCount int `json:"starred_repos_count"`
Username string `json:"username"`
Visibility string `json:"visibility"`
Website string `json:"website"`
}
type GiteaAsset struct {
BrowserDownloadUrl string `json:"browser_download_url"`
CreatedAt string `json:"created_at"`
DownloadCount int `json:"download_count"`
Id int `json:"id"`
Name string `json:"name"`
Size int `json:"size"`
UUID string `json:"uuid"`
}
type GiteaRelease struct {
Assets []GiteaAsset `json:"assets"`
Author GiteaUser `json:"author"`
Body string `json:"body"`
CreatedAt string `json:"created_at"`
Draft bool `json:"draft"`
HtmlUrl string `json:"html_url"`
Id int `json:"id"`
Name string `json:"name"`
Prerelease bool `json:"prerelease"`
PublishedAt string `json:"published_at"`
TagName string `json:"tag_name"`
TarballUrl string `json:"tarball_url"`
TargetCommitish string `json:"target_commitish"`
Url string `json:"url"`
ZipballUrl string `json:"zipball_url"`
}
func GetLatestGiteaRelease(domain string, organization string, repository string, assetPattern string) (string, string, string, error) {
resp, err := http.Get("https://" + domain + "/api/v1/repos/" + organization + "/" + repository + "/releases?limit=1")
if err != nil {
return "", "", "", err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", "", "", errors.New("Getting releases for " + organization + "/" + repository + " returned status code: " + strconv.Itoa(resp.StatusCode))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", "", "", err
}
var releases []GiteaRelease
err = json.Unmarshal(body, &releases)
if err != nil {
return "", "", "", err
}
if len(releases) != 1 {
return "", "", "", errors.New("No releases for " + organization + "/" + repository)
}
for _, asset := range releases[0].Assets {
matched, err := regexp.Match(assetPattern, []byte(asset.Name))
if err != nil {
return "", "", "", err
}
if matched {
return releases[0].TagName, asset.BrowserDownloadUrl, asset.Name, nil
}
}
return "", "", "", errors.New("No assets")
}

@ -21,15 +21,10 @@ package internal
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
type GitHubUser struct {
@ -90,70 +85,39 @@ type GitHubRelease struct {
Body string `json:"body"`
}
func GetLatestRelease(organization string, repository string, assetPattern string, githubUsername string, githubPassword string) (string, string, error) {
func GetLatestGitHubRelease(organization string, repository string, assetPattern string, githubUsername string, githubPassword string) (string, string, string, error) {
resp, err := http.Get("https://" + githubUsername + ":" + githubPassword + "@api.github.com/repos/" + organization + "/" + repository + "/releases/latest")
if err != nil {
return "", "", err
return "", "", "", err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", "", errors.New("Getting latest release returned status code: " + strconv.Itoa(resp.StatusCode))
return "", "", "", errors.New("Getting latest release for " + organization + "/" + repository + " returned status code: " + strconv.Itoa(resp.StatusCode))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", "", err
return "", "", "", err
}
var release GitHubRelease
json.Unmarshal(body, &release)
err = json.Unmarshal(body, &release)
if err != nil {
return "", "", "", err
}
for _, asset := range release.Assets {
matched, err := regexp.Match(assetPattern, []byte(asset.Name))
if err != nil {
return "", "", err
return "", "", "", err
}
if matched {
return release.TagName, asset.BrowserDownloadUrl, nil
return release.TagName, asset.BrowserDownloadUrl, asset.Name, nil
}
}
return "", "", errors.New("No assets")
}
func DownloadFile(rawUrl string, destination string) (string, error) {
url, err := url.Parse(rawUrl)
if err != nil {
return "", err
}
segments := strings.Split(url.Path, "/")
fileName := segments[len(segments)-1]
path := filepath.Join(destination, fileName)
file, err := os.Create(path)
if err != nil {
return "", err
}
defer file.Close()
resp, err := http.Get(rawUrl)
if err != nil {
return "", err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", errors.New("Download file returned status code: " + strconv.Itoa(resp.StatusCode))
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return "", err
}
return path, nil
return "", "", "", errors.New("No assets")
}

@ -20,11 +20,17 @@ package internal
import (
"encoding/json"
"errors"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
)
type Module struct {
Source string
Name string
Org string
Repo string
@ -55,12 +61,12 @@ func BuildModules(tempDirectory string, version string, githubUsername string, g
moduleTempDirectory := GenerateTempPath()
os.MkdirAll(moduleTempDirectory, os.ModePerm)
version, downloadURL, err := GetLatestRelease(module.Org, module.Repo, module.AssetPattern, githubUsername, githubPassword)
version, downloadURL, fileName, err := GetLatestRelease(module.Source, module.Org, module.Repo, module.AssetPattern, githubUsername, githubPassword)
if err != nil {
return "", err
}
_, err = DownloadFile(downloadURL, moduleTempDirectory)
_, err = DownloadFile(downloadURL, moduleTempDirectory, fileName)
if err != nil {
return "", err
}
@ -95,3 +101,38 @@ func BuildModules(tempDirectory string, version string, githubUsername string, g
return buildMessage, nil
}
func GetLatestRelease(source string, organization string, repository string, assetPattern string, githubUsername string, githubPassword string) (string, string, string, error) {
if source == "NicholeMattera" {
return GetLatestGiteaRelease("git.nicholemattera.com", organization, repository, assetPattern)
}
return GetLatestGitHubRelease(organization, repository, assetPattern, githubUsername, githubPassword)
}
func DownloadFile(rawUrl string, destination string, fileName string) (string, error) {
path := filepath.Join(destination, fileName)
file, err := os.Create(path)
if err != nil {
return "", err
}
defer file.Close()
resp, err := http.Get(rawUrl)
if err != nil {
return "", err
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return "", errors.New("Download file returned status code: " + strconv.Itoa(resp.StatusCode))
}
defer resp.Body.Close()
_, err = io.Copy(file, resp.Body)
if err != nil {
return "", err
}
return path, nil
}

Loading…
Cancel
Save