Skip to content

Commit 2913c95

Browse files
committed
added basic/simple adaptive cards support
1 parent e8b2936 commit 2913c95

34 files changed

+583
-103
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ x64/
5050
build/
5151
[Bb]in/
5252
[Oo]bj/
53+
work
5354

5455
# MSTest test Results
5556
[Tt]est[Rr]esult*/

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
<artifactId>commons-validator</artifactId>
9898
<version>1.7</version>
9999
</dependency>
100+
<dependency>
101+
<groupId>com.google.code.findbugs</groupId>
102+
<artifactId>findbugs-annotations</artifactId>
103+
<version>3.0.1</version>
104+
<scope>provided</scope>
105+
</dependency>
100106
</dependencies>
101107

102108
<build>

src/main/java/jenkins/plugins/office365connector/ActionableBuilder.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import hudson.model.Job;
2020
import hudson.model.Result;
2121
import hudson.model.Run;
22-
import jenkins.plugins.office365connector.model.PotentialAction;
22+
import jenkins.plugins.office365connector.model.Action;
23+
import jenkins.plugins.office365connector.model.adaptivecard.AdaptiveCardAction;
24+
import jenkins.plugins.office365connector.model.messagecard.PotentialAction;
2325
import jenkins.scm.api.SCMHead;
2426
import jenkins.scm.api.metadata.ContributorMetadataAction;
2527
import jenkins.scm.api.metadata.ObjectMetadataAction;
@@ -34,14 +36,16 @@ public class ActionableBuilder {
3436

3537
private final Run run;
3638
private final FactsBuilder factsBuilder;
37-
private final List<PotentialAction> potentialActions = new ArrayList<>();
39+
private final List<Action> potentialActions = new ArrayList<>();
40+
private final boolean isAdaptiveCards;
3841

39-
public ActionableBuilder(Run run, FactsBuilder factsBuilder) {
42+
public ActionableBuilder(Run run, FactsBuilder factsBuilder, boolean isAdaptiveCards) {
4043
this.run = run;
4144
this.factsBuilder = factsBuilder;
45+
this.isAdaptiveCards = isAdaptiveCards;
4246
}
4347

44-
public List<PotentialAction> buildActionable() {
48+
public List<Action> buildActionable() {
4549

4650
pullRequestActionable();
4751
buildViewBuild();
@@ -56,7 +60,7 @@ private void buildViewBuild() {
5660
// hide action button when the build succeed
5761
if (run.getResult() != Result.SUCCESS) {
5862
String viewHeader = Messages.Office365ConnectorWebhookNotifier_ViewHeader(build);
59-
potentialActions.add(new PotentialAction(viewHeader, urlToJob));
63+
potentialActions.add(isAdaptiveCards ? new AdaptiveCardAction(viewHeader,urlToJob) : new PotentialAction(viewHeader, urlToJob));
6064
}
6165
}
6266

@@ -76,7 +80,7 @@ private void pullRequestActionable() {
7680
ObjectMetadataAction oma = job.getAction(ObjectMetadataAction.class);
7781
if (oma != null) {
7882
String urlString = oma.getObjectUrl();
79-
PotentialAction viewPRPotentialAction = new PotentialAction(viewHeader, urlString);
83+
Action viewPRPotentialAction = isAdaptiveCards ? null : new PotentialAction(viewHeader, urlString);
8084
potentialActions.add(viewPRPotentialAction);
8185
factsBuilder.addFact(titleHeader, oma.getObjectDisplayName());
8286
}

src/main/java/jenkins/plugins/office365connector/CardBuilder.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import jenkins.plugins.office365connector.model.Card;
2222
import jenkins.plugins.office365connector.model.FactDefinition;
2323
import jenkins.plugins.office365connector.model.Section;
24+
import jenkins.plugins.office365connector.model.adaptivecard.AdaptiveCard;
25+
import jenkins.plugins.office365connector.model.messagecard.MessageCard;
2426
import jenkins.plugins.office365connector.workflow.StepParameters;
2527

2628
/**
@@ -32,12 +34,14 @@ public class CardBuilder {
3234

3335
private final FactsBuilder factsBuilder;
3436
private final ActionableBuilder potentialActionBuilder;
37+
private final boolean isAdaptiveCards;
3538

36-
public CardBuilder(Run run, TaskListener taskListener) {
39+
public CardBuilder(Run run, TaskListener taskListener, boolean isAdaptiveCards) {
3740
this.run = run;
41+
this.isAdaptiveCards = isAdaptiveCards;
3842

3943
factsBuilder = new FactsBuilder(run, taskListener);
40-
potentialActionBuilder = new ActionableBuilder(run, factsBuilder);
44+
potentialActionBuilder = new ActionableBuilder(run, factsBuilder, isAdaptiveCards);
4145
}
4246

4347
public Card createStartedCard(List<FactDefinition> factDefinitions) {
@@ -51,8 +55,8 @@ public Card createStartedCard(List<FactDefinition> factDefinitions) {
5155
Section section = buildSection(statusName);
5256

5357
String summary = getDisplayName() + ": Build " + getRunName();
54-
Card card = new Card(summary, section);
55-
card.setPotentialAction(potentialActionBuilder.buildActionable());
58+
Card card = isAdaptiveCards ? new AdaptiveCard(summary, section, getCompletedResult(run)) : new MessageCard(summary, section);
59+
card.setAction(potentialActionBuilder.buildActionable());
5660

5761
return card;
5862
}
@@ -86,10 +90,10 @@ public Card createCompletedCard(List<FactDefinition> factDefinitions) {
8690

8791
Section section = buildSection(status);
8892

89-
Card card = new Card(summary, section);
93+
Card card = isAdaptiveCards ? new AdaptiveCard(summary, section, getCompletedResult(run)) : new MessageCard(summary, section);
9094
card.setThemeColor(getCardThemeColor(lastResult));
9195
if (run.getResult() != Result.SUCCESS) {
92-
card.setPotentialAction(potentialActionBuilder.buildActionable());
96+
card.setAction(potentialActionBuilder.buildActionable());
9397
}
9498

9599
return card;
@@ -188,13 +192,13 @@ public Card createBuildMessageCard(StepParameters stepParameters) {
188192
Section section = new Section(activityTitle, stepParameters.getMessage(), factsBuilder.collect());
189193

190194
String summary = getDisplayName() + ": Build " + getRunName();
191-
Card card = new Card(summary, section);
195+
Card card = isAdaptiveCards ? new AdaptiveCard(summary, section, getCompletedResult(run)) : new MessageCard(summary, section);
192196

193197
if (stepParameters.getColor() != null) {
194198
card.setThemeColor(stepParameters.getColor());
195199
}
196200

197-
card.setPotentialAction(potentialActionBuilder.buildActionable());
201+
card.setAction(potentialActionBuilder.buildActionable());
198202

199203
return card;
200204
}

src/main/java/jenkins/plugins/office365connector/Office365ConnectorWebhookNotifier.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void sendBuildStartedNotification(boolean isFromPreBuild) {
6161
for (Webhook webhook : webhooks) {
6262
if (decisionMaker.isAtLeastOneRuleMatched(webhook)) {
6363
if (webhook.isStartNotification()) {
64-
CardBuilder cardBuilder = new CardBuilder(run, taskListener);
64+
CardBuilder cardBuilder = new CardBuilder(run, taskListener, webhook.isAdaptiveCards());
6565
Card card = cardBuilder.createStartedCard(webhook.getFactDefinitions());
6666
executeWorker(webhook, card);
6767
}
@@ -76,7 +76,7 @@ public void sendBuildCompletedNotification() {
7676
for (Webhook webhook : webhooks) {
7777
if (decisionMaker.isAtLeastOneRuleMatched(webhook)) {
7878
if (decisionMaker.isStatusMatched(webhook)) {
79-
CardBuilder cardBuilder = new CardBuilder(run, taskListener);
79+
CardBuilder cardBuilder = new CardBuilder(run, taskListener, webhook.isAdaptiveCards());
8080
Card card = cardBuilder.createCompletedCard(webhook.getFactDefinitions());
8181
executeWorker(webhook, card);
8282
}
@@ -93,7 +93,9 @@ private static List<Webhook> extractWebhooks(Job job) {
9393
}
9494

9595
public void sendBuildStepNotification(StepParameters stepParameters) {
96-
CardBuilder cardBuilder = new CardBuilder(run, taskListener);
96+
Webhook webhook = new Webhook(stepParameters.getWebhookUrl());
97+
98+
CardBuilder cardBuilder = new CardBuilder(run, taskListener, stepParameters.isAdaptiveCards());
9799
Card card;
98100
// TODO: improve this logic as the user may send any 'status' via pipeline step
99101
if (StringUtils.isNotBlank(stepParameters.getMessage())) {
@@ -104,14 +106,13 @@ public void sendBuildStepNotification(StepParameters stepParameters) {
104106
card = cardBuilder.createCompletedCard(stepParameters.getFactDefinitions());
105107
}
106108

107-
Webhook webhook = new Webhook(stepParameters.getWebhookUrl());
108109
executeWorker(webhook, card);
109110
}
110111

111112
private void executeWorker(Webhook webhook, Card card) {
112113
try {
113114
String url = run.getEnvironment(taskListener).expand(webhook.getUrl());
114-
String data = gson.toJson(card);
115+
String data = gson.toJson(card == null ? null : card.toPaylod());
115116
HttpWorker worker = new HttpWorker(url, data, webhook.getTimeout(), taskListener.getLogger());
116117
worker.submit();
117118
} catch (IOException | InterruptedException | RejectedExecutionException e) {

src/main/java/jenkins/plugins/office365connector/Webhook.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@
3131
import org.kohsuke.stapler.DataBoundSetter;
3232
import org.kohsuke.stapler.QueryParameter;
3333
import org.kohsuke.stapler.StaplerRequest;
34+
import org.slf4j.Logger;
35+
import org.slf4j.LoggerFactory;
3436

3537
public class Webhook extends AbstractDescribableImpl<Webhook> {
3638

3739
public static final Integer DEFAULT_TIMEOUT = 30000;
40+
private static final Logger log = LoggerFactory.getLogger(Webhook.class);
3841

3942
private String name;
4043
private String url;
@@ -50,6 +53,8 @@ public class Webhook extends AbstractDescribableImpl<Webhook> {
5053

5154
private int timeout;
5255

56+
private boolean adaptiveCards;
57+
5358
private List<Macro> macros = Collections.emptyList();
5459

5560
private List<FactDefinition> factDefinitions = Collections.emptyList();
@@ -162,6 +167,15 @@ public List<Macro> getMacros() {
162167
return Util.fixNull(macros);
163168
}
164169

170+
@DataBoundSetter
171+
public void setAdaptiveCards(final boolean adaptiveCards) {
172+
this.adaptiveCards = adaptiveCards;
173+
}
174+
175+
public boolean isAdaptiveCards() {
176+
return adaptiveCards;
177+
}
178+
165179
@DataBoundSetter
166180
public void setMacros(List<Macro> macros) {
167181
this.macros = Util.fixNull(macros);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package jenkins.plugins.office365connector.model;
2+
3+
import java.util.List;
4+
5+
public interface Action {
6+
7+
void setName(String name);
8+
9+
void setTarget(List<String> target);
10+
11+
String getName();
12+
}
Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,18 @@
1-
/**
2-
* Licensed under the Apache License, Version 2.0 (the "License");
3-
* you may not use this file except in compliance with the License.
4-
* You may obtain a copy of the License at
5-
* <p>
6-
* http://www.apache.org/licenses/LICENSE-2.0
7-
* <p>
8-
* Unless required by applicable law or agreed to in writing, software
9-
* distributed under the License is distributed on an "AS IS" BASIS,
10-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
* See the License for the specific language governing permissions and
12-
* limitations under the License.
13-
*/
14-
151
package jenkins.plugins.office365connector.model;
162

17-
import java.util.Arrays;
183
import java.util.List;
194

20-
/**
21-
* @author srhebbar
22-
*/
23-
public class Card {
24-
25-
private String summary;
26-
private String themeColor = "3479BF";
27-
28-
// even plugin needs only single 'section' connector API expects arrays
29-
private List<Section> sections;
30-
31-
private List<PotentialAction> potentialAction;
32-
33-
public Card(String summary, Section section) {
34-
this.summary = summary;
35-
this.sections = Arrays.asList(section);
36-
}
5+
public interface Card {
376

38-
public String getSummary() {
39-
return summary;
40-
}
7+
public Object toPaylod();
418

42-
public List<Section> getSections() {
43-
return this.sections;
44-
}
9+
void setAction(List<Action> actions);
4510

46-
public void setThemeColor(String themeColor) {
47-
this.themeColor = themeColor;
48-
}
11+
void setThemeColor(String cardThemeColor);
4912

50-
public String getThemeColor() {
51-
return themeColor;
52-
}
13+
String getSummary();
5314

54-
public void setPotentialAction(List<PotentialAction> potentialActions) {
55-
this.potentialAction = potentialActions;
56-
}
15+
List<Section> getSections();
5716

58-
public List<PotentialAction> getPotentialAction() {
59-
return this.potentialAction;
60-
}
17+
String getThemeColor();
6118
}

0 commit comments

Comments
 (0)