Files
probot.github.io/api/5.0.0/context.js.html
2018-01-18 08:17:47 -06:00

181 lines
6.2 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>context.js - Documentation</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
<div class="navicon"></div>
</label>
<label for="nav-trigger" class="overlay"></label>
<nav>
<li class="nav-link nav-home-link"><a href="index.html">Home</a></li><li class="nav-heading">Classes</li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Context.html">Context</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Context.html#config">config</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Context.html#issue">issue</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Context.html#repo">repo</a></span></li><li class="nav-heading"><span class="nav-item-type type-class">C</span><span class="nav-item-name"><a href="Robot.html">Robot</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Robot.html#on">on</a></span></li><li class="nav-item"><span class="nav-item-type type-function">F</span><span class="nav-item-name"><a href="Robot.html#route">route</a></span></li><li class="nav-heading"><a href="global.html">Globals</a></li>
</nav>
<div id="main">
<h1 class="page-title">context.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>const path = require('path')
const yaml = require('js-yaml')
/**
* Helpers for extracting information from the webhook event, which can be
* passed to GitHub API calls.
*
* @property {github} github - An authenticated GitHub API client
* @property {payload} payload - The webhook event payload
* @property {logger} log - A logger
*/
class Context {
constructor (event, github, log) {
Object.assign(this, event)
this.github = github
this.log = log
}
/**
* Return the `owner` and `repo` params for making API requests against a
* repository.
*
* @param {object} [object] - Params to be merged with the repo params.
*
* @example
*
* const params = context.repo({path: '.github/stale.yml'})
* // Returns: {owner: 'username', repo: 'reponame', path: '.github/stale.yml'}
*
*/
repo (object) {
const repo = this.payload.repository
return Object.assign({
owner: repo.owner.login || repo.owner.name,
repo: repo.name
}, object)
}
/**
* Return the `owner`, `repo`, and `number` params for making API requests
* against an issue or pull request. The object passed in will be merged with
* the repo params.
*
* @example
*
* const params = context.issue({body: 'Hello World!'})
* // Returns: {owner: 'username', repo: 'reponame', number: 123, body: 'Hello World!'}
*
* @param {object} [object] - Params to be merged with the issue params.
*/
issue (object) {
const payload = this.payload
return Object.assign({
number: (payload.issue || payload.pull_request || payload).number
}, this.repo(), object)
}
/**
* Returns a boolean if the actor on the event was a bot.
* @type {boolean}
*/
get isBot () {
return this.payload.sender.type === 'Bot'
}
/**
* Reads the app configuration from the given YAML file in the `.github`
* directory of the repository.
*
* @example &lt;caption>Contents of &lt;code>.github/myapp.yml&lt;/code>.&lt;/caption>
*
* close: true
* comment: Check the specs on the rotary girder.
*
* @example &lt;caption>App that reads from &lt;code>.github/myapp.yml&lt;/code>.&lt;/caption>
*
* // Load config from .github/myapp.yml in the repository
* const config = await context.config('myapp.yml')
*
* if (config.close) {
* context.github.issues.comment(context.issue({body: config.comment}))
* context.github.issues.edit(context.issue({state: 'closed'}))
* }
*
* @example &lt;caption>Using a &lt;code>defaultConfig&lt;/code> object.&lt;/caption>
*
* // Load config from .github/myapp.yml in the repository and combine with default config
* const config = await context.config('myapp.yml', {comment: 'Make sure to check all the specs.'})
*
* if (config.close) {
* context.github.issues.comment(context.issue({body: config.comment}));
* context.github.issues.edit(context.issue({state: 'closed'}))
* }
*
* @param {string} fileName - Name of the YAML file in the `.github` directory
* @param {object} [defaultConfig] - An object of default config options
* @return {Promise&lt;Object>} - Configuration object read from the file
*/
async config (fileName, defaultConfig) {
const params = this.repo({path: path.posix.join('.github', fileName)})
try {
const res = await this.github.repos.getContent(params)
const config = yaml.safeLoad(Buffer.from(res.data.content, 'base64').toString()) || {}
return Object.assign({}, defaultConfig, config)
} catch (err) {
if (err.code === 404) {
if (defaultConfig) {
return defaultConfig
}
return null
} else {
throw err
}
}
}
}
module.exports = Context
</code></pre>
</article>
</section>
</div>
<br class="clear">
<footer>
Generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Thu Jan 18 2018 08:17:45 GMT-0600 (CST) using the Minami theme.
</footer>
<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>