announcing good-first-issue tags added on @tangled.sh (not affiliated with tangled!)

Compare changes

Choose any two refs to compare.

+1
Cargo.lock
··· 999 999 "env_logger", 1000 1000 "jacquard", 1001 1001 "jetstream", 1002 + "log", 1002 1003 "reqwest", 1003 1004 "serde", 1004 1005 "serde_json",
+1
Cargo.toml
··· 8 8 env_logger = "0.11.8" 9 9 jacquard = "0.2.1" 10 10 jetstream = { path = "../links/jetstream" } 11 + log = "0.4.28" 11 12 reqwest = { version = "0.12.23", features = ["json"] } 12 13 serde = { version = "1.0.228", features = ["derive"] } 13 14 serde_json = "1.0.145"
docs/october-dolly.png

This is a binary file and will not be displayed.

+7
license
··· 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
··· 1 + # ๐ŸŽƒ Happy hacktober! ๐Ÿง™๐Ÿผโ€โ™€๏ธ 2 + 3 + ![tangle's dolly as a witchy pumpkin](./docs/october-dolly.png) 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
··· 1 1 use clap::Parser; 2 - use url::Url; 3 - use jetstream::{ 4 - JetstreamCompression, JetstreamConfig, JetstreamConnector, 5 - events::{CommitOp, Cursor, EventKind, JetstreamEvent}, 6 - exports::Nsid, 7 - }; 8 2 use jacquard::{ 9 3 api::{ 10 4 app_bsky::feed::post::Post, 11 - app_bsky::richtext::facet::{Facet, ByteSlice}, 5 + app_bsky::richtext::facet::{ByteSlice, Facet}, 6 + com_atproto::repo::create_record::CreateRecord, 12 7 com_atproto::server::create_session::CreateSession, 13 - com_atproto::repo::create_record::CreateRecord, 14 8 }, 15 9 client::{BasicClient, Session}, 16 10 types::{ 17 - datetime::Datetime, 18 - ident::AtIdentifier, 19 - language::Language, 20 - collection::Collection, 21 - value::Data, 22 - string::AtUri, 11 + collection::Collection, datetime::Datetime, ident::AtIdentifier, language::Language, 12 + string::AtUri, value::Data, 23 13 }, 24 14 }; 15 + use jetstream::{ 16 + JetstreamCompression, JetstreamConfig, JetstreamConnector, 17 + events::{CommitOp, Cursor, EventKind, JetstreamEvent}, 18 + exports::Nsid, 19 + }; 20 + use url::Url; 25 21 22 + use serde::Deserialize; 26 23 use std::time::Duration; 27 - use serde::Deserialize; 28 24 29 25 type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>; 30 26 ··· 54 50 /// don't actually post 55 51 #[arg(long, action)] 56 52 dry_run: bool, 53 + /// send a checkin to this url every 5 mins 54 + #[arg(long)] 55 + healthcheck_ping: Option<Url>, 57 56 } 58 57 59 - 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)) 58 + struct IssueDetails { 59 + repo_full_name: String, 60 + repo_url: String, 61 + title: String, 62 + issues_url: String, 147 63 } 148 64 149 65 /// com.bad-example.identity.resolveMiniDoc bit we care about ··· 185 101 } 186 102 187 103 /// 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> { 104 + async fn get_record<T: for<'a> Deserialize<'a>>( 105 + client: &reqwest::Client, 106 + at_uri: &str, 107 + ) -> Result<T> { 189 108 let mut url: Url = "https://slingshot.microcosm.blue".parse()?; 190 109 url.set_path("/xrpc/com.bad-example.repo.getUriRecord"); 191 110 url.query_pairs_mut().append_pair("at_uri", at_uri); ··· 218 137 } 219 138 } 220 139 140 + 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 + 221 314 #[tokio::main] 222 315 async fn main() -> Result<()> { 223 316 env_logger::init(); ··· 226 319 // Create HTTP client and session 227 320 let client = BasicClient::new(args.pds); 228 321 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); 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); 241 328 client.set_session(session).await?; 242 329 243 330 let slingshot_client = reqwest::Client::builder() ··· 258 345 .connect_cursor(args.jetstream_cursor.map(Cursor::from_raw_u64)) 259 346 .await?; 260 347 261 - println!("receiving jetstream messages..."); 348 + 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..."); 262 354 loop { 263 - let Some(event) = receiver.recv().await else { 264 - eprintln!("consumer: could not receive event, bailing"); 355 + let Some(event) = receiver.recv().await else { 356 + log::error!("consumer: could not receive event, bailing"); 265 357 break; 266 358 }; 359 + let cursor = event.cursor; 267 360 268 - 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, 361 + let CreateLabelRecord { add: adds, subject } = match event_to_create_label(event) { 362 + Ok(clr) => clr, 287 363 Err(e) => { 288 - eprintln!("failed to get issue record: {e} for {subject}"); 364 + log::debug!("ignoring unparseable event (at {cursor:?}): {e}"); 289 365 continue; 290 366 } 291 367 }; 292 368 293 - 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, 369 + let issue_details = match extract_issue_info(&slingshot_client, adds, subject.clone()).await 370 + { 371 + Ok(deets) => deets, 300 372 Err(e) => { 301 - eprintln!("failed to get repo record: {e} for {subject}"); 373 + log::warn!("failed to extract issue details (at {cursor:?}): {e}"); 302 374 continue; 303 375 } 304 376 }; 305 377 306 - 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}"); 378 + 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: 320 387 321 - let issues_url = format!("https://tangled.org/{nice_tangled_repo_id}/{repo_name}/issues"); 388 + good-first-issue label added for {repo_full_name} ({repo_url}): 322 389 323 - 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"); 390 + > {title} ({issues_url})"# 391 + ); 327 392 continue; 328 393 } 329 394 330 - 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 - }; 395 + 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?; 340 400 341 - break; 401 + 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 + }; 342 408 } 343 409 344 410 Ok(())