import LaunchDarkly , { LDUser } from "launchdarkly-node-server-sdk" ; import { getLogger } from "./logger" ; import envVars from "./env" ; import crypto from "crypto" ; import { LoggerWithTarget } from "probot/lib/wrap-logger" ; const logger = getLogger ( "feature-flags" ) ; const launchdarklyClient = LaunchDarkly . init ( envVars . LAUNCHDARKLY_KEY || "" , { offline : ! envVars . LAUNCHDARKLY_KEY , logger } ) ; export enum BooleanFlags { MAINTENANCE_MODE = "maintenance-mode" , SIMPLER_PROCESSOR = "simpler-processor" , USE_NEW_GITHUB_CLIENT__FOR_PR = "git-hub-client-for-pullrequests" , SUPPORT_BRANCH_AND_MERGE_WORKFLOWS_FOR_DEPLOYMENTS = "support-branch-and-merge-workflows-for-deployments" , TRACE_LOGGING = "trace-logging" , SUPPORT_BRANCH_AND_MERGE_WORKFLOWS_FOR_BUILDS = "support-branch-and-merge-workflows-for-builds" , USE_SQS_FOR_BRANCH = "use-sqs-for-branch" , ASSOCIATE_PR_TO_ISSUES_IN_BODY = "associate-pr-to-issues-in-body" , VERBOSE_LOGGING = "verbose-logging" , USE_NEW_GITHUB_CLIENT_FOR_BRANCHES = "use-new-github-client-for-branches" } export enum StringFlags { BLOCKED_INSTALLATIONS = "blocked-installations" } const createLaunchdarklyUser = ( jiraHost ? ) : LDUser => { if ( ! jiraHost ) { return { key : "global" } ; } const hash = crypto . createHash ( "sha1" ) ; hash . update ( jiraHost ) ; return { key : hash . digest ( "hex" ) } ; } ; const getLaunchDarklyValue = async ( flag , defaultValue , jiraHost ? ) : Promise < boolean | string > => { try { await launchdarklyClient . waitForInitialization ( ) ; const user = createLaunchdarklyUser ( jiraHost ) ; return launchdarklyClient . variation ( flag , user , defaultValue ) ; } catch ( err ) { logger . error ( { flag , err } , "Error resolving value for feature flag" ) ; return defaultValue ; } } ; export const booleanFlag = async ( flag , defaultValue , jiraHost ? ) : Promise < boolean > => Boolean ( await getLaunchDarklyValue ( flag , defaultValue , jiraHost ) ) ; export const stringFlag = async ( flag , defaultValue , jiraHost ? ) : Promise < string > => String ( await getLaunchDarklyValue ( flag , defaultValue , jiraHost ) ) ; export const isBlocked = async ( installationId , logger ) : Promise < boolean > => { try { const blockedInstallationsString = await stringFlag ( StringFlags . BLOCKED_INSTALLATIONS , "[]" ) ; const blockedInstallations = JSON . parse ( blockedInstallationsString ) ; return blockedInstallations . includes ( installationId ) ; } catch ( e ) { logger . error ( { err : e , installationId } , "Cannot define if isBlocked" ) ; return false ; } } ;