forgejo/routers/api/v1/repo/sync_fork.go
JakobDev 8296a23d79
Some checks failed
/ release (push) Waiting to run
testing / backend-checks (push) Has been skipped
testing / frontend-checks (push) Has been skipped
testing / test-unit (push) Has been skipped
testing / test-e2e (push) Has been skipped
testing / test-mysql (push) Has been skipped
testing / test-pgsql (push) Has been skipped
testing / test-sqlite (push) Has been skipped
testing / test-remote-cacher (redis) (push) Has been skipped
testing / test-remote-cacher (valkey) (push) Has been skipped
testing / test-remote-cacher (garnet) (push) Has been skipped
testing / test-remote-cacher (redict) (push) Has been skipped
testing / security-check (push) Has been skipped
Integration tests for the release process / release-simulation (push) Has been cancelled
feat: sync forks (#2364)
This allows syncing a branch of a fork with a branch of the base repo. It looks like this:
![grafik](/attachments/4508920c-7d0b-4330-9083-e3048733e38d)
This is only possible, if the fork don't have commits that are not in the main repo.

The feature is already working, but it is missing Finetuning, a better API, translations and tests, so this is currently WIP. It is also not tested with go-git.

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Features
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/2364): <!--number 2364 --><!--line 0 --><!--description c3luYyBmb3Jrcw==-->sync forks<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/2364
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: JakobDev <jakobdev@gmx.de>
Co-committed-by: JakobDev <jakobdev@gmx.de>
2025-04-07 07:00:38 +00:00

185 lines
4.8 KiB
Go

// Copyright 2025 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"net/http"
git_model "forgejo.org/models/git"
"forgejo.org/services/context"
repo_service "forgejo.org/services/repository"
)
func getSyncForkInfo(ctx *context.APIContext, branch string) {
if !ctx.Repo.Repository.IsFork {
ctx.Error(http.StatusBadRequest, "NoFork", "The Repo must be a fork")
return
}
syncForkInfo, err := repo_service.GetSyncForkInfo(ctx, ctx.Repo.Repository, branch)
if err != nil {
if git_model.IsErrBranchNotExist(err) {
ctx.NotFound(err, branch)
return
}
ctx.Error(http.StatusInternalServerError, "GetSyncForkInfo", err)
return
}
ctx.JSON(http.StatusOK, syncForkInfo)
}
// SyncForkBranchInfo returns information about syncing the default fork branch with the base branch
func SyncForkDefaultInfo(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/sync_fork repository repoSyncForkDefaultInfo
// ---
// summary: Gets information about syncing the fork default branch with the base branch
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/SyncForkInfo"
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
getSyncForkInfo(ctx, ctx.Repo.Repository.DefaultBranch)
}
// SyncForkBranchInfo returns information about syncing a fork branch with the base branch
func SyncForkBranchInfo(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/sync_fork/{branch} repository repoSyncForkBranchInfo
// ---
// summary: Gets information about syncing a fork branch with the base branch
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: branch
// in: path
// description: The branch
// type: string
// required: true
// responses:
// "200":
// "$ref": "#/responses/SyncForkInfo"
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
getSyncForkInfo(ctx, ctx.Params("branch"))
}
func syncForkBranch(ctx *context.APIContext, branch string) {
if !ctx.Repo.Repository.IsFork {
ctx.Error(http.StatusBadRequest, "NoFork", "The Repo must be a fork")
return
}
syncForkInfo, err := repo_service.GetSyncForkInfo(ctx, ctx.Repo.Repository, branch)
if err != nil {
if git_model.IsErrBranchNotExist(err) {
ctx.NotFound(err, branch)
return
}
ctx.Error(http.StatusInternalServerError, "GetSyncForkInfo", err)
return
}
if !syncForkInfo.Allowed {
ctx.Error(http.StatusBadRequest, "NotAllowed", "You can't sync this branch")
return
}
err = repo_service.SyncFork(ctx, ctx.Doer, ctx.Repo.Repository, branch)
if err != nil {
ctx.Error(http.StatusInternalServerError, "SyncFork", err)
return
}
ctx.Status(http.StatusNoContent)
}
// SyncForkBranch syncs the default of a fork with the base branch
func SyncForkDefault(ctx *context.APIContext) {
// swagger:operation POST /repos/{owner}/{repo}/sync_fork repository repoSyncForkDefault
// ---
// summary: Syncs the default branch of a fork with the base branch
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
syncForkBranch(ctx, ctx.Repo.Repository.DefaultBranch)
}
// SyncForkBranch syncs a fork branch with the base branch
func SyncForkBranch(ctx *context.APIContext) {
// swagger:operation POST /repos/{owner}/{repo}/sync_fork/{branch} repository repoSyncForkBranch
// ---
// summary: Syncs a fork branch with the base branch
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: branch
// in: path
// description: The branch
// type: string
// required: true
// responses:
// "204":
// "$ref": "#/responses/empty"
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
syncForkBranch(ctx, ctx.Params("branch"))
}