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

feat-pagerduty - added conversion for pagerduty plugin #245

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions convert/jenkinsjson/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,16 @@ func collectStepsWithID(currentNode jenkinsjson.Node, stepWithIDList *[]StepWith
for _, step := range steps {
*stepWithIDList = append(*stepWithIDList, StepWithID{Step: step, ID: id})
}

case "withKubeConfig":
*stepWithIDList = append(*stepWithIDList, StepWithID{Step: jenkinsjson.ConvertKubeCtl(currentNode, currentNode.ParameterMap), ID: id})

case "pagerdutyChangeEvent":
*stepWithIDList = append(*stepWithIDList, StepWithID{Step: jenkinsjson.ConvertPagerDutyChangeEvent(currentNode, currentNode.ParameterMap), ID: id})

case "pagerduty":
*stepWithIDList = append(*stepWithIDList, StepWithID{Step: jenkinsjson.ConvertPagerDuty(currentNode, currentNode.ParameterMap), ID: id})

default:
placeholderStr := fmt.Sprintf("echo %q", "This is a place holder for: "+currentNode.AttributesMap["jenkins.pipeline.step.type"])
b, err := json.MarshalIndent(currentNode.ParameterMap, "", " ")
Expand Down
75 changes: 75 additions & 0 deletions convert/jenkinsjson/convertTestFiles/pagerduty/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
pipeline {
agent any
environment {
// Ensure your PagerDuty routing key and integration key are securely stored in Jenkins credentials.
PAGERDUTY_ROUTING_KEY = "your_routing_key"
PAGERDUTY_DEDUP_KEY = "your_dedup_key"
PAGERDUTY_INTEGRATION_KEY = "your_integration_key" // Add your integration key for change events here
}
stages {
stage('Build') {
steps {
echo 'Building...'
// Insert your build steps here
}
}
stage('Test') {
steps {
echo 'Testing...'
// Insert your test steps here
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
// Insert your deploy steps here
}
}
}
post {
success {
echo 'Job succeeded!'
pagerduty(
resolve: true,
routingKey: "${env.PAGERDUTY_ROUTING_KEY}",
dedupKey: "${env.PAGERDUTY_DEDUP_KEY}",
incidentSummary: "Build Succeeded for ${env.JOB_NAME}",
incidentSeverity: "info",
incidentSource: "${env.JOB_NAME}"
)
}
failure {
echo 'Job failed!'
pagerduty(
resolve: false,
routingKey: "${env.PAGERDUTY_ROUTING_KEY}",
dedupKey: "${env.PAGERDUTY_DEDUP_KEY}",
incidentSummary: "Build Failed for ${env.JOB_NAME}",
incidentSeverity: "critical",
incidentSource: "${env.JOB_NAME}"
)
}
aborted {
echo 'Job was aborted!'
pagerduty(
resolve: false,
routingKey: "${env.PAGERDUTY_ROUTING_KEY}",
dedupKey: "${env.PAGERDUTY_DEDUP_KEY}",
incidentSummary: "Build Aborted for ${env.JOB_NAME}",
incidentSeverity: "warning",
incidentSource: "${env.JOB_NAME}"
)
}
unstable {
echo 'Job is unstable!'
pagerduty(
resolve: false,
routingKey: "${env.PAGERDUTY_ROUTING_KEY}",
dedupKey: "${env.PAGERDUTY_DEDUP_KEY}",
incidentSummary: "Build Unstable for ${env.JOB_NAME}",
incidentSeverity: "warning",
incidentSource: "${env.JOB_NAME}"
)
}
}
}
16 changes: 16 additions & 0 deletions convert/jenkinsjson/convertTestFiles/pagerduty/pagerduty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
- step:
identifier: pagerduty51d22b
name: Pagerduty
spec:
image: plugins/pagerduty
settings:
dedup_key: E54EC853A59A3815EF3632D5F854CF26
incident_severity: critical
incident_source: test-pager-duty
incident_summary: Build Failed for test-pager-duty
job_status: failure
log_level: debug
resolve: 'false'
routing_key: a666ad1326f34605d06c0dbd4d87c1cb
timeout: ''
type: Plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"spanId": "51d22b52c315ebd9",
"traceId": "6d2b4c2026d5c84a377d2dd97898ad1a",
"parent": "test-pager-duty",
"all-info": "span(name: pagerduty, spanId: 51d22b52c315ebd9, parentSpanId: d948447763f8ffaa, traceId: 6d2b4c2026d5c84a377d2dd97898ad1a, attr: ci.pipeline.run.user:SYSTEM;harness-attribute:{\r\n \"incidentSeverity\" : \"critical\",\r\n \"incidentSource\" : \"test-pager-duty\",\r\n \"resolve\" : false,\r\n \"dedupKey\" : \"E54EC853A59A3815EF3632D5F854CF26\",\r\n \"routingKey\" : \"a666ad1326f34605d06c0dbd4d87c1cb\",\r\n \"incidentSummary\" : \"Build Failed for test-pager-duty\"\r\n};harness-attribute-extra-pip: io.jenkins.plugins.opentelemetry.MigrateHarnessUrlChildAction@72cf357e:io.jenkins.plugins.opentelemetry.MigrateHarnessUrlChildAction@72cf357e;harness-attribute-extra-pip: org.jenkinsci.plugins.workflow.actions.TimingAction@57568f5:org.jenkinsci.plugins.workflow.actions.TimingAction@57568f5;harness-others:;jenkins.pipeline.step.id:26;jenkins.pipeline.step.name:PagerDuty trigger/resolve step;jenkins.pipeline.step.plugin.name:pagerduty;jenkins.pipeline.step.plugin.version:0.7.1;jenkins.pipeline.step.type:pagerduty;)",
"name": "test-pager-duty #22",
"attributesMap": {
"harness-attribute-extra-pip: io.jenkins.plugins.opentelemetry.MigrateHarnessUrlChildAction@72cf357e": "io.jenkins.plugins.opentelemetry.MigrateHarnessUrlChildAction@72cf357e",
"harness-others": "",
"jenkins.pipeline.step.name": "PagerDuty trigger/resolve step",
"ci.pipeline.run.user": "SYSTEM",
"jenkins.pipeline.step.id": "26",
"jenkins.pipeline.step.type": "pagerduty",
"harness-attribute-extra-pip: org.jenkinsci.plugins.workflow.actions.TimingAction@57568f5": "org.jenkinsci.plugins.workflow.actions.TimingAction@57568f5",
"harness-attribute": "{\r\n \"incidentSeverity\" : \"critical\",\r\n \"incidentSource\" : \"test-pager-duty\",\r\n \"resolve\" : false,\r\n \"dedupKey\" : \"E54EC853A59A3815EF3632D5F854CF26\",\r\n \"routingKey\" : \"a666ad1326f34605d06c0dbd4d87c1cb\",\r\n \"incidentSummary\" : \"Build Failed for test-pager-duty\"\r\n}",
"jenkins.pipeline.step.plugin.name": "pagerduty",
"jenkins.pipeline.step.plugin.version": "0.7.1"
},
"type": "Run Phase Span",
"parentSpanId": "d948447763f8ffaa",
"parameterMap": {
"incidentSource": "test-pager-duty",
"resolve": false,
"dedupKey": "E54EC853A59A3815EF3632D5F854CF26",
"incidentSummary": "Build Failed for test-pager-duty",
"incidentSeverity": "critical",
"routingKey": "a666ad1326f34605d06c0dbd4d87c1cb"
},
"spanName": "pagerduty"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pipeline {
agent any
environment {
// Ensure your PagerDuty routing key and integration key are securely stored in Jenkins credentials.
PAGERDUTY_ROUTING_KEY = "your_routing_key"
PAGERDUTY_DEDUP_KEY = "your_dedup_key"
PAGERDUTY_INTEGRATION_KEY = "your_integration_key" // Add your integration key for change events here
}
stages {
stage('Build') {
steps {
echo 'Building...'
// Insert your build steps here
}
}
stage('Test') {
steps {
echo 'Testing...'
// Insert your test steps here
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
// Insert your deploy steps here
}
}
}
post {
always {
// Create a change event in PagerDuty after the job completes, regardless of outcome
pagerdutyChangeEvent(
integrationKey: "${env.PAGERDUTY_INTEGRATION_KEY}",
summaryText: "Job ${env.JOB_NAME} completed with status ${currentBuild.currentResult}",
customDetails: [
buildNumber: "${env.BUILD_NUMBER}",
jobName: "${env.JOB_NAME}",
jobURL: "${env.BUILD_URL}"
]
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
- step:
identifier: pagerdutychangeevent499434
name: Pagerduty_Change_Event
spec:
image: plugins/pagerduty
settings:
custom_details: '{"buildNumber":"22","jobName":"test-pager-duty","jobURL":"http://localhost:8080/job/test-pager-duty/22/"}'
incident_source: ''
incident_summary: Job test-pager-duty completed with status SUCCESS
log_level: debug
routing_key: a666ad1326f34605d06c0dbd4d87c1cb
create_change_event: "true"
timeout: ''
type: Plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"spanId": "499434b70188dc78",
"traceId": "6d2b4c2026d5c84a377d2dd97898ad1a",
"parent": "test-pager-duty",
"all-info": "span(name: pagerdutyChangeEvent, spanId: 499434b70188dc78, parentSpanId: d948447763f8ffaa, traceId: 6d2b4c2026d5c84a377d2dd97898ad1a, attr: ci.pipeline.run.user:SYSTEM;harness-attribute:{\r\n \"customDetails\" : {\r\n \"buildNumber\" : \"22\",\r\n \"jobName\" : \"test-pager-duty\",\r\n \"jobURL\" : \"http://localhost:8080/job/test-pager-duty/22/\"\r\n },\r\n \"summaryText\" : \"Job test-pager-duty completed with status SUCCESS\",\r\n \"integrationKey\" : \"a666ad1326f34605d06c0dbd4d87c1cb\"\r\n};harness-attribute-extra-pip: io.jenkins.plugins.opentelemetry.MigrateHarnessUrlChildAction@4a32f3a7:io.jenkins.plugins.opentelemetry.MigrateHarnessUrlChildAction@4a32f3a7;harness-attribute-extra-pip: org.jenkinsci.plugins.workflow.actions.TimingAction@a2cf09d:org.jenkinsci.plugins.workflow.actions.TimingAction@a2cf09d;harness-others:;jenkins.pipeline.step.id:24;jenkins.pipeline.step.name:PagerDuty Change Event step;jenkins.pipeline.step.plugin.name:pagerduty;jenkins.pipeline.step.plugin.version:0.7.1;jenkins.pipeline.step.type:pagerdutyChangeEvent;)",
"name": "test-pager-duty #22",
"attributesMap": {
"harness-attribute-extra-pip: io.jenkins.plugins.opentelemetry.MigrateHarnessUrlChildAction@4a32f3a7": "io.jenkins.plugins.opentelemetry.MigrateHarnessUrlChildAction@4a32f3a7",
"harness-attribute-extra-pip: org.jenkinsci.plugins.workflow.actions.TimingAction@a2cf09d": "org.jenkinsci.plugins.workflow.actions.TimingAction@a2cf09d",
"harness-others": "",
"jenkins.pipeline.step.name": "PagerDuty Change Event step",
"ci.pipeline.run.user": "SYSTEM",
"jenkins.pipeline.step.id": "24",
"jenkins.pipeline.step.type": "pagerdutyChangeEvent",
"harness-attribute": "{\r\n \"customDetails\" : {\r\n \"buildNumber\" : \"22\",\r\n \"jobName\" : \"test-pager-duty\",\r\n \"jobURL\" : \"http://localhost:8080/job/test-pager-duty/22/\"\r\n },\r\n \"summaryText\" : \"Job test-pager-duty completed with status SUCCESS\",\r\n \"integrationKey\" : \"a666ad1326f34605d06c0dbd4d87c1cb\"\r\n}",
"jenkins.pipeline.step.plugin.name": "pagerduty",
"jenkins.pipeline.step.plugin.version": "0.7.1"
},
"type": "Run Phase Span",
"parentSpanId": "d948447763f8ffaa",
"parameterMap": {
"summaryText": "Job test-pager-duty completed with status SUCCESS",
"customDetails": {
"jobName": "test-pager-duty",
"jobURL": "http://localhost:8080/job/test-pager-duty/22/",
"buildNumber": "22"
},
"integrationKey": "a666ad1326f34605d06c0dbd4d87c1cb"
},
"spanName": "pagerdutyChangeEvent"
}
76 changes: 76 additions & 0 deletions convert/jenkinsjson/json/pager_duty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package json

import (
"encoding/json"
"log"

harness "github.com/drone/spec/dist/go"
)

// ConvertPagerduty creates a Harness step for nunit plugin.
func ConvertPagerDuty(node Node, arguments map[string]interface{}) *harness.Step {
incidentSource, _ := arguments["incidentSource"].(string)
resolve, _ := arguments["resolve"].(bool)
dedupKey, _ := arguments["dedupKey"].(string)
incidentSummary, _ := arguments["incidentSummary"].(string)
incidentSeverity, _ := arguments["incidentSeverity"].(string)
routingKey, _ := arguments["routingKey"].(string)

convertPagerduty := &harness.Step{
Id: SanitizeForId(node.SpanName, node.SpanId),
Name: "Pagerduty",
Type: "plugin",
Spec: &harness.StepPlugin{
Image: "plugins/pagerduty",
With: map[string]interface{}{
"log_level": "debug",
"routing_key": routingKey,
"incident_summary": incidentSummary,
"incident_source": incidentSource,
"incident_severity": incidentSeverity,
"resolve": resolve,
"job_status": "failure",
"dedup_key": dedupKey,
},
},
}

return convertPagerduty
}

// ConvertPagerduty creates a Harness step for nunit plugin.
func ConvertPagerDutyChangeEvent(node Node, arguments map[string]interface{}) *harness.Step {
incidentSource, _ := arguments["incidentSource"].(string)
incidentSummary, _ := arguments["summaryText"].(string)
routingKey, _ := arguments["integrationKey"].(string)
customDetails, _ := arguments["customDetails"].(map[string]interface{})

customDetailsStr := ""
if customDetails != nil {
jsonBytes, err := json.Marshal(customDetails)
if err != nil {
log.Printf("Failed to marshal customDetails: %v", err)
} else {
customDetailsStr = string(jsonBytes)
}
}

convertPagerdutyChangeEvent := &harness.Step{
Name: "Pagerduty_Change_Event",
Type: "plugin",
Id: SanitizeForId(node.SpanName, node.SpanId),
Spec: &harness.StepPlugin{
Image: "plugins/pagerduty",
With: map[string]interface{}{
"log_level": "debug",
"routing_key": routingKey,
"incident_summary": incidentSummary,
"incident_source": incidentSource,
"create_change_event": true,
"custom_details": customDetailsStr,
},
},
}

return convertPagerdutyChangeEvent
}
72 changes: 72 additions & 0 deletions convert/jenkinsjson/json/pager_duty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package json

import (
"testing"

harness "github.com/drone/spec/dist/go"
"github.com/google/go-cmp/cmp"
)

func TestConvertPagerDuty(t *testing.T) {

var tests []runner
tests = append(tests, prepare(t, "/pagerduty/pagerduty_snippet", &harness.Step{
Id: "pagerduty51d22b",
Name: "Pagerduty",
Type: "plugin",
Spec: &harness.StepPlugin{
Image: "plugins/pagerduty",
With: map[string]interface{}{
"log_level": string("debug"),
"routing_key": string("a666ad1326f34605d06c0dbd4d87c1cb"),
"incident_summary": string("Build Failed for test-pager-duty"),
"incident_source": string("test-pager-duty"),
"incident_severity": string("critical"),
"resolve": bool(false),
"job_status": string("failure"),
"dedup_key": string("E54EC853A59A3815EF3632D5F854CF26"),
},
},
}))

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ConvertPagerDuty(tt.input, tt.input.ParameterMap)

if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("ConvertPagerDuty() mismatch (-want +got):\n%s", diff)
}
})
}
}

func TestConvertPagerDutyChangeEvent(t *testing.T) {

var tests []runner
tests = append(tests, prepare(t, "/pagerdutyChangeEvent/pagerdutyChangeEvent_snippet", &harness.Step{
Id: "pagerdutyChangeEvent499434",
Name: "Pagerduty_Change_Event",
Type: "plugin",
Spec: &harness.StepPlugin{
Image: "plugins/pagerduty",
With: map[string]interface{}{
"log_level": string("debug"),
"routing_key": string("a666ad1326f34605d06c0dbd4d87c1cb"),
"incident_summary": string("Job test-pager-duty completed with status SUCCESS"),
"incident_source": string(""),
"custom_details": string(`{"buildNumber":"22","jobName":"test-pager-duty","jobURL":"http://localhost:8080/job/test-pager-duty/22/"}`),
"create_change_event": bool(true),
},
},
}))

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ConvertPagerDutyChangeEvent(tt.input, tt.input.ParameterMap)

if diff := cmp.Diff(got, tt.want); diff != "" {
t.Errorf("ConvertPagerDutyChangeEvent() mismatch (-want +got):\n%s", diff)
}
})
}
}
Loading