Add templating support to welcome bot

This commit is contained in:
Pedro Pombeiro 2018-02-14 15:06:20 +01:00
parent 4f1a3be931
commit 4513976cb9
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
2 changed files with 22 additions and 2 deletions

View File

@ -96,7 +96,12 @@ The bot gets its settings from a per-repo file located at `.github/github-bot.ym
Examples of settings that can be configured:
- `github-team/slug`: Slug of the team that owns the respective repository
- `welcome-bot/message`: First time contributor welcome message
- `welcome-bot/message-template`: First time contributor welcome message template. Examples of template values allowed:
- `{user}`: Replaced by the PR submitter's user name
- `{repo-name}`: Replaced by the PR's target repository name
- `{pr-number}`: Replaced by the PR number
- `slack/notification/room`: Slack room used for notifications (e.g. `status-probot`)
- Repository project board settings:

View File

@ -26,6 +26,20 @@ module.exports = (robot) => {
})
}
function executeTemplate (templateString, templateVars) {
let s = templateString
for (const templateVar in templateVars) {
if (templateVars.hasOwnProperty(templateVar)) {
const value = templateVars[templateVar]
s = s.replace(`{${templateVar}}`, value)
}
}
return s
}
async function greetNewContributor (context, robot) {
const { github, payload } = context
const config = await getConfig(context, 'github-bot.yml', defaultConfig(robot, '.github/github-bot.yml'))
@ -51,7 +65,8 @@ async function greetNewContributor (context, robot) {
const userPullRequests = ghissuesPayload.data.filter(issue => issue.pull_request)
if (userPullRequests.length === 1) {
try {
const welcomeMessage = welcomeBotConfig.message
const welcomeMessage = executeTemplate(welcomeBotConfig['message-template'], { user: payload.pull_request.user.login, 'pr-number': prNumber, 'repo-name': repoName })
if (process.env.DRY_RUN) {
robot.log(`${botName} - Would have created comment in GHI`, ownerName, repoName, prNumber, welcomeMessage)
} else {