From 5dda050152181a5277f1b3a243d066a2b71a6012 Mon Sep 17 00:00:00 2001 From: Ivan Shmidt Date: Thu, 7 May 2020 11:28:49 +0200 Subject: [PATCH] feat: destroyed everything Co-authored-by: Nikita Dudnik --- plugins/circleci/src/api/index.ts | 97 ++++------------ .../src/pages/SettingsPage/SettingsPage.tsx | 104 ++++++++---------- 2 files changed, 72 insertions(+), 129 deletions(-) diff --git a/plugins/circleci/src/api/index.ts b/plugins/circleci/src/api/index.ts index b0126378f6..b132b6a208 100644 --- a/plugins/circleci/src/api/index.ts +++ b/plugins/circleci/src/api/index.ts @@ -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 = { - circleHost: '/circleci/api', - vcs: {}, -}; export const circleCIApiRef = new ApiRef({ id: 'plugin.circleci.service', description: 'Used by the CircleCI plugin to make requests', }); export class CircleCIApi { - private token: string = ''; - options: Partial; - - authed: boolean = false; - constructor(options?: Partial) { - 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); } } diff --git a/plugins/circleci/src/pages/SettingsPage/SettingsPage.tsx b/plugins/circleci/src/pages/SettingsPage/SettingsPage.tsx index 028b09d07f..0605409ad1 100644 --- a/plugins/circleci/src/pages/SettingsPage/SettingsPage.tsx +++ b/plugins/circleci/src/pages/SettingsPage/SettingsPage.tsx @@ -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 = () => { - - + title={ + <> + Project Credentials{authed ? : } + } > - {authed ? ( - <>Authenticated - ) : ( - <> - - setToken(e.target.value)} - /> - - - - setOwner(e.target.value)} - /> - - - setRepo(e.target.value)} - /> - - - - - - )} + + setToken(e.target.value)} + /> + + + setOwner(e.target.value)} + /> + + + setRepo(e.target.value)} + /> + + + +