Add support for viewing different branches of a repo

This commit is contained in:
Arya Kiran
2023-03-16 22:15:21 +05:30
parent 3e6d0bec06
commit 319ddc505b
4 changed files with 16 additions and 7 deletions
+9 -5
View File
@@ -26,6 +26,7 @@ type Repo struct {
Readme string
Link string
Tags []string
Branch []string
}
type RepoFiles struct {
@@ -39,8 +40,11 @@ type RepoFiles struct {
func HandleRepo(c *fiber.Ctx) error {
var repoArray []Repo
var repoFilesArray []RepoFiles
resp, statusErr := http.Get("https://github.com/" + c.Params("user") + "/" + c.Params("repo"))
branchExists := ""
if strings.Count(c.Params("branch"), "")-1 > 0 {
branchExists = "/tree/" + c.Params("branch")
}
resp, statusErr := http.Get("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + branchExists)
if statusErr != nil {
log.Println(statusErr)
}
@@ -48,7 +52,7 @@ func HandleRepo(c *fiber.Ctx) error {
// I need a better way to do this
return c.Status(404).Render("error", fiber.Map{
"title": "Error",
"error": "Repository " + c.Params("user") + "/" + c.Params("repo") + " not found",
"error": "Repository " + c.Params("user") + "/" + c.Params("repo") + branchExists + " not found",
})
}
@@ -105,7 +109,7 @@ func HandleRepo(c *fiber.Ctx) error {
})
})
sc.Visit("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + "/")
sc.Visit("https://github.com/" + c.Params("user") + "/" + c.Params("repo") + branchExists)
// Add scrape-based info to repoArray
repoArray = append(repoArray, Scrape)
@@ -124,7 +128,7 @@ func HandleRepo(c *fiber.Ctx) error {
readmeOutput := utils.UGCPolicy().SanitizeBytes(mightBeUnsafe)
return c.Render("repo", fiber.Map{
"title": "Repository " + c.Params("user") + "/" + c.Params("repo"),
"title": "Repository " + c.Params("user") + "/" + c.Params("repo") + branchExists,
"repo": repoArray,
"files": repoFilesArray,
"readme": string(readmeOutput),
+1
View File
@@ -108,6 +108,7 @@ func Serve() {
app.Get("/:user/:repo", pages.HandleRepo)
app.Get("/:user/:repo/blob/:branch/+", pages.FileView)
app.Get("/:user/:repo/tree/:branch/+", pages.DirView)
app.Get("/:user/:repo/tree/:branch", pages.HandleRepo)
app.Get("/download/:user/:repo/:branch", func(c *fiber.Ctx) error {
utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch")+".zip")
return nil
+3 -1
View File
@@ -6,7 +6,9 @@
<a href="https://github.com/{{.Fullname}}/tree/{{.DirName}}" class="button"
>View on GitHub</a
>
<a href="/{{.Fullname}}" class="button">Back to {{.Fullname}}</a>
<a href="/{{.Fullname}}/tree/{{.Branch}}" class="button"
>Back to {{.Fullname}}</a
>
</div>
<div class="userProfile">
+3 -1
View File
@@ -11,7 +11,9 @@
}
</style>
<div class="downloadParent">
<a href="/{{.fullname}}" class="button">Back to {{.fullname}}</a>
<a href="/{{.fullname}}/tree/{{.branch}}" class="button"
>Back to {{.fullname}}</a
>
</div>
<div class="userReadme">