@@ -15517,54 +15517,64 @@ const {
15517
15517
15518
15518
const { dedent, execCmd } = __nccwpck_require__ ( 8505 )
15519
15519
15520
- const getOctokit = ( token ) => {
15521
- const Octokit = GitHub . plugin ( throttling )
15522
-
15523
- const options = getOctokitOptions ( token , {
15524
- throttle : {
15525
- onRateLimit : ( retryAfter , options ) => {
15526
- core . warning ( `Request quota exhausted for request ${ options . method } ${ options . url } ` )
15527
-
15528
- if ( options . request . retryCount === 0 ) {
15529
- // only retries once
15530
- core . info ( `Retrying after ${ retryAfter } seconds!` )
15531
- return true
15520
+ class Git {
15521
+ constructor ( ) {
15522
+ const Octokit = GitHub . plugin ( throttling )
15523
+
15524
+ const options = getOctokitOptions ( GITHUB_TOKEN , {
15525
+ throttle : {
15526
+ onRateLimit : ( retryAfter , options ) => {
15527
+ core . warning ( `Request quota exhausted for request ${ options . method } ${ options . url } ` )
15528
+
15529
+ if ( options . request . retryCount === 0 ) {
15530
+ // only retries once
15531
+ core . info ( `Retrying after ${ retryAfter } seconds!` )
15532
+ return true
15533
+ }
15534
+ } ,
15535
+ onAbuseLimit : ( retryAfter , options ) => {
15536
+ // does not retry, only logs a warning
15537
+ core . warning ( `Abuse detected for request ${ options . method } ${ options . url } ` )
15532
15538
}
15533
- } ,
15534
- onAbuseLimit : ( retryAfter , options ) => {
15535
- // does not retry, only logs a warning
15536
- core . warning ( `Abuse detected for request ${ options . method } ${ options . url } ` )
15537
15539
}
15538
- }
15539
- } )
15540
+ } )
15540
15541
15541
- return new Octokit ( options )
15542
- }
15542
+ const octokit = new Octokit ( options )
15543
+
15544
+ // We only need the rest client
15545
+ this . github = octokit . rest
15546
+ }
15547
+
15548
+ async initRepo ( repo ) {
15549
+ // Reset repo specific values
15550
+ this . existingPr = undefined
15551
+ this . prBranch = undefined
15552
+ this . baseBranch = undefined
15543
15553
15544
- const init = ( repo ) => {
15545
- let github
15546
- let baseBranch
15547
- let prBranch
15548
- let existingPr
15554
+ // Set values to current repo
15555
+ this . repo = repo
15556
+ this . workingDir = path . join ( TMP_DIR , repo . fullName )
15557
+ this . gitUrl = `https://${ GITHUB_TOKEN } @${ repo . fullName } .git`
15549
15558
15550
- const workingDir = path . join ( TMP_DIR , repo . fullName )
15551
- const gitUrl = `https://${ GITHUB_TOKEN } @${ repo . fullName } .git`
15559
+ await this . clone ( )
15560
+ await this . setIdentity ( )
15561
+ await this . getBaseBranch ( )
15562
+ }
15552
15563
15553
- const clone = ( ) => {
15554
- core . debug ( `Cloning ${ repo . fullName } into ${ workingDir } ` )
15564
+ async clone ( ) {
15565
+ core . debug ( `Cloning ${ this . repo . fullName } into ${ this . workingDir } ` )
15555
15566
15556
15567
return execCmd (
15557
- `git clone --depth 1 ${ repo . branch !== 'default' ? '--branch "' + repo . branch + '"' : '' } ${ gitUrl } ${ workingDir } `
15568
+ `git clone --depth 1 ${ this . repo . branch !== 'default' ? '--branch "' + this . repo . branch + '"' : '' } ${ this . gitUrl } ${ this . workingDir } `
15558
15569
)
15559
15570
}
15560
15571
15561
- const setIdentity = async ( client ) => {
15572
+ async setIdentity ( ) {
15562
15573
let username = GIT_USERNAME
15563
15574
let email = GIT_EMAIL
15564
- github = client
15565
15575
15566
15576
if ( email === undefined ) {
15567
- const { data } = await github . users . getAuthenticated ( )
15577
+ const { data } = await this . github . users . getAuthenticated ( )
15568
15578
email = data . email
15569
15579
username = data . login
15570
15580
}
@@ -15573,21 +15583,21 @@ const init = (repo) => {
15573
15583
15574
15584
return execCmd (
15575
15585
`git config --local user.name "${ username } " && git config --local user.email "${ email } "` ,
15576
- workingDir
15586
+ this . workingDir
15577
15587
)
15578
15588
}
15579
15589
15580
- const getBaseBranch = async ( ) => {
15581
- baseBranch = await execCmd (
15590
+ async getBaseBranch ( ) {
15591
+ this . baseBranch = await execCmd (
15582
15592
`git rev-parse --abbrev-ref HEAD` ,
15583
- workingDir
15593
+ this . workingDir
15584
15594
)
15585
15595
}
15586
15596
15587
- const createPrBranch = async ( ) => {
15597
+ async createPrBranch ( ) {
15588
15598
const prefix = BRANCH_PREFIX . replace ( 'SOURCE_REPO_NAME' , GITHUB_REPOSITORY . split ( '/' ) [ 1 ] )
15589
15599
15590
- let newBranch = path . join ( prefix , repo . branch )
15600
+ let newBranch = path . join ( prefix , this . repo . branch )
15591
15601
15592
15602
if ( OVERWRITE_EXISTING_PR === false ) {
15593
15603
newBranch += `-${ Math . round ( ( new Date ( ) ) . getTime ( ) / 1000 ) } `
@@ -15597,89 +15607,89 @@ const init = (repo) => {
15597
15607
15598
15608
await execCmd (
15599
15609
`git checkout -b "${ newBranch } "` ,
15600
- workingDir
15610
+ this . workingDir
15601
15611
)
15602
15612
15603
- prBranch = newBranch
15613
+ this . prBranch = newBranch
15604
15614
}
15605
15615
15606
- const add = async ( file ) => {
15616
+ async add ( file ) {
15607
15617
return execCmd (
15608
15618
`git add -f ${ file } ` ,
15609
- workingDir
15619
+ this . workingDir
15610
15620
)
15611
15621
}
15612
15622
15613
- const hasChanges = async ( ) => {
15623
+ async hasChanges ( ) {
15614
15624
const statusOutput = await execCmd (
15615
15625
`git status --porcelain` ,
15616
- workingDir
15626
+ this . workingDir
15617
15627
)
15618
15628
15619
15629
return parse ( statusOutput ) . length !== 0
15620
15630
}
15621
15631
15622
- const commit = async ( msg ) => {
15632
+ async commit ( msg ) {
15623
15633
let message = msg !== undefined ? msg : `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY } `
15624
15634
if ( COMMIT_BODY ) {
15625
15635
message += `\n\n${ COMMIT_BODY } `
15626
15636
}
15627
15637
return execCmd (
15628
15638
`git commit -m "${ message } "` ,
15629
- workingDir
15639
+ this . workingDir
15630
15640
)
15631
15641
}
15632
15642
15633
- const status = async ( ) => {
15643
+ async status ( ) {
15634
15644
return execCmd (
15635
15645
`git status` ,
15636
- workingDir
15646
+ this . workingDir
15637
15647
)
15638
15648
}
15639
15649
15640
- const push = async ( ) => {
15650
+ async push ( ) {
15641
15651
return execCmd (
15642
- `git push ${ gitUrl } --force` ,
15643
- workingDir
15652
+ `git push ${ this . gitUrl } --force` ,
15653
+ this . workingDir
15644
15654
)
15645
15655
}
15646
15656
15647
- const findExistingPr = async ( ) => {
15648
- const { data } = await github . pulls . list ( {
15649
- owner : repo . user ,
15650
- repo : repo . name ,
15657
+ async findExistingPr ( ) {
15658
+ const { data } = await this . github . pulls . list ( {
15659
+ owner : this . repo . user ,
15660
+ repo : this . repo . name ,
15651
15661
state : 'open' ,
15652
- head : `${ repo . user } :${ prBranch } `
15662
+ head : `${ this . repo . user } :${ this . prBranch } `
15653
15663
} )
15654
15664
15655
- existingPr = data [ 0 ]
15665
+ this . existingPr = data [ 0 ]
15656
15666
15657
- return existingPr
15667
+ return this . existingPr
15658
15668
}
15659
15669
15660
- const setPrWarning = async ( ) => {
15661
- await github . pulls . update ( {
15662
- owner : repo . user ,
15663
- repo : repo . name ,
15664
- pull_number : existingPr . number ,
15670
+ async setPrWarning ( ) {
15671
+ await this . github . pulls . update ( {
15672
+ owner : this . repo . user ,
15673
+ repo : this . repo . name ,
15674
+ pull_number : this . existingPr . number ,
15665
15675
body : dedent ( `
15666
15676
⚠️ This PR is being automatically resynced ⚠️
15667
15677
15668
- ${ existingPr . body }
15678
+ ${ this . existingPr . body }
15669
15679
` )
15670
15680
} )
15671
15681
}
15672
15682
15673
- const removePrWarning = async ( ) => {
15674
- await github . pulls . update ( {
15675
- owner : repo . user ,
15676
- repo : repo . name ,
15677
- pull_number : existingPr . number ,
15678
- body : existingPr . body . replace ( '⚠️ This PR is being automatically resynced ⚠️' , '' )
15683
+ async removePrWarning ( ) {
15684
+ await this . github . pulls . update ( {
15685
+ owner : this . repo . user ,
15686
+ repo : this . repo . name ,
15687
+ pull_number : this . existingPr . number ,
15688
+ body : this . existingPr . body . replace ( '⚠️ This PR is being automatically resynced ⚠️' , '' )
15679
15689
} )
15680
15690
}
15681
15691
15682
- const createOrUpdatePr = async ( changedFiles ) => {
15692
+ async createOrUpdatePr ( changedFiles ) {
15683
15693
const body = dedent ( `
15684
15694
Synced local file(s) with [${ GITHUB_REPOSITORY } ](https://github.com/${ GITHUB_REPOSITORY } ).
15685
15695
@@ -15690,13 +15700,13 @@ const init = (repo) => {
15690
15700
This PR was created automatically by the [repo-file-sync-action](https://github.com/BetaHuhn/repo-file-sync-action) workflow run [#${ process . env . GITHUB_RUN_ID || 0 } ](https://github.com/${ GITHUB_REPOSITORY } /actions/runs/${ process . env . GITHUB_RUN_ID || 0 } )
15691
15701
` )
15692
15702
15693
- if ( existingPr ) {
15703
+ if ( this . existingPr ) {
15694
15704
core . info ( `Overwriting existing PR` )
15695
15705
15696
- const { data } = await github . pulls . update ( {
15697
- owner : repo . user ,
15698
- repo : repo . name ,
15699
- pull_number : existingPr . number ,
15706
+ const { data } = await this . github . pulls . update ( {
15707
+ owner : this . repo . user ,
15708
+ repo : this . repo . name ,
15709
+ pull_number : this . existingPr . number ,
15700
15710
body : body
15701
15711
} )
15702
15712
@@ -15705,41 +15715,41 @@ const init = (repo) => {
15705
15715
15706
15716
core . info ( `Creating new PR` )
15707
15717
15708
- const { data } = await github . pulls . create ( {
15709
- owner : repo . user ,
15710
- repo : repo . name ,
15718
+ const { data } = await this . github . pulls . create ( {
15719
+ owner : this . repo . user ,
15720
+ repo : this . repo . name ,
15711
15721
title : `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY } ` ,
15712
15722
body : body ,
15713
- head : prBranch ,
15714
- base : baseBranch
15723
+ head : this . prBranch ,
15724
+ base : this . baseBranch
15715
15725
} )
15716
15726
15727
+ this . existingPr = data
15728
+
15717
15729
return data
15718
15730
}
15719
15731
15720
- return {
15721
- workingDir,
15722
- clone,
15723
- setIdentity,
15724
- getBaseBranch,
15725
- createPrBranch,
15726
- add,
15727
- hasChanges,
15728
- commit,
15729
- status,
15730
- push,
15731
- findExistingPr,
15732
- setPrWarning,
15733
- removePrWarning,
15734
- createOrUpdatePr
15732
+ async addPrLabels ( labels ) {
15733
+ await this . github . issues . addLabels ( {
15734
+ owner : this . repo . user ,
15735
+ repo : this . repo . name ,
15736
+ issue_number : this . existingPr . number ,
15737
+ labels : labels
15738
+ } )
15735
15739
}
15736
- }
15737
15740
15738
- module . exports = {
15739
- init,
15740
- getOctokit
15741
+ async addPrAssignees ( assignees ) {
15742
+ await this . github . issues . addAssignees ( {
15743
+ owner : this . repo . user ,
15744
+ repo : this . repo . name ,
15745
+ issue_number : this . existingPr . number ,
15746
+ assignees : assignees
15747
+ } )
15748
+ }
15741
15749
}
15742
15750
15751
+ module . exports = Git
15752
+
15743
15753
/***/ } ) ,
15744
15754
15745
15755
/***/ 8505 :
@@ -16017,7 +16027,6 @@ const { forEach, dedent, addTrailingSlash, pathIsDirectory, copy, remove } = __n
16017
16027
16018
16028
const {
16019
16029
parseConfig,
16020
- GITHUB_TOKEN ,
16021
16030
COMMIT_EACH_FILE ,
16022
16031
COMMIT_PREFIX ,
16023
16032
PR_LABELS ,
@@ -16030,8 +16039,8 @@ const {
16030
16039
} = __nccwpck_require__ ( 4570 )
16031
16040
16032
16041
const run = async ( ) => {
16033
- const client = Git . getOctokit ( GITHUB_TOKEN )
16034
- // const client = github.getOctokit(GITHUB_TOKEN )
16042
+ // Reuse octokit for each repo
16043
+ const git = new Git ( )
16035
16044
16036
16045
const repos = await parseConfig ( )
16037
16046
@@ -16043,12 +16052,9 @@ const run = async () => {
16043
16052
core . info ( `Branch : ${ item . repo . branch } ` )
16044
16053
core . info ( ' ' )
16045
16054
try {
16046
- const git = Git . init ( item . repo )
16047
16055
16048
16056
// Clone and setup the git repository locally
16049
- await git . clone ( )
16050
- await git . setIdentity ( client )
16051
- await git . getBaseBranch ( )
16057
+ await git . initRepo ( item . repo )
16052
16058
16053
16059
let existingPr
16054
16060
if ( SKIP_PR === false ) {
@@ -16170,22 +16176,12 @@ const run = async () => {
16170
16176
16171
16177
if ( PR_LABELS !== undefined && PR_LABELS . length > 0 ) {
16172
16178
core . info ( `Adding label(s) "${ PR_LABELS . join ( ', ' ) } " to PR` )
16173
- await client . issues . addLabels ( {
16174
- owner : item . repo . user ,
16175
- repo : item . repo . name ,
16176
- issue_number : pullRequest . number ,
16177
- labels : PR_LABELS
16178
- } )
16179
+ await git . addPrLabels ( PR_LABELS )
16179
16180
}
16180
16181
16181
16182
if ( ASSIGNEES !== undefined && ASSIGNEES . length > 0 ) {
16182
16183
core . info ( `Adding assignee(s) "${ ASSIGNEES . join ( ', ' ) } " to PR` )
16183
- await client . issues . addAssignees ( {
16184
- owner : item . repo . user ,
16185
- repo : item . repo . name ,
16186
- issue_number : pullRequest . number ,
16187
- assignees : ASSIGNEES
16188
- } )
16184
+ await git . addPrAssignees ( ASSIGNEES )
16189
16185
}
16190
16186
}
16191
16187
0 commit comments