115 lines
2.9 KiB
Go
115 lines
2.9 KiB
Go
package repo
|
|
|
|
import (
|
|
"net/http"
|
|
"sort"
|
|
|
|
"code.gitea.io/gitea/models"
|
|
"code.gitea.io/gitea/modules/context"
|
|
"code.gitea.io/gitea/modules/git"
|
|
wiki_service "code.gitea.io/gitea/services/wiki"
|
|
hat_git "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git"
|
|
hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"
|
|
)
|
|
|
|
func ListWikiPages(ctx *context.APIContext) {
|
|
if !ctx.Repo.Repository.HasWiki() {
|
|
return
|
|
}
|
|
|
|
wikiCloneWiki := ctx.Repo.Repository.WikiCloneLink()
|
|
wikiRepo, commit, err := findWikiRepoCommit(ctx.Context)
|
|
defer func() {
|
|
if wikiRepo != nil {
|
|
wikiRepo.Close()
|
|
}
|
|
}()
|
|
if err != nil {
|
|
ctx.ServerError("findWikiRepoCommit", err)
|
|
return
|
|
}
|
|
|
|
entries, err := commit.ListEntries()
|
|
if err != nil {
|
|
ctx.ServerError("ListEntries", err)
|
|
return
|
|
}
|
|
|
|
pages := make([]hat_api.WikiesResponse, 0, len(entries))
|
|
for _, entry := range entries {
|
|
if !entry.IsRegular() {
|
|
continue
|
|
}
|
|
lastCommit, firstCommit, err := hat_git.GetFirstAndLastCommitByPath(wikiRepo, "master", entry.Name())
|
|
if err != nil {
|
|
ctx.ServerError("GetCommitByPath", err)
|
|
return
|
|
}
|
|
wikiName, err := wiki_service.FilenameToName(entry.Name())
|
|
if err != nil {
|
|
if models.IsErrWikiInvalidFileName(err) {
|
|
continue
|
|
}
|
|
ctx.ServerError("wiki_service.FilenameToName", err)
|
|
return
|
|
}
|
|
pages = append(pages, hat_api.WikiesResponse{
|
|
WikiCloneLink: hat_api.CloneLink{
|
|
HTTPS: wikiCloneWiki.HTTPS,
|
|
SSH: wikiCloneWiki.SSH,
|
|
},
|
|
WikiMeta: hat_api.WikiMeta{
|
|
Name: wikiName,
|
|
Commit: hat_api.WikiCommit{
|
|
Author: hat_api.WikiUser{
|
|
Name: lastCommit.Author.Name,
|
|
Email: lastCommit.Author.Email,
|
|
When: lastCommit.Author.When.Unix(),
|
|
},
|
|
Commiter: hat_api.WikiUser{
|
|
Name: lastCommit.Committer.Name,
|
|
Email: lastCommit.Committer.Email,
|
|
When: lastCommit.Author.When.Unix(),
|
|
},
|
|
ID: lastCommit.ID.String(),
|
|
Message: lastCommit.Message(),
|
|
},
|
|
FirstCommit: hat_api.WikiCommit{
|
|
Author: hat_api.WikiUser{
|
|
Name: firstCommit.Author.Name,
|
|
Email: firstCommit.Author.Email,
|
|
When: firstCommit.Author.When.Unix(),
|
|
},
|
|
Commiter: hat_api.WikiUser{
|
|
Name: firstCommit.Committer.Name,
|
|
Email: firstCommit.Committer.Email,
|
|
When: firstCommit.Author.When.Unix(),
|
|
},
|
|
ID: firstCommit.ID.String(),
|
|
Message: firstCommit.Message(),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
sort.Slice(pages, func(i, j int) bool {
|
|
return pages[i].FirstCommit.Author.When > pages[j].FirstCommit.Author.When
|
|
})
|
|
|
|
ctx.JSON(http.StatusOK, pages)
|
|
}
|
|
|
|
func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
|
|
wikiRepo, err := git.OpenRepository(ctx, ctx.Repo.Repository.WikiPath())
|
|
if err != nil {
|
|
ctx.ServerError("OpenRepository", err)
|
|
return nil, nil, err
|
|
}
|
|
|
|
commit, err := wikiRepo.GetBranchCommit("master")
|
|
if err != nil {
|
|
return wikiRepo, nil, err
|
|
}
|
|
return wikiRepo, commit, nil
|
|
}
|