-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
253 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,7 +89,7 @@ out | |
|
||
# Nuxt.js build / generate output | ||
.nuxt | ||
dist | ||
build | ||
|
||
# Gatsby files | ||
.cache/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: 'Unleash Feature Flags' | ||
description: 'Lets you use the getunleash.io feature flags management solution in GitHub Actions' | ||
inputs: | ||
app-name: | ||
description: 'The application name for the GitHub Action as you want it to appear in Unleash metrics' | ||
type: 'string' | ||
required: true | ||
api-key: | ||
description: 'The frontend API token to use with Unleash' | ||
type: 'string' | ||
required: true | ||
url: | ||
description: 'Url to Unleash proxy or frontend api' | ||
type: 'string' | ||
required: true | ||
environment: | ||
description: 'The environment for which to evaluate the feature flags' | ||
type: 'string' | ||
required: false | ||
default: 'default' | ||
is-enabled: | ||
description: 'Newline-separated list of feature flag names to evaluate' | ||
type: 'string' | ||
required: false | ||
get-variant: | ||
description: 'Newline-separated list of feature flag names to get variants for' | ||
type: 'string' | ||
required: false | ||
context: | ||
description: 'Multiline list of key=value pairs that will be string split on =' | ||
type: 'string' | ||
required: false | ||
outputs: | ||
the-value: | ||
description: 'What we found in the input' | ||
runs: | ||
using: 'node20' | ||
main: 'dist/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { createUnleashAction } from "./unleash-action"; | ||
import { getInput, getMultilineInput, setOutput } from '@actions/core'; | ||
|
||
const appName = getInput('app-name'); | ||
const url = getInput('url'); | ||
const clientKey = getInput('api-key'); | ||
const environment = getInput('environment'); | ||
|
||
const context: Record<string, string> = {}; | ||
const contextLines = getMultilineInput('context'); | ||
contextLines?.forEach(l => { | ||
let keyVal = l.split('='); | ||
context[keyVal[0]] = keyVal[1]; | ||
}); | ||
|
||
const features = getMultilineInput('is-enabled'); | ||
const variants = getMultilineInput('get-variant'); | ||
|
||
createUnleashAction({ | ||
url: url, | ||
clientKey: clientKey, | ||
appName: appName, | ||
environment: environment, | ||
context: context, | ||
features: features, | ||
variants: variants, | ||
setResult: setOutput | ||
}).then(() => { | ||
console.log("Done!"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { UnleashClient } from "unleash-proxy-client"; | ||
import Metrics from "unleash-proxy-client/build/metrics"; | ||
import fetch from "node-fetch"; | ||
|
||
interface IUnleashActionOptions { | ||
url: string; | ||
clientKey: string; | ||
appName: string; | ||
environment: string; | ||
context: Record<string, string>; | ||
features?: string[]; | ||
variants?: string[]; | ||
setResult: (name: string, value: any) => void; | ||
} | ||
|
||
export const createUnleashAction = async ( | ||
options: IUnleashActionOptions | ||
): Promise<void> => { | ||
const action = new UnleashAction(options); | ||
await action.run(); | ||
await action.end(); | ||
}; | ||
|
||
export class UnleashAction { | ||
private unleash: UnleashClient; | ||
private metrics: Metrics; | ||
private features: string[]; | ||
private variants: string[]; | ||
private setResult: (name: string, value: any) => void; | ||
|
||
constructor(options: IUnleashActionOptions) { | ||
this.unleash = this.createClient(options); | ||
this.unleash.on("ready", () => { | ||
console.log("Ready!"); | ||
}); | ||
|
||
this.metrics = this.createMetrics(options); | ||
|
||
this.features = options.features || []; | ||
this.variants = options.variants || []; | ||
this.setResult = options.setResult; | ||
} | ||
|
||
async run(): Promise<void> { | ||
console.log("starting."); | ||
await this.unleash.start(); | ||
|
||
console.log("Checking features."); | ||
await this.checkFeatures(); | ||
|
||
console.log("Checking variants."); | ||
await this.checkVariants(); | ||
} | ||
|
||
async end(): Promise<void> { | ||
console.log("Sending metrics."); | ||
await this.metrics.sendMetrics(); | ||
|
||
console.log("Stopping."); | ||
await this.unleash.stop(); | ||
} | ||
|
||
private createClient(options: IUnleashActionOptions): UnleashClient { | ||
return new UnleashClient({ | ||
appName: options.appName, | ||
url: options.url, | ||
clientKey: options.clientKey, | ||
environment: options.environment, | ||
refreshInterval: 0, | ||
metricsInterval: 0, | ||
disableMetrics: true, | ||
}); | ||
} | ||
|
||
private createMetrics(options: IUnleashActionOptions): Metrics { | ||
return new Metrics({ | ||
fetch: fetch, | ||
headerName: "Authorization", | ||
onError: (e) => {}, | ||
appName: options.appName, | ||
url: options.url, | ||
clientKey: options.clientKey, | ||
disableMetrics: false, | ||
metricsInterval: 0, | ||
}); | ||
} | ||
|
||
private async checkFeatures(): Promise<void> { | ||
this.features.forEach((featureName) => { | ||
const isEnabled = this.unleash.isEnabled(featureName); | ||
this.metrics.count(featureName, isEnabled); | ||
this.setResult(featureName, isEnabled); | ||
}); | ||
} | ||
|
||
private async checkVariants(): Promise<void> { | ||
this.variants.forEach((featureName) => { | ||
const variant = this.unleash.getVariant(featureName); | ||
if (variant.name) { | ||
this.metrics.countVariant(featureName, variant.name); | ||
} | ||
this.metrics.count(featureName, variant.enabled); | ||
this.setResult(featureName, variant.enabled); | ||
this.setResult(`${featureName}_variant`, variant.payload?.value); | ||
}); | ||
} | ||
} |
Oops, something went wrong.