From ce9c70ad435df038d5ce628c29aa982c51c47285 Mon Sep 17 00:00:00 2001 From: ivinokur Date: Mon, 11 Dec 2023 14:17:53 +0200 Subject: [PATCH 1/3] Align the git services skip-oauth flag with oauth-1 providers --- .../GitServicesTab/ProviderIcon/index.tsx | 5 +- .../__snapshots__/index.spec.tsx.snap | 164 ++++++++++++++++-- .../GitServicesTab/__tests__/index.spec.tsx | 7 +- .../UserPreferences/GitServicesTab/index.tsx | 12 +- .../src/pages/UserPreferences/const.ts | 4 +- 5 files changed, 168 insertions(+), 24 deletions(-) diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/ProviderIcon/index.tsx b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/ProviderIcon/index.tsx index beea5fd2a..3c8370177 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/ProviderIcon/index.tsx +++ b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/ProviderIcon/index.tsx @@ -66,7 +66,10 @@ export class ProviderIcon extends React.PureComponent { } private isSkipOauth(providerName: api.GitOauthProvider): boolean { - return this.props.skipOauthProviders.includes(providerName); + // Use includes filter to handle the bitbucket-server oauth 2 provider. + // The bitbucket server oauth2 provider name is 'bitbucket', + // but the corresponding 'skip oauth' item is 'bitbucket-server'. + return this.props.skipOauthProviders.some(s => s.includes(providerName)); } private hasOauthToken(providerName: api.GitOauthProvider): boolean { diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/__snapshots__/index.spec.tsx.snap b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/__snapshots__/index.spec.tsx.snap index b6a53d8a3..9032d92c6 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/__snapshots__/index.spec.tsx.snap +++ b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/__snapshots__/index.spec.tsx.snap @@ -12,7 +12,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` >
@@ -231,7 +231,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` aria-label="Actions" className="pf-c-dropdown__toggle pf-m-plain" disabled={false} - id="pf-dropdown-toggle-id-4" + id="pf-dropdown-toggle-id-0" onClick={[Function]} onKeyDown={[Function]} type="button" @@ -260,7 +260,7 @@ exports[`GitServices with 4 git services snapshot 1`] = `
@@ -359,7 +359,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` aria-label="Actions" className="pf-c-dropdown__toggle pf-m-plain" disabled={true} - id="pf-dropdown-toggle-id-5" + id="pf-dropdown-toggle-id-1" onClick={[Function]} onKeyDown={[Function]} type="button" @@ -388,7 +388,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` - Bitbucket Server (OAuth 1.0) + Bitbucket
@@ -487,7 +487,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` aria-label="Actions" className="pf-c-dropdown__toggle pf-m-plain" disabled={true} - id="pf-dropdown-toggle-id-6" + id="pf-dropdown-toggle-id-2" onClick={[Function]} onKeyDown={[Function]} type="button" @@ -516,7 +516,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` + + Bitbucket (OAuth 1.0) + + + + https://bitbucket-server.dummy.endpoint.org + + + +
+ + + + + Unauthorized + +
+ + +
+ +
+ + + + + +
@@ -615,7 +743,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` aria-label="Actions" className="pf-c-dropdown__toggle pf-m-plain" disabled={true} - id="pf-dropdown-toggle-id-7" + id="pf-dropdown-toggle-id-4" onClick={[Function]} onKeyDown={[Function]} type="button" diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/index.spec.tsx b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/index.spec.tsx index b7dea1613..7408c1485 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/index.spec.tsx +++ b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/index.spec.tsx @@ -105,6 +105,10 @@ describe('GitServices', () => { .withName('bitbucket') .withEndpointUrl('https://bitbucket.dummy.endpoint.org') .build(), + new FakeGitOauthBuilder() + .withName('bitbucket-server') + .withEndpointUrl('https://bitbucket-server.dummy.endpoint.org') + .build(), new FakeGitOauthBuilder() .withName('azure-devops') .withEndpointUrl('https://azure.dummy.endpoint.com/') @@ -124,11 +128,12 @@ describe('GitServices', () => { const actions = screen.queryAllByRole('button', { name: /actions/i }); - expect(actions.length).toEqual(4); + expect(actions.length).toEqual(5); expect(actions[0]).not.toBeDisabled(); expect(actions[1]).toBeDisabled(); expect(actions[2]).toBeDisabled(); expect(actions[3]).toBeDisabled(); + expect(actions[4]).toBeDisabled(); }); test('snapshot', () => { diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/index.tsx b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/index.tsx index 166c1336d..961f58efa 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/index.tsx +++ b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/index.tsx @@ -116,7 +116,10 @@ export class GitServices extends React.PureComponent { } private isSkipOauth(providerName: api.GitOauthProvider): boolean { - return this.props.skipOauthProviders.includes(providerName); + // Use includes filter to handle the bitbucket-server oauth 2 provider. + // The bitbucket server oauth2 provider name is 'bitbucket', + // but the corresponding 'skip oauth' item is 'bitbucket-server'. + return this.props.skipOauthProviders.some(s => s.includes(providerName)); } private hasOauthToken(providerName: api.GitOauthProvider): boolean { @@ -149,7 +152,12 @@ export class GitServices extends React.PureComponent { title: 'Clear', onClick: (event, rowIndex) => { event.stopPropagation(); - this.props.deleteSkipOauth(gitOauth[rowIndex].name); + // Use includes filter to handle the bitbucket-server oauth 2 provider. + // The bitbucket server oauth2 provider name is 'bitbucket', + // but the corresponding 'skip oauth' item is 'bitbucket-server'. + const itemName = gitOauth[rowIndex].name; + const providerName = this.props.skipOauthProviders.find(s => s.includes(itemName)); + this.props.deleteSkipOauth(providerName !== undefined ? providerName : itemName); }, }, ]; diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/const.ts b/packages/dashboard-frontend/src/pages/UserPreferences/const.ts index bce6af685..93a364b7d 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/const.ts +++ b/packages/dashboard-frontend/src/pages/UserPreferences/const.ts @@ -16,10 +16,10 @@ export const GIT_OAUTH_PROVIDERS: Record = { 'azure-devops': 'Microsoft Azure DevOps', // Either Bitbucket Cloud or Bitbucket Server // https://github.com/eclipse-che/che-server/blob/main/wsmaster/che-core-api-auth-bitbucket/src/main/java/org/eclipse/che/security/oauth/BitbucketOAuthAuthenticator.java - 'bitbucket-server': 'Bitbucket', + 'bitbucket-server': 'Bitbucket (OAuth 1.0)', // Bitbucket Server only // https://github.com/eclipse-che/che-server/blob/main/wsmaster/che-core-api-auth-bitbucket/src/main/java/org/eclipse/che/security/oauth1/BitbucketServerOAuthAuthenticator.java - bitbucket: 'Bitbucket Server (OAuth 1.0)', + bitbucket: 'Bitbucket', github: 'GitHub', github_2: 'GitHub (The second provider)', gitlab: 'GitLab', From 01fb39db02ff0b9c6fbb83b3275b8f1f0c56b946 Mon Sep 17 00:00:00 2001 From: ivinokur Date: Mon, 11 Dec 2023 16:33:26 +0200 Subject: [PATCH 2/3] fixup! Align the git services skip-oauth flag with oauth-1 providers --- .../__snapshots__/index.spec.tsx.snap | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/__snapshots__/index.spec.tsx.snap b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/__snapshots__/index.spec.tsx.snap index 9032d92c6..22aa145c9 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/__snapshots__/index.spec.tsx.snap +++ b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/__tests__/__snapshots__/index.spec.tsx.snap @@ -12,7 +12,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` >
@@ -231,7 +231,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` aria-label="Actions" className="pf-c-dropdown__toggle pf-m-plain" disabled={false} - id="pf-dropdown-toggle-id-0" + id="pf-dropdown-toggle-id-5" onClick={[Function]} onKeyDown={[Function]} type="button" @@ -260,7 +260,7 @@ exports[`GitServices with 4 git services snapshot 1`] = `
@@ -359,7 +359,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` aria-label="Actions" className="pf-c-dropdown__toggle pf-m-plain" disabled={true} - id="pf-dropdown-toggle-id-1" + id="pf-dropdown-toggle-id-6" onClick={[Function]} onKeyDown={[Function]} type="button" @@ -388,7 +388,7 @@ exports[`GitServices with 4 git services snapshot 1`] = `
@@ -487,7 +487,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` aria-label="Actions" className="pf-c-dropdown__toggle pf-m-plain" disabled={true} - id="pf-dropdown-toggle-id-2" + id="pf-dropdown-toggle-id-7" onClick={[Function]} onKeyDown={[Function]} type="button" @@ -516,7 +516,7 @@ exports[`GitServices with 4 git services snapshot 1`] = `
@@ -615,7 +615,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` aria-label="Actions" className="pf-c-dropdown__toggle pf-m-plain" disabled={true} - id="pf-dropdown-toggle-id-3" + id="pf-dropdown-toggle-id-8" onClick={[Function]} onKeyDown={[Function]} type="button" @@ -644,7 +644,7 @@ exports[`GitServices with 4 git services snapshot 1`] = `
@@ -743,7 +743,7 @@ exports[`GitServices with 4 git services snapshot 1`] = ` aria-label="Actions" className="pf-c-dropdown__toggle pf-m-plain" disabled={true} - id="pf-dropdown-toggle-id-4" + id="pf-dropdown-toggle-id-9" onClick={[Function]} onKeyDown={[Function]} type="button" From 346dc2526111125f1696f11285403f93b72b2ce0 Mon Sep 17 00:00:00 2001 From: ivinokur Date: Wed, 20 Dec 2023 12:49:07 +0200 Subject: [PATCH 3/3] fixup! Align the git services skip-oauth flag with oauth-1 providers --- .../GitServicesTab/ProviderIcon/index.tsx | 5 +++- .../GitOauthConfig/__tests__/index.spec.ts | 23 ++++++++++++++++++- .../src/store/GitOauthConfig/index.ts | 7 +++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/ProviderIcon/index.tsx b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/ProviderIcon/index.tsx index 3c8370177..9ab59ebef 100644 --- a/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/ProviderIcon/index.tsx +++ b/packages/dashboard-frontend/src/pages/UserPreferences/GitServicesTab/ProviderIcon/index.tsx @@ -73,7 +73,10 @@ export class ProviderIcon extends React.PureComponent { } private hasOauthToken(providerName: api.GitOauthProvider): boolean { - return this.props.providersWithToken.includes(providerName); + // Use includes filter to handle the bitbucket-server oauth 2 provider. + // The bitbucket server oauth2 provider name is 'bitbucket', + // but the corresponding 'provider with token' item is 'bitbucket-server'. + return this.props.providersWithToken.some(p => p.includes(providerName)); } public render(): React.ReactElement { diff --git a/packages/dashboard-frontend/src/store/GitOauthConfig/__tests__/index.spec.ts b/packages/dashboard-frontend/src/store/GitOauthConfig/__tests__/index.spec.ts index d8687d760..490829926 100644 --- a/packages/dashboard-frontend/src/store/GitOauthConfig/__tests__/index.spec.ts +++ b/packages/dashboard-frontend/src/store/GitOauthConfig/__tests__/index.spec.ts @@ -32,6 +32,10 @@ const gitOauth = [ name: 'azure-devops', endpointUrl: 'https://dev.azure.com', }, + { + name: 'bitbucket-server', + endpointUrl: 'https://bitbucket-server.com', + }, ] as IGitOauth[]; const mockGetOAuthProviders = jest.fn().mockResolvedValue(gitOauth); @@ -46,8 +50,18 @@ const mockFetchTokens = jest.fn().mockResolvedValue([ { tokenName: 'github-personal-access-token', gitProvider: 'oauth2-token', + gitProviderEndpoint: 'https://github.com/', + }, + { + tokenName: 'azure-devops-personal-access-token', + gitProvider: 'oauth2-token', gitProviderEndpoint: 'https://dev.azure.com/', }, + { + tokenName: 'bitbucket-server-personal-access-token', + gitProvider: 'che-token--', + gitProviderEndpoint: 'https://bitbucket-server.com/', + }, ] as any[]); jest.mock('../../../services/backend-client/oAuthApi', () => { @@ -66,6 +80,13 @@ jest.mock('../../../services/backend-client/personalAccessTokenApi', () => { // mute the outputs console.error = jest.fn(); +window = Object.create(window); +Object.defineProperty(window, 'location', { + value: { + hostname: 'bitbucket-server', + }, +}); + describe('GitOauthConfig store, actions', () => { let store: MockStoreEnhanced>; @@ -86,7 +107,7 @@ describe('GitOauthConfig store, actions', () => { const expectedAction: TestStore.KnownAction = { supportedGitOauth: gitOauth, - providersWithToken: ['github', 'azure-devops'], + providersWithToken: ['github', 'azure-devops', 'bitbucket-server'], type: TestStore.Type.RECEIVE_GIT_OAUTH_PROVIDERS, }; diff --git a/packages/dashboard-frontend/src/store/GitOauthConfig/index.ts b/packages/dashboard-frontend/src/store/GitOauthConfig/index.ts index bc8b18cba..afc88e60a 100644 --- a/packages/dashboard-frontend/src/store/GitOauthConfig/index.ts +++ b/packages/dashboard-frontend/src/store/GitOauthConfig/index.ts @@ -167,8 +167,13 @@ export const actionCreators: ActionCreators = { : token.gitProviderEndpoint; // compare Git OAuth Endpoint url ONLY with OAuth tokens + const gitProvider = token.gitProvider; if ( - token.gitProvider.startsWith('oauth2') && + (gitProvider.startsWith('oauth2') || + // The git provider value format of a bitbucket-server token is 'che-token--' + new RegExp(`^che-token-<.*>-<${window.location.hostname}>$`).test( + gitProvider, + )) && normalizedGitOauthEndpoint === normalizedTokenGitProviderEndpoint ) { providersWithToken.push(gitOauth.name);