-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[eas-cli] use existing google service account key (#650)
* [eas-cli] use existing google service account key * changelog * pr feedback * update snap
- Loading branch information
Showing
10 changed files
with
212 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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
69 changes: 69 additions & 0 deletions
69
packages/eas-cli/src/credentials/android/actions/UseExistingGoogleServiceAccountKey.ts
This file contains 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,69 @@ | ||
import chalk from 'chalk'; | ||
|
||
import { GoogleServiceAccountKeyFragment } from '../../../graphql/generated'; | ||
import Log from '../../../log'; | ||
import { promptAsync } from '../../../prompts'; | ||
import { Account } from '../../../user/Account'; | ||
import { fromNow } from '../../../utils/date'; | ||
import { Context } from '../../context'; | ||
|
||
export class UseExistingGoogleServiceAccountKey { | ||
constructor(private account: Account) {} | ||
|
||
public async runAsync(ctx: Context): Promise<GoogleServiceAccountKeyFragment | null> { | ||
if (ctx.nonInteractive) { | ||
throw new Error( | ||
`Existing Google Service Account Key cannot be chosen in non-interactive mode.` | ||
); | ||
} | ||
const gsaKeyFragments = await ctx.android.getGoogleServiceAccountKeysForAccountAsync( | ||
this.account | ||
); | ||
if (gsaKeyFragments.length === 0) { | ||
Log.error("There aren't any Google Service Account Keys associated with your account."); | ||
return null; | ||
} | ||
return await this.selectGoogleServiceAccountKeyAsync(gsaKeyFragments); | ||
} | ||
|
||
private async selectGoogleServiceAccountKeyAsync( | ||
keys: GoogleServiceAccountKeyFragment[] | ||
): Promise<GoogleServiceAccountKeyFragment | null> { | ||
const sortedKeys = this.sortGoogleServiceAccountKeysByUpdatedAtDesc(keys); | ||
const { chosenKey } = await promptAsync({ | ||
type: 'select', | ||
name: 'chosenKey', | ||
message: 'Select a Google Service Account Key:', | ||
choices: sortedKeys.map(key => ({ | ||
title: this.formatGoogleServiceAccountKey(key), | ||
value: key, | ||
})), | ||
}); | ||
return chosenKey; | ||
} | ||
|
||
private sortGoogleServiceAccountKeysByUpdatedAtDesc( | ||
keys: GoogleServiceAccountKeyFragment[] | ||
): GoogleServiceAccountKeyFragment[] { | ||
return keys.sort( | ||
(keyA, keyB) => | ||
new Date(keyB.updatedAt).getMilliseconds() - new Date(keyA.updatedAt).getMilliseconds() | ||
); | ||
} | ||
|
||
private formatGoogleServiceAccountKey({ | ||
projectIdentifier, | ||
privateKeyIdentifier, | ||
clientEmail, | ||
clientIdentifier, | ||
updatedAt, | ||
}: GoogleServiceAccountKeyFragment): string { | ||
let line: string = ''; | ||
line += `Client Email: ${clientEmail}, Project Id: ${projectIdentifier}`; | ||
line += chalk.gray( | ||
`\n Client Id: ${clientIdentifier}, Private Key Id: ${privateKeyIdentifier}` | ||
); | ||
line += chalk.gray(`\n Updated: ${fromNow(new Date(updatedAt))} ago,`); | ||
return line; | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...-cli/src/credentials/android/actions/__tests__/UseExistingGoogleServiceAccountKey-test.ts
This file contains 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,59 @@ | ||
import { asMock } from '../../../../__tests__/utils'; | ||
import { promptAsync } from '../../../../prompts'; | ||
import { | ||
getNewAndroidApiMock, | ||
testGoogleServiceAccountKeyFragment, | ||
} from '../../../__tests__/fixtures-android'; | ||
import { createCtxMock } from '../../../__tests__/fixtures-context'; | ||
import { getAppLookupParamsFromContextAsync } from '../BuildCredentialsUtils'; | ||
import { UseExistingGoogleServiceAccountKey } from '../UseExistingGoogleServiceAccountKey'; | ||
|
||
jest.mock('../../../../prompts'); | ||
asMock(promptAsync).mockImplementation(() => ({ chosenKey: testGoogleServiceAccountKeyFragment })); | ||
|
||
describe(UseExistingGoogleServiceAccountKey, () => { | ||
it('uses an existing Google Service Account Key in Interactive Mode', async () => { | ||
const ctx = createCtxMock({ | ||
nonInteractive: false, | ||
android: { | ||
...getNewAndroidApiMock(), | ||
getGoogleServiceAccountKeysForAccountAsync: jest.fn(() => [ | ||
testGoogleServiceAccountKeyFragment, | ||
]), | ||
}, | ||
}); | ||
const appLookupParams = await getAppLookupParamsFromContextAsync(ctx); | ||
const useExistingGoogleServiceAccountKeyAction = new UseExistingGoogleServiceAccountKey( | ||
appLookupParams.account | ||
); | ||
const selectedKey = await useExistingGoogleServiceAccountKeyAction.runAsync(ctx); | ||
expect(ctx.android.getGoogleServiceAccountKeysForAccountAsync).toHaveBeenCalledTimes(1); | ||
expect(selectedKey).toMatchObject(testGoogleServiceAccountKeyFragment); | ||
}); | ||
it("returns null if the account doesn't have any Google Service Account Keys", async () => { | ||
const ctx = createCtxMock({ | ||
nonInteractive: false, | ||
android: { | ||
...getNewAndroidApiMock(), | ||
getGoogleServiceAccountKeysForAccountAsync: jest.fn(() => []), | ||
}, | ||
}); | ||
const appLookupParams = await getAppLookupParamsFromContextAsync(ctx); | ||
const useExistingGoogleServiceAccountKeyAction = new UseExistingGoogleServiceAccountKey( | ||
appLookupParams.account | ||
); | ||
const selectedKey = await useExistingGoogleServiceAccountKeyAction.runAsync(ctx); | ||
expect(ctx.android.getGoogleServiceAccountKeysForAccountAsync).toHaveBeenCalledTimes(1); | ||
expect(selectedKey).toBe(null); | ||
}); | ||
it('errors in Non-Interactive Mode', async () => { | ||
const ctx = createCtxMock({ nonInteractive: true }); | ||
const appLookupParams = await getAppLookupParamsFromContextAsync(ctx); | ||
const useExistingGoogleServiceAccountKeyAction = new UseExistingGoogleServiceAccountKey( | ||
appLookupParams.account | ||
); | ||
|
||
// fail if users are running in non-interactive mode | ||
await expect(useExistingGoogleServiceAccountKeyAction.runAsync(ctx)).rejects.toThrowError(); | ||
}); | ||
}); |
This file contains 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
38 changes: 38 additions & 0 deletions
38
packages/eas-cli/src/credentials/android/api/graphql/queries/GoogleServiceAccountKeyQuery.ts
This file contains 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,38 @@ | ||
import { print } from 'graphql'; | ||
import gql from 'graphql-tag'; | ||
|
||
import { graphqlClient, withErrorHandlingAsync } from '../../../../../graphql/client'; | ||
import { | ||
GoogleServiceAccountKeyByAccountQuery, | ||
GoogleServiceAccountKeyFragment, | ||
} from '../../../../../graphql/generated'; | ||
import { GoogleServiceAccountKeyFragmentNode } from '../../../../../graphql/types/credentials/GoogleServiceAccountKey'; | ||
|
||
export const GoogleServiceAccountKeyQuery = { | ||
async getAllForAccountAsync(accountName: string): Promise<GoogleServiceAccountKeyFragment[]> { | ||
const data = await withErrorHandlingAsync( | ||
graphqlClient | ||
.query<GoogleServiceAccountKeyByAccountQuery>( | ||
gql` | ||
query GoogleServiceAccountKeyByAccountQuery($accountName: String!) { | ||
account { | ||
byName(accountName: $accountName) { | ||
id | ||
googleServiceAccountKeys { | ||
id | ||
...GoogleServiceAccountKeyFragment | ||
} | ||
} | ||
} | ||
} | ||
${print(GoogleServiceAccountKeyFragmentNode)} | ||
`, | ||
{ | ||
accountName, | ||
} | ||
) | ||
.toPromise() | ||
); | ||
return data.account.byName.googleServiceAccountKeys; | ||
}, | ||
}; |
This file contains 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 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.