npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fountainhead/branch-officer

v2.0.0

Published

Automates the deployment of GitHub Pull Requests as Preview Environments using Helm and Helmfile

Downloads

2

Readme

branch-officer

Automates the deployment of GitHub Pull Requests as Preview Environments using Helm and Helmfile.

CircleCI

Installation

npm install -g @fountainhead/branch-officer

Basic Usage

Branch Officer typically runs as part of your Continuous Integeration/Deployment workflow (although it can be run through other means), so it expects a GITHUB_TOKEN environment variable. Once this is set, you can invoke it like so:

branch-officer \
  --file $(pwd)/helmfile.d/preview-environments.yaml \
  --owner KontosoCorporation \
  --repo WidgetWarehouse

In this example, Branch Officer will enumerate all currently open on the KontosoCorporation/WidgetWarehouse repository, compare the releases entry in the Helmfile preview-environments.yaml, and display a table of Release entries that will be created, modified, deleted or left un-modified.

If you are satisfied with the proposed changes, you may add the --apply flag to modify the supplied Helmfile accordingly:

branch-officer \
  --file $(pwd)/helmfile.d/preview-environments.yaml \
  --owner KontosoCorporation \
  --repo WidgetWarehouse \
  --apply

The changes to your Helmfile may then be committed to version control and applied to your cluster using helmfile sync.

Advanced Usage

Pull Request Metadata

By default, Branch Officer will create a Release entry with the following structure:

name: {{ name of the PR branch }}
chart: charts/{{ owner name }}/{{ repo name }}
values:
  image:
    tag: {{ SHA of the PR's most recent commit }}

It is possible to modify the values of a Release on a per-release basis, by specifying 'metadata' in the Pull Request. This is encoded as YAML-formatted front-matter in the body of the Pull Request, which is then deep-merged with the default structure.

For example, if the body of your Pull Request looks like this:

---
apiReplicas: 2
uiReplicas: 1
---
This patch addresses the race condition caused by multiple API server instances
competing over a shared resource. To demonstrate that this works, I've set the
'apiReplicas' value in the Helm Release to '2'.

Then the Values will be merged into the Helmfile like so:

name: fix-race-condition
chart: charts/KontosoCorporation/WidgetWarehouse
values:
  apiReplicas: 2
  uiReplicas: 1
  image:
    tag: {{ SHA of the PR's most recent commit }}

In order to create more exotic Release entries, including modification of properties outside of release.values, please refer to the next section.

Custom Pull Request > Release Mapper

It is also possible to create a JavaScript function that allows full control over the Release entry that will be created in the Helmfile. For example, use the SHA of the PR's most recent commit to update multiple images, create a mapper like so:

// mapPrToRelease.js

module.exports = ({branch, parsedMetadata, sha}) => ({
  name: branch,
  chart: 'charts/kontoso',
  values: [{
    api: {
      image: {
      name: 'quay.io/kontoso/widget-warehouse-api'
      tag: `sha-${sha}`
    },
    ui: {
      image: {
      name: 'quay.io/kontoso/widget-warehouse-ui'
      tag: `sha-${sha}`
    }
  }]
})

Then, use the --mapper argument to specify a path to the Mapper file:

branch-officer \
  --file $(pwd)/helmfile.d/preview-environments.yaml \
  --mapper $(pwd)/mapPrToRelease.js \
  --owner KontosoCorporation \
  --repo WidgetWarehouse \
  --apply

The function exported by your Mapper file will be called with an object containing the following properties:

  • number (number)- The Pull Request number. For example, 123.
  • title (string) - The Title of the Pull Request. For example, 'Fix Race Condition'.
  • body (string) - The Body of the Pull Request.
  • sha (string) - The SHA of the most recent commit on the Pull Request.
  • branch (string) - The name of the branch the Pull Request was made for.
  • parsedMetadata (object) - Any YAML-formatted front matter from the body of the Pull Request, parsed as an Object. If no metadata was supplied or parsing failed, this defaults to an empty object ({}).
  • buildSucceeded (boolean) - Whether all reported 'build statuses' on the PR have passed. This is always true, as the Mapper function will not be called for failing PRs.

Required Status Checks

By default, Branch Officer requires that all applicable Status Checks pass in order for a Release to be created/updated. If a PR is currently failing to be built, it will be ignored.

It may be desirable to only require certain status checks to be considered. This is possible by supplying the --requiredStatusChecks argument. This argument may be passed multiple times to specify multiple required statuses. For example, to allow a release to be created/updated for a PR provided it passes the status checks named ci/circleci: test_ui and ci/circleci: test_api, use the following invocation:

branch-officer \
  --file $(pwd)/helmfile.d/preview-environments.yaml \
  --owner KontosoCorporation \
  --repo WidgetWarehouse \
  --requiredStatusChecks "ci/circleci: test_ui" \
  --requiredStatusChecks "ci/circleci: test_api" \
  --apply