Skip to content

Feature: API for generating codeowners files #13

Description

@ThisIsMissEm

Hi!

I've recently been working on a project where I programmatically generate the codeowners file based on some configuration. So far, I've written a fairly simple class that gives an API to describe a codeowners file (rules, comments, and newlines), and I'm wondering if you'd like this contributed back?

I'm thinking more along the lines of parsing a CODEOWNERS file to a simple AST (see below), and then set of methods to work with that AST and a means to convert the AST to a CODEOWNERS file.

Example AST:

{
  type: 'rule' | 'comment' | 'newline'
  // For type == comment
  message?: string
  // For type == rule
  pattern: string
  owners: string[]
}

The Codeowners function would then change to:

// If options.read == true, then call Codeowners.prototype.readSync() followed by Codeowners.prototype.parse()
// options.read will default to true, as this closest resembles the existing behaviour
new Codeowners(currentPath, fileName = 'CODEOWNERS', options?: { read: true })
Codeowners.prototype.getOwner(filepath: string);

Codeowners.prototype.parse() // called by constructor

Codeowners.prototype.addComment(message?: string)
Codeowners.prototype.addRule(pattern: string, owners: string[])
Codeowners.prototype.addNewline()

Codeowners.prototype.toString() // output the current AST as a CODEOWNERS file

async Codeowners.prototype.read()
async Codeowners.prototype.write()

Codeowners.prototype.readSync()
Codeowners.prototype.writeSync()

Thoughts? This would allow programmatic management of CODEOWNERS files, we could also optionally do a breaking API change of:

new Codeowners(filepath: string, options?: { read: true });

Codeowners.discover(currentPath: string, filename = 'CODEOWNERS')

Such that existing code using the API would be:

- const owners = new Codeowners(workingDir);
+ const ownersFile = Codeowners.discover(workingDir);
+ const owners = new Codeowners(ownersFile, options?: { read: true });

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions