-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tooling: add dapr-bot - assign issue functionality #131
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0225e3e
tooling: add dapr-bot - assign issue functionality
mikeee e961a4c
add: test step to dapr-bot-test workflow
mikeee 1d69f23
fix: add working dir to bot run and test workflows
mikeee ba3e413
fix: change repo owner and name
mikeee 14f2cb2
docs: add issue assign step to docs
mikeee fe94932
Merge branch 'main' into dapr-bot
mikeee 895a483
chore: rename master to main
mikeee a1a55bb
add: LICENSE symlink
mikeee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: | ||
- master | ||
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,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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rename to main