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.
 
 

102 lines
2.7 KiB

// staletea
// Copyright (C) 2019 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 web
import (
"fmt"
"gitea.com/jonasfranz/staletea/config"
"gitea.com/jonasfranz/staletea/models"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
)
func showDashboard(ctx *gin.Context) {
session := sessions.Default(ctx)
user, ok := session.Get("user").(*models.User)
if !ok || user == nil {
ctx.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/login", config.BaseURL.Get().(string)))
return
}
activatedRepos, err := models.FindRepositoriesByUserID(user.ID)
if err != nil {
_ = ctx.AbortWithError(500, err)
return
}
remoteRepos, err := user.GiteaClient().ListMyRepos()
if err != nil {
_ = ctx.AbortWithError(500, err)
return
}
combinedRepos := make(map[int64]*models.Repository, len(remoteRepos))
for _, repo := range remoteRepos {
combinedRepos[repo.ID] = &models.Repository{
ID: repo.ID,
UserID: user.ID,
Owner: repo.Owner.UserName,
Name: repo.Name,
Activated: false,
}
}
for _, repo := range activatedRepos {
combinedRepos[repo.ID] = repo
}
data := map[string]interface{}{
"repos": combinedRepos,
"user": user,
}
session.Set("repos", combinedRepos)
if err := session.Save(); err != nil {
ctx.AbortWithError(500, err)
return
}
ctx.HTML(200, "dashboard.tmpl", data)
}
func handleActivate(ctx *gin.Context) {
session := sessions.Default(ctx)
user, ok := session.Get("user").(*models.User)
if !ok || user == nil {
ctx.Redirect(http.StatusTemporaryRedirect, fmt.Sprintf("%s/login", config.BaseURL.Get().(string)))
return
}
repos, ok := session.Get("repos").(map[int64]*models.Repository)
if !ok {
ctx.Status(500)
return
}
activatedRepos := make([]*models.Repository, 0, len(repos))
for _, repo := range repos {
repoID := strconv.FormatInt(repo.ID, 10)
if ctx.PostForm(repoID) != "on" {
continue
}
repo.Activated = true
activatedRepos = append(activatedRepos, repo)
}
if err := models.SetActivatedRepositories(activatedRepos, user); err != nil {
ctx.AbortWithError(500, err)
return
}
}