Skip to content
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

Add MSTeams CodeBlock element #293

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions adaptivecard/adaptivecard.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,13 @@ const (
TypeElementTextRun string = "TextRun" // Introduced in version 1.2
)

// Known exension types for an Adaptive Card element.
//
// - https://learn.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-format?tabs=adaptive-md%2Cdesktop%2Cconnector-html#codeblock-in-adaptive-cards
const (
TypeElementMSTeamsCodeBlock string = "CodeBlock"
)

// Sentinel errors for this package.
var (
// ErrInvalidType indicates that an invalid type was specified.
Expand Down Expand Up @@ -601,6 +608,15 @@ type Element struct {
// Separator, when true, indicates that a separating line shown should be
// drawn at the top of the element.
Separator bool `json:"separator,omitempty"`

// CodeSnippet provides the content for a CodeBlock element, specific to MSTeams.
CodeSnippet string `json:"codeSnippet,omitempty"`

// Language specifies the langauge of a CodeBlock element, specific to MSTeams.
Language string `json:"language,omitempty"`

// StartLineNumber specifies the initial line number of CodeBlock element, specific to MSTeams.
StartLineNumber int `json:"startLineNumber,omitempty"`
}

// Container is an Element type that allows grouping items together.
Expand Down Expand Up @@ -1374,6 +1390,10 @@ func (e Element) Validate() error {
v.SelfValidate(TableRows(e.Rows))

v.SelfValidate(TableColumnDefinitions(e.Columns))

case e.Type == TypeElementMSTeamsCodeBlock:
v.NotEmptyValue(e.CodeSnippet, "CodeSnippet", e.Type, ErrMissingValue)
v.NotEmptyValue(e.Language, "Language", e.Type, ErrMissingValue)
}

// Return the last recorded validation error, or nil if no validation
Expand Down Expand Up @@ -3145,6 +3165,18 @@ func (c *Card) AddContainer(prepend bool, container Container) error {
return nil
}

// NewCodeBlock creates a new CodeBlock element with snippet, language, and
// optional firstLine. This is an MSTeams extension element.
func NewCodeBlock(snippet string, language string, firstLine int) Element {
codeBlock := Element{
Type: TypeElementMSTeamsCodeBlock,
CodeSnippet: snippet,
Language: language,
StartLineNumber: firstLine,
}
return codeBlock
}

// cardBodyHasMention indicates whether an Adaptive Card body contains all
// specified Mention values. For every user mention, we require at least one
// match in an applicable Element in the Card Body.
Expand Down
1 change: 1 addition & 0 deletions adaptivecard/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func supportedElementTypes() []string {
TypeElementTable, // Introduced in version 1.5
TypeElementTextBlock,
TypeElementTextRun,
TypeElementMSTeamsCodeBlock,
}
}

Expand Down
Loading