Skip to content

Commit 4cfdc8f

Browse files
authored
Merge branch 'main' into feature/pubsub-macro
2 parents 075526b + 90f07be commit 4cfdc8f

File tree

8 files changed

+225
-0
lines changed

8 files changed

+225
-0
lines changed

.github/workflows/dapr-bot-test.yml

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: dapr-bot-test
2+
3+
on:
4+
push:
5+
paths: # Explicitly declare which paths (could potentially be combined into dapr-bot*
6+
- ".github/workflows/dapr-bot.yml"
7+
- ".github/workflows/dapr-bot-test.yml"
8+
- ".github/workflows/dapr-bot/*"
9+
pull_request:
10+
branches:
11+
- main
12+
paths: # Explicitly declare which paths (could potentially be combined into dapr-bot*
13+
- ".github/workflows/dapr-bot.yml"
14+
- ".github/workflows/dapr-bot-test.yml"
15+
- ".github/workflows/dapr-bot/*"
16+
17+
env:
18+
CARGO_TERM_COLOR: always
19+
20+
jobs:
21+
22+
test:
23+
runs-on: ubuntu-latest
24+
defaults:
25+
run:
26+
working-directory: ./.github/workflows/dapr-bot
27+
steps:
28+
- uses: actions/checkout@v4
29+
30+
- uses: dtolnay/rust-toolchain@stable
31+
32+
- uses: swatinem/rust-cache@v2
33+
- name: Cargo clippy
34+
run: |
35+
cargo clippy -- -W warnings
36+
37+
- name: Cargo fmt
38+
run: |
39+
cargo fmt -- --check --color ${{ env.CARGO_TERM_COLOR }}
40+
41+
- name: Cargo test
42+
run: |
43+
cargo test
44+
45+
- name: Cargo build
46+
run: |
47+
cargo build

.github/workflows/dapr-bot.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: dapr-bot
2+
on:
3+
issue_comment:
4+
types: [created]
5+
env:
6+
CARGO_TERM_COLOR: always
7+
jobs:
8+
run:
9+
runs-on: ubuntu-latest
10+
defaults:
11+
run:
12+
working-directory: ./.github/workflows/dapr-bot
13+
steps:
14+
- uses: actions/checkout@v4
15+
- uses: dtolnay/rust-toolchain@stable
16+
- uses: swatinem/rust-cache@v2
17+
- name: Cargo run
18+
env:
19+
GITHUB_TOKEN: ${{ github.token }}
20+
run: |
21+
cargo run

.github/workflows/dapr-bot/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "dapr-bot"
3+
authors = ["[email protected]"]
4+
license-file = "LICENSE"
5+
version = "0.1.0"
6+
edition = "2021"
7+
8+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9+
10+
[dependencies]
11+
exitcode = "1.1.2"
12+
octocrab = "0.34.1"
13+
serde = { version = "1.0.197", features = ["derive"] }
14+
serde_json = "1.0.114"
15+
tokio = { version = "1.36.0", features = ["full"] }

.github/workflows/dapr-bot/LICENSE

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../LICENSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use super::GitHub;
2+
3+
impl GitHub {
4+
pub async fn create_comment(
5+
&self,
6+
owner: &str,
7+
repo: &str,
8+
number: u64,
9+
comment: String,
10+
) -> std::result::Result<octocrab::models::issues::Comment, octocrab::Error> {
11+
self.client
12+
.issues(owner, repo)
13+
.create_comment(number, comment)
14+
.await
15+
}
16+
pub async fn add_assignee(
17+
&self,
18+
owner: &str,
19+
repo: &str,
20+
number: u64,
21+
assignee: String,
22+
) -> std::result::Result<octocrab::models::issues::Issue, octocrab::Error> {
23+
self.client
24+
.issues(owner, repo)
25+
.add_assignees(number, &[&assignee])
26+
.await
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use octocrab::Octocrab;
2+
3+
pub mod issues;
4+
5+
pub struct GitHub {
6+
client: Octocrab,
7+
}
8+
9+
impl GitHub {
10+
pub fn new_client(token: String) -> GitHub {
11+
match Octocrab::builder().personal_token(token).build() {
12+
Ok(client) => GitHub { client },
13+
Err(e) => {
14+
panic!("Unable to create client: {:?}", e)
15+
}
16+
}
17+
}
18+
}
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

CONTRIBUTING.md

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ This section describes the guidelines for contributing code / docs to Dapr.
5454
All contributions come through pull requests. To submit a proposed change, we recommend following this workflow:
5555

5656
1. Make sure there's an issue (bug or proposal) raised, which sets the expectations for the contribution you are about to make.
57+
- Assign yourself to the issue by commenting with `/assign`
5758
1. Fork the relevant repo and create a new branch
5859
1. Create your change
5960
- Code changes require tests

0 commit comments

Comments
 (0)