Skip to content

Commit ec2d589

Browse files
committed
♻️ Combine GitHub and Git into one class
1 parent 04ec06a commit ec2d589

File tree

3 files changed

+232
-240
lines changed

3 files changed

+232
-240
lines changed

dist/index.js

+116-120
Original file line numberDiff line numberDiff line change
@@ -15517,54 +15517,64 @@ const {
1551715517

1551815518
const { dedent, execCmd } = __nccwpck_require__(8505)
1551915519

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 }`)
1553215538
}
15533-
},
15534-
onAbuseLimit: (retryAfter, options) => {
15535-
// does not retry, only logs a warning
15536-
core.warning(`Abuse detected for request ${ options.method } ${ options.url }`)
1553715539
}
15538-
}
15539-
})
15540+
})
1554015541

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
1554315553

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`
1554915558

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+
}
1555215563

15553-
const clone = () => {
15554-
core.debug(`Cloning ${ repo.fullName } into ${ workingDir }`)
15564+
async clone() {
15565+
core.debug(`Cloning ${ this.repo.fullName } into ${ this.workingDir }`)
1555515566

1555615567
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 }`
1555815569
)
1555915570
}
1556015571

15561-
const setIdentity = async (client) => {
15572+
async setIdentity() {
1556215573
let username = GIT_USERNAME
1556315574
let email = GIT_EMAIL
15564-
github = client
1556515575

1556615576
if (email === undefined) {
15567-
const { data } = await github.users.getAuthenticated()
15577+
const { data } = await this.github.users.getAuthenticated()
1556815578
email = data.email
1556915579
username = data.login
1557015580
}
@@ -15573,21 +15583,21 @@ const init = (repo) => {
1557315583

1557415584
return execCmd(
1557515585
`git config --local user.name "${ username }" && git config --local user.email "${ email }"`,
15576-
workingDir
15586+
this.workingDir
1557715587
)
1557815588
}
1557915589

15580-
const getBaseBranch = async () => {
15581-
baseBranch = await execCmd(
15590+
async getBaseBranch() {
15591+
this.baseBranch = await execCmd(
1558215592
`git rev-parse --abbrev-ref HEAD`,
15583-
workingDir
15593+
this.workingDir
1558415594
)
1558515595
}
1558615596

15587-
const createPrBranch = async () => {
15597+
async createPrBranch() {
1558815598
const prefix = BRANCH_PREFIX.replace('SOURCE_REPO_NAME', GITHUB_REPOSITORY.split('/')[1])
1558915599

15590-
let newBranch = path.join(prefix, repo.branch)
15600+
let newBranch = path.join(prefix, this.repo.branch)
1559115601

1559215602
if (OVERWRITE_EXISTING_PR === false) {
1559315603
newBranch += `-${ Math.round((new Date()).getTime() / 1000) }`
@@ -15597,89 +15607,89 @@ const init = (repo) => {
1559715607

1559815608
await execCmd(
1559915609
`git checkout -b "${ newBranch }"`,
15600-
workingDir
15610+
this.workingDir
1560115611
)
1560215612

15603-
prBranch = newBranch
15613+
this.prBranch = newBranch
1560415614
}
1560515615

15606-
const add = async (file) => {
15616+
async add(file) {
1560715617
return execCmd(
1560815618
`git add -f ${ file }`,
15609-
workingDir
15619+
this.workingDir
1561015620
)
1561115621
}
1561215622

15613-
const hasChanges = async () => {
15623+
async hasChanges() {
1561415624
const statusOutput = await execCmd(
1561515625
`git status --porcelain`,
15616-
workingDir
15626+
this.workingDir
1561715627
)
1561815628

1561915629
return parse(statusOutput).length !== 0
1562015630
}
1562115631

15622-
const commit = async (msg) => {
15632+
async commit(msg) {
1562315633
let message = msg !== undefined ? msg : `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY }`
1562415634
if (COMMIT_BODY) {
1562515635
message += `\n\n${ COMMIT_BODY }`
1562615636
}
1562715637
return execCmd(
1562815638
`git commit -m "${ message }"`,
15629-
workingDir
15639+
this.workingDir
1563015640
)
1563115641
}
1563215642

15633-
const status = async () => {
15643+
async status() {
1563415644
return execCmd(
1563515645
`git status`,
15636-
workingDir
15646+
this.workingDir
1563715647
)
1563815648
}
1563915649

15640-
const push = async () => {
15650+
async push() {
1564115651
return execCmd(
15642-
`git push ${ gitUrl } --force`,
15643-
workingDir
15652+
`git push ${ this.gitUrl } --force`,
15653+
this.workingDir
1564415654
)
1564515655
}
1564615656

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,
1565115661
state: 'open',
15652-
head: `${ repo.user }:${ prBranch }`
15662+
head: `${ this.repo.user }:${ this.prBranch }`
1565315663
})
1565415664

15655-
existingPr = data[0]
15665+
this.existingPr = data[0]
1565615666

15657-
return existingPr
15667+
return this.existingPr
1565815668
}
1565915669

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,
1566515675
body: dedent(`
1566615676
⚠️ This PR is being automatically resynced ⚠️
1566715677
15668-
${ existingPr.body }
15678+
${ this.existingPr.body }
1566915679
`)
1567015680
})
1567115681
}
1567215682

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 ⚠️', '')
1567915689
})
1568015690
}
1568115691

15682-
const createOrUpdatePr = async (changedFiles) => {
15692+
async createOrUpdatePr(changedFiles) {
1568315693
const body = dedent(`
1568415694
Synced local file(s) with [${ GITHUB_REPOSITORY }](https://github.com/${ GITHUB_REPOSITORY }).
1568515695
@@ -15690,13 +15700,13 @@ const init = (repo) => {
1569015700
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 })
1569115701
`)
1569215702

15693-
if (existingPr) {
15703+
if (this.existingPr) {
1569415704
core.info(`Overwriting existing PR`)
1569515705

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,
1570015710
body: body
1570115711
})
1570215712

@@ -15705,41 +15715,41 @@ const init = (repo) => {
1570515715

1570615716
core.info(`Creating new PR`)
1570715717

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,
1571115721
title: `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY }`,
1571215722
body: body,
15713-
head: prBranch,
15714-
base: baseBranch
15723+
head: this.prBranch,
15724+
base: this.baseBranch
1571515725
})
1571615726

15727+
this.existingPr = data
15728+
1571715729
return data
1571815730
}
1571915731

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+
})
1573515739
}
15736-
}
1573715740

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+
}
1574115749
}
1574215750

15751+
module.exports = Git
15752+
1574315753
/***/ }),
1574415754

1574515755
/***/ 8505:
@@ -16017,7 +16027,6 @@ const { forEach, dedent, addTrailingSlash, pathIsDirectory, copy, remove } = __n
1601716027

1601816028
const {
1601916029
parseConfig,
16020-
GITHUB_TOKEN,
1602116030
COMMIT_EACH_FILE,
1602216031
COMMIT_PREFIX,
1602316032
PR_LABELS,
@@ -16030,8 +16039,8 @@ const {
1603016039
} = __nccwpck_require__(4570)
1603116040

1603216041
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()
1603516044

1603616045
const repos = await parseConfig()
1603716046

@@ -16043,12 +16052,9 @@ const run = async () => {
1604316052
core.info(`Branch : ${ item.repo.branch }`)
1604416053
core.info(' ')
1604516054
try {
16046-
const git = Git.init(item.repo)
1604716055

1604816056
// 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)
1605216058

1605316059
let existingPr
1605416060
if (SKIP_PR === false) {
@@ -16170,22 +16176,12 @@ const run = async () => {
1617016176

1617116177
if (PR_LABELS !== undefined && PR_LABELS.length > 0) {
1617216178
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)
1617916180
}
1618016181

1618116182
if (ASSIGNEES !== undefined && ASSIGNEES.length > 0) {
1618216183
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)
1618916185
}
1619016186
}
1619116187

0 commit comments

Comments
 (0)