UNPKG

bitbucket-env-manager

Version:

Deploys Bitbucket Environment

214 lines (196 loc) 5.35 kB
const rp = require('request-promise'); const login = ({ client, secret }) => { if (!client || !secret) { throw new Error('Missing params!'); } const basicAuth = Buffer.from(`${client}:${secret}`).toString('base64'); return rp({ url: 'https://bitbucket.org/site/oauth2/access_token', method: 'POST', body: 'grant_type=client_credentials', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json; charset=UTF-8', Authorization: `Basic ${basicAuth}`, }, }); }; const exchangeToken = ({ client, secret, refresh_token }) => { if (!client || !secret || !refresh_token) { throw new Error('Missing params!'); } const basicAuth = Buffer.from(`${client}:${secret}`).toString('base64'); return rp({ url: 'https://bitbucket.org/site/oauth2/access_token', method: 'POST', body: `grant_type=refresh_token&refresh_token=${refresh_token}`, headers: { 'Content-Type': 'application/x-www-form-urlencoded', Accept: 'application/json; charset=UTF-8', Authorization: `Basic ${basicAuth}`, }, }); }; const get_envs = ({ access_token, workspace, repo_slug }) => { if (!access_token || !workspace || !repo_slug) { throw new Error('Missing params!'); } return rp({ url: `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/environments/`, method: 'GET', headers: { Accept: 'application/json', Authorization: `Bearer ${access_token}`, }, qs: { page: 1, pagelen: 100, }, }); }; const get_vars = ({ access_token, workspace, repo_slug, environment_uuid, page = 1, pagelen = 300, }) => { if (!access_token || !workspace || !repo_slug || !environment_uuid) { throw new Error('Missing params!'); } return rp({ url: `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/deployments_config/environments/${environment_uuid}/variables`, method: 'GET', headers: { Accept: 'application/json', Authorization: `Bearer ${access_token}`, }, qs: { page: page, pagelen: pagelen, }, }); }; const get_env = ({ environment_uuid, access_token, workspace, repo_slug }) => { if (!environment_uuid || !access_token || !workspace || !repo_slug) { throw new Error('Missing params!'); } return rp({ url: `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/environments/${environment_uuid}`, method: 'GET', headers: { Accept: 'application/json', Authorization: `Bearer ${access_token}`, }, }); }; const delete_env = ({ environment_uuid, access_token, workspace, repo_slug }) => { if (!environment_uuid || !access_token || !workspace || !repo_slug) { throw new Error('Missing params!'); } return rp({ url: `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/environments/${environment_uuid}`, method: 'DELETE', headers: { Authorization: `Bearer ${access_token}`, }, }); }; const create_env = ({ env_name, env_rank, // 0,1,2 access_token, workspace, repo_slug, env_config, }) => { if ( !env_name || typeof env_rank === null || typeof env_rank === undefined || !access_token || !workspace || !repo_slug || isNaN(env_rank) || !env_config ) { throw new Error('Missing params!'); } const rank = Math.max(Math.min(env_rank, 2), 0); const rankMap = { 0: 'Test', 1: 'Staging', 2: 'Production', }; return rp({ url: `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/environments/`, method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${access_token}`, }, body: JSON.stringify({ name: env_name, deployment_gate_enabled: false, rank, hidden: rank === 2, type: 'deployment_environment', slug: env_name.toLowerCase(), environment_type: { type: 'deployment_environment_type', name: rankMap[rank], rank: rank, }, restrictions: { type: 'deployment_restrictions_configuration', admin_only: !!env_config.admin_only, }, }), }); }; const create_var = ({ environment_uuid, access_token, workspace, repo_slug, var_key, var_value, var_secured, }) => { if (!environment_uuid || !access_token || !workspace || !repo_slug) { throw new Error('Missing params!'); } if (!var_key || !var_value) { throw new Error('Missing "var" params!'); } if (typeof var_secured === undefined || typeof var_secured === null) { throw new Error('Missing "var_secured" boolean param!'); } return rp({ url: `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/deployments_config/environments/${environment_uuid}/variables`, method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', Authorization: `Bearer ${access_token}`, }, body: JSON.stringify({ type: 'pipeline_variable', key: var_key, value: var_value, secured: !!var_secured, }), }); }; module.exports = { login, exchangeToken, get_envs, get_vars, get_env, delete_env, create_env, create_var, };