Skip to content

Commit 0e8ef88

Browse files
authored
fix: issue with skipping bot pushes (#382)
* fix: issue with skipping bot pushes * test: exit sync early if no action necessary
1 parent 08d02c2 commit 0e8ef88

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

src/bot/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,6 @@ function bot(app: Probot) {
293293
const metadata = getMetadata(context.payload.repository.description)
294294
botLogger.info('Metadata: ', metadata)
295295

296-
// Ignore if it was the bot
297-
if (context.payload.sender.type === 'Bot') {
298-
botLogger.info('Push was from bot, skipping')
299-
return
300-
}
301-
302296
// Call sync logic for either (1) fork branch change or (2) mirror default change
303297
if (!isMirror && !metadata.mirror) {
304298
await syncPushToMirror(context.payload)

src/server/git/controller.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,25 @@ export const syncReposHandler = async ({
3131
const privateInstallationId = octokitData.private.installationId
3232
const privateAccessToken = octokitData.private.accessToken
3333

34+
const forkRef = await contributionOctokit.rest.git.getRef({
35+
owner: input.forkOwner,
36+
repo: input.forkName,
37+
ref: `heads/${input.forkBranchName}`,
38+
})
39+
40+
const mirrorRef = await privateOctokit.rest.git.getRef({
41+
owner: input.mirrorOwner,
42+
repo: input.mirrorName,
43+
ref: `heads/${input.mirrorBranchName}`,
44+
})
45+
46+
if (forkRef.data.object.sha === mirrorRef.data.object.sha) {
47+
gitApiLogger.debug('Fork and mirror are already in sync')
48+
return {
49+
success: true,
50+
}
51+
}
52+
3453
const forkRepo = await contributionOctokit.rest.repos.get({
3554
owner: input.forkOwner,
3655
repo: input.forkName,

test/server/git/controller.test.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ import * as auth from '../../../src/utils/auth'
44
import * as dir from '../../../src/utils/dir'
55
import simpleGit from 'simple-git'
66

7+
const getRefSpy = jest.fn()
8+
79
const fakeOctokitData = {
810
accessToken: 'fake-token',
911
octokit: {
1012
rest: {
13+
git: {
14+
getRef: getRefSpy,
15+
},
1116
repos: {
1217
get: jest.fn().mockResolvedValue({
1318
data: {
@@ -60,6 +65,22 @@ describe('Git controller', () => {
6065
privateOrg: 'github-test',
6166
})
6267

68+
getRefSpy.mockResolvedValueOnce({
69+
data: {
70+
object: {
71+
sha: 'fork-sha',
72+
},
73+
},
74+
})
75+
76+
getRefSpy.mockResolvedValueOnce({
77+
data: {
78+
object: {
79+
sha: 'mirror-sha',
80+
},
81+
},
82+
})
83+
6384
jest
6485
.spyOn(auth, 'generateAuthUrl')
6586
.mockReturnValueOnce(
@@ -104,6 +125,22 @@ describe('Git controller', () => {
104125
privateOrg: 'github-test',
105126
})
106127

128+
getRefSpy.mockResolvedValueOnce({
129+
data: {
130+
object: {
131+
sha: 'fork-sha',
132+
},
133+
},
134+
})
135+
136+
getRefSpy.mockResolvedValueOnce({
137+
data: {
138+
object: {
139+
sha: 'mirror-sha',
140+
},
141+
},
142+
})
143+
107144
jest
108145
.spyOn(auth, 'generateAuthUrl')
109146
.mockReturnValueOnce(
@@ -138,4 +175,46 @@ describe('Git controller', () => {
138175
expect(gitMock.push).toHaveBeenCalledTimes(1)
139176
expect(gitMock.push).toHaveBeenCalledWith(['--force'])
140177
})
178+
179+
it('should return success early if the fork and mirror are already in sync', async () => {
180+
jest.spyOn(config, 'getConfig').mockResolvedValue({
181+
publicOrg: 'github',
182+
privateOrg: 'github-test',
183+
})
184+
185+
getRefSpy.mockResolvedValueOnce({
186+
data: {
187+
object: {
188+
sha: 'sha',
189+
},
190+
},
191+
})
192+
193+
getRefSpy.mockResolvedValueOnce({
194+
data: {
195+
object: {
196+
sha: 'sha',
197+
},
198+
},
199+
})
200+
201+
const result = await syncReposHandler({
202+
input: {
203+
accessToken: '123',
204+
orgId: 'test-org',
205+
destinationTo: 'mirror',
206+
forkOwner: 'github',
207+
forkName: 'fork-repo',
208+
mirrorOwner: 'github-test',
209+
mirrorName: 'mirror-repo',
210+
mirrorBranchName: 'mirror-branch',
211+
forkBranchName: 'fork-branch',
212+
},
213+
})
214+
215+
expect(result).toEqual({ success: true })
216+
expect(gitMock.checkoutBranch).toHaveBeenCalledTimes(0)
217+
expect(gitMock.rebase).toHaveBeenCalledTimes(0)
218+
expect(gitMock.push).toHaveBeenCalledTimes(0)
219+
})
141220
})

0 commit comments

Comments
 (0)