 2457f5ff22
			
		
	
	
	2457f5ff22
	
	
	
		
			
			- Massive replacement of changing `code.gitea.io/gitea` to `forgejo.org`. - Resolves forgejo/discussions#258 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7337 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Reviewed-by: Beowulf <beowulf@beocode.eu> Reviewed-by: Panagiotis "Ivory" Vasilopoulos <git@n0toose.net> Co-authored-by: Gusted <postmaster@gusted.xyz> Co-committed-by: Gusted <postmaster@gusted.xyz>
		
			
				
	
	
		
			69 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			69 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2021 The Gitea Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package private
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"fmt"
 | |
| 	"net/http"
 | |
| 
 | |
| 	repo_model "forgejo.org/models/repo"
 | |
| 	"forgejo.org/modules/gitrepo"
 | |
| 	"forgejo.org/modules/log"
 | |
| 	"forgejo.org/modules/private"
 | |
| 	gitea_context "forgejo.org/services/context"
 | |
| )
 | |
| 
 | |
| // This file contains common functions relating to setting the Repository for the internal routes
 | |
| 
 | |
| // RepoAssignment assigns the repository and gitrepository to the private context
 | |
| func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc {
 | |
| 	ownerName := ctx.Params(":owner")
 | |
| 	repoName := ctx.Params(":repo")
 | |
| 
 | |
| 	repo := loadRepository(ctx, ownerName, repoName)
 | |
| 	if ctx.Written() {
 | |
| 		// Error handled in loadRepository
 | |
| 		return nil
 | |
| 	}
 | |
| 
 | |
| 	gitRepo, err := gitrepo.OpenRepository(ctx, repo)
 | |
| 	if err != nil {
 | |
| 		log.Error("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err)
 | |
| 		ctx.JSON(http.StatusInternalServerError, private.Response{
 | |
| 			Err: fmt.Sprintf("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err),
 | |
| 		})
 | |
| 		return nil
 | |
| 	}
 | |
| 
 | |
| 	ctx.Repo = &gitea_context.Repository{
 | |
| 		Repository: repo,
 | |
| 		GitRepo:    gitRepo,
 | |
| 	}
 | |
| 
 | |
| 	// We opened it, we should close it
 | |
| 	cancel := func() {
 | |
| 		// If it's been set to nil then assume someone else has closed it.
 | |
| 		if ctx.Repo.GitRepo != nil {
 | |
| 			ctx.Repo.GitRepo.Close()
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return cancel
 | |
| }
 | |
| 
 | |
| func loadRepository(ctx *gitea_context.PrivateContext, ownerName, repoName string) *repo_model.Repository {
 | |
| 	repo, err := repo_model.GetRepositoryByOwnerAndName(ctx, ownerName, repoName)
 | |
| 	if err != nil {
 | |
| 		log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
 | |
| 		ctx.JSON(http.StatusInternalServerError, private.Response{
 | |
| 			Err: fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err),
 | |
| 		})
 | |
| 		return nil
 | |
| 	}
 | |
| 	if repo.OwnerName == "" {
 | |
| 		repo.OwnerName = ownerName
 | |
| 	}
 | |
| 	return repo
 | |
| }
 |