-
Notifications
You must be signed in to change notification settings - Fork 1
Refactor and check #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3a9a985
Add checks workflow
IvanS1991 5d4097c
Merge pull request #2 from IvanS1991/Add_checks_workflow
IvanS1991 fee5673
Reorganize main.rs into data/models modules
IvanS1991 3183c18
Add some unit tests for models
IvanS1991 d6e0f48
Fix checks.yml
IvanS1991 e5c1832
Merge pull request #3 from IvanS1991/Refactor_project
IvanS1991 13357ad
Remove obsolete test mod file, run cargo fmt
IvanS1991 caae978
Merge pull request #4 from IvanS1991/Run-format-and-remove-obsolete-f…
IvanS1991 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 hidden or 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,27 @@ | ||
| name: Checks | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| test: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| strategy: | ||
| matrix: | ||
| target: [x86_64-unknown-linux-gnu] | ||
|
|
||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v3 | ||
|
|
||
| - name: Install Rust | ||
| uses: dtolnay/rust-toolchain@stable | ||
| with: | ||
| targets: ${{ matrix.target }} | ||
|
|
||
| - name: Run tests | ||
| uses: actions-rs/cargo@v1 | ||
| with: | ||
| command: test | ||
| args: --all --verbose | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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,168 @@ | ||
| use crate::models::github::Response; | ||
| use futures::future::join_all; | ||
| use octocrab::params::State; | ||
| use octocrab::{GitHubError, Octocrab, Result}; | ||
| use std::error::Error; | ||
|
|
||
| pub struct GitHub<'a> { | ||
| pub octocrab: &'a Octocrab, | ||
| } | ||
|
|
||
| impl<'a> GitHub<'a> { | ||
| pub async fn get_repos_with_changes( | ||
| &self, | ||
| owner: &String, | ||
| repositories: &Vec<String>, | ||
| base: &String, | ||
| head: &String, | ||
| ) -> Result<Vec<String>, String> { | ||
| let mut responses = vec![]; | ||
|
|
||
| for repo in repositories { | ||
| let res = self.octocrab.get( | ||
| format!("/repos/{owner}/{repo}/compare/{base}...{head}"), | ||
| None::<&()>, | ||
| ); | ||
|
|
||
| responses.push(res); | ||
| } | ||
|
|
||
| let responses: Vec<octocrab::Result<Response>> = join_all(responses).await; | ||
| let mut repos_with_changes = vec![]; | ||
|
|
||
| for (index, res) in responses.iter().enumerate() { | ||
| let repo = repositories.get(index).expect("Failed to get repo"); | ||
|
|
||
| match res { | ||
| Ok(res) => { | ||
| if res.total_commits > 0 { | ||
| repos_with_changes.push(repo.clone()); | ||
| } else { | ||
| println!("{repo}'s {head} and {base} branches are in sync, skipping."); | ||
| } | ||
| } | ||
| Err(e) => { | ||
| let err = &self.generate_error( | ||
| format!("Error comparing {repo}'s {head} and {base} branches"), | ||
| e, | ||
| ); | ||
|
|
||
| if let Err(err) = err { | ||
| println!("{err}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(repos_with_changes) | ||
| } | ||
|
|
||
| pub async fn create_pr( | ||
| &self, | ||
| owner: &String, | ||
| repo: String, | ||
| base: &String, | ||
| base_name: &Option<String>, | ||
| head: &String, | ||
| reviewers: &[String], | ||
| dry_run: bool, | ||
| ) -> Result<(), String> { | ||
| let res = &self | ||
| .octocrab | ||
| .pulls(owner, &repo) | ||
| .list() | ||
| .state(State::Open) | ||
| .head(head) | ||
| .base(base) | ||
| .send() | ||
| .await; | ||
|
|
||
| match res { | ||
| Ok(pr) => { | ||
| if !pr.items.is_empty() { | ||
| pr.items.iter().for_each(|item| { | ||
| println!("{repo} already has an opened Pull request: https://github.com/{owner}/{repo}/pull/{}", item.number); | ||
| }); | ||
| return Ok(()); | ||
| } | ||
|
|
||
| println!("Creating Pull request for {repo}..."); | ||
|
|
||
| if dry_run { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let date = chrono::Local::now().format("%Y-%m-%d").to_string(); | ||
|
|
||
| let name = match base_name { | ||
| None => base, | ||
| Some(name) => name, | ||
| }; | ||
|
|
||
| let res = &self | ||
| .octocrab | ||
| .pulls(owner, &repo) | ||
| .create(format!("Release to {name} {date}"), head, base) | ||
| .send() | ||
| .await; | ||
|
|
||
| match res { | ||
| Ok(pr) => { | ||
| println!( | ||
| "Pull request created in {repo}: https://github.com/{owner}/{repo}/pull/{}", | ||
| pr.number | ||
| ); | ||
|
|
||
| self.request_review(owner, &repo, reviewers, pr.number) | ||
| .await | ||
| } | ||
| Err(e) => { | ||
| self.generate_error(format!("Failed creating a Pull Request in {repo}"), &e) | ||
| } | ||
| } | ||
| } | ||
| Err(e) => self.generate_error(format!("Failed to fetch {repo}'s pull requests."), &e), | ||
| } | ||
| } | ||
|
|
||
| pub async fn request_review( | ||
| &self, | ||
| owner: &String, | ||
| repo: &str, | ||
| reviewers: &[String], | ||
| pr_id: u64, | ||
| ) -> Result<(), String> { | ||
| let res = &self | ||
| .octocrab | ||
| .pulls(owner, repo) | ||
| .request_reviews(pr_id, reviewers, []) | ||
| .await; | ||
|
|
||
| match res { | ||
| Ok(_res) => { | ||
| println!( | ||
| "Review requested from {} in {repo}'s Pull Request", | ||
| reviewers.join(", ") | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
| Err(e) => self.generate_error( | ||
| format!("Failed to request review in {repo}'s Pull Request"), | ||
| &e, | ||
| ), | ||
| } | ||
| } | ||
|
|
||
| pub fn generate_error<T: Error>(&self, message: String, e: &T) -> Result<(), String> { | ||
| if let Some(s) = e.source() { | ||
| let err = s | ||
| .downcast_ref::<GitHubError>() | ||
| .expect("Failed to extract source error"); | ||
|
|
||
| return Err(format!("{message}: {}", err.message)); | ||
| } | ||
|
|
||
| Err(message) | ||
| } | ||
| } |
This file contains hidden or 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 @@ | ||
| pub mod github; |
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.
Uh oh!
There was an error while loading. Please reload this page.