File opensuse-lfs-fallback-to-obs.patch of Package gitea

commit e053ad2fa8be275fb56517c35e6badc434660af0
Author: Stephan Kulow <coolo@suse.de>
Date:   Fri Sep 2 14:34:34 2022 +0200

    Redirect old LFS oids to api.opensuse.org
    
    This is very specific to gitea.opensuse.org deployment where we want to avoid
    copying all the old tar revisions from OBS to git LFS. So if the LFS OID is
    not on the gitea store, we check the imported historic packages and redirect
    there.

Index: gitea-1.23.1/modules/setting/lfs.go
===================================================================
--- gitea-1.23.1.orig/modules/setting/lfs.go
+++ gitea-1.23.1/modules/setting/lfs.go
@@ -22,6 +22,7 @@ var LFS = struct {
 	MaxFileSize    int64         `ini:"LFS_MAX_FILE_SIZE"`
 	LocksPagingNum int           `ini:"LFS_LOCKS_PAGING_NUM"`
 	MaxBatchSize   int           `ini:"LFS_MAX_BATCH_SIZE"`
+	FallbackToOBS  bool          `ini:"FALLBACK_TO_OBS"`
 
 	Storage *Storage
 }{}
Index: gitea-1.23.1/services/lfs/server.go
===================================================================
--- gitea-1.23.1.orig/services/lfs/server.go
+++ gitea-1.23.1/services/lfs/server.go
@@ -272,6 +272,14 @@ func BatchHandler(ctx *context.Context)
 				}
 			}
 
+			if setting.LFS.FallbackToOBS && (!exists || meta == nil) {
+				obsresp := obsResponseOrNil(rc, p)
+				if obsresp != nil {
+					responseObjects = append(responseObjects, obsresp)
+					continue
+				}
+			}
+
 			if !exists || meta == nil {
 				err = &lfs_module.ObjectError{
 					Code:    http.StatusNotFound,
@@ -464,6 +472,27 @@ func getAuthenticatedRepository(ctx *con
 	return repository
 }
 
+func obsResponseOrNil(rc *requestContext, pointer lfs_module.Pointer) *lfs_module.ObjectResponse {
+	url := fmt.Sprintf("http://localhost:9999/check/%s/%d", pointer.Oid, pointer.Size)
+	resp, err := http.Get(url)
+	if err != nil {
+		log.Debug("URL %s returned error", url)
+		return nil
+	}
+	defer resp.Body.Close()
+	body, err := io.ReadAll(resp.Body)
+	if err != nil {
+		log.Debug("URL %s didn't return body", url)
+		return nil
+	}
+	link := string(body)
+	rep := &lfs_module.ObjectResponse{Pointer: pointer}
+	rep.Actions = make(map[string]*lfs_module.Link)
+	header := make(map[string]string)
+	rep.Actions["download"] = &lfs_module.Link{Href: link, Header: header}
+	return rep
+}
+
 func buildObjectResponse(rc *requestContext, pointer lfs_module.Pointer, download, upload bool, err *lfs_module.ObjectError) *lfs_module.ObjectResponse {
 	rep := &lfs_module.ObjectResponse{Pointer: pointer}
 	if err != nil {
Index: gitea-1.23.1/routers/web/repo/view.go
===================================================================
--- gitea-1.23.1.orig/routers/web/repo/view.go
+++ gitea-1.23.1/routers/web/repo/view.go
@@ -86,7 +86,9 @@ func getFileReader(ctx gocontext.Context
 
 	meta, err := git_model.GetLFSMetaObjectByOid(ctx, repoID, pointer.Oid)
 	if err != nil { // fallback to plain file
-		log.Warn("Unable to access LFS pointer %s in repo %d: %v", pointer.Oid, repoID, err)
+		// OBS-LFS bridge should be consulted in the git_model.GetLFSMetaObjectByOid() ??
+		// bug: https://github.com/openSUSE/openSUSE-git/issues/83
+		log.Info("Unable to access LFS pointer %s in repo %d: %v", pointer.Oid, repoID, err)
 		return buf, dataRc, &fileInfo{isTextFile, false, blob.Size(), nil, st}, nil
 	}
 
openSUSE Build Service is sponsored by