StaleBot for Gitea
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

63 lines
1.8 KiB

// staletea
// Copyright (C) 2020 Jonas Franz
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package loader
import (
"bytes"
"code.gitea.io/sdk/gitea"
"gitea.com/jonasfranz/staletea/config"
"gopkg.in/yaml.v2"
)
func NewGiteaLoader(url, token string) Loader {
return &GiteaLoader{
client: gitea.NewClient(url, token),
}
}
type GiteaLoader struct {
client *gitea.Client
}
func (l *GiteaLoader) LoadRepositories() ([]*Repository, error) {
repos, err := l.client.ListMyRepos()
if err != nil {
return nil, err
}
convertedRepos := make([]*Repository, len(repos))
for index, repo := range repos {
convertedRepos[index] = &Repository{
ID: repo.ID,
Owner: repo.Owner.UserName,
Name: repo.Name,
ConfigBranch: repo.DefaultBranch,
}
}
return convertedRepos, nil
}
func (l *GiteaLoader) DownloadConfig(repo *Repository) (*RepositoryConfig, error) {
file, err := l.client.GetFile(repo.Owner, repo.Name, repo.ConfigBranch, config.RepositoryConfigFileName.Get().(string))
if err != nil {
return nil, err
}
repoConfig := new(RepositoryConfig)
if err := yaml.NewDecoder(bytes.NewReader(file)).Decode(repoConfig); err != nil {
return nil, err
}
return repoConfig, nil
}