Last active
February 15, 2019 23:34
-
-
Save mlms13/82bed7552a7dcfe771009ff2c013ed8e to your computer and use it in GitHub Desktop.
Categorize Git Commits
This file contains hidden or 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
module Git = { | |
type commitish = Sha(string); | |
type commit = { | |
title: string, | |
descriptiong: string, | |
author: string, | |
date: Date.t | |
}; | |
let getCommitsForRange: (commitish, commitish) => list(commit); | |
}; | |
module Category = { | |
type t = | |
| Feature | |
| Bugfix | |
| Performance | |
| Design | |
| Docs; | |
}; | |
module Rule = { | |
type pattern = | |
| TitleStartsWith(string) | |
| TitleEndsWith(string) | |
| TitleContains(string); | |
type t = | |
| Pattern(pattern) | |
| And(list(t)) | |
| Or(list(t)); | |
let evaluate: (string, t) => bool; | |
}; | |
module CategoryRule = { | |
type t = { | |
category: Category.t, | |
rule: Rule.t, | |
}; | |
let make = (category, rule) => {category, rule}; | |
let bugfix = make(Category.Bugfix); | |
let perf = make(Category.Performance); | |
let evaulate = (txt, {category, rule}) => | |
Rule.evaluate(txt, rule) ? Some(category) : None; | |
}; | |
module Changelog = { | |
type t = Map.t(Category.t, list(Git.commit)); | |
let fromCommits: ((Git.commit => option(Category.t)), list(Git.commit)) => t; | |
}; | |
let categorize = (rules, commit) => | |
List.foldLeft( | |
(category, rule) => | |
switch (category) { | |
| Some(cat) => Some(cat) | |
| None => CategoryRule.evaluate(commit, rule) | |
} | |
None, | |
rules, | |
); | |
/******************************************************************************* | |
* Example usage | |
******************************************************************************/ | |
let bugRule = Rule.(or([ | |
pattern(startsWith(":bug:")), | |
pattern(cotains("fixes #")) | |
]); | |
let perfRule = Rule.(pattern(startsWith(":rocket:"))); | |
let myRules = [bugRule, perfRule]; | |
Git.(getCommitsForRange(sha("abcdef"), sha("012345"))) | |
|> Changelog.fromCommits(categorize(myRules)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment