forked from Gitlink/gitea-1156
122 lines
2.9 KiB
Go
122 lines
2.9 KiB
Go
package repo
|
|
|
|
import (
|
|
"net/http"
|
|
"sort"
|
|
|
|
"code.gitea.io/gitea/models"
|
|
"code.gitea.io/gitea/modules/context"
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
webWiki "code.gitea.io/gitea/routers/web/repo"
|
|
wiki_service "code.gitea.io/gitea/services/wiki"
|
|
)
|
|
|
|
func ListWikiPages(ctx *context.APIContext) {
|
|
// swagger:operation GET /repos/{owner}/{repo}/wikies repository repoWikiList
|
|
// ---
|
|
// summary: List the wikies in a repository
|
|
// produces:
|
|
// - application/json
|
|
// parameters:
|
|
// - name: owner
|
|
// in: path
|
|
// description: owner of the repo
|
|
// type: string
|
|
// required: true
|
|
// - name: repo
|
|
// in: path
|
|
// description: name of the repo
|
|
// type: string
|
|
// required: true
|
|
// responses:
|
|
// "200":
|
|
// "$ref": "#/responses/WikiList"
|
|
|
|
if !ctx.Repo.Repository.HasWiki() {
|
|
return
|
|
}
|
|
|
|
wikiCloneWiki := ctx.Repo.Repository.WikiCloneLink()
|
|
wikiRepo, commit, err := webWiki.FindWikiRepoCommit(ctx.Context)
|
|
if err != nil {
|
|
if wikiRepo != nil {
|
|
wikiRepo.Close()
|
|
}
|
|
return
|
|
}
|
|
defer func() {
|
|
if wikiRepo != nil {
|
|
wikiRepo.Close()
|
|
}
|
|
}()
|
|
|
|
entries, err := commit.ListEntries()
|
|
if err != nil {
|
|
ctx.ServerError("entries", err)
|
|
return
|
|
}
|
|
|
|
pages := make([]api.WikiesResponse, 0, len(entries))
|
|
for _, entry := range entries {
|
|
if !entry.IsRegular() {
|
|
continue
|
|
}
|
|
lastCommit, firstCommit, _ := wikiRepo.GetFirstAndLastCommitByPath("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("FilenameToName", err)
|
|
return
|
|
}
|
|
pages = append(pages, api.WikiesResponse{
|
|
WikiCloneLink: api.CloneLink{
|
|
HTTPS: wikiCloneWiki.HTTPS,
|
|
SSH: wikiCloneWiki.SSH,
|
|
},
|
|
WikiMeta: api.WikiMeta{
|
|
Name: wikiName,
|
|
Commit: api.WikiCommit{
|
|
Author: api.WikiUser{
|
|
Name: lastCommit.Author.Name,
|
|
Email: lastCommit.Author.Email,
|
|
When: lastCommit.Author.When.Unix(),
|
|
},
|
|
Commiter: api.WikiUser{
|
|
Name: lastCommit.Committer.Name,
|
|
Email: lastCommit.Committer.Email,
|
|
When: lastCommit.Author.When.Unix(),
|
|
},
|
|
ID: lastCommit.ID.String(),
|
|
Message: lastCommit.Message(),
|
|
},
|
|
FirstCommit: api.WikiCommit{
|
|
Author: api.WikiUser{
|
|
Name: firstCommit.Author.Name,
|
|
Email: firstCommit.Author.Email,
|
|
When: firstCommit.Author.When.Unix(),
|
|
},
|
|
Commiter: api.WikiUser{
|
|
Name: firstCommit.Committer.Name,
|
|
Email: firstCommit.Committer.Email,
|
|
When: firstCommit.Author.When.Unix(),
|
|
},
|
|
ID: firstCommit.ID.String(),
|
|
Message: firstCommit.Message(),
|
|
},
|
|
},
|
|
})
|
|
}
|
|
//sort by time
|
|
sort.Slice(pages, func(i, j int) bool {
|
|
return pages[i].FirstCommit.Author.When > pages[j].FirstCommit.Author.When
|
|
})
|
|
ctx.JSON(http.StatusOK, pages)
|
|
|
|
}
|