Skip to content

Commit 6c79b8d

Browse files
committed
feat: now fetching installation id from the db and using it to create the GHwrapper
1 parent 0f5468b commit 6c79b8d

File tree

4 files changed

+52
-35
lines changed

4 files changed

+52
-35
lines changed

fplus-lib/src/core/allocator/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use octocrab::models::repos::ContentItems;
22

33
use crate::config::get_env_var_or_default;
4-
use crate::{base64::decode_allocator_model, error::LDNError, external_services::github::GithubWrapper};
4+
use crate::external_services::github::github_async_new;
5+
use crate::{base64::decode_allocator_model, error::LDNError};
56

67
use self::file::AllocatorModel;
78

@@ -14,7 +15,7 @@ pub async fn process_allocator_file(file_name: &str) -> Result<AllocatorModel, L
1415
let branch = "main";
1516
let path = file_name.to_string();
1617

17-
let gh = GithubWrapper::new(owner.to_string(), repo.to_string());
18+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
1819
let content_items = gh.get_file(&path, branch).await.map_err(|e| LDNError::Load(e.to_string()))?;
1920
let model = content_items_to_allocator_model(content_items).map_err(|e| LDNError::Load(e.to_string()))?;
2021

fplus-lib/src/core/mod.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ use chrono::{DateTime, Utc};
1212
use serde_json::from_str;
1313

1414
use crate::{
15-
base64::{self},
15+
base64,
1616
config::get_env_var_or_default,
1717
error::LDNError,
1818
external_services::github::{
19-
CreateMergeRequestData, CreateRefillMergeRequestData, GithubWrapper,
19+
github_async_new, CreateMergeRequestData, CreateRefillMergeRequestData, GithubWrapper
2020
},
2121
parsers::ParsedIssue,
2222
};
@@ -155,7 +155,7 @@ pub struct ApplicationGithubInfo {
155155
impl LDNApplication {
156156

157157
pub async fn single_active(pr_number: u64, owner: String, repo: String) -> Result<ApplicationFile, LDNError> {
158-
let gh: GithubWrapper = GithubWrapper::new(owner, repo);
158+
let gh = github_async_new(owner, repo).await;
159159
let (_, pull_request) = gh.get_pull_request_files(pr_number).await.unwrap();
160160
let pull_request = pull_request.get(0).unwrap();
161161
let pull_request: Response = reqwest::Client::new()
@@ -183,7 +183,7 @@ impl LDNApplication {
183183
owner: String,
184184
repo: String
185185
) -> Result<Option<(String, String, ApplicationFile, PullRequest)>, LDNError> {
186-
let gh = GithubWrapper::new(owner, repo);
186+
let gh = github_async_new(owner, repo).await;
187187
let files = match gh.get_pull_request_files(pr.number).await {
188188
Ok(files) => files,
189189
Err(_) => return Ok(None),
@@ -250,7 +250,7 @@ impl LDNApplication {
250250

251251
pub async fn load(application_id: String, owner: String, repo: String) -> Result<Self, LDNError> {
252252

253-
let gh: GithubWrapper = GithubWrapper::new(owner.clone(), repo.clone());
253+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
254254
let pull_requests = gh.list_pull_requests().await.unwrap();
255255
let pull_requests = future::try_join_all(
256256
pull_requests
@@ -353,7 +353,7 @@ impl LDNApplication {
353353
}
354354

355355
pub async fn active_apps_with_last_update(owner: String, repo: String, filter: Option<String>) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
356-
let gh: GithubWrapper = GithubWrapper::new(owner.clone(), repo.clone());
356+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
357357
let mut apps: Vec<ApplicationFileWithDate> = Vec::new();
358358
let pull_requests = gh.list_pull_requests().await.unwrap();
359359
let pull_requests = future::try_join_all(
@@ -385,8 +385,8 @@ impl LDNApplication {
385385
}
386386

387387
pub async fn merged_apps_with_last_update(owner: String, repo: String, filter: Option<String>) -> Result<Vec<ApplicationFileWithDate>, LDNError> {
388-
389-
let gh = Arc::new(GithubWrapper::new(owner.clone(), repo.clone()));
388+
let gh = Arc::new(github_async_new(owner.to_string(), repo.to_string()).await);
389+
390390
let applications_path = "applications";
391391
let mut all_files_result = gh.get_files(applications_path).await.map_err(|e| {
392392
LDNError::Load(format!("Failed to retrieve all files from GitHub. Reason: {}", e))
@@ -428,7 +428,7 @@ impl LDNApplication {
428428
/// Create New Application
429429
pub async fn new_from_issue(info: CreateApplicationInfo) -> Result<Self, LDNError> {
430430
let issue_number = info.issue_number;
431-
let gh: GithubWrapper = GithubWrapper::new(info.owner.clone(), info.repo.clone());
431+
let gh = github_async_new(info.owner.to_string(), info.repo.to_string()).await;
432432
let (parsed_ldn, _) = LDNApplication::parse_application_issue(
433433
issue_number.clone(),
434434
info.owner.clone(),
@@ -762,7 +762,7 @@ impl LDNApplication {
762762
owner: String,
763763
repo: String,
764764
) -> Result<(ParsedIssue, String), LDNError> {
765-
let gh: GithubWrapper = GithubWrapper::new(owner, repo);
765+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
766766
let issue = gh
767767
.list_issue(issue_number.parse().unwrap())
768768
.await
@@ -796,7 +796,7 @@ impl LDNApplication {
796796
.find_first(|(_, app)| app.id == application_id);
797797
if app.is_some() && app.unwrap().1.lifecycle.get_state() == AppState::Granted {
798798
let app = app.unwrap().1.reached_total_datacap();
799-
let gh: GithubWrapper = GithubWrapper::new(owner.clone(), repo.clone());
799+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
800800
let ldn_app = LDNApplication::load(application_id.clone(), owner.clone(), repo.clone()).await?;
801801
let ContentItems { items } = gh.get_file(&ldn_app.file_name, "main").await.unwrap();
802802
Self::issue_full_dc(app.issue_number.clone(), owner.clone(), repo.clone()).await?;
@@ -1037,7 +1037,7 @@ impl LDNApplication {
10371037
}
10381038

10391039
pub async fn merge_application(pr_number: u64, owner: String, repo: String) -> Result<bool, LDNError> {
1040-
let gh = GithubWrapper::new(owner.clone(), repo.clone());
1040+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
10411041

10421042
gh.merge_pull_request(pr_number).await.map_err(|e| {
10431043
LDNError::Load(format!(
@@ -1064,7 +1064,7 @@ impl LDNApplication {
10641064
actor
10651065
);
10661066

1067-
let gh = GithubWrapper::new(owner, repo);
1067+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
10681068
let author = match gh.get_last_commit_author(pr_number).await {
10691069
Ok(author) => {
10701070
log::info!("- Last commit author: {}", author);
@@ -1325,7 +1325,7 @@ impl LDNApplication {
13251325
.await
13261326
{
13271327
Some(()) => {
1328-
let gh = GithubWrapper::new(owner.clone(), repo.clone());
1328+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
13291329
match gh.get_pull_request_by_head(&ldn_application.branch_name).await {
13301330
Ok(prs) => {
13311331
if let Some(pr) = prs.get(0) {
@@ -1495,7 +1495,7 @@ impl LDNApplication {
14951495
}
14961496

14971497
async fn issue_waiting_for_gov_review(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
1498-
let gh = GithubWrapper::new(owner, repo);
1498+
let gh = github_async_new(owner, repo).await;
14991499
gh.add_comment_to_issue(
15001500
issue_number.parse().unwrap(),
15011501
"Application is waiting for governance review",
@@ -1512,7 +1512,7 @@ impl LDNApplication {
15121512
}
15131513

15141514
async fn issue_datacap_request_trigger(application_file: ApplicationFile, owner: String, repo: String) -> Result<bool, LDNError> {
1515-
let gh: GithubWrapper = GithubWrapper::new(owner, repo);
1515+
let gh = github_async_new(owner, repo).await;
15161516

15171517
let client_address = application_file.lifecycle.client_on_chain_address.clone();
15181518
let total_requested = application_file.datacap.total_requested_amount.clone();
@@ -1555,7 +1555,7 @@ impl LDNApplication {
15551555
active_allocation: Option<&Allocation>,
15561556
owner: String, repo: String
15571557
) -> Result<bool, LDNError> {
1558-
let gh = GithubWrapper::new(owner, repo);
1558+
let gh = github_async_new(owner, repo).await;
15591559

15601560
let issue_number = application_file.issue_number.clone();
15611561

@@ -1613,7 +1613,7 @@ impl LDNApplication {
16131613
.iter()
16141614
.find(|obj| Some(&obj.id) == application_file.lifecycle.active_request.clone().as_ref());
16151615

1616-
let gh = GithubWrapper::new(owner, repo);
1616+
let gh = github_async_new(owner, repo).await;
16171617

16181618
let issue_number = application_file.issue_number.clone();
16191619

@@ -1680,7 +1680,7 @@ Your Datacap Allocation Request has been {} by the Notary
16801680
owner: String,
16811681
repo: String
16821682
) -> Result<bool, LDNError> {
1683-
let gh = GithubWrapper::new(owner, repo);
1683+
let gh = github_async_new(owner, repo).await;
16841684
gh.add_comment_to_issue(
16851685
issue_number.parse().unwrap(),
16861686
"Application is ready to sign",
@@ -1697,7 +1697,7 @@ Your Datacap Allocation Request has been {} by the Notary
16971697
}
16981698

16991699
async fn issue_start_sign_dc(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
1700-
let gh = GithubWrapper::new(owner, repo);
1700+
let gh = github_async_new(owner, repo).await;
17011701
gh.add_comment_to_issue(
17021702
issue_number.parse().unwrap(),
17031703
"Application is in the process of signing datacap",
@@ -1713,7 +1713,7 @@ Your Datacap Allocation Request has been {} by the Notary
17131713
Ok(true)
17141714
}
17151715
async fn issue_granted(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
1716-
let gh = GithubWrapper::new(owner, repo);
1716+
let gh = github_async_new(owner, repo).await;
17171717
gh.add_comment_to_issue(issue_number.parse().unwrap(), "Application is Granted")
17181718
.await
17191719
.map_err(|e| {
@@ -1726,7 +1726,7 @@ Your Datacap Allocation Request has been {} by the Notary
17261726
Ok(true)
17271727
}
17281728
async fn issue_refill(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
1729-
let gh = GithubWrapper::new(owner, repo);
1729+
let gh = github_async_new(owner, repo).await;
17301730
gh.add_comment_to_issue(issue_number.parse().unwrap(), "Application is in Refill")
17311731
.await
17321732
.map_err(|e| {
@@ -1748,7 +1748,7 @@ Your Datacap Allocation Request has been {} by the Notary
17481748
Ok(true)
17491749
}
17501750
async fn issue_full_dc(issue_number: String, owner: String, repo: String) -> Result<bool, LDNError> {
1751-
let gh = GithubWrapper::new(owner, repo);
1751+
let gh = github_async_new(owner, repo).await;
17521752
gh.add_comment_to_issue(issue_number.parse().unwrap(), "Application is Completed")
17531753
.await
17541754
.map_err(|e| {
@@ -1762,7 +1762,7 @@ Your Datacap Allocation Request has been {} by the Notary
17621762
}
17631763

17641764
async fn add_error_label(issue_number: String, comment: String, owner: String, repo: String) -> Result<(), LDNError> {
1765-
let gh = GithubWrapper::new(owner, repo);
1765+
let gh = github_async_new(owner, repo).await;
17661766
let num: u64 = issue_number.parse().expect("Not a valid integer");
17671767
gh.add_error_label(num, comment).await
17681768
.map_err(|e| {
@@ -1777,7 +1777,7 @@ Your Datacap Allocation Request has been {} by the Notary
17771777
}
17781778

17791779
async fn update_issue_labels(issue_number: String, new_labels: &[&str], owner: String, repo: String) -> Result<(), LDNError> {
1780-
let gh = GithubWrapper::new(owner, repo);
1780+
let gh = github_async_new(owner, repo).await;
17811781
let num: u64 = issue_number.parse().expect("Not a valid integer");
17821782
gh.update_issue_labels(
17831783
num,
@@ -1929,7 +1929,7 @@ impl LDNPullRequest {
19291929
repo: String
19301930
) -> Result<String, LDNError> {
19311931
let initial_commit = Self::application_initial_commit(&owner_name, &application_id);
1932-
let gh: GithubWrapper = GithubWrapper::new(owner, repo);
1932+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
19331933
let head_hash = gh.get_main_branch_sha().await.unwrap();
19341934
let create_ref_request = gh
19351935
.build_create_ref_request(app_branch_name.clone(), head_hash)
@@ -1972,7 +1972,7 @@ impl LDNPullRequest {
19721972
repo: String
19731973
) -> Result<u64, LDNError> {
19741974
let initial_commit = Self::application_initial_commit(&owner_name, &application_id);
1975-
let gh: GithubWrapper = GithubWrapper::new(owner.clone(), repo.clone());
1975+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
19761976
let head_hash = gh.get_main_branch_sha().await.unwrap();
19771977
let create_ref_request = gh
19781978
.build_create_ref_request(branch_name.clone(), head_hash)
@@ -2031,7 +2031,7 @@ impl LDNPullRequest {
20312031
owner: String,
20322032
repo: String
20332033
) -> Option<()> {
2034-
let gh: GithubWrapper = GithubWrapper::new(owner, repo);
2034+
let gh = github_async_new(owner.to_string(), repo.to_string()).await;
20352035
match gh
20362036
.update_file_content(
20372037
&path,
@@ -2107,7 +2107,7 @@ mod tests {
21072107
log::info!("Starting end-to-end test");
21082108

21092109
// Test Creating an application
2110-
let gh: GithubWrapper = GithubWrapper::new(OWNER.to_string(), REPO.to_string());
2110+
let gh = github_async_new(OWNER.to_string(), REPO.to_string()).await;
21112111

21122112
log::info!("Creating a new LDNApplication from issue");
21132113
let ldn_application = match LDNApplication::new_from_issue(CreateApplicationInfo {

fplus-lib/src/external_services/github.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![allow(dead_code)]
2+
use fplus_database::database::allocators::get_allocator;
23
use http::header::USER_AGENT;
34
use http::{Request, Uri};
45
use hyper_rustls::HttpsConnectorBuilder;
@@ -86,10 +87,25 @@ struct Author {
8687
name: String,
8788
}
8889

90+
pub async fn github_async_new(owner: String, repo: String) -> GithubWrapper {
91+
let allocator_result = get_allocator(owner.as_str(), repo.as_str()).await;
92+
93+
let allocator = match allocator_result {
94+
Ok(allocator) => allocator,
95+
Err(e) => {
96+
log::error!("Failed to get allocator from database: {:?}", e);
97+
std::process::exit(1);
98+
}
99+
};
100+
101+
let installation_id = allocator.unwrap().installation_id.unwrap_or(0).to_string();
102+
103+
return GithubWrapper::new(owner, repo, installation_id);
104+
}
105+
89106
impl GithubWrapper {
90-
pub fn new(owner: String, repo: String) -> Self {
107+
pub fn new(owner: String, repo: String, installation_id_str: String) -> Self {
91108
let app_id_str = get_env_var_or_default("GITHUB_APP_ID");
92-
let installation_id_str = get_env_var_or_default("GITHUB_INSTALLATION_ID");
93109

94110
let app_id = app_id_str.parse::<u64>().unwrap_or_else(|_| {
95111
log::error!("Failed to parse GITHUB_APP_ID as u64, using default value");

fplus-lib/src/parsers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,14 +343,14 @@ impl From<IssueValidData> for Datacap {
343343

344344
#[cfg(test)]
345345
mod tests {
346-
use crate::external_services::github::GithubWrapper;
346+
use crate::external_services::github::github_async_new;
347347

348348
static OWNER: &str = "filecoin-project";
349349
static REPO: &str = "filplus-tooling-backend-test";
350350

351351
#[tokio::test]
352352
async fn test_parser() {
353-
let gh = GithubWrapper::new(OWNER.to_string(), REPO.to_string());
353+
let gh = github_async_new(OWNER.to_string(), REPO.to_string()).await;
354354
let issue = gh.list_issue(706).await.unwrap();
355355
let parsed_ldn = super::ParsedIssue::from_issue_body(&issue.body.unwrap());
356356
dbg!(&parsed_ldn);

0 commit comments

Comments
 (0)