Webhook riceve dati da Jenkins
In un team numeroso che collabora su un progetto, a volte è utile sapere se un nuovo aggiornamento nel progetto è pronto per essere lavorato. Una notifica come commento può essere inviata agli sviluppatori nel corrispondente compito Planfix quando le nuove migliorie sono pronte. Questa funzionalità può essere implementata utilizzando Jenkins e incoming webhooks.
Come fare
- Crea commit di questo tipo: 123456 - commit text, dove 123456 è il numero del compito in Planfix.
- Crea un webhook per ricevere i commit.
- Aggiungi codice in Jenkins Pipeline che invii i dati al webhook.
Configurazione del webhook
Codice in Jenkins
Due funzioni ausiliarie:
@NonCPS
def getTaskNumber(text) {
def m = (text =~ /\d+/);
def taskNumber = m ? m[0] : "";
return taskNumber
}
@NonCPS
def getChangesByTaskNumber() {
def changes = [:]
def maxMessages = 50;
def changeLogSets = currentBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
def entries = changeLogSets[i].items
for (int j = 0; j < entries.length && i + j < maxMessages; j++) {
def entry = entries[j]
def taskNumber = getTaskNumber(entry.msg);
if(taskNumber != "") {
if(!changes.containsKey(taskNumber)) {
changes[taskNumber] = "";
}
changes[taskNumber] += entry.msg + "<br>"
}
}
}
return changes
}
Codice da inserire dopo che le operazioni sono state completate:
def deployedTasks = getChangesByTaskNumber()
deployedTasks.eachWithIndex { key, value, i ->
httpRequest contentType: "APPLICATION_JSON_UTF8", httpMode: "POST", requestBody: "{ \"task\" : ${key}, \"comment\" : \"${value.replaceAll('"','\\\\"')}\", \"project\" : \"project-name\", \"branch\" : \"${env.gitlabBranch}\"}", responseHandle: "NONE", url: "https://myacc.planfix.ru/webhook/json/b7mh-dead-687f-lpwq";, consoleLogResponseBody: true
}
Risultato finale
Lo sviluppatore vedrà un commento simile nel compito: