Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/bot/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,6 @@ function bot(app: Probot) {
const metadata = getMetadata(context.payload.repository.description)
botLogger.info('Metadata: ', metadata)

// Ignore if it was the bot
if (context.payload.sender.type === 'Bot') {
botLogger.info('Push was from bot, skipping')
return
}

// Call sync logic for either (1) fork branch change or (2) mirror default change
if (!isMirror && !metadata.mirror) {
await syncPushToMirror(context.payload)
Expand Down
19 changes: 19 additions & 0 deletions src/server/git/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ export const syncReposHandler = async ({
const privateInstallationId = octokitData.private.installationId
const privateAccessToken = octokitData.private.accessToken

const forkRef = await contributionOctokit.rest.git.getRef({
owner: input.forkOwner,
repo: input.forkName,
ref: `heads/${input.forkBranchName}`,
})

const mirrorRef = await privateOctokit.rest.git.getRef({
owner: input.mirrorOwner,
repo: input.mirrorName,
ref: `heads/${input.mirrorBranchName}`,
})

if (forkRef.data.object.sha === mirrorRef.data.object.sha) {
gitApiLogger.debug('Fork and mirror are already in sync')
return {
success: true,
}
}

const forkRepo = await contributionOctokit.rest.repos.get({
owner: input.forkOwner,
repo: input.forkName,
Expand Down
79 changes: 79 additions & 0 deletions test/server/git/controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import * as auth from '../../../src/utils/auth'
import * as dir from '../../../src/utils/dir'
import simpleGit from 'simple-git'

const getRefSpy = jest.fn()

const fakeOctokitData = {
accessToken: 'fake-token',
octokit: {
rest: {
git: {
getRef: getRefSpy,
},
repos: {
get: jest.fn().mockResolvedValue({
data: {
Expand Down Expand Up @@ -60,6 +65,22 @@ describe('Git controller', () => {
privateOrg: 'github-test',
})

getRefSpy.mockResolvedValueOnce({
data: {
object: {
sha: 'fork-sha',
},
},
})

getRefSpy.mockResolvedValueOnce({
data: {
object: {
sha: 'mirror-sha',
},
},
})

jest
.spyOn(auth, 'generateAuthUrl')
.mockReturnValueOnce(
Expand Down Expand Up @@ -104,6 +125,22 @@ describe('Git controller', () => {
privateOrg: 'github-test',
})

getRefSpy.mockResolvedValueOnce({
data: {
object: {
sha: 'fork-sha',
},
},
})

getRefSpy.mockResolvedValueOnce({
data: {
object: {
sha: 'mirror-sha',
},
},
})

jest
.spyOn(auth, 'generateAuthUrl')
.mockReturnValueOnce(
Expand Down Expand Up @@ -138,4 +175,46 @@ describe('Git controller', () => {
expect(gitMock.push).toHaveBeenCalledTimes(1)
expect(gitMock.push).toHaveBeenCalledWith(['--force'])
})

it('should return success early if the fork and mirror are already in sync', async () => {
jest.spyOn(config, 'getConfig').mockResolvedValue({
publicOrg: 'github',
privateOrg: 'github-test',
})

getRefSpy.mockResolvedValueOnce({
data: {
object: {
sha: 'sha',
},
},
})

getRefSpy.mockResolvedValueOnce({
data: {
object: {
sha: 'sha',
},
},
})

const result = await syncReposHandler({
input: {
accessToken: '123',
orgId: 'test-org',
destinationTo: 'mirror',
forkOwner: 'github',
forkName: 'fork-repo',
mirrorOwner: 'github-test',
mirrorName: 'mirror-repo',
mirrorBranchName: 'mirror-branch',
forkBranchName: 'fork-branch',
},
})

expect(result).toEqual({ success: true })
expect(gitMock.checkoutBranch).toHaveBeenCalledTimes(0)
expect(gitMock.rebase).toHaveBeenCalledTimes(0)
expect(gitMock.push).toHaveBeenCalledTimes(0)
})
})
Loading