aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJob <LordChunk@users.noreply.github.com>2024-10-04 19:12:48 +0200
committerGitHub <noreply@github.com>2024-10-04 13:12:48 -0400
commit7e68bc88238104d2ee8b5a877fc1ad437f1778a4 (patch)
treed4fbb1c153f61417988705803a294201ab17a447
parent0bd75390f535c2f7f6422117476c953a178103ba (diff)
downloadgitea-7e68bc88238104d2ee8b5a877fc1ad437f1778a4.tar.gz
gitea-7e68bc88238104d2ee8b5a877fc1ad437f1778a4.zip
Fix PR creation on forked repositories (#31863)
Resolves #20475
-rw-r--r--routers/api/v1/repo/pull.go17
-rw-r--r--tests/integration/pull_create_test.go27
2 files changed, 41 insertions, 3 deletions
diff --git a/routers/api/v1/repo/pull.go b/routers/api/v1/repo/pull.go
index 7eb4a8b8a2..4e3de77032 100644
--- a/routers/api/v1/repo/pull.go
+++ b/routers/api/v1/repo/pull.go
@@ -1124,9 +1124,20 @@ func parseCompareInfo(ctx *context.APIContext, form api.CreatePullRequestOption)
// Check if current user has fork of repository or in the same repository.
headRepo := repo_model.GetForkedRepo(ctx, headUser.ID, baseRepo.ID)
if headRepo == nil && !isSameRepo {
- log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
- ctx.NotFound("GetForkedRepo")
- return nil, nil, nil, "", ""
+ err := baseRepo.GetBaseRepo(ctx)
+ if err != nil {
+ ctx.Error(http.StatusInternalServerError, "GetBaseRepo", err)
+ return nil, nil, nil, "", ""
+ }
+
+ // Check if baseRepo's base repository is the same as headUser's repository.
+ if baseRepo.BaseRepo == nil || baseRepo.BaseRepo.OwnerID != headUser.ID {
+ log.Trace("parseCompareInfo[%d]: does not have fork or in same repository", baseRepo.ID)
+ ctx.NotFound("GetBaseRepo")
+ return nil, nil, nil, "", ""
+ }
+ // Assign headRepo so it can be used below.
+ headRepo = baseRepo.BaseRepo
}
var headGitRepo *git.Repository
diff --git a/tests/integration/pull_create_test.go b/tests/integration/pull_create_test.go
index 5a06a7817f..9812d2073d 100644
--- a/tests/integration/pull_create_test.go
+++ b/tests/integration/pull_create_test.go
@@ -199,3 +199,30 @@ func TestPullBranchDelete(t *testing.T) {
session.MakeRequest(t, req, http.StatusOK)
})
}
+
+/*
+Setup:
+The base repository is: user2/repo1
+Fork repository to: user1/repo1
+Push extra commit to: user2/repo1, which changes README.md
+Create a PR on user1/repo1
+
+Test checks:
+Check if pull request can be created from base to the fork repository.
+*/
+func TestPullCreatePrFromBaseToFork(t *testing.T) {
+ onGiteaRun(t, func(t *testing.T, u *url.URL) {
+ sessionFork := loginUser(t, "user1")
+ testRepoFork(t, sessionFork, "user2", "repo1", "user1", "repo1", "")
+
+ // Edit base repository
+ sessionBase := loginUser(t, "user2")
+ testEditFile(t, sessionBase, "user2", "repo1", "master", "README.md", "Hello, World (Edited)\n")
+
+ // Create a PR
+ resp := testPullCreateDirectly(t, sessionFork, "user1", "repo1", "master", "user2", "repo1", "master", "This is a pull title")
+ // check the redirected URL
+ url := test.RedirectURL(resp)
+ assert.Regexp(t, "^/user1/repo1/pulls/[0-9]*$", url)
+ })
+}
olor: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/* Copyright 2013-2014 Pierre Ossman <ossman@cendio.se> for Cendio AB
 * 
 * This 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 2 of the License, or
 * (at your option) any later version.
 * 
 * This software 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 software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 * USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

#ifdef WIN32
#include <windows.h>
#else
#include <sys/resource.h>
#include <sys/time.h>
#endif

#include "util.h"

#ifdef WIN32
typedef struct {
  FILETIME kernelTime;
  FILETIME userTime;
} syscounter_t;
#else
typedef struct rusage syscounter_t;
#endif

static syscounter_t _globalCounter[2];
static cpucounter_t globalCounter = _globalCounter;

void startCpuCounter(void)
{
  startCpuCounter(globalCounter);
}

void endCpuCounter(void)
{
  endCpuCounter(globalCounter);
}

double getCpuCounter(void)
{
  return getCpuCounter(globalCounter);
}

cpucounter_t newCpuCounter(void)
{
  syscounter_t *c;

  c = (syscounter_t*)malloc(sizeof(syscounter_t) * 2);
  if (c == nullptr)
    return nullptr;

  memset(c, 0, sizeof(syscounter_t) * 2);

  return c;
}

void freeCpuCounter(cpucounter_t c)
{
  free(c);
}

static void measureCpu(syscounter_t *counter)
{
#ifdef WIN32
  FILETIME dummy1, dummy2;

  GetProcessTimes(GetCurrentProcess(), &dummy1, &dummy2,
                  &counter->kernelTime, &counter->userTime);
#else
  getrusage(RUSAGE_SELF, counter);
#endif
}

void startCpuCounter(cpucounter_t c)
{
  syscounter_t *s = (syscounter_t*)c;
  measureCpu(&s[0]);
}

void endCpuCounter(cpucounter_t c)
{
  syscounter_t *s = (syscounter_t*)c;
  measureCpu(&s[1]);
}

double getCpuCounter(cpucounter_t c)
{
  syscounter_t *s = (syscounter_t*)c;
  double sysSeconds, userSeconds;

#ifdef WIN32
  uint64_t counters[2];

  counters[0] = (uint64_t)s[0].kernelTime.dwHighDateTime << 32 |
                s[0].kernelTime.dwLowDateTime;
  counters[1] = (uint64_t)s[1].kernelTime.dwHighDateTime << 32 |
                s[1].kernelTime.dwLowDateTime;

  sysSeconds = (double)(counters[1] - counters[0]) / 10000000.0;

  counters[0] = (uint64_t)s[0].userTime.dwHighDateTime << 32 |
                s[0].userTime.dwLowDateTime;
  counters[1] = (uint64_t)s[1].userTime.dwHighDateTime << 32 |
                s[1].userTime.dwLowDateTime;

  userSeconds = (double)(counters[1] - counters[0]) / 10000000.0;
#else
  sysSeconds = (double)(s[1].ru_stime.tv_sec -
                        s[0].ru_stime.tv_sec);
  sysSeconds += (double)(s[1].ru_stime.tv_usec -
                         s[0].ru_stime.tv_usec) / 1000000.0;

  userSeconds = (double)(s[1].ru_utime.tv_sec -
                         s[0].ru_utime.tv_sec);
  userSeconds += (double)(s[1].ru_utime.tv_usec -
                          s[0].ru_utime.tv_usec) / 1000000.0;
#endif

  return sysSeconds + userSeconds;
}

#ifdef WIN32
static LARGE_INTEGER timeStart, timeEnd;
#else
static struct timeval timeStart, timeEnd;
#endif

void startTimeCounter(void)
{
#ifdef WIN32
  QueryPerformanceCounter(&timeStart);
#else
  gettimeofday(&timeStart, nullptr);
#endif
}

void endTimeCounter(void)
{
#ifdef WIN32
  QueryPerformanceCounter(&timeEnd);
#else
  gettimeofday(&timeEnd, nullptr);
#endif
}

double getTimeCounter(void)
{
  double time;

#ifdef WIN32
  LARGE_INTEGER freq;

  QueryPerformanceFrequency(&freq);

  time = timeEnd.QuadPart - timeStart.QuadPart;
  time = time / freq.QuadPart;
#else
  time = (double)timeEnd.tv_sec - timeStart.tv_sec;
  time += (double)(timeEnd.tv_usec - timeStart.tv_usec) / 1000000.0;
#endif

  return time;
}