Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 0761977

Browse files
committed
alice: please: contribute: Attempting checkout of default branch if none exists
Signed-off-by: John Andersen <[email protected]>
1 parent c54a203 commit 0761977

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

entities/alice/alice/cli.py

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class AlicePleaseContributeCLIConfig:
8888
class AlicePleaseContributeRecommendedCommunityStandards:
8989
# TODO SystemContext __new__ auto populate config to have upstream set to
9090
# dataflow generated from methods in this class with memory orchestarator.
91+
ReadmePath = NewType("ReadmePath", object)
9192
RepoString = NewType("repo.string", str)
9293
ReadmeContents = NewType("repo.directory.readme.contents", str)
9394
HasReadme = NewType("repo.directory.readme.exists", bool)
@@ -108,28 +109,27 @@ async def guess_repo_string_is_directory(
108109
return
109110
return AliceGitRepo(directory=repo_string, URL=None)
110111

111-
def has_readme(self, repo: AliceGitRepo,) -> "HasReadme":
112-
return pathlib.Path(repo.directory, "README.md").exists()
113-
114112
# TODO Run this system context where readme contexts is given on CLI or
115113
# overriden via disabling of static overlay and application of overlay to
116114
# generate contents dynamiclly.
117-
def create_readme_file(
115+
def create_readme_file_if_not_exists(
118116
self,
119117
repo: AliceGitRepo,
120-
has_readme: "HasReadme",
121118
readme_contents: Optional["ReadmeContents"] = "# My Awesome Project's README",
122-
):
119+
) -> "ReadmePath":
123120
# Do not create readme if it already exists
124-
if has_readme:
125-
return
126-
pathlib.Path(repo.directory, "README.md").write_text(readme_contents)
121+
path = pathlib.Path(repo.directory, "README.md")
122+
if path.exists():
123+
return path
124+
path.write_text(readme_contents)
125+
return path
127126

128127

129128
# An overlay which could be installed if you have dffml-feature-git
130129
# (aka dffml-operations-git) installed.
131130
class AlicePleaseContributeRecommendedCommunityStandardsOverlayOperationsGit:
132131
GuessedGitURL = NewType("guessed.git.url", bool)
132+
DefaultBranchName = NewType("default.branch.name", str)
133133

134134
# The operations we use defined elsewhere
135135
check_if_valid_git_repository_URL = (
@@ -140,6 +140,25 @@ class AlicePleaseContributeRecommendedCommunityStandardsOverlayOperationsGit:
140140
dffml_feature_git.feature.operations.git_repo_default_branch
141141
)
142142

143+
async def create_branch_if_none_exists(
144+
self, repo: AliceGitRepo, name: Optional["DefaultBranchName"] = "main",
145+
) -> dffml_feature_git.feature.definitions.GitBranchType:
146+
"""
147+
If there are no branches, the git_repo_default_branch operation will
148+
return None, aka there si no default branch. Therefore, in this
149+
operation, we check if there are any branches at all, and if there are
150+
not we create a new branch. We could optionally facilitate interaction
151+
of multiple similar operations which wish to create a default branch if
152+
none exist by creating a new defintion which is locked which could be
153+
used to synchronise communication aka request for lock from some service
154+
which has no native locking (transmistion of NFT via DIDs over abitrary
155+
channels for example).
156+
"""
157+
await dffml.run_command(
158+
["git", "branch", "-M", name], cwd=repo.directory, logger=self.logger,
159+
)
160+
return name
161+
143162
def guess_repo_string_is_url(
144163
self,
145164
repo_string: AlicePleaseContributeRecommendedCommunityStandards.RepoString,
@@ -160,6 +179,8 @@ def guessed_repo_string_is_operations_git_url(
160179
) -> dffml_feature_git.feature.definitions.URLType:
161180
return repo_url
162181

182+
183+
class AlicePleaseContributeRecommendedCommunityStandardsOverlayAliceOperationsGit:
163184
def git_repo_to_alice_git_repo(
164185
repo: dffml_feature_git.feature.definitions.git_repository,
165186
) -> AliceGitRepo:
@@ -303,7 +324,6 @@ class AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubIssue:
303324
304325
"""
305326

306-
ReadmePath = NewType("ReadmePath", str)
307327
ReadmeIssue = NewType("ReadmeIssue", str)
308328
ReadmeIssueTitle = NewType("ReadmeIssueTitle", str)
309329
ReadmeIssueBody = NewType("ReadmeIssueBody", str)
@@ -350,11 +370,13 @@ def readme_commit_message(
350370
"""
351371
).lstrip()
352372

373+
# TODO(alice) There is a bug with Optional which can be revield by use here
353374
@staticmethod
354375
def meta_issue_body(
355376
repo: AliceGitRepo,
377+
base: AlicePleaseContributeRecommendedCommunityStandardsOverlayGit.BaseBranch,
378+
readme_path: AlicePleaseContributeRecommendedCommunityStandards.ReadmePath,
356379
readme_issue: Optional["ReadmeIssue"] = None,
357-
readme_path: Optional["ReadmePath"] = None,
358380
) -> "MetaIssueBody":
359381
"""
360382
>>> AlicePleaseContributeRecommendedCommunityStandardsGitHubIssueOverlay.meta_issue_body(
@@ -369,8 +391,8 @@ def meta_issue_body(
369391
"""
370392
return "\n".join(
371393
[
372-
"- [x] [README]({repo.URL}/blob/{base}/{readme_path.relative_to(repo.directory).as_posix()})"
373-
if readme_path is not None
394+
f"- [x] [README]({repo.URL}/blob/{base}/{readme_path.relative_to(repo.directory).as_posix()})"
395+
if readme_issue is None
374396
else f"- [ ] {readme_issue}",
375397
]
376398
)
@@ -456,6 +478,7 @@ async def readme_pr(
456478
AlicePleaseContributeRecommendedCommunityStandards,
457479
AlicePleaseContributeRecommendedCommunityStandardsOverlayGit,
458480
AlicePleaseContributeRecommendedCommunityStandardsOverlayOperationsGit,
481+
AlicePleaseContributeRecommendedCommunityStandardsOverlayAliceOperationsGit,
459482
AlicePleaseContributeRecommendedCommunityStandardsOverlayCLI,
460483
AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubIssue,
461484
AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequest,

0 commit comments

Comments
 (0)