Skip to content

Commit 3468fa3

Browse files
committed
feat(php-prefixer): Separate origin and prefixed repo/branches
1 parent 898802b commit 3468fa3

File tree

5 files changed

+186
-60
lines changed

5 files changed

+186
-60
lines changed

__tests__/git-helper.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ test('branch contains tag error', async () => {
8080
})
8181

8282
test('branch exists', async () => {
83-
const result1 = await gitHelper.branchExists(false, 'master')
83+
const result1 = await gitHelper.branchExists(false, '', 'master')
8484
expect(result1).toBeFalsy()
8585

86-
const result2 = await gitHelper.branchExists(false, 'main')
86+
const result2 = await gitHelper.branchExists(false, '', 'main')
8787
expect(result2).toBeTruthy()
8888
})
8989

@@ -93,12 +93,12 @@ test('checkout new branch', async () => {
9393
let branch = await tmpIGitHelper.currentBranch()
9494
expect(branch).toBe('master')
9595

96-
await tmpIGitHelper.checkoutToBranch(false, 'prefixed-1.0.0')
96+
await tmpIGitHelper.checkoutToBranch(false, '', 'prefixed-1.0.0')
9797
branch = await tmpIGitHelper.currentBranch()
9898
expect(branch).toBe('prefixed-1.0.0')
9999

100100
await tmpIGitHelper.checkout('master', '')
101-
await tmpIGitHelper.checkoutToBranch(false, 'prefixed-1.0.0')
101+
await tmpIGitHelper.checkoutToBranch(false, '', 'prefixed-1.0.0')
102102

103103
branch = await tmpIGitHelper.currentBranch()
104104
expect(branch).toBe('prefixed-1.0.0')

__tests__/main-helper.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import * as gitSourceProvider from 'github-checkout/lib/git-source-provider'
88
import * as fs from 'fs'
99
import {createGitHelper} from '../src/git-helper'
1010

11-
test('main', async () => {
11+
test('main - prefix anibalsanchez/hello-wp-world', async () => {
1212
core.debug('[main-helper.test] test main')
1313

1414
env.GITHUB_WORKSPACE = cwd()
@@ -56,3 +56,30 @@ test('main', async () => {
5656

5757
gitSourceProvider.cleanup(sourceSettings.repositoryPath)
5858
})
59+
60+
test('main - prefix illuminate/support 6.x', async () => {
61+
core.debug('[main-helper.test] test main - prefix illuminate/support 6.x')
62+
63+
env.GITHUB_WORKSPACE = cwd()
64+
env.GITHUB_REPOSITORY = 'PHP-Prefixer/prefixed-illuminate-support'
65+
66+
// phpPrefixerSettings
67+
env.INPUT_PERSONAL_ACCESS_TOKEN = env.PHP_PREFIXER_PERSONAL_ACCESS_TOKEN || ''
68+
env.INPUT_PROJECT_ID = env.PHP_PREFIXER_PROJECT_ID || ''
69+
70+
// sourceSettings
71+
env.INPUT_REPOSITORY = 'illuminate/support'
72+
env.INPUT_REF = '6.x'
73+
env.INPUT_TOKEN = env.PHP_PREFIXER_GH_TOKEN || ''
74+
env.INPUT_SCHEMA =
75+
'{"project-name": "Prefixed Project","namespaces-prefix": "PPP","global-scope-prefix": "PPP_"}'
76+
77+
env['INPUT_PERSIST-CREDENTIALS'] = 'true'
78+
env['INPUT_FETCH-DEPTH'] = '0'
79+
80+
core.debug('[main-helper.test] run err code 0')
81+
const errorCode0 = await run()
82+
expect(errorCode0).toBe(0)
83+
84+
await cleanup()
85+
})

__tests__/php-prefixer-helper.test.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ beforeEach(() => {
6363
targetDirPath: '',
6464
personalAccessToken: env.PHP_PREFIXER_PERSONAL_ACCESS_TOKEN || '',
6565
projectId: env.PHP_PREFIXER_PROJECT_ID || '',
66-
ghPersonalAccessToken: env.PHP_PREFIXER_GH_TOKEN || ''
66+
ghPersonalAccessToken: env.PHP_PREFIXER_GH_TOKEN || '',
67+
schema: ''
6768
}
6869

6970
env.GITHUB_REPOSITORY = 'lorem/ipsum'
@@ -145,10 +146,10 @@ test('waiting job true - guzzle master 7.1.1 / prefixed prefixed-7.1.1', async (
145146

146147
test('waiting job false - local master / prefixed', async () => {
147148
const gitHelper = await createTmpRepo()
148-
gitHelper.checkoutToBranch(false, 'prefixed')
149+
gitHelper.checkoutToBranch(false, '', 'prefixed')
149150
await getExecOutput(`touch ${srcTmpPath}/license.txt`)
150151
await gitHelper.commitAll()
151-
gitHelper.checkoutToBranch(false, 'master')
152+
gitHelper.checkoutToBranch(false, '', 'master')
152153

153154
// Download source
154155
sourceSettings.ref = 'refs/heads/master'
@@ -167,13 +168,13 @@ test('waiting job false - local master / prefixed', async () => {
167168

168169
test('waiting job false - local 7.0 / prefixed-7.0', async () => {
169170
const gitHelper = await createTmpRepo()
170-
gitHelper.checkoutToBranch(false, '7.0')
171+
gitHelper.checkoutToBranch(false, '', '7.0')
171172
await getExecOutput(`touch ${srcTmpPath}/license-time1.txt`)
172173
await gitHelper.commitAll()
173-
gitHelper.checkoutToBranch(false, 'prefixed-7.0')
174+
gitHelper.checkoutToBranch(false, '', 'prefixed-7.0')
174175
await getExecOutput(`touch ${srcTmpPath}/license-time2.txt`)
175176
await gitHelper.commitAll()
176-
gitHelper.checkoutToBranch(false, '7.0')
177+
gitHelper.checkoutToBranch(false, '', '7.0')
177178

178179
// Download source
179180
sourceSettings.ref = '7.0'
@@ -194,12 +195,12 @@ test('waiting job false - local master 7.1.1 / prefixed prefixed-7.1.1', async (
194195
const gitHelper = await createTmpRepo()
195196
gitHelper.tag('7.1.1')
196197

197-
gitHelper.checkoutToBranch(false, 'prefixed')
198+
gitHelper.checkoutToBranch(false, '', 'prefixed')
198199
await getExecOutput(`touch ${srcTmpPath}/license.txt`)
199200
await gitHelper.commitAll()
200201
gitHelper.tag('prefixed-7.1.1')
201202

202-
gitHelper.checkoutToBranch(false, 'master')
203+
gitHelper.checkoutToBranch(false, '', 'master')
203204

204205
// Download source
205206
sourceSettings.ref = '7.1.1'
@@ -252,7 +253,11 @@ test('prefix - local master / prefixed', async () => {
252253

253254
const upstreamSettings = {...sourceSettings, repositoryPath: upstreamTmpPath}
254255
const upstreamIGitHelper = await createGitHelper(upstreamSettings)
255-
const branchExists = await upstreamIGitHelper.branchExists(false, 'prefixed')
256+
const branchExists = await upstreamIGitHelper.branchExists(
257+
false,
258+
'',
259+
'prefixed'
260+
)
256261
expect(branchExists).toBeTruthy()
257262

258263
// Prefix again, to check that there are "No Changes"
@@ -302,7 +307,11 @@ test('prefix - local master 7.1.1 / prefixed prefixed-7.1.1', async () => {
302307

303308
const upstreamSettings = {...sourceSettings, repositoryPath: upstreamTmpPath}
304309
const upstreamIGitHelper = await createGitHelper(upstreamSettings)
305-
const branchExists = await upstreamIGitHelper.branchExists(false, 'prefixed')
310+
const branchExists = await upstreamIGitHelper.branchExists(
311+
false,
312+
'',
313+
'prefixed'
314+
)
306315
expect(branchExists).toBeTruthy()
307316
const tagExists = await upstreamIGitHelper.tagExists('prefixed-7.1.1')
308317
expect(tagExists).toBeTruthy()

src/git-helper.ts

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@ import {IGitSourceSettings} from 'github-checkout/lib/git-source-settings'
44

55
export interface IGitHelper {
66
branchContainsTag(tag: string): Promise<string>
7-
branchExists(remote: boolean, pattern: string): Promise<boolean>
7+
branchExists(
8+
remote: boolean,
9+
remoteName: string,
10+
pattern: string
11+
): Promise<boolean>
812
checkout(ref: string, startPoint: string): Promise<void>
9-
checkoutToBranch(remote: boolean, branch: string): Promise<boolean>
13+
checkoutToBranch(
14+
remote: boolean,
15+
remoteName: string,
16+
branch: string
17+
): Promise<boolean>
1018
commitAll(): Promise<void>
1119
currentBranch(): Promise<string>
1220
currentRevision(): Promise<string>
1321
currentTag(): Promise<string | undefined>
22+
fetchRemote(remoteName: string, fetchDepth?: number): Promise<void>
1423
hasChanges(): Promise<boolean>
1524
init(): Promise<void>
1625
lastMatchingTag(pattern: string): Promise<string | undefined>
17-
pull(remote: boolean, branch: string): Promise<void>
26+
pull(remoteName: string, branch: string): Promise<void>
1827
push(remote: string, branch: string): Promise<void>
19-
remoteAdd(isRemote: boolean, remoteName: string): Promise<void>
28+
remoteAdd(remote: boolean, remoteName: string): Promise<void>
2029
remoteExists(remote: string): Promise<boolean>
2130
revisionDate(ref: string): Promise<Date>
2231
tag(tag: string): Promise<void>
@@ -65,15 +74,15 @@ export class GitHelper implements IGitHelper {
6574
}
6675
}
6776

68-
async branchExists(remote: boolean, pattern: string): Promise<boolean> {
69-
if (remote) {
70-
return await this.gitCommandManager.branchExists(
71-
remote,
72-
`origin/${pattern}`
73-
)
74-
}
75-
76-
return await this.gitCommandManager.branchExists(remote, pattern)
77+
async branchExists(
78+
remote: boolean,
79+
remoteName: string,
80+
pattern: string
81+
): Promise<boolean> {
82+
return await this.gitCommandManager.branchExists(
83+
remote,
84+
remoteName ? `${remoteName}/${pattern}` : pattern
85+
)
7786
}
7887

7988
async checkout(ref: string, startPoint: string): Promise<void> {
@@ -85,17 +94,15 @@ export class GitHelper implements IGitHelper {
8594
return await this.gitCommandManager.checkout(ref, startPoint)
8695
}
8796

88-
async checkoutToBranch(remote: boolean, branch: string): Promise<boolean> {
89-
let branchExists: boolean
90-
91-
if (remote) {
92-
branchExists = await this.gitCommandManager.branchExists(
93-
true,
94-
`origin/${branch}`
95-
)
96-
} else {
97-
branchExists = await this.gitCommandManager.branchExists(false, branch)
98-
}
97+
async checkoutToBranch(
98+
remote: boolean,
99+
remoteName: string,
100+
branch: string
101+
): Promise<boolean> {
102+
const branchExists = await this.gitCommandManager.branchExists(
103+
remote,
104+
remote ? `${remoteName}/${branch}` : branch
105+
)
99106

100107
if (branchExists) {
101108
await this.gitCommandManager.checkout(branch, '')
@@ -130,6 +137,10 @@ export class GitHelper implements IGitHelper {
130137
}
131138
}
132139

140+
async fetchRemote(remoteName: string, fetchDepth?: number): Promise<void> {
141+
return this.gitCommandManager.fetchRemote(remoteName, fetchDepth)
142+
}
143+
133144
async hasChanges(): Promise<boolean> {
134145
const result = await this.gitCommandManager.statusPorcelain()
135146

@@ -174,25 +185,23 @@ export class GitHelper implements IGitHelper {
174185
return undefined
175186
}
176187

177-
async pull(remote: boolean, branch: string): Promise<void> {
178-
if (remote) {
179-
return this.gitCommandManager.pull('origin', branch)
180-
}
181-
182-
return this.gitCommandManager.pull('', branch)
188+
async pull(remoteName: string, branch: string): Promise<void> {
189+
// await this.gitCommandManager.stash()
190+
await this.gitCommandManager.pull(remoteName, branch)
191+
// return this.gitCommandManager.stashPop()
183192
}
184193

185194
async push(remote: string, branch: string): Promise<void> {
186195
return this.gitCommandManager.push(remote, branch)
187196
}
188197

189-
async remoteAdd(isRemote: boolean, remoteName: string): Promise<void> {
198+
async remoteAdd(remote: boolean, remoteName: string): Promise<void> {
190199
const qualifiedRepository = this.qualifiedRepository()
191200
let fetchUri = await this.gitCommandManager.tryGetFetchUrl()
192201

193-
if (isRemote && fetchUri && !fetchUri.endsWith(qualifiedRepository)) {
202+
if (remote && fetchUri && !fetchUri.endsWith(qualifiedRepository)) {
194203
const baseUrl = fetchUri.split(this.settings.repositoryOwner)[0]
195-
fetchUri = `${baseUrl}/${qualifiedRepository}.git`
204+
fetchUri = `${baseUrl}${qualifiedRepository}.git`
196205
}
197206

198207
return this.gitCommandManager.remoteAdd(remoteName, fetchUri)

0 commit comments

Comments
 (0)