def getTimesheetsFromZendesk(templateQueryContext): # --- setup zendesk settings --- serverURL = "https://yourZendeskHost" userName = "******" token = "yourZendeskToken" # --- setup zendesk settings --- clr.AddReference("System.Web") clr.AddReference("System.Net.Http") clr.AddReference("Newtonsoft.Json") from System.Text import Encoding from Newtonsoft.Json.Linq import JObject from System.Collections.Generic import List from System.Web import HttpUtility from System.Net.Http import HttpClient from System.Net.Http.Headers import AuthenticationHeaderValue user = Context.SelectSingleWithParams({ "Query": "From U In UserDetail Where U.UserDetailUuid = @UserDetailUuid Select U", "@UserDetailUuid": templateQueryContext.UserDetailUuid }) query = HttpUtility.UrlEncode("type:ticket updated:" + templateQueryContext.BeginTime.ToString("yyyy-MM-dd") + " assignee:" + user.Username) queryURL = serverURL + "/api/v2/search.json?query=" + query credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(userName + "/token:"+ token)); modelEntity = ModelEntity({ "Name": "Result" }) modelEntity.Properties.Add(TextProperty({ "Name": "Description" })) modelEntity.Properties.Add(TextProperty({ "Name": "ResultTitle" })) modelEntity.Properties.Add(TextProperty({ "Name": "ResultSortOrder" })) result = List[EntityObject]() with HttpClient() as client: client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue("Basic", credentials) response = client.GetAsync(queryURL).Result response.EnsureSuccessStatusCode() text = response.Content.ReadAsStringAsync().Result json = JObject.Parse(text) for item in json.results: id = str(item.id) name = str(item.via.source["from"].name) subject = str(item.subject) status = str(item.status) entity = modelEntity.CreateEntityObject() entity.Description = "#" + id + ": " + subject + " (" + name + ", " + status + ")" entity.ResultTitle = entity.Description entity.ResultSortOrder = entity.Description result.Add(entity) templateQueryContext.Templates = result
def ToString(jobject, prettyPrint=False): return (JObject.ToString(jobject) if prettyPrint else JObject.ToString( jobject, Formatting.None))
def search(self, jql): clr.AddReference("System.Web") from System.Web import HttpUtility from System.Net import HttpWebRequest from System.IO import StreamReader clr.AddReference("Newtonsoft.Json") from Newtonsoft.Json import JsonTextReader from Newtonsoft.Json.Linq import JObject from System import Decimal import Newtonsoft.Json clr.ImportExtensions(Newtonsoft.Json.Linq) usernamepw = Convert.ToBase64String( Encoding.UTF8.GetBytes( String.Format("{0}:{1}", self.username, self.password))) fieldsparam = String.Join(",", self.requestedFields) requestUri = String.Format( "{0}rest/api/2/search?jql={1}&fields={2}", self.repository.AbsoluteUri, HttpUtility.UrlEncode(jql), fieldsparam) Logger.Write(LogLevel.Verbose, "Jira.Search: {0}", requestUri) request = HttpWebRequest.Create(requestUri) request.ContentType = "application/json" request.Headers.Add("Authorization", "Basic " + usernamepw) request.Method = "GET" with request.GetResponse() as response: with StreamReader(response.GetResponseStream()) as sr: with JsonTextReader(sr) as jr: result = JObject.Load(jr) issues = result["issues"] items = list() for issue in issues: item = Issue() item.Key = Newtonsoft.Json.Linq.Extensions.Value[ String](issue["key"]) fields = issue["fields"] item.Updated = Newtonsoft.Json.Linq.Extensions.Value[ DateTime](fields["updated"]) # transform seconds to hours estimate = Newtonsoft.Json.Linq.Extensions.Value[ System.Object](fields["timeoriginalestimate"]) if estimate is not None: estimate = Newtonsoft.Json.Linq.Extensions.Value[ Decimal](fields["timeoriginalestimate"]) estimate = estimate / (60.0 * 60.0) item.TimeOriginalEstimate = estimate status = fields["status"] item.Status = Newtonsoft.Json.Linq.Extensions.Value[ String](status["name"]) item.Summary = Newtonsoft.Json.Linq.Extensions.Value[ String](fields["summary"]) type = fields["issuetype"] item.Type = Newtonsoft.Json.Linq.Extensions.Value[ String](type["name"]) item.Link = self.repository.ToString( ) + "browse/" + item.Key subTasks = fields["subtasks"] item.SubTaskKeys = System.Linq.Enumerable.Cast[ JObject](subTasks).Select( lambda t: Newtonsoft.Json.Linq.Extensions. Value[String](t["key"])).ToArray[String]() items.Add(item) return items
def ToJObject(pythonObject): return JObject.FromObject(pythonObject)