diff --git a/modules/structs/repo_branch.go b/modules/structs/repo_branch.go
index a9aa1d330a184..55c98d60b9124 100644
--- a/modules/structs/repo_branch.go
+++ b/modules/structs/repo_branch.go
@@ -133,3 +133,11 @@ type EditBranchProtectionOption struct {
type UpdateBranchProtectionPriories struct {
IDs []int64 `json:"ids"`
}
+
+type MergeUpstreamRequest struct {
+ Branch string `json:"branch"`
+}
+
+type MergeUpstreamResponse struct {
+ MergeStyle string `json:"merge_type"`
+}
diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go
index 2f943d306cc5c..b1a42a85e6a82 100644
--- a/routers/api/v1/api.go
+++ b/routers/api/v1/api.go
@@ -1190,6 +1190,7 @@ func Routes() *web.Router {
m.Get("/archive/*", reqRepoReader(unit.TypeCode), repo.GetArchive)
m.Combo("/forks").Get(repo.ListForks).
Post(reqToken(), reqRepoReader(unit.TypeCode), bind(api.CreateForkOption{}), repo.CreateFork)
+ m.Post("/merge-upstream", reqToken(), mustNotBeArchived, reqRepoWriter(unit.TypeCode), bind(api.MergeUpstreamRequest{}), repo.MergeUpstream)
m.Group("/branches", func() {
m.Get("", repo.ListBranches)
m.Get("/*", repo.GetBranch)
diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go
index 2fcdd0205875a..87d17dba875fc 100644
--- a/routers/api/v1/repo/branch.go
+++ b/routers/api/v1/repo/branch.go
@@ -18,6 +18,7 @@ import (
"code.gitea.io/gitea/modules/optional"
repo_module "code.gitea.io/gitea/modules/repository"
api "code.gitea.io/gitea/modules/structs"
+ "code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/context"
@@ -1186,3 +1187,47 @@ func UpdateBranchProtectionPriories(ctx *context.APIContext) {
ctx.Status(http.StatusNoContent)
}
+
+func MergeUpstream(ctx *context.APIContext) {
+ // swagger:operation POST /repos/{owner}/{repo}/merge-upstream repository repoMergeUpstream
+ // ---
+ // summary: Merge a branch from upstream
+ // 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: body
+ // in: body
+ // schema:
+ // "$ref": "#/definitions/MergeUpstreamRequest"
+ // responses:
+ // "200":
+ // "$ref": "#/responses/MergeUpstreamResponse"
+ // "400":
+ // "$ref": "#/responses/error"
+ // "404":
+ // "$ref": "#/responses/notFound"
+ form := web.GetForm(ctx).(*api.MergeUpstreamRequest)
+ mergeStyle, err := repo_service.MergeUpstream(ctx, ctx.Doer, ctx.Repo.Repository, form.Branch)
+ if err != nil {
+ if errors.Is(err, util.ErrInvalidArgument) {
+ ctx.Error(http.StatusBadRequest, "MergeUpstream", err)
+ return
+ } else if errors.Is(err, util.ErrNotExist) {
+ ctx.Error(http.StatusNotFound, "MergeUpstream", err)
+ return
+ }
+ ctx.Error(http.StatusInternalServerError, "MergeUpstream", err)
+ return
+ }
+ ctx.JSON(http.StatusOK, &api.MergeUpstreamResponse{MergeStyle: mergeStyle})
+}
diff --git a/routers/api/v1/swagger/repo.go b/routers/api/v1/swagger/repo.go
index b9d2a0217cd4f..f754c80a5b3d5 100644
--- a/routers/api/v1/swagger/repo.go
+++ b/routers/api/v1/swagger/repo.go
@@ -448,3 +448,15 @@ type swaggerCompare struct {
// in:body
Body api.Compare `json:"body"`
}
+
+// swagger:response MergeUpstreamRequest
+type swaggerMergeUpstreamRequest struct {
+ // in:body
+ Body api.MergeUpstreamRequest `json:"body"`
+}
+
+// swagger:response MergeUpstreamResponse
+type swaggerMergeUpstreamResponse struct {
+ // in:body
+ Body api.MergeUpstreamResponse `json:"body"`
+}
diff --git a/services/repository/merge_upstream.go b/services/repository/merge_upstream.go
index 85ca8f7e31a7d..a6a30a710ab23 100644
--- a/services/repository/merge_upstream.go
+++ b/services/repository/merge_upstream.go
@@ -1,4 +1,4 @@
-// Copyright 2024 The Gitea Authors. All rights reserved.
+// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repository
diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl
index 82a301da2fe99..fb37d45ce8de2 100644
--- a/templates/swagger/v1_json.tmpl
+++ b/templates/swagger/v1_json.tmpl
@@ -10867,6 +10867,52 @@
}
}
},
+ "/repos/{owner}/{repo}/merge-upstream": {
+ "post": {
+ "produces": [
+ "application/json"
+ ],
+ "tags": [
+ "repository"
+ ],
+ "summary": "Merge a branch from upstream",
+ "operationId": "repoMergeUpstream",
+ "parameters": [
+ {
+ "type": "string",
+ "description": "owner of the repo",
+ "name": "owner",
+ "in": "path",
+ "required": true
+ },
+ {
+ "type": "string",
+ "description": "name of the repo",
+ "name": "repo",
+ "in": "path",
+ "required": true
+ },
+ {
+ "name": "body",
+ "in": "body",
+ "schema": {
+ "$ref": "#/definitions/MergeUpstreamRequest"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "$ref": "#/responses/MergeUpstreamResponse"
+ },
+ "400": {
+ "$ref": "#/responses/error"
+ },
+ "404": {
+ "$ref": "#/responses/notFound"
+ }
+ }
+ }
+ },
"/repos/{owner}/{repo}/milestones": {
"get": {
"produces": [
@@ -22827,6 +22873,26 @@
"x-go-name": "MergePullRequestForm",
"x-go-package": "code.gitea.io/gitea/services/forms"
},
+ "MergeUpstreamRequest": {
+ "type": "object",
+ "properties": {
+ "branch": {
+ "type": "string",
+ "x-go-name": "Branch"
+ }
+ },
+ "x-go-package": "code.gitea.io/gitea/modules/structs"
+ },
+ "MergeUpstreamResponse": {
+ "type": "object",
+ "properties": {
+ "merge_type": {
+ "type": "string",
+ "x-go-name": "MergeStyle"
+ }
+ },
+ "x-go-package": "code.gitea.io/gitea/modules/structs"
+ },
"MigrateRepoOptions": {
"description": "MigrateRepoOptions options for migrating repository's\nthis is used to interact with api v1",
"type": "object",
@@ -26008,6 +26074,18 @@
"type": "string"
}
},
+ "MergeUpstreamRequest": {
+ "description": "",
+ "schema": {
+ "$ref": "#/definitions/MergeUpstreamRequest"
+ }
+ },
+ "MergeUpstreamResponse": {
+ "description": "",
+ "schema": {
+ "$ref": "#/definitions/MergeUpstreamResponse"
+ }
+ },
"Milestone": {
"description": "Milestone",
"schema": {
diff --git a/tests/integration/repo_merge_upstream_test.go b/tests/integration/repo_merge_upstream_test.go
new file mode 100644
index 0000000000000..293be51144ea5
--- /dev/null
+++ b/tests/integration/repo_merge_upstream_test.go
@@ -0,0 +1,80 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package integration
+
+import (
+ "fmt"
+ "net/http"
+ "net/url"
+ "testing"
+
+ auth_model "code.gitea.io/gitea/models/auth"
+ repo_model "code.gitea.io/gitea/models/repo"
+ "code.gitea.io/gitea/models/unittest"
+ user_model "code.gitea.io/gitea/models/user"
+ api "code.gitea.io/gitea/modules/structs"
+ "code.gitea.io/gitea/modules/util"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestRepoMergeUpstream(t *testing.T) {
+ onGiteaRun(t, func(*testing.T, *url.URL) {
+ forkUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 4})
+
+ baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
+ baseUser := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: baseRepo.OwnerID})
+
+ checkFileContent := func(exp string) {
+ req := NewRequest(t, "GET", fmt.Sprintf("/%s/test-repo-fork/raw/branch/master/new-file.txt", forkUser.Name))
+ resp := MakeRequest(t, req, http.StatusOK)
+ require.Equal(t, exp, resp.Body.String())
+ }
+
+ session := loginUser(t, forkUser.Name)
+ token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
+
+ // create a fork
+ req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/%s/forks", baseUser.Name, baseRepo.Name), &api.CreateForkOption{
+ Name: util.ToPointer("test-repo-fork"),
+ }).AddTokenAuth(token)
+ MakeRequest(t, req, http.StatusAccepted)
+ forkRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerID: forkUser.ID, Name: "test-repo-fork"})
+
+ // add a file in base repo
+ require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-1"))
+
+ // the repo shows a prompt to "sync fork"
+ resp := session.MakeRequest(t, NewRequestf(t, "GET", "/%s/test-repo-fork", forkUser.Name), http.StatusOK)
+ htmlDoc := NewHTMLParser(t, resp.Body)
+ respMsg, _ := htmlDoc.Find(".ui.message").Html()
+ assert.Contains(t, respMsg, `This branch is 1 commit behind user2/repo1:master`)
+ mergeUpstreamLink := htmlDoc.Find("button[data-url*='merge-upstream']").AttrOr("data-url", "")
+ require.NotEmpty(t, mergeUpstreamLink)
+
+ // click the "sync fork" button
+ req = NewRequestWithValues(t, "POST", mergeUpstreamLink, map[string]string{"_csrf": GetUserCSRFToken(t, session)})
+ session.MakeRequest(t, req, http.StatusOK)
+ checkFileContent("test-content-1")
+
+ // update the files
+ require.NoError(t, createOrReplaceFileInBranch(forkUser, forkRepo, "new-file-other.txt", "master", "test-content-other"))
+ require.NoError(t, createOrReplaceFileInBranch(baseUser, baseRepo, "new-file.txt", "master", "test-content-2"))
+ resp = session.MakeRequest(t, NewRequestf(t, "GET", "/%s/test-repo-fork", forkUser.Name), http.StatusOK)
+ htmlDoc = NewHTMLParser(t, resp.Body)
+ respMsg, _ = htmlDoc.Find(".ui.message:not(.positive)").Html()
+ assert.Contains(t, respMsg, `The base branch user2/repo1:master has new changes`)
+ // and do the merge-upstream by API
+ req = NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/test-repo-fork/merge-upstream", forkUser.Name), &api.MergeUpstreamRequest{
+ Branch: "master",
+ }).AddTokenAuth(token)
+ resp = MakeRequest(t, req, http.StatusOK)
+ checkFileContent("test-content-2")
+
+ var mergeResp api.MergeUpstreamResponse
+ DecodeJSON(t, resp, &mergeResp)
+ assert.Equal(t, "merge", mergeResp.MergeStyle)
+ })
+}