···8env_logger = "0.11.8"
9jacquard = "0.2.1"
10jetstream = { path = "../links/jetstream" }
011reqwest = { version = "0.12.23", features = ["json"] }
12serde = { version = "1.0.228", features = ["derive"] }
13serde_json = "1.0.145"
···8env_logger = "0.11.8"
9jacquard = "0.2.1"
10jetstream = { path = "../links/jetstream" }
11+log = "0.4.28"
12reqwest = { version = "0.12.23", features = ["json"] }
13serde = { version = "1.0.228", features = ["derive"] }
14serde_json = "1.0.145"
docs/october-dolly.png
This is a binary file and will not be displayed.
+7
license
···0000000
···1+Copyright (c) 2025 @bad-example.com
2+3+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+5+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+7+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+32
readme.md
···00000000000000000000000000000000
···1+# ๐ Happy hacktober! ๐ง๐ผโโ๏ธ
2+3+
4+5+[This bot](https://bsky.app/profile/hacktober.tngl.sh) listens to the [jetstream](github.com/bluesky-social/jetstream) firehose, filters for labels added to issues on [tangled.org](https://tangled.org/), checks if they are the official [`good-first-issue`](https://tangled.org/goodfirstissues) label, and then [posts](https://bsky.app/profile/hacktober.tngl.sh/post/3m2oflabdmc2u) about it!
6+7+8+### It's made with:
9+10+- [jacquard](https://docs.rs/jacquard/latest/jacquard/): auth and posting
11+- [microcosm slingshot](https://slingshot.microcosm.blue/): identity resolution and record fetching
12+- [microcosm jetstream](https://tangled.org/@microcosm.blue/microcosm-rs/tree/main/jetstream): firehose listener
13+- [tangled's](https://tangled.org/) PDS hosts the bot's account!
14+15+### It's made by:
16+17+- [@bad-example.com](https://bsky.app/profile/bad-example.com): [ko-fi](https://ko-fi.com/bad_example), [github sponsors](https://github.com/sponsors/uniphil/)
18+19+20+### It would be nice if this bot would:
21+22+- [ ] pull [OG repo images](https://bsky.app/profile/oppi.li/post/3m2orohxal22j) so it can post with an external link embed
23+24+### It would be nice if this bot *could*:
25+26+- [ ] link directly to the issue, instead of the repo's all-issues page. i don't think it's possible right now because the issue page URL needs the issue id number, which is only kept in tangled's appview at the moment.
27+- [ ] reply to its posts when issues are closed as complete! again currently the open/closed state for tangled is only in the appview, so this is not currently possible to detect.
28+29+30+### Things to watch out for if you hack on it
31+32+- [ ] The microcosm jetstream package isn't published, so this currently uses a horrible local path reference for it, and that reference uses a very old folder name that you won't get by default from cloning [microcosm-rs](https://tangled.org/@microcosm.blue/microcosm-rs). If you rename microcosm-rs's folder name to `links`, it should work! or ping me and i'll fix it.
+243-177
src/main.rs
···1use clap::Parser;
2-use url::Url;
3-use jetstream::{
4- JetstreamCompression, JetstreamConfig, JetstreamConnector,
5- events::{CommitOp, Cursor, EventKind, JetstreamEvent},
6- exports::Nsid,
7-};
8use jacquard::{
9 api::{
10 app_bsky::feed::post::Post,
11- app_bsky::richtext::facet::{Facet, ByteSlice},
012 com_atproto::server::create_session::CreateSession,
13- com_atproto::repo::create_record::CreateRecord,
14 },
15 client::{BasicClient, Session},
16 types::{
17- datetime::Datetime,
18- ident::AtIdentifier,
19- language::Language,
20- collection::Collection,
21- value::Data,
22- string::AtUri,
23 },
24};
00000025026use std::time::Duration;
27-use serde::Deserialize;
2829type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
30···54 /// don't actually post
55 #[arg(long, action)]
56 dry_run: bool,
00057}
5859-async fn post(
60- client: &BasicClient,
61- identifier: &AtIdentifier<'_>,
62- repo_name: &str,
63- repo_url: &str,
64- title: &str,
65- repo_issues_url: &str,
66-) -> Result<()> {
67- let message = format!(r#"good-first-issue added for {repo_name}:
68-69-> {title}"#);
70-71- let repo_feature = serde_json::json!({
72- "$type": "app.bsky.richtext.facet#link",
73- "uri": repo_url,
74- });
75- let repo_facet = Facet {
76- features: vec![Data::from_json(&repo_feature)?],
77- index: ByteSlice {
78- byte_start: 27,
79- byte_end: 29 + repo_name.len() as i64,
80- extra_data: Default::default(),
81- },
82- extra_data: Default::default(),
83- };
84-85- let title_starts_at = (29 + repo_name.len() + 5) as i64;
86-87- let repo_issues_feature = serde_json::json!({
88- "$type": "app.bsky.richtext.facet#link",
89- "uri": repo_issues_url,
90- });
91- let issues_facet = Facet {
92- features: vec![Data::from_json(&repo_issues_feature)?],
93- index: ByteSlice {
94- byte_start: title_starts_at,
95- byte_end: title_starts_at + title.len() as i64,
96- extra_data: Default::default(),
97- },
98- extra_data: Default::default(),
99- };
100-101- // Make a post
102- let post = Post {
103- created_at: Datetime::now(),
104- langs: Some(vec![Language::new("en")?]),
105- text: message.into(),
106- facets: Some(vec![repo_facet, issues_facet]),
107- embed: Default::default(),
108- entities: Default::default(),
109- labels: Default::default(),
110- reply: Default::default(),
111- tags: Default::default(),
112- extra_data: Default::default(),
113- };
114-115- let json = serde_json::to_value(post)?;
116- let data = Data::from_json(&json)?;
117-118- println!("\nposting...");
119- client
120- .send(CreateRecord::new()
121- .repo(identifier.clone())
122- .collection(Post::nsid())
123- .record(data)
124- .build())
125- .await?
126- .into_output()?;
127-128- Ok(())
129-}
130-131-fn event_to_create_label<T: for <'a> Deserialize<'a>>(event: JetstreamEvent) -> Result<(T, Cursor)> {
132- if event.kind != EventKind::Commit {
133- return Err("not a commit".into());
134- }
135- let commit = event.commit.ok_or("commit event missing commit data")?;
136- if commit.operation != CommitOp::Create {
137- return Err("not a create event".into());
138- }
139-140- let raw = commit.record.ok_or("commit missing record")?;
141-142- // todo: delete post if label is removed
143- // delete sample: at://did:plc:hdhoaan3xa3jiuq4fg4mefid/sh.tangled.label.op/3m2jvx4c6wf22
144- // tldr: has a "delete" array just like "add" on the same op collection
145- let t = serde_json::from_str(raw.get())?;
146- Ok((t, event.cursor))
147}
148149/// com.bad-example.identity.resolveMiniDoc bit we care about
···185}
186187/// get some atproto record content (from slingshot)
188-async fn get_record<T: for<'a> Deserialize<'a>>(client: &reqwest::Client, at_uri: &str) -> Result<T> {
000189 let mut url: Url = "https://slingshot.microcosm.blue".parse()?;
190 url.set_path("/xrpc/com.bad-example.repo.getUriRecord");
191 url.query_pairs_mut().append_pair("at_uri", at_uri);
···218 }
219}
220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000221#[tokio::main]
222async fn main() -> Result<()> {
223 env_logger::init();
···226 // Create HTTP client and session
227 let client = BasicClient::new(args.pds);
228 let bot_id = AtIdentifier::new(&args.identifier)?;
229- let session = Session::from(
230- client
231- .send(
232- CreateSession::new()
233- .identifier(&bot_id.to_string())
234- .password(args.app_password)
235- .build(),
236- )
237- .await?
238- .into_output()?,
239- );
240- println!("logged in as {} ({})", session.handle, session.did);
241 client.set_session(session).await?;
242243 let slingshot_client = reqwest::Client::builder()
···258 .connect_cursor(args.jetstream_cursor.map(Cursor::from_raw_u64))
259 .await?;
260261- println!("receiving jetstream messages...");
00000262 loop {
263- let Some(event) = receiver.recv().await else {
264- eprintln!("consumer: could not receive event, bailing");
265 break;
266 };
0267268- let Ok((CreateLabelRecord { add, subject }, cursor)) = event_to_create_label(event) else {
269- continue;
270- };
271-272- let mut added_good_first_issue = false;
273- for added in add {
274- if added.key == "at://did:plc:wshs7t2adsemcrrd4snkeqli/sh.tangled.label.definition/good-first-issue" {
275- println!("found a good first issue label!! {:?}", cursor);
276- added_good_first_issue = true;
277- break; // inner
278- }
279- eprintln!("found a label but it wasn't good-first-issue, ignoring...");
280- }
281- if !added_good_first_issue {
282- continue;
283- }
284-285- let IssueRecord { title, repo } = match get_record(&slingshot_client, &subject).await {
286- Ok(m) => m,
287 Err(e) => {
288- eprintln!("failed to get issue record: {e} for {subject}");
289 continue;
290 }
291 };
292293- let Ok(repo_uri) = AtUri::new(&repo) else {
294- eprintln!("failed to parse repo to aturi for {subject}");
295- continue;
296- };
297-298- let RepoRecord { name: repo_name } = match get_record(&slingshot_client, &repo).await {
299- Ok(m) => m,
300 Err(e) => {
301- eprintln!("failed to get repo record: {e} for {subject}");
302 continue;
303 }
304 };
305306- let nice_tangled_repo_id = match repo_uri.authority() {
307- AtIdentifier::Handle(h) => format!("@{h}"),
308- AtIdentifier::Did(did) => match get_handle(&slingshot_client, did.as_str()).await {
309- Err(e) => {
310- eprintln!("failed to get mini doc from repo identifier: {e} for {subject}");
311- continue;
312- }
313- Ok(None) => did.to_string(),
314- Ok(Some(h)) => format!("@{h}"),
315- }
316- };
317-318- let repo_full_name = format!("{nice_tangled_repo_id}/{repo_name}");
319- let repo_url = format!("https://tangled.org/{nice_tangled_repo_id}/{repo_name}");
320321- let issues_url = format!("https://tangled.org/{nice_tangled_repo_id}/{repo_name}/issues");
322323- if args.dry_run {
324- println!("--dry-run, but would have posted:");
325- println!("good-first-issue label added for {repo_full_name} ({repo_url}):");
326- println!("> {title} ({issues_url})\n");
327 continue;
328 }
329330- if let Err(e) = post(
331- &client,
332- &bot_id,
333- &repo_full_name,
334- &repo_url,
335- &title,
336- &issues_url,
337- ).await {
338- eprintln!("failed to post for {subject}: {e}");
339- };
340341- break;
000000342 }
343344 Ok(())
···1use clap::Parser;
0000002use jacquard::{
3 api::{
4 app_bsky::feed::post::Post,
5+ app_bsky::richtext::facet::{ByteSlice, Facet},
6+ com_atproto::repo::create_record::CreateRecord,
7 com_atproto::server::create_session::CreateSession,
08 },
9 client::{BasicClient, Session},
10 types::{
11+ collection::Collection, datetime::Datetime, ident::AtIdentifier, language::Language,
12+ string::AtUri, value::Data,
000013 },
14};
15+use jetstream::{
16+ JetstreamCompression, JetstreamConfig, JetstreamConnector,
17+ events::{CommitOp, Cursor, EventKind, JetstreamEvent},
18+ exports::Nsid,
19+};
20+use url::Url;
2122+use serde::Deserialize;
23use std::time::Duration;
02425type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
26···50 /// don't actually post
51 #[arg(long, action)]
52 dry_run: bool,
53+ /// send a checkin to this url every 5 mins
54+ #[arg(long)]
55+ healthcheck_ping: Option<Url>,
56}
5758+struct IssueDetails {
59+ repo_full_name: String,
60+ repo_url: String,
61+ title: String,
62+ issues_url: String,
0000000000000000000000000000000000000000000000000000000000000000000000000000000000063}
6465/// com.bad-example.identity.resolveMiniDoc bit we care about
···101}
102103/// get some atproto record content (from slingshot)
104+async fn get_record<T: for<'a> Deserialize<'a>>(
105+ client: &reqwest::Client,
106+ at_uri: &str,
107+) -> Result<T> {
108 let mut url: Url = "https://slingshot.microcosm.blue".parse()?;
109 url.set_path("/xrpc/com.bad-example.repo.getUriRecord");
110 url.query_pairs_mut().append_pair("at_uri", at_uri);
···137 }
138}
139140+fn event_to_create_label<T: for<'a> Deserialize<'a>>(event: JetstreamEvent) -> Result<T> {
141+ if event.kind != EventKind::Commit {
142+ return Err("not a commit".into());
143+ }
144+ let commit = event.commit.ok_or("commit event missing commit data")?;
145+ if commit.operation != CommitOp::Create {
146+ return Err("not a create event".into());
147+ }
148+149+ let raw = commit.record.ok_or("commit missing record")?;
150+151+ // todo: delete post if label is removed
152+ // delete sample: at://did:plc:hdhoaan3xa3jiuq4fg4mefid/sh.tangled.label.op/3m2jvx4c6wf22
153+ // tldr: has a "delete" array just like "add" on the same op collection
154+ Ok(serde_json::from_str(raw.get())?)
155+}
156+157+async fn extract_issue_info(
158+ client: &reqwest::Client,
159+ adds: Vec<AddLabel>,
160+ subject: String,
161+) -> Result<IssueDetails> {
162+ let mut added_good_first_issue = false;
163+ for added in adds {
164+ if added.key
165+ == "at://did:plc:wshs7t2adsemcrrd4snkeqli/sh.tangled.label.definition/good-first-issue"
166+ {
167+ log::info!("found a good first issue label!!");
168+ added_good_first_issue = true;
169+ break; // inner
170+ }
171+ log::debug!("found a label but it wasn't good-first-issue, ignoring...");
172+ }
173+ if !added_good_first_issue {
174+ return Err("good-first-issue label not found in added labels".into());
175+ }
176+177+ let IssueRecord { title, repo } = match get_record(client, &subject).await {
178+ Ok(m) => m,
179+ Err(e) => return Err(format!("failed to get issue record: {e} for {subject}").into()),
180+ };
181+182+ let Ok(repo_uri) = AtUri::new(&repo) else {
183+ return Err("failed to parse repo to aturi for {subject}".into());
184+ };
185+186+ let RepoRecord { name: repo_name } = match get_record(client, &repo).await {
187+ Ok(m) => m,
188+ Err(e) => return Err(format!("failed to get repo record: {e} for {subject}").into()),
189+ };
190+191+ let nice_tangled_repo_id = match repo_uri.authority() {
192+ AtIdentifier::Handle(h) => format!("@{h}"),
193+ AtIdentifier::Did(did) => match get_handle(client, did.as_str()).await {
194+ Err(e) => {
195+ return Err(format!(
196+ "failed to get mini doc from repo identifier: {e} for {subject}"
197+ )
198+ .into());
199+ }
200+ Ok(None) => did.to_string(),
201+ Ok(Some(h)) => format!("@{h}"),
202+ },
203+ };
204+205+ let repo_full_name = format!("{nice_tangled_repo_id}/{repo_name}");
206+ let repo_url = format!("https://tangled.org/{nice_tangled_repo_id}/{repo_name}");
207+208+ let issues_url = format!("https://tangled.org/{nice_tangled_repo_id}/{repo_name}/issues");
209+210+ Ok(IssueDetails {
211+ repo_full_name,
212+ repo_url,
213+ title,
214+ issues_url,
215+ })
216+}
217+218+async fn post(
219+ client: &BasicClient,
220+ identifier: &AtIdentifier<'_>,
221+ IssueDetails {
222+ repo_full_name,
223+ repo_url,
224+ title,
225+ issues_url,
226+ }: &IssueDetails,
227+) -> Result<()> {
228+ let message = format!(
229+ r#"New from {repo_full_name}:
230+231+> {title}"#
232+ );
233+234+ let pre_len = 9;
235+236+ let repo_feature = serde_json::json!({
237+ "$type": "app.bsky.richtext.facet#link",
238+ "uri": repo_url,
239+ });
240+ let repo_facet = Facet {
241+ features: vec![Data::from_json(&repo_feature)?],
242+ index: ByteSlice {
243+ byte_start: pre_len,
244+ byte_end: pre_len + repo_full_name.len() as i64,
245+ extra_data: Default::default(),
246+ },
247+ extra_data: Default::default(),
248+ };
249+250+ let title_starts_at = pre_len + (repo_full_name.len() + 5) as i64;
251+252+ let repo_issues_feature = serde_json::json!({
253+ "$type": "app.bsky.richtext.facet#link",
254+ "uri": issues_url,
255+ });
256+ let issues_facet = Facet {
257+ features: vec![Data::from_json(&repo_issues_feature)?],
258+ index: ByteSlice {
259+ byte_start: title_starts_at,
260+ byte_end: title_starts_at + title.len() as i64,
261+ extra_data: Default::default(),
262+ },
263+ extra_data: Default::default(),
264+ };
265+266+ // Make a post
267+ let post = Post {
268+ created_at: Datetime::now(),
269+ langs: Some(vec![Language::new("en")?]),
270+ text: message.into(),
271+ facets: Some(vec![repo_facet, issues_facet]),
272+ embed: Default::default(),
273+ entities: Default::default(),
274+ labels: Default::default(),
275+ reply: Default::default(),
276+ tags: Default::default(),
277+ extra_data: Default::default(),
278+ };
279+280+ let json = serde_json::to_value(post)?;
281+ let data = Data::from_json(&json)?;
282+283+ log::info!("\nposting...");
284+ client
285+ .send(
286+ CreateRecord::new()
287+ .repo(identifier.clone())
288+ .collection(Post::nsid())
289+ .record(data)
290+ .build(),
291+ )
292+ .await?
293+ .into_output()?;
294+295+ Ok(())
296+}
297+298+async fn hc_ping(url: Url, client: reqwest::Client) {
299+ let mut interval = tokio::time::interval(Duration::from_secs(5 * 60));
300+ loop {
301+ interval.tick().await;
302+ log::trace!("sending healthcheck ping...");
303+ if let Err(e) = client
304+ .get(url.clone())
305+ .send()
306+ .await
307+ .and_then(reqwest::Response::error_for_status)
308+ {
309+ log::warn!("error sending healthcheck ping: {e}");
310+ }
311+ }
312+}
313+314#[tokio::main]
315async fn main() -> Result<()> {
316 env_logger::init();
···319 // Create HTTP client and session
320 let client = BasicClient::new(args.pds);
321 let bot_id = AtIdentifier::new(&args.identifier)?;
322+ let create_session = CreateSession::new()
323+ .identifier(bot_id.to_string())
324+ .password(&args.app_password)
325+ .build();
326+ let session = Session::from(client.send(create_session.clone()).await?.into_output()?);
327+ log::debug!("logged in as {} ({})", session.handle, session.did);
000000328 client.set_session(session).await?;
329330 let slingshot_client = reqwest::Client::builder()
···345 .connect_cursor(args.jetstream_cursor.map(Cursor::from_raw_u64))
346 .await?;
347348+ if let Some(hc) = args.healthcheck_ping {
349+ log::info!("starting healthcheck ping task...");
350+ tokio::task::spawn(hc_ping(hc.clone(), slingshot_client.clone()));
351+ }
352+353+ log::info!("receiving jetstream messages...");
354 loop {
355+ let Some(event) = receiver.recv().await else {
356+ log::error!("consumer: could not receive event, bailing");
357 break;
358 };
359+ let cursor = event.cursor;
360361+ let CreateLabelRecord { add: adds, subject } = match event_to_create_label(event) {
362+ Ok(clr) => clr,
00000000000000000363 Err(e) => {
364+ log::debug!("ignoring unparseable event (at {cursor:?}): {e}");
365 continue;
366 }
367 };
368369+ let issue_details = match extract_issue_info(&slingshot_client, adds, subject.clone()).await
370+ {
371+ Ok(deets) => deets,
0000372 Err(e) => {
373+ log::warn!("failed to extract issue details (at {cursor:?}): {e}");
374 continue;
375 }
376 };
377378+ if args.dry_run {
379+ let IssueDetails {
380+ repo_full_name,
381+ repo_url,
382+ title,
383+ issues_url,
384+ } = issue_details;
385+ log::info!(
386+ r#"--dry-run, but would have posted:
00000387388+good-first-issue label added for {repo_full_name} ({repo_url}):
389390+> {title} ({issues_url})"#
391+ );
00392 continue;
393 }
394395+ if let Err(e) = post(&client, &bot_id, &issue_details).await {
396+ log::warn!("failed to post for {subject}: {e}, refreshing session for one retry...");
397+ let session = Session::from(client.send(create_session.clone()).await?.into_output()?);
398+ log::debug!("logged in as {} ({})", session.handle, session.did);
399+ client.set_session(session).await?;
00000400401+ if let Err(e) = post(&client, &bot_id, &issue_details).await {
402+ log::error!(
403+ "failed to post after a session refresh: {e:?}, something is wrong. bye."
404+ );
405+ break;
406+ }
407+ };
408 }
409410 Ok(())