2022-11-30 23:08:16 +09:00
|
|
|
package pages
|
|
|
|
|
|
|
|
import (
|
2022-12-01 01:37:27 +09:00
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
2022-11-30 23:08:16 +09:00
|
|
|
"codeberg.org/Odyssium/gothub/utils"
|
2022-12-01 01:37:27 +09:00
|
|
|
"github.com/carlmjohnson/requests"
|
2022-11-30 23:08:16 +09:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2022-12-01 01:37:27 +09:00
|
|
|
"github.com/gomarkdown/markdown"
|
2022-11-30 23:08:16 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
type Repo struct {
|
|
|
|
Fullname string
|
|
|
|
Description string
|
|
|
|
HtmlUrl string
|
|
|
|
Fork bool
|
|
|
|
Parent string
|
|
|
|
Stars int64
|
|
|
|
Forks int64
|
|
|
|
Watchers int64
|
|
|
|
Language string
|
|
|
|
License string
|
|
|
|
DefaultBranch string
|
|
|
|
}
|
|
|
|
|
|
|
|
type RepoFiles struct {
|
2022-12-01 01:37:27 +09:00
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
Type string
|
|
|
|
Fullname string
|
|
|
|
DefaultBranch string
|
2022-11-30 23:08:16 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleRepo(c *fiber.Ctx) error {
|
|
|
|
var repoArray []Repo
|
2022-12-01 01:37:27 +09:00
|
|
|
var repoFilesArray []RepoFiles
|
2022-11-30 23:08:16 +09:00
|
|
|
// get repo
|
|
|
|
repo := utils.GetRequest("https://api.github.com/repos/" + c.Params("user") + "/" + c.Params("repo"))
|
2022-12-01 01:37:27 +09:00
|
|
|
if repo.Get("message").String() == "Not Found" {
|
|
|
|
return c.Status(404).Render("error", fiber.Map{
|
|
|
|
"error": "Repository " + c.Params("user") + "/" + c.Params("repo") + " not found",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
repoFiles := utils.GetRequest("https://api.github.com/repos/" + c.Params("user") + "/" + c.Params("repo") + "/contents")
|
|
|
|
bruh := repoFiles.Get("#.@pretty").Array()
|
|
|
|
for _, item := range bruh {
|
|
|
|
repoFilesArray = append(repoFilesArray, RepoFiles{
|
|
|
|
Name: item.Get("path").String(),
|
|
|
|
Path: item.Get("path").String(),
|
|
|
|
Type: item.Get("type").String(),
|
|
|
|
Fullname: repo.Get("full_name").String(),
|
|
|
|
DefaultBranch: repo.Get("default_branch").String(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var readmee string
|
|
|
|
|
|
|
|
err := requests.
|
|
|
|
URL("https://raw.githubusercontent.com/" + c.Params("user") + "/" + c.Params("repo") + "/" + repo.Get("default_branch").String() + "/README.md").
|
|
|
|
ToString(&readmee).
|
|
|
|
Fetch(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
readmee = ""
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mightBeUnsafe := markdown.ToHTML([]byte(readmee), nil, nil)
|
|
|
|
|
|
|
|
// Trust Nobody
|
|
|
|
readmeOutput := UGCPolicy().SanitizeBytes(mightBeUnsafe)
|
|
|
|
|
2022-11-30 23:08:16 +09:00
|
|
|
repoArray = append(repoArray, Repo{
|
|
|
|
Fullname: repo.Get("full_name").String(),
|
|
|
|
Description: repo.Get("description").String(),
|
|
|
|
HtmlUrl: repo.Get("html_url").String(),
|
|
|
|
Fork: repo.Get("fork").Bool(),
|
|
|
|
Stars: repo.Get("stargazers_count").Int(),
|
|
|
|
Forks: repo.Get("forks_count").Int(),
|
|
|
|
Watchers: repo.Get("watchers_count").Int(),
|
|
|
|
Language: repo.Get("language").String(),
|
|
|
|
License: repo.Get("license").Get("name").String(),
|
|
|
|
Parent: repo.Get("parent").Get("full_name").String(),
|
|
|
|
DefaultBranch: repo.Get("default_branch").String(),
|
|
|
|
})
|
|
|
|
|
|
|
|
return c.Render("repo", fiber.Map{
|
2022-12-01 01:37:27 +09:00
|
|
|
"repo": repoArray,
|
|
|
|
"files": repoFilesArray,
|
|
|
|
"readme": string(readmeOutput),
|
2022-11-30 23:08:16 +09:00
|
|
|
})
|
|
|
|
}
|