Repository navigation
feat(datasource): reply to a Pylon issue from Forest on the customer's channel [PRD-1873] #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
christophebrun-forest
merged 9 commits into
main
from
feature/prd-1873-pylon-datasource-reply-to-an-issue-from-forest-on-the
Nov 4, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
da54565
feat(datasource): add Pylon reply and note client methods [PRD-1873]
christophebrun-forest 8e06c7a
feat(datasource): expose email recipients in the Pylon message thread…
christophebrun-forest 143b144
feat(datasource): add the Pylon ReplyToIssue action plugin [PRD-1873]
christophebrun-forest 1be7d81
feat(datasource): sign Pylon replies as the Forest operator [PRD-1873]
christophebrun-forest 4359b4c
docs(datasource): document the Pylon ReplyToIssue plugin [PRD-1873]
christophebrun-forest 3617bcb
fix(datasource): address review findings on Pylon ReplyToIssue [PRD-1…
christophebrun-forest c4da6bd
fix(datasource): address PR review findings on Pylon ReplyToIssue [PR…
christophebrun-forest 50928c7
fix(datasource): address second review on Pylon ReplyToIssue [PRD-1873]
christophebrun-forest 20ff8e1
test(datasource): assert the reply payload in the Pylon ReplyToIssue …
christophebrun-forest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
...forest_admin_datasource_pylon/lib/forest_admin_datasource_pylon/plugins/reply_to_issue.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| require 'cgi' | ||
|
|
||
| module ForestAdminDatasourcePylon | ||
| module Plugins | ||
| # Answers a Pylon issue on the channel the customer used, or posts an | ||
| # internal note on it. See the README for the channel rule and who signs. | ||
| class ReplyToIssue < ForestAdminDatasourceCustomizer::Plugins::Plugin | ||
| BaseAction = ForestAdminDatasourceCustomizer::Decorators::Action::BaseAction | ||
| ActionScope = ForestAdminDatasourceCustomizer::Decorators::Action::Types::ActionScope | ||
| ForestException = ForestAdminDatasourceToolkit::Exceptions::ForestException | ||
|
|
||
| NAME = 'Reply to Pylon issue'.freeze | ||
|
|
||
| def run(_datasource_customizer, collection_customizer = nil, options = {}) | ||
| options = {} unless options.is_a?(Hash) | ||
| datasource = options[:datasource] | ||
| raise ForestException, 'ReplyToIssue plugin requires :datasource' unless datasource | ||
| raise ForestException, 'ReplyToIssue plugin requires a collection' unless collection_customizer | ||
|
|
||
| opts = { | ||
| issue_id_field: options[:issue_id_field], | ||
| allow_internal_note: options.fetch(:allow_internal_note, true) ? true : false, | ||
| post_as_operator: options[:post_as_operator] ? true : false, | ||
| support_emails: normalize_emails(options[:support_emails]) | ||
| } | ||
|
|
||
| collection_customizer.add_action((options[:action_name] || NAME).to_s, build_action(datasource, opts)) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def normalize_emails(value) | ||
| Array(value).map { |email| email.to_s.strip.downcase }.reject(&:empty?).uniq | ||
| end | ||
|
|
||
| def build_action(datasource, opts) | ||
| BaseAction.new(scope: ActionScope::SINGLE, form: FormBuilder.build(datasource, opts), | ||
| &executor(datasource, opts)) | ||
| end | ||
|
|
||
| # A note needs the issue only. A reply reads the thread again rather than | ||
| # trusting the form: the conversation may have moved since it rendered. | ||
| def executor(datasource, opts) | ||
| lambda do |context, result_builder| | ||
| issue_id = ThreadLookup.issue_id(context, opts) | ||
| next result_builder.error(message: ThreadLookup.no_issue(opts[:issue_id_field])) if issue_id.nil? | ||
|
|
||
| body = context.form_values[FormBuilder::MESSAGE_LABEL].to_s | ||
| next result_builder.error(message: 'A message is required.') if blank_html?(body) | ||
|
|
||
| status, message = if FormBuilder.public_reply?(context, opts) | ||
| reply(datasource, ThreadLookup.read(datasource, issue_id, opts), body, context, opts) | ||
| else | ||
| note(datasource, issue_id, body) | ||
| end | ||
| result_builder.public_send(status, message: message) | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| end | ||
|
|
||
| # An emptied rich-text editor still submits markup such as `<p><br></p>`. | ||
| def blank_html?(body) | ||
| return false if body.match?(/<img\b/i) | ||
|
|
||
| text = CGI.unescapeHTML(body.gsub(/<[^>]*>/, ' ').gsub(/ /i, ' ')) | ||
| text.gsub(/[[:space:]\u200B-\u200D\u2060\uFEFF]/, '').empty? | ||
| end | ||
|
|
||
| def reply(datasource, state, body, context, opts) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return [:error, state.error] if state.error | ||
| return [:error, no_target_message(state.issue_id, opts)] if state.target.nil? | ||
|
|
||
| payload, dropped, refusal = reply_payload(state.target, body, context) | ||
| return [:error, refusal] if refusal | ||
|
|
||
| operator = OperatorLookup.call(datasource, context.caller) if opts[:post_as_operator] | ||
| return [:error, operator.error] if operator&.error | ||
|
|
||
| payload['user_id'] = operator.user['id'] if operator | ||
| write(state.issue_id, 'reply', reply_success(state, payload, operator&.user) + left_out(dropped)) do | ||
| datasource.client.reply_to_issue(state.issue_id, payload) | ||
| end | ||
|
qltysh[bot] marked this conversation as resolved.
Resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| end | ||
|
|
||
| def reply_payload(target, body, context) | ||
| payload = { 'body_html' => body, 'message_id' => target.message_id } | ||
| return [payload, []] unless target.email? | ||
|
|
||
| to, cc, dropped = target.sanitize(to: context.form_values[FormBuilder::TO_LABEL], | ||
| cc: context.form_values[FormBuilder::CC_LABEL]) | ||
| refusal = "At least one recipient (To) is required to reply by email.#{left_out(dropped)}" if to.empty? | ||
| return [nil, dropped, refusal] if refusal | ||
|
|
||
| [payload.merge('email_info' => { 'to_emails' => to, 'cc_emails' => cc }), dropped] | ||
| end | ||
|
|
||
| def left_out(dropped) | ||
| dropped.empty? ? '' : " Left out as addresses of your team: #{dropped.join(", ")}." | ||
| end | ||
|
|
||
| def note(datasource, issue_id, body) | ||
| write(issue_id, 'internal note', "Internal note added to Pylon issue #{issue_id}.") do | ||
| datasource.client.create_issue_note(issue_id, 'body_html' => body) | ||
| end | ||
| end | ||
|
|
||
| # A 4xx is Pylon naming what is wrong, handed over as it is. Anything else | ||
| # may have failed after Pylon delivered the message, so the operator is | ||
| # told to check before sending it again rather than shown a bare failure. | ||
| def write(issue_id, what, success) | ||
| yield | ||
| [:success, success] | ||
| rescue APIError => e | ||
| ForestAdminDatasourcePylon.logger.warn( | ||
| "[forest_admin_datasource_pylon] #{what} on issue #{issue_id} failed: #{e.message}" | ||
| ) | ||
| return [:error, e.message] if (400..499).cover?(e.status.to_i) | ||
|
|
||
| [:error, "Pylon did not confirm the #{what} on issue #{issue_id} (#{e.message}). It may have been " \ | ||
| 'delivered: check the issue in Pylon before sending it again.'] | ||
| end | ||
|
|
||
| def no_target_message(issue_id, opts) | ||
| message = "Pylon issue #{issue_id} has no customer-visible message, so it cannot get a public reply." | ||
| opts[:allow_internal_note] ? "#{message} Post an internal note instead." : message | ||
| end | ||
|
|
||
| def reply_success(state, payload, operator) | ||
| signed = if operator | ||
| "signed by #{[operator["name"], operator["email"]].find { |v| !v.to_s.strip.empty? }}" | ||
| else | ||
| 'signed by the account of the Pylon API token' | ||
| end | ||
| if payload['email_info'] | ||
| return "Reply emailed to #{payload["email_info"]["to_emails"].join(", ")} on Pylon issue " \ | ||
| "#{state.issue_id}, #{signed}." | ||
| end | ||
|
|
||
| "Reply sent via #{state.target.channel_label} on Pylon issue #{state.issue_id}, #{signed}." | ||
| end | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function with high complexity (count = 6): run [qlty:function-complexity]