Skip to content

Commit

Permalink
Merge pull request #17 from sendbird/feat/command-params
Browse files Browse the repository at this point in the history
feat: add command parameters with test param
  • Loading branch information
bang9 authored Sep 7, 2023
2 parents d9703ee + f4069b7 commit 52b39d1
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 25 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ You can use the following commands in the PR comment:
| -------------------- | ------------------------------------- |
| `/bot create ticket` | Creates a new release ticket in Jira. |

## Test

You can pass `--test` parameter to command: `/bot create ticket --test`

## Slack notifications

It mentions the `@{{product}}-approver` Slack group to notify ticket creation in the channel.
Expand Down
35 changes: 24 additions & 11 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/command/command.create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class CreateCommand extends CommandAbstract {
}

this.log('Workflow request to create a ticket')
const {workflowUrl} = await workflow.createTicket(this.args)
const {workflowUrl} = await workflow.createTicket(this.args, this.params)

this.log('Add a comment about processing ticket creation')
await this.args.octokit.rest.issues.createComment({
Expand Down
8 changes: 7 additions & 1 deletion src/command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ export type CommandArguments = {
[key: string]: unknown
}

export type CommandParameters = {
test: boolean
[key: string]: unknown
}

export abstract class CommandAbstract implements Command {
constructor(
protected readonly target: string,
protected readonly args: CommandArguments
protected readonly args: CommandArguments,
protected readonly params: CommandParameters
) {
this.log(`target: ${target}`)
}
Expand Down
25 changes: 21 additions & 4 deletions src/command/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as core from '@actions/core'
import type {Command, CommandArguments} from './command'
import {COMMAND_ACTIONS, COMMAND_TRIGGER} from '../constants'
import type {Command, CommandArguments, CommandParameters} from './command'
import {
COMMAND_ACTIONS,
COMMAND_DEFAULT_PARAMS,
COMMAND_PARAM_PREFIX,
COMMAND_TRIGGER
} from '../constants'
import CreateCommand from './command.create'

export function buildCommand(
Expand All @@ -12,11 +17,23 @@ export function buildCommand(
return null
}

const [action, target] = text.replace(COMMAND_TRIGGER, '').trim().split(' ')
const [action, target, ...paramCandidates] = text
.replace(COMMAND_TRIGGER, '')
.trim()
.split(' ')

const params = paramCandidates
.filter(it => it.startsWith(COMMAND_PARAM_PREFIX))
.map(it => it.replace(COMMAND_PARAM_PREFIX, ''))
.map(it => it.split('='))
.reduce<CommandParameters>(
(acc, [key, value = true]) => ({...acc, [key]: value}),
COMMAND_DEFAULT_PARAMS
)

switch (action) {
case COMMAND_ACTIONS.CREATE:
return new CreateCommand(target, args)
return new CreateCommand(target, args, params)
default:
return null
}
Expand Down
6 changes: 6 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import {CommandParameters} from './command/command'

export const COMMAND_TRIGGER = '/bot'
export const COMMAND_ACTIONS = {
CREATE: 'create'
}
export const COMMAND_TARGETS = {
TICKET: 'ticket'
}
export const COMMAND_PARAM_PREFIX = '--'

export const BRANCH_RELEASE_PREFIX = 'release'
export const BRANCH_HOTFIX_PREFIX = 'hotfix'
Expand All @@ -16,3 +19,6 @@ export const WORKFLOWS = {
}

export const SENDBIRD_BOT_USERNAME = 'sendbird-sdk-deployment'
export const COMMAND_DEFAULT_PARAMS: CommandParameters = {
test: false
}
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async function run(): Promise<void> {
})

const comment = payload.comment.body.toLowerCase()

const command = buildCommand(comment, {
gh_token,
circleci_token,
Expand Down
26 changes: 19 additions & 7 deletions src/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from '@actions/core'
import * as github from '@actions/github'
import fetch from 'node-fetch'

import type {CommandArguments} from './command/command'
import type {CommandArguments, CommandParameters} from './command/command'
import {WORKFLOW_REPO, WORKFLOW_SCRIPT_VERSION, WORKFLOWS} from './constants'
import {
buildReleaseJiraTicket,
Expand Down Expand Up @@ -52,12 +52,21 @@ export const workflow = {
log(message: string) {
core.info(`Workflow: ${message}`)
},
async createTicket(args: CommandArguments): Promise<{workflowUrl: string}> {
const parameters = await buildCreateTicketParams(args)
async createTicket(
commandArgs: CommandArguments,
commandParams: CommandParameters
): Promise<{workflowUrl: string}> {
const ticketParams = await buildCreateTicketParams(
commandArgs,
commandParams
)

if (parameters.test) this.log('Run on test environment')
if (ticketParams.test) this.log('Run on test environment')

const {repository, response} = await workflowRequest(args, parameters)
const {repository, response} = await workflowRequest(
commandArgs,
ticketParams
)
this.log(`response: ${JSON.stringify(response, null, 2)}`)

if (response.message === 'Project not found') {
Expand Down Expand Up @@ -86,7 +95,10 @@ export const workflow = {
}
}
}
async function buildCreateTicketParams(args: CommandArguments) {
async function buildCreateTicketParams(
args: CommandArguments,
params: CommandParameters
) {
const basicParams = buildBasicRequestParams(WORKFLOWS.CREATE_TICKET)
const release_version = extractVersion(args.branch)

Expand All @@ -96,7 +108,7 @@ async function buildCreateTicketParams(args: CommandArguments) {

return {
...basicParams,
test: core.getInput('test') !== '',
test: core.getBooleanInput('test') || params.test,
product_jira_project_key: core.getInput('product_jira_project_key'),
product_jira_version_prefix: core.getInput('product_jira_version_prefix'),
release_branch: args.branch,
Expand Down

0 comments on commit 52b39d1

Please sign in to comment.