Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit 0b357fd

Browse files
authored
Remove extra <!-- end list --> generated by comrak (#850)
In some cases, `comrak` will automatically insert an XML `<!-- end list -->` comment after a list item. We manually remove this when converting a `comrak` tree back to a string.
1 parent 7a295e6 commit 0b357fd

File tree

1 file changed

+151
-1
lines changed

1 file changed

+151
-1
lines changed

server/bleep/src/agent/transcoder.rs

Lines changed: 151 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ pub fn decode(llm_message: &str) -> (String, Option<String>) {
3535
let comrak_to_string = |node| {
3636
let mut out = Vec::<u8>::new();
3737
comrak::format_commonmark(node, &options, &mut out).unwrap();
38-
String::from_utf8_lossy(&out).trim().to_owned()
38+
String::from_utf8_lossy(&out)
39+
.trim()
40+
.replace("\n\n<!-- end list -->", "")
3941
};
4042

4143
// `comrak` will not recognize footnote definitions unless they have been referenced at least
@@ -1107,4 +1109,152 @@ Foo *bar* `[^summary]: allow this, it is in code quotes` quux.";
11071109
assert_eq!(expected, body);
11081110
assert_eq!("Baz fred **thud** corge.", conclusion.unwrap());
11091111
}
1112+
1113+
#[test]
1114+
fn test_decode_erroneous_endlist() {
1115+
let input = r#"The code in [`cmd/worker/slack.go`](cmd/worker/slack.go#L1-L42) is a Go program that sends a message to a Slack channel using a webhook URL.
1116+
1117+
Here's a breakdown of the code:
1118+
1119+
- Lines 1-8: The package declaration and import statements. The program imports packages for handling bytes, formatting, HTTP requests, and environment variables.
1120+
1121+
<QuotedCode>
1122+
<Code>
1123+
package main
1124+
1125+
import (
1126+
"bytes"
1127+
"fmt"
1128+
"net/http"
1129+
"os"
1130+
)
1131+
</Code>
1132+
<Language>Go</Language>
1133+
<Path>cmd/worker/slack.go</Path>
1134+
<StartLine>1</StartLine>
1135+
<EndLine>8</EndLine>
1136+
</QuotedCode>
1137+
1138+
- Lines 10-12: A constant `SLACK_WEBHOOK_URL` is declared. This constant is used to get the Slack webhook URL from the environment variables.
1139+
1140+
<QuotedCode>
1141+
<Code>
1142+
const (
1143+
SLACK_WEBHOOK_URL = "SLACK_WEBHOOK_URL"
1144+
)
1145+
</Code>
1146+
<Language>Go</Language>
1147+
<Path>cmd/worker/slack.go</Path>
1148+
<StartLine>10</StartLine>
1149+
<EndLine>12</EndLine>
1150+
</QuotedCode>
1151+
1152+
- Lines 14-41: The `sendSlackMessage` function is defined. This function takes an organization name as an argument and sends a message to a Slack channel.
1153+
1154+
<QuotedCode>
1155+
<Code>
1156+
func sendSlackMessage(org string) error {
1157+
1158+
endpoint := os.Getenv(SLACK_WEBHOOK_URL)
1159+
if endpoint == "" {
1160+
return fmt.Errorf("sendSlackMessage: environment variables %s must not be empty",
1161+
SLACK_WEBHOOK_URL)
1162+
}
1163+
1164+
orgRelease := fmt.Sprintf("https://github.com/%s/%s/blob/main/%s/release.yaml",
1165+
REPO_OWNER, REPO_ENVS, org)
1166+
message := fmt.Sprintf("New organization %#q added.\nHelmRelease: %s",
1167+
org, orgRelease)
1168+
1169+
requestBody := []byte(`{"text": "` + message + `"}`)
1170+
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(requestBody))
1171+
if err != nil {
1172+
return fmt.Errorf("sendSlackMessage: failed to create request: %v", err)
1173+
}
1174+
req.Header.Set("Content-Type", "application/json")
1175+
1176+
client := &http.Client{}
1177+
resp, err := client.Do(req)
1178+
if err != nil {
1179+
return fmt.Errorf("sendSlackMessage: failed to send Slack message: %v", err)
1180+
}
1181+
defer resp.Body.Close()
1182+
1183+
return nil
1184+
}
1185+
</Code>
1186+
<Language>Go</Language>
1187+
<Path>cmd/worker/slack.go</Path>
1188+
<StartLine>14</StartLine>
1189+
<EndLine>41</EndLine>
1190+
</QuotedCode>
1191+
1192+
[^summary]: The code in `cmd/worker/slack.go` is a Go program that sends a message to a Slack channel using a webhook URL. The `sendSlackMessage` function constructs a message about a new organization, creates an HTTP POST request with this message, and sends it to the Slack webhook URL."#;
1193+
1194+
let expected_body = r#"The code in [`cmd/worker/slack.go`](cmd/worker/slack.go#L1-L42) is a Go program that sends a message to a Slack channel using a webhook URL.
1195+
1196+
Here's a breakdown of the code:
1197+
1198+
- Lines 1-8: The package declaration and import statements. The program imports packages for handling bytes, formatting, HTTP requests, and environment variables.
1199+
1200+
``` type:Quoted,lang:Go,path:cmd/worker/slack.go,lines:1-8
1201+
package main
1202+
1203+
import (
1204+
"bytes"
1205+
"fmt"
1206+
"net/http"
1207+
"os"
1208+
)
1209+
```
1210+
1211+
- Lines 10-12: A constant `SLACK_WEBHOOK_URL` is declared. This constant is used to get the Slack webhook URL from the environment variables.
1212+
1213+
``` type:Quoted,lang:Go,path:cmd/worker/slack.go,lines:10-12
1214+
const (
1215+
SLACK_WEBHOOK_URL = "SLACK_WEBHOOK_URL"
1216+
)
1217+
```
1218+
1219+
- Lines 14-41: The `sendSlackMessage` function is defined. This function takes an organization name as an argument and sends a message to a Slack channel.
1220+
1221+
``` type:Quoted,lang:Go,path:cmd/worker/slack.go,lines:14-41
1222+
func sendSlackMessage(org string) error {
1223+
1224+
endpoint := os.Getenv(SLACK_WEBHOOK_URL)
1225+
if endpoint == "" {
1226+
return fmt.Errorf("sendSlackMessage: environment variables %s must not be empty",
1227+
SLACK_WEBHOOK_URL)
1228+
}
1229+
1230+
orgRelease := fmt.Sprintf("https://github.com/%s/%s/blob/main/%s/release.yaml",
1231+
REPO_OWNER, REPO_ENVS, org)
1232+
message := fmt.Sprintf("New organization %#q added.\nHelmRelease: %s",
1233+
org, orgRelease)
1234+
1235+
requestBody := []byte(`{"text": "` + message + `"}`)
1236+
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(requestBody))
1237+
if err != nil {
1238+
return fmt.Errorf("sendSlackMessage: failed to create request: %v", err)
1239+
}
1240+
req.Header.Set("Content-Type", "application/json")
1241+
1242+
client := &http.Client{}
1243+
resp, err := client.Do(req)
1244+
if err != nil {
1245+
return fmt.Errorf("sendSlackMessage: failed to send Slack message: %v", err)
1246+
}
1247+
defer resp.Body.Close()
1248+
1249+
return nil
1250+
}
1251+
```"#;
1252+
1253+
let expected_conclusion = r#"The code in `cmd/worker/slack.go` is a Go program that sends a message to a Slack channel using a webhook URL. The `sendSlackMessage` function constructs a message about a new organization, creates an HTTP POST request with this message, and sends it to the Slack webhook URL."#;
1254+
1255+
let (body, conclusion) = decode(input);
1256+
1257+
assert_eq!(expected_body, body);
1258+
assert_eq!(expected_conclusion, conclusion.unwrap());
1259+
}
11101260
}

0 commit comments

Comments
 (0)