feat: destroyed everything

Co-authored-by: Nikita Dudnik <nikdudnik@gmail.com>
This commit is contained in:
Ivan Shmidt
2020-05-07 11:28:49 +02:00
parent a24ea32746
commit 5dda050152
2 changed files with 72 additions and 129 deletions
+24 -73
View File
@@ -14,94 +14,45 @@
* limitations under the License.
*/
import { CircleCI, CircleCIOptions } from 'circleci-api';
import {
CircleCIOptions,
getMe,
getBuildSummaries,
getFullBuild,
postBuildActions,
BuildAction,
} from 'circleci-api';
import { ApiRef } from '@backstage/core';
const defaultOptions: Partial<CircleCIOptions> = {
circleHost: '/circleci/api',
vcs: {},
};
export const circleCIApiRef = new ApiRef<CircleCIApi>({
id: 'plugin.circleci.service',
description: 'Used by the CircleCI plugin to make requests',
});
export class CircleCIApi {
private token: string = '';
options: Partial<CircleCIOptions>;
authed: boolean = false;
constructor(options?: Partial<CircleCIOptions>) {
this.options = Object.assign(Object.create(null), defaultOptions, options);
apiUrl: string;
constructor(apiUrl: string = '/circleci/api') {
this.apiUrl = apiUrl;
}
setToken(token: string) {
this.token = token;
this.persistToken();
async retry(buildNumber: number, options: CircleCIOptions) {
return postBuildActions(
options.token,
buildNumber,
BuildAction.RETRY,
options,
);
}
setVCSOptions(vcs: CircleCIOptions['vcs']) {
this.options.vcs = vcs;
this.persistVCSOptions();
async getBuilds(options: CircleCIOptions) {
return getBuildSummaries(options.token, { vcs: {}, ...options });
}
async persistVCSOptions() {
const key = circleCIApiRef.id;
sessionStorage.setItem(key + '_options', JSON.stringify(this.options.vcs));
async getUser(options: CircleCIOptions) {
return getMe(options.token, options);
}
async restorePersistedSettings() {
if (this.authed) return Promise.resolve();
const key = circleCIApiRef.id;
const persistedToken = sessionStorage.getItem(key);
let persistedVCSOptions: {} | undefined;
try {
persistedVCSOptions = JSON.parse(
sessionStorage.getItem(key + '_options') as string,
);
} catch (e) {}
if (persistedToken && persistedVCSOptions) {
this.token = persistedToken;
this.options.vcs = persistedVCSOptions;
return Promise.resolve();
}
return Promise.reject();
}
async persistToken() {
if (this.authed) return;
const key = circleCIApiRef.id;
sessionStorage.setItem(key, this.token);
}
async validateToken() {
if (!this.token || this.token === '') {
return Promise.reject('Wrong token');
}
// TODO: switch towards using personal token
await this.api.builds();
this.authed = true;
return Promise.resolve();
}
private get api() {
return new CircleCI({ ...this.options, token: this.token });
}
async retry(buildId: string) {
return this.api.retry(Number(buildId));
}
async getBuilds() {
return this.api.builds();
}
async getUser() {
return this.api.me();
}
async getBuild(buildId: string) {
return this.api.build(parseInt(buildId, 10));
async getBuild(buildNumber: number, options: CircleCIOptions) {
return getFullBuild(options.token, buildNumber, options);
}
}
@@ -7,7 +7,7 @@ import {
Content,
ContentHeader,
SupportButton,
// StatusOK,
StatusOK,
StatusFailed,
} from '@backstage/core';
import { Link as RouterLink } from 'react-router-dom';
@@ -17,7 +17,7 @@ export const SettingsPage = () => {
const api = useApi(circleCIApiRef);
const apiGitInfo = api.options.vcs;
const [authed, setAuthed] = React.useState(api.authed);
const [token, setToken] = React.useState('');
const [token, setToken] = React.useState(api.token);
React.useEffect(() => {
api
@@ -49,63 +49,55 @@ export const SettingsPage = () => {
<Grid container spacing={3}>
<Grid item xs={6}>
<InfoCard
title="Project Credentials"
subheader={
<div>
<StatusFailed />
</div>
title={
<>
Project Credentials{authed ? <StatusOK /> : <StatusFailed />}
</>
}
>
<List>
{authed ? (
<>Authenticated</>
) : (
<>
<ListItem>
<TextField
name="circleci-token"
type="password"
label="Token"
value={token}
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={async () => {
api.setVCSOptions({ owner, repo });
api.setToken(token);
api
.validateToken()
.then(() => setAuthed(true))
.catch(() => setAuthed(false));
}}
>
Save credentials
</Button>
</ListItem>
</>
)}
<ListItem>
<TextField
name="circleci-token"
type="password"
label="Token"
value={token}
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={async () => {
api.setVCSOptions({ owner, repo });
api.setToken(token);
api
.validateToken()
.then(() => setAuthed(true))
.catch(() => setAuthed(false));
}}
>
Save credentials
</Button>
</ListItem>
</List>
</InfoCard>
</Grid>