1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| import { createTemplateAction } from '@backstage/plugin-scaffolder-node'; import myCache from '../../../../packages/global/node-cache';
export function createExampleAction() { return createTemplateAction<{ Harbor_project_name: string; GitLab_Username: string; }>({ id: 'acme:harbor:create', description: 'create harbor project', schema: { input: { type: 'object', required: ['Harbor_project_name', 'GitLab_Username'], properties: { Harbor_project_name: { title: 'Harbor project name', description: 'Harbor project name', type: 'string', }, GitLab_Username: { title: 'GitLab Username', description: 'GitLab Username', type: 'string', }, }, }, }, async handler(ctx) { const harbor_project_name = ctx.input.Harbor_project_name; const HARBOR_BASE_URL = 'https://harbor.xxxxxx.com.cn'; const admin_Token_Base64 = 'xxxxxxxxxxxxxxxxxx'; let projectExists = false;
const tenantId: string = 'xxxxxxxxxx'; const client_id = 'xxxxxxxxx'; const client_secret = 'xxxxxxxxxx'; const MICROSOFT_LOGIN_BASE_URL = `https://login.microsoftonline.com/${tenantId}/oauth2/v2.0/token`; const username = ctx.input.GitLab_Username.toLocaleLowerCase();
let refresh_Token: string | undefined = myCache.get(`${username}_refreshToken`); let id_token = "";
try { const response = await fetch( `${HARBOR_BASE_URL}/api/v2.0/projects/${harbor_project_name}`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': `Basic ${admin_Token_Base64}` } } ); if (response.status === 200) { projectExists = true; } } catch (error) { ctx.logger.error('Error get Harbor Project:', error); throw error; }
if (!projectExists) { try { const params = new URLSearchParams({ client_id: client_id, client_secret: client_secret, redirect_uri: 'https://harbor.xxxxxx.com.cn/c/oidc/callback', grant_type: 'refresh_token', scope: 'openid profile offline_access' });
if (refresh_Token) { params.append('refresh_token', refresh_Token); }
const response_microsoft = await fetch(`${MICROSOFT_LOGIN_BASE_URL}`, { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', }, body: params.toString() });
if (!response_microsoft.ok) { throw new Error(`Fail to connect to Microsoft login site, Status: ${response_microsoft.status}`); }
const data = await response_microsoft.json(); id_token = data.id_token; refresh_Token = data.refresh_token; myCache.set(`${username}_refreshToken`, refresh_Token, 0); } catch (error) { ctx.logger.error('Error connect to Microsoft login site:', error); throw error; }
try { const response_csrf = await fetch(`${HARBOR_BASE_URL}/c/login`, { method: 'GET', }); const X_Harbor_Csrf_Token = response_csrf.headers.get('X-Harbor-Csrf-Token');
const body = { project_name: ctx.input.Harbor_project_name, public: false };
const response_harbor = await fetch(`${HARBOR_BASE_URL}/api/v2.0/projects`, { method: 'POST', headers: { 'Authorization': `Bearer ${id_token}`, 'Content-Type': 'application/json', 'X-Harbor-Csrf-Token': `${X_Harbor_Csrf_Token}` }, body: JSON.stringify(body) });
if (response_harbor.status !== 201) { throw new Error('Fail to create Harbor project.'); } } catch (error) { ctx.logger.error('Error to create Harbor project.:', error); throw error; } } }, }); }
|