forked from dapr/rust-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/pubsub-macro
- Loading branch information
Showing
8 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
name: dapr-bot-test | ||
|
||
on: | ||
push: | ||
paths: # Explicitly declare which paths (could potentially be combined into dapr-bot* | ||
- ".github/workflows/dapr-bot.yml" | ||
- ".github/workflows/dapr-bot-test.yml" | ||
- ".github/workflows/dapr-bot/*" | ||
pull_request: | ||
branches: | ||
- main | ||
paths: # Explicitly declare which paths (could potentially be combined into dapr-bot* | ||
- ".github/workflows/dapr-bot.yml" | ||
- ".github/workflows/dapr-bot-test.yml" | ||
- ".github/workflows/dapr-bot/*" | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
|
||
test: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ./.github/workflows/dapr-bot | ||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: dtolnay/rust-toolchain@stable | ||
|
||
- uses: swatinem/rust-cache@v2 | ||
- name: Cargo clippy | ||
run: | | ||
cargo clippy -- -W warnings | ||
- name: Cargo fmt | ||
run: | | ||
cargo fmt -- --check --color ${{ env.CARGO_TERM_COLOR }} | ||
- name: Cargo test | ||
run: | | ||
cargo test | ||
- name: Cargo build | ||
run: | | ||
cargo build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: dapr-bot | ||
on: | ||
issue_comment: | ||
types: [created] | ||
env: | ||
CARGO_TERM_COLOR: always | ||
jobs: | ||
run: | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ./.github/workflows/dapr-bot | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: dtolnay/rust-toolchain@stable | ||
- uses: swatinem/rust-cache@v2 | ||
- name: Cargo run | ||
env: | ||
GITHUB_TOKEN: ${{ github.token }} | ||
run: | | ||
cargo run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "dapr-bot" | ||
authors = ["[email protected]"] | ||
license-file = "LICENSE" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
exitcode = "1.1.2" | ||
octocrab = "0.34.1" | ||
serde = { version = "1.0.197", features = ["derive"] } | ||
serde_json = "1.0.114" | ||
tokio = { version = "1.36.0", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use super::GitHub; | ||
|
||
impl GitHub { | ||
pub async fn create_comment( | ||
&self, | ||
owner: &str, | ||
repo: &str, | ||
number: u64, | ||
comment: String, | ||
) -> std::result::Result<octocrab::models::issues::Comment, octocrab::Error> { | ||
self.client | ||
.issues(owner, repo) | ||
.create_comment(number, comment) | ||
.await | ||
} | ||
pub async fn add_assignee( | ||
&self, | ||
owner: &str, | ||
repo: &str, | ||
number: u64, | ||
assignee: String, | ||
) -> std::result::Result<octocrab::models::issues::Issue, octocrab::Error> { | ||
self.client | ||
.issues(owner, repo) | ||
.add_assignees(number, &[&assignee]) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use octocrab::Octocrab; | ||
|
||
pub mod issues; | ||
|
||
pub struct GitHub { | ||
client: Octocrab, | ||
} | ||
|
||
impl GitHub { | ||
pub fn new_client(token: String) -> GitHub { | ||
match Octocrab::builder().personal_token(token).build() { | ||
Ok(client) => GitHub { client }, | ||
Err(e) => { | ||
panic!("Unable to create client: {:?}", e) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
mod github; | ||
|
||
use core::panic; | ||
use std::{error::Error, fs::File, io::BufReader, path::Path, process::exit}; | ||
|
||
use octocrab::models; | ||
|
||
use github::GitHub; | ||
|
||
// Defining the repo explicitly as the octocrab model for the event doesn't deserialize a | ||
// owner/repo. | ||
const OWNER: &str = "dapr"; | ||
const REPOSITORY: &str = "rust-sdk"; | ||
|
||
const GITHUB_TOKEN: &str = "GITHUB_TOKEN"; | ||
|
||
const GITHUB_EVENT_PATH: &str = "GITHUB_EVENT_PATH"; | ||
const GITHUB_EVENT_NAME: &str = "GITHUB_EVENT_NAME"; | ||
|
||
const ISSUE_COMMENT_EVENT_NAME: &str = "issue_comment"; | ||
|
||
fn get_payload<P: AsRef<Path>>( | ||
path: P, | ||
) -> Result<models::events::payload::IssueCommentEventPayload, Box<dyn Error>> { | ||
// Open the file in read-only mode with buffer. | ||
let file = File::open(path)?; | ||
let reader = BufReader::new(file); | ||
|
||
// Read the JSON contents of the file as an instance. | ||
let event = serde_json::from_reader(reader)?; | ||
|
||
// Return the event. | ||
Ok(event) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> octocrab::Result<()> { | ||
let github_event_path: String = | ||
std::env::var(GITHUB_EVENT_PATH).expect("GITHUB_EVENT_PATH env missing"); | ||
let github_event_name: String = | ||
std::env::var(GITHUB_EVENT_NAME).expect("GITHUB_EVENT_NAME env missing"); | ||
let github_token: String = std::env::var(GITHUB_TOKEN).expect("GITHUB_TOKEN env missing"); | ||
|
||
if github_event_name != ISSUE_COMMENT_EVENT_NAME { | ||
println!("Event is not an issue_comment, the app will now exit."); | ||
exit(exitcode::TEMPFAIL); | ||
} | ||
|
||
// deserialize event payload | ||
let event = get_payload(github_event_path).unwrap(); | ||
|
||
// check the issue body | ||
if !event.clone().comment.body.unwrap().starts_with("/assign") { | ||
println!("Event does not start with /assign"); | ||
exit(exitcode::TEMPFAIL); | ||
} | ||
|
||
let assignee: String = event.comment.user.login; | ||
|
||
let issue: u64 = event.issue.number; | ||
|
||
// spawn a client | ||
let github_client = GitHub::new_client(github_token); | ||
|
||
// assign the user | ||
match github_client | ||
.add_assignee(OWNER, REPOSITORY, issue, assignee.clone()) | ||
.await | ||
{ | ||
Ok(_) => { | ||
match github_client | ||
.create_comment( | ||
OWNER, | ||
REPOSITORY, | ||
issue, | ||
format!("🚀 issue assigned to you {assignee}"), | ||
) | ||
.await | ||
{ | ||
Ok(_) => { | ||
println!("Comment on assign successful") | ||
} | ||
Err(e) => { | ||
panic!("Comment on assign failed: {:?}", e) | ||
} | ||
} | ||
} | ||
Err(e) => { | ||
panic!("Failed to assign issue: {:?}", e) | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters