diff --git a/README.md b/README.md index c1d3e82..7f4cd68 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/bot_scripts/greet-new-contributor.js b/bot_scripts/greet-new-contributor.js index 119ae2e..562816c 100644 --- a/bot_scripts/greet-new-contributor.js +++ b/bot_scripts/greet-new-contributor.js @@ -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 {