-
Notifications
You must be signed in to change notification settings - Fork 221
fix: full sync all app installations, not just the first #1044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rafaelleonardocruz
wants to merge
3
commits into
github-community-projects:main-enterprise
Choose a base branch
from
rafaelleonardocruz:fix/full-sync-all-installations
base: main-enterprise
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+227
−12
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9b63a02
fix: full sync all app installations, not just the first
rafaelleonardocruz f720b1a
fix: treat a missing sync result as an installation failure
rafaelleonardocruz ad45b4d
Merge branch 'main-enterprise' into fix/full-sync-all-installations
rafaelleonardocruz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| const plugin = require('../../index') | ||
|
|
||
| // The runtime config is fetched from the admin repo over the API. The | ||
| // installation fan-out under test does not depend on its contents. | ||
| jest.mock('../../lib/configManager', () => { | ||
| return class ConfigManager { | ||
| async loadGlobalSettingsYaml () { | ||
| return {} | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| describe('syncInstallation', () => { | ||
| let robot, Settings, installations | ||
|
|
||
| const installation = (id, login) => ({ id, account: { login } }) | ||
|
|
||
| const createOctokit = () => ({ | ||
| paginate: jest.fn(() => Promise.resolve(installations)), | ||
| rest: { | ||
| apps: { | ||
| listInstallations: { endpoint: { merge: jest.fn(() => ({})) } }, | ||
| getAuthenticated: jest.fn(() => Promise.resolve({ data: { slug: 'safe-settings' } })) | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| const createApp = () => plugin(robot, {}, Settings) | ||
|
|
||
| beforeEach(() => { | ||
| installations = [] | ||
| Settings = { syncAll: jest.fn(() => Promise.resolve({ errors: [] })) } | ||
| robot = { | ||
| on: jest.fn(), | ||
| auth: jest.fn(() => Promise.resolve(createOctokit())), | ||
| log: { | ||
| trace: jest.fn(), | ||
| debug: jest.fn(), | ||
| info: jest.fn(), | ||
| error: jest.fn() | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| describe('with multiple installations', () => { | ||
| beforeEach(() => { | ||
| installations = [installation(1, 'org-one'), installation(2, 'org-two')] | ||
| }) | ||
|
|
||
| it('syncs every installation with its own owner', async () => { | ||
| const app = createApp() | ||
|
|
||
| const result = await app.syncInstallation() | ||
|
|
||
| expect(Settings.syncAll).toHaveBeenCalledTimes(2) | ||
| expect(Settings.syncAll.mock.calls.map(call => call[2])).toEqual([ | ||
| { repo: 'admin', owner: 'org-one' }, | ||
| { repo: 'admin', owner: 'org-two' } | ||
| ]) | ||
| expect(result.results).toHaveLength(2) | ||
| expect(result.errors).toEqual([]) | ||
| }) | ||
|
|
||
| it('passes the nop flag through to every installation', async () => { | ||
| const app = createApp() | ||
|
|
||
| await app.syncInstallation(true) | ||
|
|
||
| expect(Settings.syncAll.mock.calls.map(call => call[0])).toEqual([true, true]) | ||
| }) | ||
|
|
||
| it('logs a single summary of the sync', async () => { | ||
| const app = createApp() | ||
|
|
||
| await app.syncInstallation() | ||
|
|
||
| expect(robot.log.info).toHaveBeenCalledTimes(1) | ||
| expect(robot.log.info).toHaveBeenCalledWith(expect.stringContaining('Synced 2 of 2 installation(s); 0 failed')) | ||
| }) | ||
|
|
||
| it('aggregates errors reported by the individual syncs', async () => { | ||
| Settings.syncAll | ||
| .mockResolvedValueOnce({ errors: ['boom'] }) | ||
| .mockResolvedValueOnce({ errors: [] }) | ||
| const app = createApp() | ||
|
|
||
| const result = await app.syncInstallation() | ||
|
|
||
| expect(result.errors).toEqual(['boom']) | ||
| }) | ||
| }) | ||
|
|
||
| describe('when an installation fails', () => { | ||
| const failure = new Error('installation is suspended') | ||
|
|
||
| beforeEach(() => { | ||
| installations = [installation(1, 'org-one'), installation(2, 'org-two')] | ||
| Settings.syncAll | ||
| .mockRejectedValueOnce(failure) | ||
| .mockResolvedValueOnce({ errors: [] }) | ||
| }) | ||
|
|
||
| it('still syncs the remaining installations', async () => { | ||
| const app = createApp() | ||
|
|
||
| await app.syncInstallation() | ||
|
|
||
| expect(Settings.syncAll).toHaveBeenCalledTimes(2) | ||
| expect(Settings.syncAll.mock.calls[1][2]).toEqual({ repo: 'admin', owner: 'org-two' }) | ||
| }) | ||
|
|
||
| it('surfaces the failure in the returned errors', async () => { | ||
| const app = createApp() | ||
|
|
||
| const result = await app.syncInstallation() | ||
|
|
||
| expect(result.errors).toEqual([failure]) | ||
| expect(result.results).toHaveLength(1) | ||
| }) | ||
|
|
||
| it('logs the failing installation id and account', async () => { | ||
| const app = createApp() | ||
|
|
||
| await app.syncInstallation() | ||
|
|
||
| expect(robot.log.error).toHaveBeenCalledWith(expect.stringContaining('installation 1 for org-one')) | ||
| expect(robot.log.info).toHaveBeenCalledWith(expect.stringContaining('Synced 1 of 2 installation(s); 1 failed')) | ||
| }) | ||
| }) | ||
|
|
||
| describe('without any installation', () => { | ||
| it('returns null and does not sync', async () => { | ||
| const app = createApp() | ||
|
|
||
| const result = await app.syncInstallation() | ||
|
|
||
| expect(result).toBeNull() | ||
| expect(Settings.syncAll).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.