@@ -154,6 +154,14 @@ async def create_branch_if_none_exists(
154
154
which has no native locking (transmistion of NFT via DIDs over abitrary
155
155
channels for example).
156
156
"""
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
157
165
await dffml .run_command (
158
166
["git" , "branch" , "-M" , name ], cwd = repo .directory , logger = self .logger ,
159
167
)
@@ -216,16 +224,10 @@ async def contribute_readme_md(
216
224
base : "BaseBranch" ,
217
225
commit_message : "ReadmeCommitMessage" ,
218
226
) -> "ReadmeBranch" :
227
+ branch_name : str = "alice-contribute-recommended-community-standards-readme"
219
228
# Attempt multiple commands
220
229
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 ,],
229
231
cwd = repo .directory ,
230
232
logger = self .logger ,
231
233
raise_on_failure = False ,
@@ -235,13 +237,7 @@ async def contribute_readme_md(
235
237
if b"is not a commit and a branch" in result :
236
238
# Retry without explict branch when repo has no commits
237
239
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 ,],
245
241
cwd = repo .directory ,
246
242
logger = self .logger ,
247
243
)
@@ -256,6 +252,7 @@ async def contribute_readme_md(
256
252
cwd = repo .directory ,
257
253
logger = self .logger ,
258
254
)
255
+ return branch_name
259
256
260
257
261
258
AlicePleaseContributeRecommendedCommunityStandardsExecutedFromCLI = NewType (
@@ -457,12 +454,18 @@ async def create_meta_issue(
457
454
# GitHub Actions cron job to execute later). set_close_meta_issue_trigger
458
455
class AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequest :
459
456
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 )
460
460
461
- @staticmethod
462
461
async def readme_pr (
462
+ self ,
463
463
repo : AliceGitRepo ,
464
464
base : AlicePleaseContributeRecommendedCommunityStandardsOverlayGit .BaseBranch ,
465
+ origin : "WriteableGitRemoteOrigin" ,
465
466
head : AlicePleaseContributeRecommendedCommunityStandardsOverlayGit .ReadmeBranch ,
467
+ title : "Title" ,
468
+ body : "Body" ,
466
469
) -> "ReadmePR" :
467
470
"""
468
471
@@ -474,22 +477,93 @@ async def readme_pr(
474
477
$ gh issue -R "${GITHUB_REPO_URL}" create --title "Recommended Community Standards (alice)" --body "${META_ISSUE_BODY}"
475
478
476
479
"""
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
+ )
477
486
await dffml .run_command (
478
487
[
479
488
"gh" ,
480
489
"pr" ,
481
490
"create" ,
482
491
"--base" ,
483
- default_branch ,
492
+ base ,
484
493
"--head" ,
485
494
head ,
495
+ "--title" ,
496
+ title ,
486
497
"--body" ,
487
498
body ,
488
499
],
489
500
cwd = repo .directory ,
501
+ logger = self .logger ,
490
502
)
491
503
492
504
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
+
493
567
# TODO(alice) Replace with definition as system context
494
568
AlicePleaseContributeCLIDataFlow = dffml .DataFlow (
495
569
* itertools .chain (
@@ -513,6 +587,8 @@ async def readme_pr(
513
587
AlicePleaseContributeRecommendedCommunityStandardsOverlayCLI ,
514
588
AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubIssue ,
515
589
AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequest ,
590
+ AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubPullRequestReferenceIssue ,
591
+ AlicePleaseContributeRecommendedCommunityStandardsOverlayGitHubWritableRemotesFromPermissions ,
516
592
]
517
593
]
518
594
)
0 commit comments