|
| 1 | +mod github; |
| 2 | + |
| 3 | +use core::panic; |
| 4 | +use std::{error::Error, fs::File, io::BufReader, path::Path, process::exit}; |
| 5 | + |
| 6 | +use octocrab::models; |
| 7 | + |
| 8 | +use github::GitHub; |
| 9 | + |
| 10 | +// Defining the repo explicitly as the octocrab model for the event doesn't deserialize a |
| 11 | +// owner/repo. |
| 12 | +const OWNER: &str = "dapr"; |
| 13 | +const REPOSITORY: &str = "rust-sdk"; |
| 14 | + |
| 15 | +const GITHUB_TOKEN: &str = "GITHUB_TOKEN"; |
| 16 | + |
| 17 | +const GITHUB_EVENT_PATH: &str = "GITHUB_EVENT_PATH"; |
| 18 | +const GITHUB_EVENT_NAME: &str = "GITHUB_EVENT_NAME"; |
| 19 | + |
| 20 | +const ISSUE_COMMENT_EVENT_NAME: &str = "issue_comment"; |
| 21 | + |
| 22 | +fn get_payload<P: AsRef<Path>>( |
| 23 | + path: P, |
| 24 | +) -> Result<models::events::payload::IssueCommentEventPayload, Box<dyn Error>> { |
| 25 | + // Open the file in read-only mode with buffer. |
| 26 | + let file = File::open(path)?; |
| 27 | + let reader = BufReader::new(file); |
| 28 | + |
| 29 | + // Read the JSON contents of the file as an instance. |
| 30 | + let event = serde_json::from_reader(reader)?; |
| 31 | + |
| 32 | + // Return the event. |
| 33 | + Ok(event) |
| 34 | +} |
| 35 | + |
| 36 | +#[tokio::main] |
| 37 | +async fn main() -> octocrab::Result<()> { |
| 38 | + let github_event_path: String = |
| 39 | + std::env::var(GITHUB_EVENT_PATH).expect("GITHUB_EVENT_PATH env missing"); |
| 40 | + let github_event_name: String = |
| 41 | + std::env::var(GITHUB_EVENT_NAME).expect("GITHUB_EVENT_NAME env missing"); |
| 42 | + let github_token: String = std::env::var(GITHUB_TOKEN).expect("GITHUB_TOKEN env missing"); |
| 43 | + |
| 44 | + if github_event_name != ISSUE_COMMENT_EVENT_NAME { |
| 45 | + println!("Event is not an issue_comment, the app will now exit."); |
| 46 | + exit(exitcode::TEMPFAIL); |
| 47 | + } |
| 48 | + |
| 49 | + // deserialize event payload |
| 50 | + let event = get_payload(github_event_path).unwrap(); |
| 51 | + |
| 52 | + // check the issue body |
| 53 | + if !event.clone().comment.body.unwrap().starts_with("/assign") { |
| 54 | + println!("Event does not start with /assign"); |
| 55 | + exit(exitcode::TEMPFAIL); |
| 56 | + } |
| 57 | + |
| 58 | + let assignee: String = event.comment.user.login; |
| 59 | + |
| 60 | + let issue: u64 = event.issue.number; |
| 61 | + |
| 62 | + // spawn a client |
| 63 | + let github_client = GitHub::new_client(github_token); |
| 64 | + |
| 65 | + // assign the user |
| 66 | + match github_client |
| 67 | + .add_assignee(OWNER, REPOSITORY, issue, assignee.clone()) |
| 68 | + .await |
| 69 | + { |
| 70 | + Ok(_) => { |
| 71 | + match github_client |
| 72 | + .create_comment( |
| 73 | + OWNER, |
| 74 | + REPOSITORY, |
| 75 | + issue, |
| 76 | + format!("🚀 issue assigned to you {assignee}"), |
| 77 | + ) |
| 78 | + .await |
| 79 | + { |
| 80 | + Ok(_) => { |
| 81 | + println!("Comment on assign successful") |
| 82 | + } |
| 83 | + Err(e) => { |
| 84 | + panic!("Comment on assign failed: {:?}", e) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + Err(e) => { |
| 89 | + panic!("Failed to assign issue: {:?}", e) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Ok(()) |
| 94 | +} |
0 commit comments