From cf963ceae8258e00abda8730324f3f4139649d21 Mon Sep 17 00:00:00 2001 From: Nikita Nek Dudnik Date: Mon, 4 May 2020 13:46:15 +0200 Subject: [PATCH] Add owner and repo options to authenticate method --- plugins/circleci/src/api/index.ts | 25 +++++++++++------- .../src/components/LoginCard/LoginCard.tsx | 26 ++++++++++++++++--- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/plugins/circleci/src/api/index.ts b/plugins/circleci/src/api/index.ts index 849ac4ce1a..aadb2b144d 100644 --- a/plugins/circleci/src/api/index.ts +++ b/plugins/circleci/src/api/index.ts @@ -14,8 +14,15 @@ * limitations under the License. */ -import { CircleCI, GitType, CircleCIOptions } from 'circleci-api'; +import { CircleCI, GitType, CircleCIOptions, GitInfo } from 'circleci-api'; import { ApiRef } from '@backstage/core'; +import { default } from '../../../../packages/core/src/components/Status/Status.stories'; + +const defaultVcsOptions: GitInfo = { + type: GitType.GITHUB, // default: github + owner: 'CircleCITest3', + repo: 'circleci-test', +} const options: Partial = { // Required for all requests @@ -25,20 +32,20 @@ const options: Partial = { // Anything set here can be overriden when making the request // Git information is required for project/build/etc endpoints - vcs: { - type: GitType.GITHUB, // default: github - owner: 'CircleCITest3', - repo: 'circleci-test', - }, + vcs: defaultVcsOptions }; export class CircleCIApi { api: null | CircleCI = null; constuctor() {} - async authenticate(token: string) { + async authenticate({token, owner, repo}: {token: string, owner: string, repo: string}) { try { - if (token === '') return Promise.reject(); - this.api = new CircleCI({ ...options, token }); + if (token === '' || owner === '' || repo === '') return Promise.reject(); + this.api = new CircleCI({ ...options, token, vcs: { + type: GitType.GITHUB, // default: github + owner, + repo, + }}); // await this.api.me(); return Promise.resolve(); } catch (e) { diff --git a/plugins/circleci/src/components/LoginCard/LoginCard.tsx b/plugins/circleci/src/components/LoginCard/LoginCard.tsx index 806f218cf9..e50ca4bc0b 100644 --- a/plugins/circleci/src/components/LoginCard/LoginCard.tsx +++ b/plugins/circleci/src/components/LoginCard/LoginCard.tsx @@ -32,12 +32,15 @@ const useSessionStorage = (key: string): [string, (value: string) => void] => { return [value, setValue]; }; export const LoginCard = () => { - const [token, setToken] = useSessionStorage(circleCIApiRef.id); + const [token, setToken] = useSessionStorage(circleCIApiRef.id + '_token'); + const [owner, setOwner] = useSessionStorage(circleCIApiRef.id + '_owner'); + const [repo, setRepo] = useSessionStorage(circleCIApiRef.id + '_repo'); + const api = useApi(circleCIApiRef); React.useEffect(() => { if (token && token !== '') { - api.authenticate(token); + api.authenticate({token, owner, repo}); } }, []); return ( @@ -55,13 +58,28 @@ export const LoginCard = () => { onChange={e => setToken(e.target.value)} /> - + + setOwner(e.target.value)} + /> + + + setRepo(e.target.value)} + /> +