Skip to content

Commit

Permalink
Rename and get it all working w/ Probot 7
Browse files Browse the repository at this point in the history
  • Loading branch information
toolmantim committed Jun 30, 2018
1 parent ceb5649 commit 25f88dc
Show file tree
Hide file tree
Showing 18 changed files with 389 additions and 352 deletions.
49 changes: 12 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,31 @@
<h1 align="center">
<img src="design/logo.svg" alt="Draftah Logo" width="350" />
<img src="design/logo.svg" alt="Release Drafter Logo" width="400" />
</h1>

<p align="center">Drafts your next release notes as pull requests are merged into master. Built with <a href="https://github.com/probot/probot">Probot</a>.</p>

---

<p align="center"><a href="https://github.com/apps/draftah"><img src="design/install-button.svg" alt="Install the GitHub App" /></a></p>
<p align="center"><a href="https://github.com/apps/release-drafter"><img src="design/install-button.svg" alt="Install the GitHub App" /></a></p>

---

## Usage

Firstly, you’ll need to install the [Draftah GitHub App](https://github.com/apps/draftah). This listens out for any releases, or any changes to the configuration.
Firstly, you’ll need to install the [Release Drafter GitHub App](https://github.com/apps/release-drafter). This listens out for any releases, or any changes to the configuration.

Then, add a `.github/draftah.yml` configuration file to the GitHub repository where you publish new releases to.
Then, add a `.github/release-drafter.yml` configuration file to the GitHub repository where you publish new releases to.

For example, given the following `.github/draftah.yml` file:
For example, given the following `.github/release-drafter.yml` file:

```yml
tag: vx.x.x
title: vx.x.x (✏️ Code Name)
body: |
template: |
## What's Changed
$CHANGES
## Upgrading
```diff
- test-project#$PREVIOUS_TAG:
+ test-project#vx.x.x:
```
```

As pull requests get merged to master, a draft release is kept up to date, such as:

```markdown
## What's Changed

* 🚜 Add a new Widgets API #2 (@toolmantim)
* 👽 Integrate alien technology #3 (@toolmantim)
* 🐄 More cowbell #4 (@toolmantim)

## Upgrading

```diff
- test-project#v1.2.0:
+ test-project#vx.x.x:
```
As pull requests get merged to master, a draft release is kept up to date, ready for you to publish via GitHub when you're ready:

## Template variables

Expand All @@ -61,16 +38,14 @@ You can use any of the following variables in your release template, and they'll

## Configuration options

You can configure Draftah using the following key in your `.github/draftah.yml` file:
You can configure Release Drafter using the following key in your `.github/release-drafter.yml` file:

|Key|Required|Description|
|-|-|-|
|`tag`|Required|A list of paths and patterns to update when a new release is published.|
|`title`|Required|The path to the file to update.|
|`body`|Required|The template body for the release. Use [variables](#template-variables) to insert the values from the release.|
|`branches`|Optional|The branches to listen for configuration updates to `.github/draftah.yml` and for merge commits. Useful if you want to test the app on a pull request branch. Default is the repository’s default branch.|
|`template`|Required|The template for the body of the draft release. Use [template variables](#template-variables) to insert values.|
|`branches`|Optional|The branches to listen for configuration updates to `.github/release-drafter.yml` and for merge commits. Useful if you want to test the app on a pull request branch. Default is the repository’s default branch.|

Draftah also supports [Probot Config](https://github.com/probot/probot-config), if you want to store your configuration files in a central repository.
Release Drafter also supports [Probot Config](https://github.com/probot/probot-config), if you want to store your configuration files in a central repository.

## Developing

Expand Down Expand Up @@ -102,4 +77,4 @@ If you need help or have a question, let me know via a GitHub issue.

## Deployment

If you want to deploy your own copy of Draftah, follow the [Probot Deployment Guide](https://probot.github.io/docs/deployment/).
If you want to deploy your own copy of Release Drafter, follow the [Probot Deployment Guide](https://probot.github.io/docs/deployment/).
Binary file modified design/github-app-logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion design/install-button.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified design/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified design/logo.sketch
Binary file not shown.
6 changes: 3 additions & 3 deletions design/logo.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified design/logo@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 39 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,42 @@
const run = require('./lib/run')
const configName = 'draftah.yml'
const getConfig = require('probot-config')
const { isRelevantBranch } = require('./lib/branch')
const { findReleases, generateBody } = require('./lib/releases')
const { findMergedPullRequests } = require('./lib/pull-requests')

module.exports = robot => {
robot.on('push', async context => {
await run({ robot, context, configName })
const configName = 'release-drafter.yml'

module.exports = app => {
app.on('push', async context => {
const config = await getConfig(context, configName) || {}
const { body: template } = config
const branch = context.payload.ref.replace(/^refs\/heads\//, '')

if (!template) {
app.log(`No valid config found`)
return
}

if (!isRelevantBranch({ branch, app, context, config })) {
return
}

const { draftRelease, lastRelease } = await findReleases({ context })
const mergedPullRequests = await findMergedPullRequests({ app, context, branch, lastRelease })

const body = generateBody({ template, lastRelease, mergedPullRequests })

if (!draftRelease) {
app.log(`Creating new draft release`)
await context.github.repos.createRelease(context.repo({
body: body,
draft: true
}))
} else {
app.log(`Updating existing draft release`, { draftRelease })
await context.github.repos.editRelease(context.repo({
release_id: draftRelease.id,
body: body
}))
}
})
}
7 changes: 7 additions & 0 deletions lib/base64.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports.encodeContent = (content) => {
return Buffer.from(content).toString('base64')
}

module.exports.decodeContent = (content) => {
return Buffer.from(content, 'base64').toString()
}
8 changes: 8 additions & 0 deletions lib/branch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports.isRelevantBranch = ({ app, context, branch, config }) => {
const validBranches = config.branches || [context.payload.repository.default_branch]
const relevant = validBranches.indexOf(branch) !== -1
if (!relevant) {
app.log(`Ignoring push. ${branch} is not one of: ${validBranches.join(', ')}`)
}
return relevant
}
14 changes: 10 additions & 4 deletions lib/pull-requests.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
module.exports.mergedPullRequests = async ({ robot, context, branch, previousTag }) => {
robot.log({ previousTag })
// Searches the commits for merged pull requests, and returns the pull requests
module.exports.findMergedPullRequests = async ({ app, context, branch, lastRelease }) => {
if (!lastRelease) {
return []
}

app.log({ lastRelease })

// Only supports up to 250 commits
const commits = await context.github.repos.compareCommits(context.repo({
base: previousTag,
base: lastRelease.tag_name,
head: branch
})).then(res => res.data.commits)

robot.log({ commits: commits.map(c => c.commit.message) })
app.log({ commits: commits.map(c => c.commit.message) })

let mergedPrNumbers = []

Expand Down
37 changes: 37 additions & 0 deletions lib/releases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const compareVersions = require('compare-versions')

module.exports.findReleases = async ({ context }) => {
let releases = await context.github.paginate(
context.github.repos.getReleases(context.repo()),
res => res.data
)

const sortedPublishedReleases = releases
.filter(r => !r.draft)
.sort((r1, r2) => compareVersions(r1.tag_name, r2.tag_name))

return {
draftRelease: releases.find((r) => r.draft),
lastRelease: sortedPublishedReleases[sortedPublishedReleases.length - 1]
}
}

module.exports.generateBody = ({ template, lastRelease, mergedPullRequests }) => {
let body = template

if (lastRelease) {
body = body.replace('$PREVIOUS_TAG', lastRelease.tag_name)
}

if (lastRelease) {
if (mergedPullRequests.length === 0) {
body = body.replace('$CHANGES', '* No changes')
} else {
body = body.replace('$CHANGES', mergedPullRequests.map(pr => (
`* ${pr.title} #${pr.number} (@${pr.user.login})`
)).join('\n'))
}
}

return body
}
70 changes: 0 additions & 70 deletions lib/run.js

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "draftah",
"name": "release-drafter",
"version": "1.0.0",
"description": "A GitHub app that bumps version numbers in readmes",
"author": "Tim Lucas <t@toolmantim.com> (https://github.com/toolmantim)",
"license": "ISC",
"repository": "https://github.com/toolmantim/draftah.git",
"repository": "https://github.com/toolmantim/release-drafter.git",
"scripts": {
"dev": "nodemon --exec \"npm start\"",
"start": "probot run ./index.js",
Expand All @@ -16,7 +16,7 @@
},
"dependencies": {
"compare-versions": "3.3.0",
"probot": "6.2.0",
"probot": "7.0.0",
"probot-config": "0.1.0",
"request": "2.87.0"
},
Expand Down

0 comments on commit 25f88dc

Please sign in to comment.