Add owner and repo options to authenticate method

This commit is contained in:
Nikita Nek Dudnik
2020-05-04 13:46:15 +02:00
parent 00efbfe87e
commit cf963ceae8
2 changed files with 38 additions and 13 deletions
+16 -9
View File
@@ -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<CircleCIOptions> = {
// Required for all requests
@@ -25,20 +32,20 @@ const options: Partial<CircleCIOptions> = {
// 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) {
@@ -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)}
/>
</ListItem>
<ListItem>
<TextField
name="circleci-owner"
label="Owner"
value={owner}
onChange={e => setOwner(e.target.value)}
/>
</ListItem>
<ListItem>
<TextField
name="circleci-repo"
label="Repo"
value={repo}
onChange={e => setRepo(e.target.value)}
/>
</ListItem>
<ListItem>
<Button
data-testid="github-auth-button"
variant="outlined"
color="primary"
onClick={() => api.authenticate(token)}
onClick={() => api.authenticate({token, owner, repo})}
>
Authenticate
</Button>