Skip to content

Commit 80e77fd

Browse files
xanjohnsBetaHuhn
andauthored
✨ Add fork workflow functionality (#135)
Implements #133 Co-authored-by: Maximilian Schiller <[email protected]>
1 parent 996aa41 commit 80e77fd

File tree

6 files changed

+101
-12
lines changed

6 files changed

+101
-12
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ Here are all the inputs [repo-file-sync-action](https://github.com/BetaHuhn/repo
120120
| `DRY_RUN` | Run everything except that nothing will be pushed | **No** | false |
121121
| `SKIP_CLEANUP` | Skips removing the temporary directory. Useful for debugging | **No** | false |
122122
| `SKIP_PR` | Skips creating a Pull Request and pushes directly to the default branch | **No** | false |
123+
| `FORK` | A Github account username. Changes will be pushed to a fork of target repos on this account. | **No** | false |
123124

124125
### Outputs
125126

@@ -389,6 +390,21 @@ The above example would result in a commit message that looks something like thi
389390
Change-type: patch
390391
```
391392

393+
### Fork and pull request workflow
394+
395+
If you do not wish to grant this action write access to target repositories, you can specify a bot/user Github acccount that you do have access to with the `FORK` parameter.
396+
397+
A fork of each target repository will be created on this account, and all changes will be pushed to a branch on the fork, instead of upstream. Pull requests will be opened from the forks to target repositories.
398+
399+
Note: while you can open pull requests to target repositories without write access, some features, like applying labels, are not possible.
400+
401+
```yml
402+
uses: BetaHuhn/repo-file-sync-action@v1
403+
with:
404+
GH_PAT: ${{ secrets.GH_PAT }}
405+
FORK: file-sync-bot
406+
```
407+
392408
### Advanced sync config
393409

394410
Here's how I keep common files in sync across my repositories. The main repository [`github-files`](https://github.com/BetaHuhn/github-files) contains all the files I want to sync and the [repo-file-sync-action](https://github.com/BetaHuhn/repo-file-sync-action) Action which runs on every push.

action.yml

+5
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ inputs:
7979
description: |
8080
Specify a different prefix for the new branch in the target repo. Defaults to repo-sync/SOURCE_REPO_NAME
8181
required: false
82+
FORK:
83+
description: |
84+
Specify the user account that will be used in a fork and pull-request workflow. Defaults
85+
false.
86+
required: false
8287

8388
outputs:
8489
pull_request_urls:

dist/index.js

+39-6
Original file line numberDiff line numberDiff line change
@@ -17261,6 +17261,9 @@ try {
1726117261
BRANCH_PREFIX: getInput({
1726217262
key: 'BRANCH_PREFIX',
1726317263
default: 'repo-sync/SOURCE_REPO_NAME'
17264+
}),
17265+
FORK: getInput({
17266+
key: 'FORK'
1726417267
})
1726517268
}
1726617269

@@ -17416,7 +17419,8 @@ const {
1741617419
GITHUB_REPOSITORY,
1741717420
OVERWRITE_EXISTING_PR,
1741817421
PR_BODY,
17419-
BRANCH_PREFIX
17422+
BRANCH_PREFIX,
17423+
FORK
1742017424
} = __nccwpck_require__(4570)
1742117425

1742217426
const { dedent, execCmd } = __nccwpck_require__(8505)
@@ -17458,6 +17462,28 @@ class Git {
1745817462
await this.clone()
1745917463
await this.setIdentity()
1746017464
await this.getBaseBranch()
17465+
17466+
if (FORK) {
17467+
const forkUrl = `https://${ GITHUB_TOKEN }@github.com/${ FORK }/${ this.repo.name }.git`
17468+
await this.createFork()
17469+
await this.createRemote(forkUrl)
17470+
17471+
}
17472+
}
17473+
17474+
async createFork() {
17475+
core.debug(`Creating fork with OWNER: ${ this.repo.user } and REPO: ${ this.repo.name }`)
17476+
await this.github.repos.createFork({
17477+
owner: this.repo.user,
17478+
repo: this.repo.name
17479+
})
17480+
}
17481+
17482+
async createRemote(forkUrl) {
17483+
return execCmd(
17484+
`git remote add fork ${ forkUrl }`,
17485+
this.workingDir
17486+
)
1746117487
}
1746217488

1746317489
async clone() {
@@ -17603,6 +17629,12 @@ class Git {
1760317629
}
1760417630

1760517631
async push() {
17632+
if (FORK) {
17633+
return execCmd(
17634+
`git push -u fork ${ this.prBranch } --force`,
17635+
this.workingDir
17636+
)
17637+
}
1760617638
return execCmd(
1760717639
`git push ${ this.gitUrl } --force`,
1760817640
this.workingDir
@@ -17614,7 +17646,7 @@ class Git {
1761417646
owner: this.repo.user,
1761517647
repo: this.repo.name,
1761617648
state: 'open',
17617-
head: `${ this.repo.user }:${ this.prBranch }`
17649+
head: `${ FORK ? FORK : this.repo.user }:${ this.prBranch }`
1761817650
})
1761917651

1762017652
this.existingPr = data[0]
@@ -17678,7 +17710,7 @@ class Git {
1767817710
repo: this.repo.name,
1767917711
title: title === undefined ? `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY }` : title,
1768017712
body: body,
17681-
head: this.prBranch,
17713+
head: `${ FORK ? FORK : this.repo.user }:${ this.prBranch }`,
1768217714
base: this.baseBranch
1768317715
})
1768417716

@@ -18020,7 +18052,8 @@ const {
1802018052
OVERWRITE_EXISTING_PR,
1802118053
SKIP_PR,
1802218054
ORIGINAL_MESSAGE,
18023-
COMMIT_AS_PR_TITLE
18055+
COMMIT_AS_PR_TITLE,
18056+
FORK
1802418057
} = __nccwpck_require__(4570)
1802518058

1802618059
const run = async () => {
@@ -18175,12 +18208,12 @@ const run = async () => {
1817518208
core.notice(`Pull Request #${ pullRequest.number } created/updated: ${ pullRequest.html_url }`)
1817618209
prUrls.push(pullRequest.html_url)
1817718210

18178-
if (PR_LABELS !== undefined && PR_LABELS.length > 0) {
18211+
if (PR_LABELS !== undefined && PR_LABELS.length > 0 && FORK === undefined) {
1817918212
core.info(`Adding label(s) "${ PR_LABELS.join(', ') }" to PR`)
1818018213
await git.addPrLabels(PR_LABELS)
1818118214
}
1818218215

18183-
if (ASSIGNEES !== undefined && ASSIGNEES.length > 0) {
18216+
if (ASSIGNEES !== undefined && ASSIGNEES.length > 0 && FORK === undefined) {
1818418217
core.info(`Adding assignee(s) "${ ASSIGNEES.join(', ') }" to PR`)
1818518218
await git.addPrAssignees(ASSIGNEES)
1818618219
}

src/config.js

+5
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ try {
108108
BRANCH_PREFIX: getInput({
109109
key: 'BRANCH_PREFIX',
110110
default: 'repo-sync/SOURCE_REPO_NAME'
111+
}),
112+
FORK: getInput({
113+
key: 'FORK',
114+
default: false,
115+
disableable: true
111116
})
112117
}
113118

src/git.js

+32-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const {
1616
GITHUB_REPOSITORY,
1717
OVERWRITE_EXISTING_PR,
1818
PR_BODY,
19-
BRANCH_PREFIX
19+
BRANCH_PREFIX,
20+
FORK
2021
} = require('./config')
2122

2223
const { dedent, execCmd } = require('./helpers')
@@ -58,6 +59,28 @@ class Git {
5859
await this.clone()
5960
await this.setIdentity()
6061
await this.getBaseBranch()
62+
63+
if (FORK) {
64+
const forkUrl = `https://${ GITHUB_TOKEN }@github.com/${ FORK }/${ this.repo.name }.git`
65+
await this.createFork()
66+
await this.createRemote(forkUrl)
67+
68+
}
69+
}
70+
71+
async createFork() {
72+
core.debug(`Creating fork with OWNER: ${ this.repo.user } and REPO: ${ this.repo.name }`)
73+
await this.github.repos.createFork({
74+
owner: this.repo.user,
75+
repo: this.repo.name
76+
})
77+
}
78+
79+
async createRemote(forkUrl) {
80+
return execCmd(
81+
`git remote add fork ${ forkUrl }`,
82+
this.workingDir
83+
)
6184
}
6285

6386
async clone() {
@@ -203,6 +226,12 @@ class Git {
203226
}
204227

205228
async push() {
229+
if (FORK) {
230+
return execCmd(
231+
`git push -u fork ${ this.prBranch } --force`,
232+
this.workingDir
233+
)
234+
}
206235
return execCmd(
207236
`git push ${ this.gitUrl } --force`,
208237
this.workingDir
@@ -214,7 +243,7 @@ class Git {
214243
owner: this.repo.user,
215244
repo: this.repo.name,
216245
state: 'open',
217-
head: `${ this.repo.user }:${ this.prBranch }`
246+
head: `${ FORK ? FORK : this.repo.user }:${ this.prBranch }`
218247
})
219248

220249
this.existingPr = data[0]
@@ -278,7 +307,7 @@ class Git {
278307
repo: this.repo.name,
279308
title: title === undefined ? `${ COMMIT_PREFIX } Synced file(s) with ${ GITHUB_REPOSITORY }` : title,
280309
body: body,
281-
head: this.prBranch,
310+
head: `${ FORK ? FORK : this.repo.user }:${ this.prBranch }`,
282311
base: this.baseBranch
283312
})
284313

src/index.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const {
1616
OVERWRITE_EXISTING_PR,
1717
SKIP_PR,
1818
ORIGINAL_MESSAGE,
19-
COMMIT_AS_PR_TITLE
19+
COMMIT_AS_PR_TITLE,
20+
FORK
2021
} = require('./config')
2122

2223
const run = async () => {
@@ -171,12 +172,12 @@ const run = async () => {
171172
core.notice(`Pull Request #${ pullRequest.number } created/updated: ${ pullRequest.html_url }`)
172173
prUrls.push(pullRequest.html_url)
173174

174-
if (PR_LABELS !== undefined && PR_LABELS.length > 0) {
175+
if (PR_LABELS !== undefined && PR_LABELS.length > 0 && FORK === undefined) {
175176
core.info(`Adding label(s) "${ PR_LABELS.join(', ') }" to PR`)
176177
await git.addPrLabels(PR_LABELS)
177178
}
178179

179-
if (ASSIGNEES !== undefined && ASSIGNEES.length > 0) {
180+
if (ASSIGNEES !== undefined && ASSIGNEES.length > 0 && FORK === undefined) {
180181
core.info(`Adding assignee(s) "${ ASSIGNEES.join(', ') }" to PR`)
181182
await git.addPrAssignees(ASSIGNEES)
182183
}

0 commit comments

Comments
 (0)