Skip to content

Commit 9568df1

Browse files
authored
Merge pull request openSUSE#1979 from dmach/osc-fork-target-org
Add '--gitea-fork-org' option to 'osc fork' command
2 parents 02b342a + 4a18cf5 commit 9568df1

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

behave/features/fork.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,30 @@ Feature: `osc fork` command
44
Background:
55
Given I set working directory to "{context.osc.temp}"
66

7+
78
@destructive
89
Scenario: Fork a git repo
910
When I execute osc with args "fork test:factory test-GitPkgA"
1011
Then the exit code is 0
1112
And stdout contains " scmsync URL: "
1213
And stdout contains "/Admin/test-GitPkgA#factory"
14+
15+
16+
@destructive
17+
Scenario: Fork multiple git repos from different orgs
18+
When I execute git-obs with args "-G admin api -X POST /orgs --data '{{"username": "devel"}}'"
19+
When I execute osc with args "fork test:factory test-GitPkgA --target-project=test:devel --no-devel-project --gitea-fork-org=devel"
20+
Then the exit code is 0
21+
And stdout contains " scmsync URL: "
22+
And stdout contains "/devel/test-GitPkgA#factory"
23+
When I execute osc with args "fork test:factory test-GitPkgA --target-project=home:Admin --no-devel-project"
24+
Then the exit code is 0
25+
And stdout contains " scmsync URL: "
26+
And stdout contains "/Admin/test-GitPkgA#factory"
27+
When I execute osc with args "fork test:devel test-GitPkgA --target-project=home:Admin --no-devel-project"
28+
# there's an existing fork from a different org, the command fails
29+
Then the exit code is 1
30+
When I execute osc with args "fork test:devel test-GitPkgA --target-project=home:Admin --new-repo-name="test-GitPkgA-devel" --no-devel-project"
31+
Then the exit code is 0
32+
And stdout contains " scmsync URL: "
33+
And stdout contains "/Admin/test-GitPkgA-devel#factory"

osc/commands/fork.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ def init_arguments(self):
5151
help="Name of the package (defaults to $package)",
5252
)
5353

54+
self.add_argument(
55+
"--gitea-fork-org",
56+
help="Name of the org owning the fork",
57+
)
58+
5459
self.add_argument_new_repo_name()
5560

5661
self.add_argument(
@@ -155,14 +160,19 @@ def run(self, args):
155160
parent_branch_obj = gitea_api.Branch.get(gitea_conn, owner, repo, fork_branch)
156161

157162
try:
158-
repo_obj = gitea_api.Fork.create(gitea_conn, owner, repo, new_repo_name=args.new_repo_name)
163+
repo_obj = gitea_api.Fork.create(gitea_conn, owner, repo, new_repo_name=args.new_repo_name, target_org=args.gitea_fork_org)
159164
fork_owner = repo_obj.owner
160165
fork_repo = repo_obj.repo
161166
print(f" * Fork created: {fork_owner}/{fork_repo}")
162167
except gitea_api.ForkExists as e:
163168
fork_owner = e.fork_owner
164169
fork_repo = e.fork_repo
165170
print(f" * Fork already exists: {fork_owner}/{fork_repo}")
171+
except gitea_api.RepoExists as e:
172+
print(f"{tty.colorize('ERROR', 'red,bold')}: Repo already exists '{e.owner}/{e.repo}' and is not a fork of '{owner}/{repo}'")
173+
print(" * Consider forking with an alternative repo name")
174+
print(f" * You may also want to delete '{e.owner}/{e.repo}' and retry")
175+
sys.exit(1)
166176

167177
# XXX: implicit branch name should be forbidden; assumptions are bad
168178
fork_scmsync = urllib.parse.urlunparse(

osc/gitea_api/exceptions.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ def __init__(self, response: GiteaHTTPResponse, owner: str, repo: str):
154154
self.repo = repo
155155

156156
def __str__(self):
157-
result = f"Repo '{self.owner}/{self.repo}' already exists"
157+
result = (
158+
f"Repo '{self.owner}/{self.repo}' already exists.\n"
159+
" - If you were forking a repo, you may consider forking with an alternative repo name.\n"
160+
" - You may also want to delete the repo and fork it from elsewhere."
161+
)
158162
return result
159163

160164

0 commit comments

Comments
 (0)