api: new methods to get branches and commit info
This commit is contained in:
parent
30e31f9a08
commit
818a93de1b
86
api.go
86
api.go
@ -45,6 +45,34 @@ type MarkdownRequest struct {
|
||||
Wiki bool
|
||||
}
|
||||
|
||||
type BranchCommit struct {
|
||||
ID string `json:"id"`
|
||||
Message string `json:"message"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
type Branch struct {
|
||||
Name string `json:"name"`
|
||||
Commit BranchCommit `json:"commit"`
|
||||
}
|
||||
|
||||
type AuthorInfo struct {
|
||||
Name string `json:"name"`
|
||||
Email string `json:"email"`
|
||||
Date time.Time `json:"date"`
|
||||
}
|
||||
|
||||
type CommitListEntryCommit struct {
|
||||
Message string `json:"message"`
|
||||
Author AuthorInfo `json:"author"`
|
||||
Committer AuthorInfo `json:"committer"`
|
||||
}
|
||||
|
||||
type CommitListEntry struct {
|
||||
ID string `json:"sha"`
|
||||
Commit CommitListEntryCommit `json:"commit"`
|
||||
}
|
||||
|
||||
func (this *Application) apiRequest(ctx context.Context, endpoint string, target interface{}) error {
|
||||
err := this.apiSem.Acquire(ctx, 1)
|
||||
if err != nil {
|
||||
@ -75,6 +103,64 @@ func (this *Application) apiRequest(ctx context.Context, endpoint string, target
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *Application) branches(ctx context.Context, repo string) ([]Branch, error) {
|
||||
var branches []Branch
|
||||
|
||||
err := this.apiRequest(ctx, `api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/branches`, &branches)
|
||||
if err != nil {
|
||||
return nil, err // e.g. ctx closed
|
||||
}
|
||||
|
||||
return branches, nil
|
||||
}
|
||||
|
||||
func (this *Application) commitsPage(ctx context.Context, repo, ref string, page, limit int) ([]CommitListEntry, error) {
|
||||
var ret []CommitListEntry
|
||||
|
||||
err := this.apiRequest(ctx, fmt.Sprintf(`api/v1/repos/%s/%s/commits?page=%d&limit=%d`, url.PathEscape(this.cfg.Gitea.Org), url.PathEscape(repo), page, limit), &ret)
|
||||
if err != nil {
|
||||
return nil, err // e.g. ctx closed
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (this *Application) commits(ctx context.Context, repo, ref string) ([]CommitListEntry, error) {
|
||||
var ret []CommitListEntry
|
||||
|
||||
nextPage := 1 // Counting starts at 1
|
||||
for {
|
||||
page, err := this.commitsPage(ctx, repo, ref, nextPage, 300)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(page) == 0 && len(ret) > 0 {
|
||||
break // Found enough already
|
||||
}
|
||||
|
||||
ret = append(ret, page...)
|
||||
nextPage += 1
|
||||
}
|
||||
|
||||
if len(ret) == 0 {
|
||||
return nil, fmt.Errorf("no commits found")
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (this *Application) oldestCommit(ctx context.Context, repo, ref string) (CommitListEntry, error) {
|
||||
cc, err := this.commits(ctx, repo, ref)
|
||||
if err != nil {
|
||||
return CommitListEntry{}, err
|
||||
}
|
||||
|
||||
// Take the last listed entry
|
||||
// TODO maybe need to iterate/sort?
|
||||
return cc[len(cc)-1], nil
|
||||
}
|
||||
|
||||
// reposPage gets a single page of the list of Git repositories in this organisation.
|
||||
func (this *Application) reposPage(ctx context.Context, page, limit int) ([]Repo, error) {
|
||||
var repos []Repo
|
||||
|
Loading…
Reference in New Issue
Block a user