Skip to content

Commit b7f618a

Browse files
committed
feat(git-helper): Separate source and target repositories
1 parent 2abe705 commit b7f618a

File tree

7 files changed

+79
-45
lines changed

7 files changed

+79
-45
lines changed

__tests__/composer-helper.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ test('composer install & optimize', async () => {
1010
const composerVendor =
1111
inputHelper.baseDirPath + '/__tests__/Mock-Composer/vendor'
1212

13-
!fs.existsSync(composerLock) || await fs.promises.rm(composerLock)
14-
!fs.existsSync(composerVendor) || await fs.promises.rm(composerVendor, {recursive: true})
13+
!fs.existsSync(composerLock) || (await fs.promises.rm(composerLock))
14+
!fs.existsSync(composerVendor) ||
15+
(await fs.promises.rm(composerVendor, {recursive: true}))
1516

1617
const composerHelper = await createComposerHelper(
1718
inputHelper.baseDirPath + '/__tests__/Mock-Composer'

__tests__/git-helper.test.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import {IGitHelper, createGitHelper} from '../src/git-helper'
66
import {getExecOutput} from '@actions/exec'
77
import {env, cwd} from 'process'
88
import {createCommandManager} from '../src/git-command-manager'
9+
import {IGitSourceSettings} from 'github-checkout/lib/git-source-settings'
910

1011
let srcTmpPath: string | undefined
12+
let sourceSettings: IGitSourceSettings
1113

1214
async function createTmpRepo(): Promise<IGitHelper> {
1315
srcTmpPath = await makeTempPath()
14-
15-
const gitHelper = await createGitHelper(srcTmpPath, false)
16+
const settings = {...sourceSettings, repositoryPath: srcTmpPath}
17+
const gitHelper = await createGitHelper(settings)
1618
await gitHelper.init()
1719

1820
await getExecOutput(`touch ${srcTmpPath}/composer.json`)
@@ -33,8 +35,27 @@ async function destroyTmpRepo() {
3335
let gitHelper: IGitHelper
3436

3537
beforeEach(async () => {
38+
sourceSettings = {
39+
repositoryPath: '',
40+
repositoryOwner: '',
41+
repositoryName: '',
42+
ref: '',
43+
commit: '',
44+
clean: true,
45+
fetchDepth: 1,
46+
lfs: false,
47+
submodules: false,
48+
nestedSubmodules: false,
49+
authToken: env.PHP_PREFIXER_GH_TOKEN || '',
50+
sshKey: '',
51+
sshKnownHosts: '',
52+
sshStrict: true,
53+
persistCredentials: true
54+
}
55+
3656
const inputHelper = new InputHelper()
37-
gitHelper = await createGitHelper(inputHelper.baseDirPath, false)
57+
sourceSettings.repositoryPath = inputHelper.baseDirPath
58+
gitHelper = await createGitHelper(sourceSettings)
3859

3960
env.GITHUB_REPOSITORY = 'lorem/ipsum'
4061
})
@@ -135,7 +156,8 @@ test('last matching tag no tag', async () => {
135156

136157
test('push', async () => {
137158
const srcTmpPath = await makeTempPath()
138-
const gitHelper = await createGitHelper(srcTmpPath, false)
159+
const settings = {...sourceSettings, repositoryPath: srcTmpPath}
160+
const gitHelper = await createGitHelper(settings)
139161
await gitHelper.init()
140162
await getExecOutput(`touch ${srcTmpPath}/composer.json`)
141163
await gitHelper.commitAll()
@@ -147,14 +169,16 @@ test('push', async () => {
147169
const targetTmpPath = await makeTempPath()
148170
await getExecOutput(`git clone ${upstreamTmpPath} ${targetTmpPath}`)
149171

150-
const targetIGitHelper = await createGitHelper(targetTmpPath, false)
172+
const targetSettings = {...sourceSettings, repositoryPath: targetTmpPath}
173+
const targetIGitHelper = await createGitHelper(targetSettings)
151174
const licenseFile = `${targetTmpPath}/license.txt`
152175
await getExecOutput(`touch ${licenseFile}`)
153176
await targetIGitHelper.commitAll()
154177
await targetIGitHelper.tag('1.2.4')
155178
await targetIGitHelper.push('origin', 'master')
156179

157-
const upstreamIGitHelper = await createGitHelper(upstreamTmpPath, false)
180+
const upstreamSettings = {...sourceSettings, repositoryPath: upstreamTmpPath}
181+
const upstreamIGitHelper = await createGitHelper(upstreamSettings)
158182
const result1 = await upstreamIGitHelper.tagExists('1.2.3')
159183
expect(result1).toBeTruthy()
160184
const result2 = await upstreamIGitHelper.tagExists('1.2.4')
@@ -187,7 +211,8 @@ test('tag exists', async () => {
187211

188212
test('has changes', async () => {
189213
const srcTmpPath = await makeTempPath()
190-
const gitHelper = await createGitHelper(srcTmpPath, false)
214+
const settings = {...sourceSettings, repositoryPath: srcTmpPath}
215+
const gitHelper = await createGitHelper(settings)
191216
await gitHelper.init()
192217

193218
await fs.promises.mkdir(`${srcTmpPath}/vendor`)
@@ -219,7 +244,10 @@ test('remote add', async () => {
219244

220245
if (srcTmpPath) {
221246
const commandManager = await createCommandManager(srcTmpPath, false)
222-
await commandManager.config('remote.origin.url', 'git@github.com:PHP-Prefixer/php-prefixer-build-action.git')
247+
await commandManager.config(
248+
'remote.origin.url',
249+
'git@github.com:PHP-Prefixer/php-prefixer-build-action.git'
250+
)
223251
await tmpIGitHelper.remoteAdd(true, 'prefixed-origin-3')
224252
const result3 = await tmpIGitHelper.remoteExists('prefixed-origin-3')
225253
expect(result3).toBeTruthy()

__tests__/main-helper.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as core from '@actions/core'
2-
import { env, cwd } from 'process'
2+
import {env, cwd} from 'process'
33
import {test, expect} from '@jest/globals'
44
import {run, cleanup} from '../src/main-helper'
55
import * as checkoutInputHelper from 'github-checkout/lib/input-helper'
@@ -33,25 +33,25 @@ test('main', async () => {
3333

3434
const now = new Date()
3535
const content = `Last build ${now.toISOString()}`
36-
await fs.promises.writeFile(`${sourceSettings.repositoryPath}/last-build`, content)
37-
const gitHelper = await createGitHelper(
38-
sourceSettings.repositoryPath,
39-
sourceSettings.lfs
36+
await fs.promises.writeFile(
37+
`${sourceSettings.repositoryPath}/last-build`,
38+
content
4039
)
40+
const gitHelper = await createGitHelper(sourceSettings)
4141
const currentBranch = await gitHelper.currentBranch()
4242
await gitHelper.commitAll()
43-
await gitHelper.push(currentBranch)
43+
await gitHelper.push('origin', currentBranch)
4444
await fs.promises.rm(sourceSettings.repositoryPath, {recursive: true})
4545
sourceSettings.repositoryPath = ''
4646

47-
core.debug('[main-helper.test] run err code 0')
47+
core.debug('[main-helper.test] run err code 0 - to be prefixed')
4848
const errorCode0 = await run()
4949
expect(errorCode0).toBe(0)
5050
await cleanup()
5151

52-
core.debug('[main-helper.test] run err code 1')
52+
core.debug('[main-helper.test] run err code 0 - already prefixed')
5353
const errorCode1 = await run()
54-
expect(errorCode1).toBe(1)
54+
expect(errorCode1).toBe(0)
5555
await cleanup()
5656

5757
gitSourceProvider.cleanup(sourceSettings.repositoryPath)

__tests__/php-prefixer-helper.test.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ import {PhpPrefixerHelper} from '../src/php-prefixer-helper'
1212
import InputHelper from '../src/input-helper'
1313

1414
let srcTmpPath: string | undefined
15+
let sourceSettings: IGitSourceSettings
16+
let phpPrefixerSettings: IPhpPrefixerSettings
1517

1618
async function createTmpRepo(): Promise<IGitHelper> {
1719
srcTmpPath = await makeTempPath()
18-
const gitHelper = await createGitHelper(srcTmpPath, false)
20+
const settings = {...sourceSettings, repositoryPath: srcTmpPath}
21+
const gitHelper = await createGitHelper(settings)
1922
await gitHelper.init()
2023

2124
await getExecOutput(`touch ${srcTmpPath}/composer.json`)
@@ -36,9 +39,6 @@ async function destroyTmpRepo() {
3639
srcTmpPath = undefined
3740
}
3841

39-
let sourceSettings: IGitSourceSettings
40-
let phpPrefixerSettings: IPhpPrefixerSettings
41-
4242
beforeEach(() => {
4343
sourceSettings = {
4444
repositoryPath: '',
@@ -65,6 +65,8 @@ beforeEach(() => {
6565
projectId: env.PHP_PREFIXER_PROJECT_ID || '',
6666
ghPersonalAccessToken: env.PHP_PREFIXER_GH_TOKEN || ''
6767
}
68+
69+
env.GITHUB_REPOSITORY = 'lorem/ipsum'
6870
})
6971

7072
test('waiting job true - guzzle master / prefixed', async () => {
@@ -219,7 +221,8 @@ test('prefix - local master / prefixed', async () => {
219221
const mockedProject = inputHelper.baseDirPath + '/__tests__/Mock-PhpPrefixer'
220222

221223
const srcTmpPath = await makeTempPath()
222-
const gitHelper = await createGitHelper(srcTmpPath, false)
224+
const settings = {...sourceSettings, repositoryPath: srcTmpPath}
225+
const gitHelper = await createGitHelper(settings)
223226
await gitHelper.init()
224227
const rsync = await createRsync()
225228
await rsync.copyProjectFiles(mockedProject, srcTmpPath)
@@ -247,7 +250,8 @@ test('prefix - local master / prefixed', async () => {
247250
const resultPrefix1 = await phpPrefixerHelper.prefix()
248251
expect(resultPrefix1).toBeTruthy()
249252

250-
const upstreamIGitHelper = await createGitHelper(upstreamTmpPath, false)
253+
const upstreamSettings = {...sourceSettings, repositoryPath: upstreamTmpPath}
254+
const upstreamIGitHelper = await createGitHelper(upstreamSettings)
251255
const branchExists = await upstreamIGitHelper.branchExists(false, 'prefixed')
252256
expect(branchExists).toBeTruthy()
253257

@@ -267,7 +271,8 @@ test('prefix - local master 7.1.1 / prefixed prefixed-7.1.1', async () => {
267271
const mockedProject = inputHelper.baseDirPath + '/__tests__/Mock-PhpPrefixer'
268272

269273
const srcTmpPath = await makeTempPath()
270-
const gitHelper = await createGitHelper(srcTmpPath, false)
274+
const settings = {...sourceSettings, repositoryPath: srcTmpPath}
275+
const gitHelper = await createGitHelper(settings)
271276
await gitHelper.init()
272277
const rsync = await createRsync()
273278
await rsync.copyProjectFiles(mockedProject, srcTmpPath)
@@ -295,7 +300,8 @@ test('prefix - local master 7.1.1 / prefixed prefixed-7.1.1', async () => {
295300

296301
await phpPrefixerHelper.prefix()
297302

298-
const upstreamIGitHelper = await createGitHelper(upstreamTmpPath, false)
303+
const upstreamSettings = {...sourceSettings, repositoryPath: upstreamTmpPath}
304+
const upstreamIGitHelper = await createGitHelper(upstreamSettings)
299305
const branchExists = await upstreamIGitHelper.branchExists(false, 'prefixed')
300306
expect(branchExists).toBeTruthy()
301307
const tagExists = await upstreamIGitHelper.tagExists('prefixed-7.1.1')

src/git-command-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface IGitCommandManager {
4444
lfsInstall(): Promise<void>
4545
log1(format?: string): Promise<string>
4646
pull(remote: string, branch: string): Promise<void>
47-
push(branch: string): Promise<void>
47+
push(remote: string, branch: string): Promise<void>
4848
remoteAdd(remoteName: string, remoteUrl: string): Promise<void>
4949
removeEnvironmentVariable(name: string): void
5050
remoteExists(remote: string): Promise<boolean>
@@ -353,14 +353,14 @@ class GitCommandManager {
353353
return output.stdout
354354
}
355355

356-
async push(branch: string): Promise<void> {
356+
async push(remote: string, branch: string): Promise<void> {
357357
await this.execGit([
358358
'push',
359359
'--tags',
360360
'--progress',
361361
'--no-verify',
362362
'-u',
363-
'origin',
363+
remote,
364364
branch
365365
])
366366
}

src/git-helper.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ export interface IGitHelper {
2424
}
2525

2626
export async function createGitHelper(
27-
workingDirectory: string,
28-
lfs: boolean
27+
settings: IGitSourceSettings
2928
): Promise<IGitHelper> {
30-
return await GitHelper.create(workingDirectory, lfs)
29+
return await GitHelper.create(settings)
3130
}
3231

3332
export class GitHelper implements IGitHelper {
@@ -215,14 +214,14 @@ export class GitHelper implements IGitHelper {
215214
return this.gitCommandManager.tagExists(pattern)
216215
}
217216

218-
static async create(
219-
workingDirectory: string,
220-
lfs: boolean
221-
): Promise<IGitHelper> {
222-
const gitCommandManager = await createCommandManager(workingDirectory, lfs)
217+
static async create(settings: IGitSourceSettings): Promise<IGitHelper> {
218+
const gitCommandManager = await createCommandManager(
219+
settings.repositoryPath,
220+
settings.lfs
221+
)
223222

224223
const gitHelper = new GitHelper(gitCommandManager)
225-
gitHelper.settings.repositoryPath = workingDirectory
224+
gitHelper.settings = settings
226225

227226
return gitHelper
228227
}

src/php-prefixer-helper.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class PhpPrefixerHelper {
129129
await this.targetGitHelper.tag(this.targetPrefixedTag)
130130
}
131131

132-
await this.targetGitHelper.push(this.targetPrefixedBranch)
132+
await this.targetGitHelper.push('prefixed', this.targetPrefixedBranch)
133133

134134
return true
135135
}
@@ -190,6 +190,8 @@ export class PhpPrefixerHelper {
190190
this.targetPath
191191
)
192192

193+
this.targetGitHelper.remoteAdd(this.isRemote, 'prefixed')
194+
193195
const branchCreated = await this.targetGitHelper.checkoutToBranch(
194196
this.isRemote,
195197
this.targetPrefixedBranch
@@ -221,10 +223,7 @@ export class PhpPrefixerHelper {
221223
targetGitHelper?: IGitHelper
222224
): Promise<PhpPrefixerHelper> {
223225
if (!sourceGitHelper) {
224-
sourceGitHelper = await createGitHelper(
225-
sourceSettings.repositoryPath,
226-
sourceSettings.lfs
227-
)
226+
sourceGitHelper = await createGitHelper(sourceSettings)
228227
}
229228

230229
if (!targetPath) {
@@ -236,7 +235,8 @@ export class PhpPrefixerHelper {
236235
await fs.promises.access(targetPath, fs.constants.W_OK)
237236

238237
if (!targetGitHelper) {
239-
targetGitHelper = await createGitHelper(targetPath, sourceSettings.lfs)
238+
const targetSettings = {...sourceSettings, repositoryPath: targetPath}
239+
targetGitHelper = await createGitHelper(targetSettings)
240240
}
241241

242242
const phpPrefixerHelper = new PhpPrefixerHelper(

0 commit comments

Comments
 (0)