Ejemplo n.º 1
0
        private SlackMessageData GenerateSlackMessagePR(string repoURL, string owner, string repo, string requestedBy, List <dynamic> summary)
        {
            var payload = new SlackMessageData();

            payload.Text = string.Format("Responding to Request for PR status of <{0}/{1}/{2}|{1}/{2}> by @{3}\n\n\n", repoURL, owner, repo, requestedBy);

            if (summary.Count <= 0)
            {
                var attachment = new SlackAttachment
                {
                    Color   = "good",
                    Pretext = "No PR statuses exist in repo"
                };
                payload.Attachments.Add(attachment);
            }

            foreach (var entry in summary)
            {
                var attachment = new SlackAttachment
                {
                    Color   = entry.State.ToString().ToLower() == "success" ? "good" : "danger",
                    Pretext = string.Format("<{0}|Pull Request #{1}> - {2}\n\n", entry.URL, entry.Number, entry.State)
                };
                attachment.Text += String.Format("Pending: {0}\t", entry.Pending);
                attachment.Text += String.Format("Error:   {0}\t", entry.Error);
                attachment.Text += String.Format("Failure: {0}\t", entry.Failure);
                attachment.Text += String.Format("Success: {0}\n", entry.Success);

                payload.Attachments.Add(attachment);
            }

            return(payload);
        }
Ejemplo n.º 2
0
        public async Task <string> Send(string message, string channel)
        {
            var payload = new SlackMessageData
            {
                Text = message
            };

            return(await Send(payload, channel));
        }
Ejemplo n.º 3
0
        public virtual void Write(Log source, LogEntry entry, object context)
        {
            // Skip messages that don't match our output severity filter
            if (!_severityFilter.Matches(entry.Type))
            {
                return;
            }

            lock (_writerLock)
            {
                SlackMessageData message = new SlackMessageData();
                message.channel                 = _targetChannel;
                message.attachments             = new SlackAttachmentData[1];
                message.attachments[0].text     = EscapeMessageText(entry.Message);
                message.attachments[0].fallback = message.attachments[0].text;
                message.attachments[0].ts       = GetUnixTime(entry.TimeStamp);
                message.attachments[0].footer   = string.Format("{0} on {1}", source.Name, _machineId);

                switch (entry.Type)
                {
                case LogMessageType.Error:
                case LogMessageType.Fatal:
                    message.attachments[0].color = "danger";
                    break;

                case LogMessageType.Warning:
                    message.attachments[0].color = "warning";
                    break;
                }

                string payload     = JsonUtility.ToJson(message, true);
                byte[] payloadData = Encoding.UTF8.GetBytes(payload);

                UnityWebRequest postRequest = new UnityWebRequest(_webhookUrl, "POST");
                postRequest.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");

                UploadHandlerRaw rawUpload = new UploadHandlerRaw(payloadData);
                postRequest.uploadHandler = rawUpload;
                postRequest.disposeUploadHandlerOnDispose = true;

                if (_requestHandler == null)
                {
                    _requestHandler = UnityWebRequestScheduler.Create();
                }

                _requestHandler.Schedule(postRequest);
            }
        }
Ejemplo n.º 4
0
        public async Task <string> Send(SlackMessageData payload, string channel)
        {
            if (!channel.StartsWith("@") && !channel.StartsWith("#"))
            {
                channel = "#" + channel;
            }

            payload.UserName   = _userName;
            payload.Icon_Emoji = _icon;
            payload.Channel    = channel;

            var jsonContent = JsonConvert.SerializeObject(payload, Formatting.Indented,
                                                          new JsonSerializerSettings {
                ContractResolver = new LowerCasePropertyNamesContractResolver()
            });

            HttpContent content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

            var response = await _httpClient.PostAsync("", content);

            return(jsonContent);
        }