Skip to content

Commit 20b7257

Browse files
authored
Merge pull request #1945 from timbrel/create-github-repo
2 parents 742d3be + bfe3543 commit 20b7257

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

Default.sublime-commands

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@
136136
"caption": "github: add fork as remote",
137137
"command": "gs_github_add_fork_as_remote"
138138
},
139+
{
140+
"caption": "github: create repo",
141+
"command": "gs_github_create_repo"
142+
},
139143
{
140144
"caption": "github: create fork",
141145
"command": "gs_github_create_fork"

github/commands/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .commit import *
33
from .configure import *
44
from .create_fork import *
5+
from .create_repo import *
56
from .add_fork_as_remote import *
67
from .pull_request import *
78
from .open_issue import *

github/commands/create_repo.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from functools import partial
2+
import os
3+
4+
from GitSavvy.common import util
5+
from GitSavvy.core.base_commands import GsWindowCommand
6+
from GitSavvy.core.ui_mixins.input_panel import show_single_line_input_panel
7+
from GitSavvy.core.utils import show_panel
8+
from GitSavvy.core.runtime import on_worker
9+
from GitSavvy.github import github
10+
11+
from GitSavvy.core.base_commands import Args, GsCommand, Kont
12+
13+
14+
__all__ = (
15+
"gs_github_create_repo",
16+
)
17+
18+
19+
def ask_for_repo_name(cmd: GsCommand, args: Args, done: Kont) -> None:
20+
suggestion = (
21+
os.path.basename(folders[0])
22+
if (folders := cmd.window.folders())
23+
else ""
24+
)
25+
26+
def on_done(name: str) -> None:
27+
if name:
28+
done(name)
29+
30+
show_single_line_input_panel("New Repo Name:", suggestion, on_done)
31+
32+
33+
def get_github_user_token(cmd: GsCommand, args: Args, done: Kont) -> None:
34+
fqdn = "github.com"
35+
token = cmd.savvy_settings.get("api_tokens", {}).get(fqdn)
36+
if not token:
37+
cmd.window.status_message(f"Abort, no API token found in the settings for {fqdn}.")
38+
return
39+
done(token)
40+
41+
42+
class gs_github_create_repo(GsWindowCommand):
43+
defaults = {
44+
"token": get_github_user_token,
45+
"name": ask_for_repo_name
46+
}
47+
48+
@on_worker
49+
def run(self, token: str, name: str) -> None:
50+
payload = github.create_user_repo(token, name)
51+
self.window.status_message("The repo was created successfully.")
52+
urls = [payload["clone_url"], payload["ssh_url"]]
53+
54+
def on_remote_name(name: str) -> None:
55+
show_panel(self.window, urls, partial(on_url, name))
56+
57+
def on_url(name: str, idx: int) -> None:
58+
url = urls[idx]
59+
self.git("remote", "add", name, url)
60+
self.window.status_message("The new remote was added successfully.")
61+
util.view.refresh_gitsavvy_interfaces(self.window)
62+
63+
show_single_line_input_panel("Add repo as", "origin", on_remote_name)

github/github.py

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
GitHub methods that are functionally separate from anything Sublime-related.
33
"""
44

5+
from __future__ import annotations
56
import re
67
from webbrowser import open as open_in_browser
78
from functools import partial
@@ -230,3 +231,19 @@ def create_fork(github_repo: GitHubRepo, default_branch_only: bool = False):
230231
github_repo,
231232
{"default_branch_only": default_branch_only}
232233
)
234+
235+
236+
def create_user_repo(token: str, repo_name: str) -> dict:
237+
return create_repo(token, None, repo_name)
238+
239+
240+
def create_repo(token: str, org: str | None, repo_name: str) -> dict:
241+
host = "api.github.com"
242+
path = f"/orgs/{org}/repos" if org else "/user/repos"
243+
auth = (token, "x-oauth-basic")
244+
payload = {"name": repo_name}
245+
246+
response = interwebs.post(host, 443, path, https=True, auth=auth, payload=payload)
247+
validate_response(response, method="POST")
248+
249+
return response.payload

0 commit comments

Comments
 (0)