Skip to content

Commit 8d37891

Browse files
authored
Fetch issue reporter handle (#250)
1 parent 6dc8a62 commit 8d37891

File tree

7 files changed

+109
-27
lines changed

7 files changed

+109
-27
lines changed

fplus-database/src/database/applications.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ pub async fn get_applications() -> Result<Vec<ApplicationModel>, sea_orm::DbErr>
3030
a.updated_at,
3131
a.sha,
3232
a.path,
33-
a.client_contract_address
33+
a.client_contract_address,
34+
a.issue_reporter_handle
3435
FROM
3536
applications a
3637
ORDER BY
@@ -58,6 +59,7 @@ pub async fn get_applications() -> Result<Vec<ApplicationModel>, sea_orm::DbErr>
5859
sha: get_string_field(&app, "sha"),
5960
path: get_string_field(&app, "path"),
6061
client_contract_address: get_string_field(&app, "client_contract_address"),
62+
issue_reporter_handle: get_string_field(&app, "issue_reporter_handle"),
6163
})
6264
.collect();
6365

@@ -351,6 +353,7 @@ pub async fn update_application(
351353
* # Returns
352354
* @return Result<ApplicationModel, sea_orm::DbErr> - The result of the operation
353355
*/
356+
#[allow(clippy::too_many_arguments)]
354357
pub async fn create_application(
355358
id: String,
356359
owner: String,
@@ -359,6 +362,7 @@ pub async fn create_application(
359362
issue_number: i64,
360363
app_file: String,
361364
path: String,
365+
issue_reporter_handle: Option<String>,
362366
) -> Result<ApplicationModel, sea_orm::DbErr> {
363367
let conn = get_database_connection().await?;
364368
//Calculate SHA
@@ -376,6 +380,7 @@ pub async fn create_application(
376380
application: Set(Some(app_file)),
377381
sha: Set(Some(file_sha)),
378382
path: Set(Some(path)),
383+
issue_reporter_handle: Set(issue_reporter_handle),
379384
..Default::default()
380385
};
381386

fplus-database/src/models/applications.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub struct Model {
2727
pub path: Option<String>,
2828
#[sea_orm(nullable)]
2929
pub client_contract_address: Option<String>,
30+
#[sea_orm(nullable)]
31+
pub issue_reporter_handle: Option<String>,
3032
}
3133

3234
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]

fplus-lib/src/core/application/file.rs

+10
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ pub struct ApplicationFile {
7070
pub allowed_sps: Option<SpsChangeRequests>,
7171
}
7272

73+
#[derive(Serialize, Deserialize, Debug, Clone)]
74+
pub struct ApplicationResponse {
75+
#[serde(flatten)]
76+
pub file: ApplicationFile,
77+
#[serde(rename = "Issue Reporter Handle")]
78+
pub issue_reporter_handle: Option<String>,
79+
pub repo: String,
80+
pub owner: String,
81+
}
82+
7383
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
7484
pub struct Client {
7585
#[serde(rename = "Name")]

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

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub mod lifecycle;
99
pub mod sps_change;
1010

1111
impl file::ApplicationFile {
12+
#[allow(clippy::too_many_arguments)]
1213
pub async fn new(
1314
issue_number: String,
1415
multisig_address: String,

fplus-lib/src/core/mod.rs

+78-26
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ use fplus_database::database::{
4747
use fplus_database::models::applications::Model as ApplicationModel;
4848

4949
use self::application::file::{
50-
AllocationRequest, AllocationRequestType, AppState, ApplicationFile, DeepCompare,
51-
ValidVerifierList, VerifierInput,
50+
AllocationRequest, AllocationRequestType, AppState, ApplicationFile, ApplicationResponse,
51+
DeepCompare, ValidVerifierList, VerifierInput,
5252
};
5353

5454
use crate::core::application::file::Allocation;
@@ -498,14 +498,14 @@ impl LDNApplication {
498498
let app = Self::single_merged(application_id, owner.clone(), repo.clone()).await?;
499499
Ok(Self {
500500
github: gh,
501-
application_id: app.1.id.clone(),
501+
application_id: app.1.file.id.clone(),
502502
file_sha: app.0.sha.clone(),
503503
file_name: app.0.path.clone(),
504504
branch_name: "main".to_string(),
505505
})
506506
}
507507

508-
pub async fn all_applications() -> Result<Vec<(ApplicationFile, String, String)>, LDNError> {
508+
pub async fn all_applications() -> Result<Vec<ApplicationResponse>, LDNError> {
509509
let db_apps = database::applications::get_applications()
510510
.await
511511
.map_err(|e| {
@@ -514,11 +514,16 @@ impl LDNApplication {
514514
e
515515
))
516516
})?;
517-
let mut all_apps: Vec<(ApplicationFile, String, String)> = Vec::new();
517+
let mut all_apps: Vec<ApplicationResponse> = Vec::new();
518518
for app in db_apps {
519519
if let Some(application_data) = app.application {
520520
if let Ok(app_file) = ApplicationFile::from_str(&application_data) {
521-
all_apps.push((app_file, app.owner, app.repo));
521+
all_apps.push(ApplicationResponse {
522+
file: app_file,
523+
issue_reporter_handle: app.issue_reporter_handle,
524+
repo: app.repo,
525+
owner: app.owner,
526+
});
522527
}
523528
}
524529
}
@@ -529,14 +534,14 @@ impl LDNApplication {
529534
owner: String,
530535
repo: String,
531536
filter: Option<String>,
532-
) -> Result<Vec<ApplicationFile>, LDNError> {
537+
) -> Result<Vec<ApplicationResponse>, LDNError> {
533538
// Get all active applications from the database.
534539
let active_apps = database::applications::get_active_applications(Some(owner), Some(repo))
535540
.await
536541
.map_err(|e| LDNError::Load(format!("Failed to get active applications: {}", e)))?;
537542

538543
// Filter and convert active applications.
539-
let mut apps: Vec<ApplicationFile> = Vec::new();
544+
let mut apps: Vec<ApplicationResponse> = Vec::new();
540545
for app_model in active_apps {
541546
// If a filter was provided and it doesn't match the application's id, continue to the next iteration.
542547
if let Some(ref filter_id) = filter {
@@ -548,7 +553,14 @@ impl LDNApplication {
548553
// Try to deserialize the `application` field to `ApplicationFile`.
549554
if let Some(app_json) = app_model.application {
550555
match from_str::<ApplicationFile>(&app_json) {
551-
Ok(app) => apps.push(app),
556+
Ok(app) => {
557+
apps.push(ApplicationResponse {
558+
file: app,
559+
issue_reporter_handle: app_model.issue_reporter_handle,
560+
repo: app_model.repo,
561+
owner: app_model.owner,
562+
});
563+
}
552564
//if error, don't push into apps
553565
Err(err) => {
554566
log::error!("Failed to parse application file from DB: {}", err);
@@ -656,7 +668,7 @@ impl LDNApplication {
656668
pub async fn new_from_issue(info: CreateApplicationInfo) -> Result<Self, LDNError> {
657669
let issue_number = info.issue_number;
658670
let gh = github_async_new(info.owner.to_string(), info.repo.to_string()).await?;
659-
let (mut parsed_ldn, _) = LDNApplication::parse_application_issue(
671+
let (mut parsed_ldn, issue_reporter_handle) = LDNApplication::parse_application_issue(
660672
issue_number.clone(),
661673
info.owner.clone(),
662674
info.repo.clone(),
@@ -849,6 +861,7 @@ impl LDNApplication {
849861
issue_number,
850862
file_content,
851863
LDNPullRequest::application_path(&app_id),
864+
Some(issue_reporter_handle),
852865
)
853866
.await
854867
.map_err(|e| {
@@ -1262,6 +1275,7 @@ impl LDNApplication {
12621275
);
12631276
let parsed_app_file = serde_json::to_string_pretty(&app_file)
12641277
.map_err(|e| LDNError::Load(format!("Failed to pare into string: {}", e)))?;
1278+
12651279
LDNPullRequest::create_pr_for_existing_application(
12661280
app_file.id.clone(),
12671281
parsed_app_file,
@@ -1546,10 +1560,10 @@ impl LDNApplication {
15461560
let merged = Self::merged(owner.clone(), repo.clone()).await?;
15471561
let application = merged
15481562
.par_iter()
1549-
.find_first(|(_, app)| app.id == application_id);
1563+
.find_first(|(_, app)| app.file.id == application_id);
15501564
if let Some(app) = application {
1551-
if app.1.lifecycle.get_state() == AppState::Granted {
1552-
let app = app.1.reached_total_datacap();
1565+
if app.1.file.lifecycle.get_state() == AppState::Granted {
1566+
let app = app.1.file.reached_total_datacap();
15531567
let gh = github_async_new(owner.to_string(), repo.to_string()).await?;
15541568
let ldn_app =
15551569
LDNApplication::load(application_id.clone(), owner.clone(), repo.clone())
@@ -1593,7 +1607,7 @@ impl LDNApplication {
15931607
} else {
15941608
Err(LDNError::Load(format!(
15951609
"Application state is {:?}. Expected Granted",
1596-
app.1.lifecycle.get_state()
1610+
app.1.file.lifecycle.get_state()
15971611
)))
15981612
}
15991613
} else {
@@ -1658,11 +1672,11 @@ impl LDNApplication {
16581672
application_id: String,
16591673
owner: String,
16601674
repo: String,
1661-
) -> Result<(ApplicationGithubInfo, ApplicationFile), LDNError> {
1675+
) -> Result<(ApplicationGithubInfo, ApplicationResponse), LDNError> {
16621676
LDNApplication::merged(owner, repo)
16631677
.await?
16641678
.into_iter()
1665-
.find(|(_, app)| app.id == application_id)
1679+
.find(|(_, app)| app.file.id == application_id)
16661680
.map_or_else(
16671681
|| {
16681682
Err(LDNError::Load(format!(
@@ -1710,7 +1724,7 @@ impl LDNApplication {
17101724
pub async fn merged(
17111725
owner: String,
17121726
repo: String,
1713-
) -> Result<Vec<(ApplicationGithubInfo, ApplicationFile)>, LDNError> {
1727+
) -> Result<Vec<(ApplicationGithubInfo, ApplicationResponse)>, LDNError> {
17141728
// Retrieve all applications in the main branch from the database.
17151729
let merged_app_models = database::applications::get_merged_applications(
17161730
Some(owner.clone()),
@@ -1720,7 +1734,7 @@ impl LDNApplication {
17201734
.map_err(|e| LDNError::Load(format!("Database error:: {}", e)))?;
17211735

17221736
// Convert applications from the main branch.
1723-
let mut merged_apps: Vec<(ApplicationGithubInfo, ApplicationFile)> = Vec::new();
1737+
let mut merged_apps: Vec<(ApplicationGithubInfo, ApplicationResponse)> = Vec::new();
17241738
for app_model in merged_app_models {
17251739
// Try to deserialize the `application` field to `ApplicationFile`.
17261740
if let Some(app_json) = app_model.application {
@@ -1731,15 +1745,25 @@ impl LDNApplication {
17311745
let path = app_model
17321746
.path
17331747
.ok_or(LDNError::Load("Failed to get path".to_string()))?;
1734-
merged_apps.push((ApplicationGithubInfo { sha, path }, app));
1748+
merged_apps.push((
1749+
ApplicationGithubInfo { sha, path },
1750+
ApplicationResponse {
1751+
file: app,
1752+
issue_reporter_handle: app_model.issue_reporter_handle,
1753+
repo: app_model.repo,
1754+
owner: app_model.owner,
1755+
},
1756+
));
17351757
}
17361758
}
17371759
}
17381760

17391761
let active_apps = Self::active(owner, repo, None).await?;
1740-
let mut apps: Vec<(ApplicationGithubInfo, ApplicationFile)> = vec![];
1762+
let mut apps: Vec<(ApplicationGithubInfo, ApplicationResponse)> = vec![];
17411763
for app in merged_apps {
1742-
if !active_apps.iter().any(|a| a.id == app.1.id) && app.1.lifecycle.is_active {
1764+
if !active_apps.iter().any(|a| a.file.id == app.1.file.id)
1765+
&& app.1.file.lifecycle.is_active
1766+
{
17431767
apps.push(app);
17441768
}
17451769
}
@@ -1750,7 +1774,9 @@ impl LDNApplication {
17501774
async fn refill(verfier: &str, refill_info: RefillInfo) -> Result<bool, LDNError> {
17511775
let apps =
17521776
LDNApplication::merged(refill_info.owner.clone(), refill_info.repo.clone()).await?;
1753-
if let Some((content, mut app)) = apps.into_iter().find(|(_, app)| app.id == refill_info.id)
1777+
if let Some((content, mut app)) = apps
1778+
.into_iter()
1779+
.find(|(_, app)| app.file.id == refill_info.id)
17541780
{
17551781
let uuid = uuidv4::uuid::v4();
17561782
let request_id = uuid.clone();
@@ -1760,19 +1786,19 @@ impl LDNApplication {
17601786
AllocationRequestType::Refill(0),
17611787
format!("{}{}", refill_info.amount, refill_info.amount_type),
17621788
);
1763-
let app_file = app.start_refill_request(new_request);
1789+
let app_file = app.file.start_refill_request(new_request);
17641790
Self::issue_refill(
1765-
app.issue_number.clone(),
1791+
app.file.issue_number.clone(),
17661792
refill_info.owner.clone(),
17671793
refill_info.repo.clone(),
17681794
)
17691795
.await?;
17701796

1771-
let pr_title = format!("Datacap for {}", app.client.name.clone());
1797+
let pr_title = format!("Datacap for {}", app.file.client.name.clone());
17721798
let parsed_app_file = serde_json::to_string_pretty(&app_file)
17731799
.map_err(|e| LDNError::Load(format!("Failed to pare into string: {}", e)))?;
17741800
LDNPullRequest::create_pr_for_existing_application(
1775-
app.id.clone(),
1801+
app.file.id.clone(),
17761802
parsed_app_file,
17771803
content.path.clone(), // filename
17781804
request_id.clone(),
@@ -2557,6 +2583,11 @@ impl LDNApplication {
25572583
application_file.issue_number, e
25582584
))
25592585
})?;
2586+
let issue_reporter_handle = gh
2587+
.get_issue_reporter_handle(
2588+
&issue_number.try_into().expect("Value must be non-negative"),
2589+
)
2590+
.await?;
25602591
database::applications::create_application(
25612592
application_id,
25622593
owner.clone(),
@@ -2565,6 +2596,7 @@ impl LDNApplication {
25652596
issue_number,
25662597
file_content.clone(),
25672598
filename.clone(),
2599+
Some(issue_reporter_handle),
25682600
)
25692601
.await
25702602
.map_err(|e| {
@@ -3540,6 +3572,12 @@ _The initial issue can be edited in order to solve the request of the verifier.
35403572
let parsed_app_file = serde_json::to_string_pretty(&gh_app.application_file)
35413573
.map_err(|e| LDNError::Load(format!("Failed to pare into string: {}", e)))?;
35423574
// Call the create_application function if the GH app is not in DB
3575+
let gh: GithubWrapper = github_async_new(owner.clone(), repo.clone()).await?;
3576+
let issue_reporter_handle = gh
3577+
.get_issue_reporter_handle(
3578+
&issue_number.try_into().expect("Value must be non-negative"),
3579+
)
3580+
.await?;
35433581
database::applications::create_application(
35443582
gh_app.application_file.id.clone(),
35453583
owner.clone(),
@@ -3548,6 +3586,7 @@ _The initial issue can be edited in order to solve the request of the verifier.
35483586
issue_number,
35493587
parsed_app_file,
35503588
gh_app.path,
3589+
Some(issue_reporter_handle),
35513590
)
35523591
.await
35533592
.map_err(|e| {
@@ -3639,6 +3678,12 @@ _The initial issue can be edited in order to solve the request of the verifier.
36393678
// Call the create_application function if the GH app is not in DB
36403679
let parsed_app_file = serde_json::to_string_pretty(&gh_app.application_file)
36413680
.map_err(|e| LDNError::Load(format!("Failed to pare into string: {}", e)))?;
3681+
let gh: GithubWrapper = github_async_new(owner.clone(), repo.clone()).await?;
3682+
let issue_reporter_handle = gh
3683+
.get_issue_reporter_handle(
3684+
&issue_number.try_into().expect("Value must be non-negative"),
3685+
)
3686+
.await?;
36423687
database::applications::create_application(
36433688
gh_app.application_file.id.clone(),
36443689
owner.clone(),
@@ -3647,6 +3692,7 @@ _The initial issue can be edited in order to solve the request of the verifier.
36473692
issue_number,
36483693
parsed_app_file,
36493694
gh_app.path,
3695+
Some(issue_reporter_handle),
36503696
)
36513697
.await
36523698
.map_err(|e| {
@@ -4427,6 +4473,11 @@ impl LDNPullRequest {
44274473
let issue_number = issue_number
44284474
.parse::<i64>()
44294475
.map_err(|e| LDNError::New(format!("Parse issue number to i64 failed: {}", e)))?;
4476+
let issue_reporter_handle = gh
4477+
.get_issue_reporter_handle(
4478+
&issue_number.try_into().expect("Value must be non-negative"),
4479+
)
4480+
.await?;
44304481
database::applications::create_application(
44314482
application_id.clone(),
44324483
owner,
@@ -4435,6 +4486,7 @@ impl LDNPullRequest {
44354486
issue_number,
44364487
file_content,
44374488
file_name,
4489+
Some(issue_reporter_handle),
44384490
)
44394491
.await
44404492
.map_err(|e| {

fplus-lib/src/external_services/github.rs

+10
Original file line numberDiff line numberDiff line change
@@ -806,4 +806,14 @@ impl GithubWrapper {
806806
.collect())
807807
})
808808
}
809+
810+
pub async fn get_issue_reporter_handle(&self, issue_number: &u64) -> Result<String, LDNError> {
811+
let issue = self.list_issue(*issue_number).await.map_err(|e| {
812+
LDNError::Load(format!(
813+
"Failed to retrieve issue {} from GitHub: {}",
814+
issue_number, e
815+
))
816+
})?;
817+
Ok(issue.user.login)
818+
}
809819
}

0 commit comments

Comments
 (0)