-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Watcher: migrate PagerDuty v1 events API to v2 API #32285
Changes from 1 commit
9705705
155b036
426c81d
003896a
d3e5921
1166ec0
942e62d
fdab5e3
4012f03
6644f2b
71629ae
c67d742
fa1fc07
dc7edb5
a0e68bb
f4ee457
6a23d31
ade99d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,8 @@ | |
public class IncidentEvent implements ToXContentObject { | ||
|
||
static final String HOST = "events.pagerduty.com"; | ||
static final String PATH = "/generic/2010-04-15/create_event.json"; | ||
static final String PATH = "/v2/enqueue"; | ||
static final String ACCEPT_HEADER = "application/vnd.pagerduty+json;version=2"; | ||
|
||
final String description; | ||
@Nullable final HttpProxy proxy; | ||
|
@@ -99,40 +100,85 @@ public HttpRequest createRequest(final String serviceKey, final Payload payload) | |
.scheme(Scheme.HTTPS) | ||
.path(PATH) | ||
.proxy(proxy) | ||
.setHeader("Accept", ACCEPT_HEADER) | ||
.jsonBody(new ToXContent() { | ||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
builder.field(Fields.SERVICE_KEY.getPreferredName(), serviceKey); | ||
builder.field(Fields.EVENT_TYPE.getPreferredName(), eventType); | ||
builder.field(Fields.DESCRIPTION.getPreferredName(), description); | ||
builder.field("routing_key", serviceKey); | ||
builder.field("event_action", eventType); | ||
if (incidentKey != null) { | ||
builder.field(Fields.INCIDENT_KEY.getPreferredName(), incidentKey); | ||
builder.field("dedup_key", incidentKey); | ||
} | ||
|
||
builder.startObject("payload"); | ||
{ | ||
builder.field("summary", description); | ||
|
||
if (attachPayload) { | ||
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. Just to make sure: This gets parsed properly on the other side if you leave it out (and dont have the 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. custom_details is not mandatory, so it will def leave it out. Ill add a null check for payload as well in this statement |
||
builder.startObject("custom_details"); | ||
builder.field(Fields.PAYLOAD.getPreferredName()); | ||
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. replace two lines with
|
||
payload.toXContent(builder, params); | ||
builder.endObject(); | ||
} | ||
|
||
// TODO externalize this into something user editable | ||
builder.field("source", "watcher"); | ||
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. maybe have the watch id in here (if possible) to easily pinpoint the source? |
||
builder.field("severity", "critical"); | ||
} | ||
builder.endObject(); | ||
|
||
if (client != null) { | ||
builder.field(Fields.CLIENT.getPreferredName(), client); | ||
} | ||
if (clientUrl != null) { | ||
builder.field(Fields.CLIENT_URL.getPreferredName(), clientUrl); | ||
} | ||
if (attachPayload) { | ||
builder.startObject(Fields.DETAILS.getPreferredName()); | ||
builder.field(Fields.PAYLOAD.getPreferredName()); | ||
payload.toXContent(builder, params); | ||
builder.endObject(); | ||
} | ||
|
||
if (contexts != null && contexts.length > 0) { | ||
builder.startArray(Fields.CONTEXTS.getPreferredName()); | ||
for (IncidentEventContext context : contexts) { | ||
context.toXContent(builder, params); | ||
} | ||
builder.endArray(); | ||
toXContentV2Contexts(builder, params, contexts); | ||
} | ||
|
||
return builder; | ||
} | ||
}) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Turns the V1 API contexts into 2 distinct lists, images and links. The V2 API has separated these out into 2 top level fields. | ||
*/ | ||
private void toXContentV2Contexts(XContentBuilder builder, ToXContent.Params params, | ||
IncidentEventContext[] contexts) throws IOException { | ||
// contexts can be either links or images, and the v2 api needs them separate | ||
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. how about making this more readable (at least to me :-)
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. dang i like those streams.. I still am a streaming noob. ty. |
||
List<IncidentEventContext> links = new ArrayList<>(); | ||
List<IncidentEventContext> images = new ArrayList<>(); | ||
for (IncidentEventContext context: contexts) { | ||
if (context.type == IncidentEventContext.Type.LINK) { | ||
links.add(context); | ||
} else if (context.type == IncidentEventContext.Type.IMAGE) { | ||
images.add(context); | ||
} | ||
} | ||
if (links.isEmpty() == false) { | ||
builder.startArray("links"); | ||
{ | ||
for (IncidentEventContext link: links) { | ||
link.toXContent(builder, params); | ||
} | ||
} | ||
builder.endArray(); | ||
} | ||
if (images.isEmpty() == false) { | ||
builder.startArray("images"); | ||
{ | ||
for (IncidentEventContext image: images) { | ||
image.toXContent(builder, params); | ||
} | ||
} | ||
builder.endArray(); | ||
} | ||
} | ||
|
||
@Override | ||
public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
builder.startObject(); | ||
|
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.
maybe also fix the above links, as they probably link to the old documentation?