Created
February 13, 2016 07:37
-
-
Save weirded/3e103609b940acb917af to your computer and use it in GitHub Desktop.
Test for CMB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sumologic.bender.plugins.cmb | |
import java.net.URI | |
import akka.actor.ActorSystem | |
import com.atlassian.jira.rest.client.api.IssueRestClient | |
import com.atlassian.jira.rest.client.api.domain.Issue | |
import com.atlassian.jira.rest.client.auth.BasicHttpAuthenticationHandler | |
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory | |
import com.sumologic.sumobot.test.{MatchTextUtil, BotPluginTestKit, SumoBotSpec} | |
import org.codehaus.jettison.json.{JSONArray, JSONObject} | |
import scala.collection.JavaConverters._ | |
class CmbTest | |
extends MatchTextUtil | |
with SumoBotSpec { | |
"Cmb" should { | |
"respond to variants of proposing jiras" in { | |
shouldMatch(Cmb.Proposal, "Proposing https://jira.example.com/jira/browse/PROJ-46819") | |
shouldMatch(Cmb.Proposal, """Re-proposing | |
https://jira.example.com/jira/browse/PROJ-46611 | |
https://jira.example.com/jira/browse/PROJ-46612 thanks""") | |
} | |
"match multiple jira numbers" in { | |
val text = """Re-proposing | |
|https://jira.example.com/jira/browse/PROJ-46611 | |
|https://jira.example.com/jira/browse/PROJ-46612 thanks""" | |
text match { | |
case Cmb.Proposal(_, issue) => | |
issue should be("PROJ-46612") | |
case _ => fail("No match!") | |
} | |
val issuesIds = Cmb.ExtractionRegex.findAllMatchIn(text).map(_.group(1)).toSeq | |
issuesIds.size should be(2) | |
issuesIds(0) should be("PROJ-46611") | |
issuesIds(1) should be("PROJ-46612") | |
} | |
"match +1 comments" in { | |
shouldMatch(Cmb.PlusOne, "+1") | |
shouldMatch(Cmb.PlusOne, "+1 PROJ-12345") | |
shouldMatch(Cmb.PlusOne, "+1 12345") | |
shouldMatch(Cmb.PlusOne, " +1 12345") | |
shouldMatch(Cmb.PlusOne, "I vote +1 on 12345") | |
} | |
} | |
// NOTE: Use this in a debugger to figure out the fields, etc for Jira. | |
"cmb" should { | |
"be able to determine who proposed a ticket" ignore { | |
val jql = "filter = \"All Open Jiras\" " + | |
"AND type = \"System Change\" " + | |
"AND status in (\"Approved\") " + | |
"AND updated > startOfDay(-5)" + | |
"ORDER BY key" | |
val factory = new AsynchronousJiraRestClientFactory | |
val url = "https://jira.example.com/jira" | |
val jiraServerUri = new URI(url) | |
val authenticationHandler = new BasicHttpAuthenticationHandler("bender", "[missing]") | |
val restClient = factory.create(jiraServerUri, authenticationHandler) | |
restClient.getSearchClient.searchJql(jql).get().getIssues.asScala.toSeq.par.foreach { | |
issue => | |
val expandos: java.lang.Iterable[IssueRestClient.Expandos] = List(IssueRestClient.Expandos.CHANGELOG).toIterable.asJava | |
val detailedIssue: Issue = restClient.getIssueClient.getIssue(issue.getKey, expandos).claim() | |
val deployments = Option(issue.getFieldByName("Required For Deployments")). | |
map(_.getValue.asInstanceOf[JSONArray]).get | |
val depl = (0 to deployments.length() - 1).map(i => deployments.get(i).asInstanceOf[JSONObject]).map(_.get("value")) | |
println(depl) | |
val proposalDetails = detailedIssue.getChangelog.asScala.find { | |
group => | |
group.getItems.asScala.exists { | |
item => | |
item.getField == "status" && item.getTo == "10016" | |
} | |
}.map { | |
proposalGroup => | |
s"proposed by ${proposalGroup.getAuthor.getName} on ${proposalGroup.getCreated.toString()}, " | |
}.getOrElse("") | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment