gitlin/pages/repo.go

95 行
2.7 KiB
Go
Raw 通常表示 履歴

package pages
import (
"context"
"log"
2022-12-01 02:47:59 +09:00
"codeberg.org/gothub/gothub/utils"
"github.com/carlmjohnson/requests"
"github.com/gofiber/fiber/v2"
"github.com/gomarkdown/markdown"
)
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 {
Name string
Path string
Type string
Fullname string
DefaultBranch string
}
func HandleRepo(c *fiber.Ctx) error {
var repoArray []Repo
var repoFilesArray []RepoFiles
// get repo
repo := utils.GetRequest("https://api.github.com/repos/" + c.Params("user") + "/" + c.Params("repo"))
if repo.Get("message").String() == "Not Found" {
return c.Status(404).Render("error", fiber.Map{
"title": "Error",
"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
2023-02-13 18:25:39 +09:00
readmeOutput := utils.UGCPolicy().SanitizeBytes(mightBeUnsafe)
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{
"title": "Repository " + c.Params("user") + "/" + c.Params("repo"),
"repo": repoArray,
"files": repoFilesArray,
"readme": string(readmeOutput),
})
}