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

Commit 42ed3da

Browse files
committed
alice: please: contribute: Successful creation of PR for readme
Signed-off-by: John Andersen <[email protected]>
1 parent 36dd7aa commit 42ed3da

File tree

1 file changed

+93
-17
lines changed

1 file changed

+93
-17
lines changed

entities/alice/alice/cli.py

Lines changed: 93 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ async def create_branch_if_none_exists(
154154
which has no native locking (transmistion of NFT via DIDs over abitrary
155155
channels for example).
156156
"""
157+
branches = (
158+
await dffml_feature_git.feature.operations.check_output(
159+
"git", "branch", "-r", cwd=repo.directory
160+
)
161+
).split("\n")
162+
# If there's branches then bail out
163+
if list(filter(bool, branches)):
164+
return
157165
await dffml.run_command(
158166
["git", "branch", "-M", name], cwd=repo.directory, logger=self.logger,
159167
)
@@ -216,16 +224,10 @@ async def contribute_readme_md(
216224
base: "BaseBranch",
217225
commit_message: "ReadmeCommitMessage",
218226
) -> "ReadmeBranch":
227+
branch_name: str = "alice-contribute-recommended-community-standards-readme"
219228
# Attempt multiple commands
220229
async for event, result in dffml.run_command_events(
221-
[
222-
"git",
223-
"checkout",
224-
base,
225-
"-b",
226-
# TODO DynamicName
227-
"alice-contribute-recommended-community-standards-readme",
228-
],
230+
["git", "checkout", base, "-b", branch_name,],
229231
cwd=repo.directory,
230232
logger=self.logger,
231233
raise_on_failure=False,
@@ -235,13 +237,7 @@ async def contribute_readme_md(
235237
if b"is not a commit and a branch" in result:
236238
# Retry without explict branch when repo has no commits
237239
await dffml.run_command(
238-
[
239-
"git",
240-
"checkout",
241-
"-b",
242-
# TODO DynamicName
243-
"alice-contribute-recommended-community-standards-readme",
244-
],
240+
["git", "checkout", "-b", branch_name,],
245241
cwd=repo.directory,
246242
logger=self.logger,
247243
)
@@ -256,6 +252,7 @@ async def contribute_readme_md(
256252
cwd=repo.directory,
257253
logger=self.logger,
258254
)
255+
return branch_name
259256

260257

261258
AlicePleaseContributeRecommendedCommunityStandardsExecutedFromCLI = NewType(
@@ -457,12 +454,18 @@ async def create_meta_issue(
457454
# GitHub Actions cron job to execute later). set_close_meta_issue_trigger
458455
class AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequest:
459456
ReadmePR = NewType("ReadmePR", str)
457+
Title = NewType("github.pr.title", str)
458+
Body = NewType("github.pr.body", str)
459+
WriteableGitRemoteOrigin = NewType("writable.github.remote.origin", str)
460460

461-
@staticmethod
462461
async def readme_pr(
462+
self,
463463
repo: AliceGitRepo,
464464
base: AlicePleaseContributeRecommendedCommunityStandardsOverlayGit.BaseBranch,
465+
origin: "WriteableGitRemoteOrigin",
465466
head: AlicePleaseContributeRecommendedCommunityStandardsOverlayGit.ReadmeBranch,
467+
title: "Title",
468+
body: "Body",
466469
) -> "ReadmePR":
467470
"""
468471
@@ -474,22 +477,93 @@ async def readme_pr(
474477
$ gh issue -R "${GITHUB_REPO_URL}" create --title "Recommended Community Standards (alice)" --body "${META_ISSUE_BODY}"
475478
476479
"""
480+
# Ensure an origin we can write to has an up to date version of head
481+
# with what we have locally so that GitHub can reference that branch for
482+
# the pull request.
483+
await dffml.run_command(
484+
["git", "push", "-u", origin, head], cwd=repo.directory, logger=self.logger,
485+
)
477486
await dffml.run_command(
478487
[
479488
"gh",
480489
"pr",
481490
"create",
482491
"--base",
483-
default_branch,
492+
base,
484493
"--head",
485494
head,
495+
"--title",
496+
title,
486497
"--body",
487498
body,
488499
],
489500
cwd=repo.directory,
501+
logger=self.logger,
490502
)
491503

492504

505+
class AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubWritableRemotesFromPermissions:
506+
async def already_owns_repo(
507+
self, repo: AliceGitRepo,
508+
) -> AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequest.WriteableGitRemoteOrigin:
509+
if repo.URL is None:
510+
return
511+
origins = {}
512+
async for event, result in dffml.run_command_events(
513+
["git", "remote", "-v"],
514+
cwd=repo.directory,
515+
logger=self.logger,
516+
events=[dffml.Subprocess.STDOUT_READLINE],
517+
):
518+
if event is dffml.Subprocess.STDOUT_READLINE:
519+
origin, url_and_usages = result.decode().strip().split("\t", maxsplit=2)
520+
origins[origin] = url_and_usages.split()[0]
521+
for origin, url in origins.items():
522+
async for event, result in dffml.run_command_events(
523+
[
524+
"gh",
525+
"repo",
526+
"view",
527+
url,
528+
"--json",
529+
"viewerPermission",
530+
"-q",
531+
".viewerPermission",
532+
],
533+
logger=self.logger,
534+
events=[dffml.Subprocess.STDOUT],
535+
):
536+
result = result.strip().decode()
537+
if event is dffml.Subprocess.STDOUT and result in (
538+
"ADMIN",
539+
"MAINTAIN",
540+
):
541+
return origin
542+
543+
544+
class AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequestReferenceIssue:
545+
@staticmethod
546+
async def readme_pr_body(
547+
readme_issue: AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubIssue.ReadmeIssue,
548+
) -> AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequest.Body:
549+
return f"Closes: {readme_issue}"
550+
551+
async def readme_pr_title(
552+
self,
553+
readme_issue: AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubIssue.ReadmeIssue,
554+
) -> AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequest.Title:
555+
"""
556+
Use the issue title as the pull request title
557+
"""
558+
async for event, result in dffml.run_command_events(
559+
["gh", "issue", "view", "--json", "title", "-q", ".title", readme_issue,],
560+
logger=self.logger,
561+
events=[dffml.Subprocess.STDOUT],
562+
):
563+
if event is dffml.Subprocess.STDOUT:
564+
return result.strip().decode()
565+
566+
493567
# TODO(alice) Replace with definition as system context
494568
AlicePleaseContributeCLIDataFlow = dffml.DataFlow(
495569
*itertools.chain(
@@ -513,6 +587,8 @@ async def readme_pr(
513587
AlicePleaseContributeRecommendedCommunityStandardsOverlayCLI,
514588
AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubIssue,
515589
AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequest,
590+
AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequestReferenceIssue,
591+
AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubWritableRemotesFromPermissions,
516592
]
517593
]
518594
)

0 commit comments

Comments
 (0)