Skip to content

Commit

Permalink
changed parsing logic to accomodate JS swagger, added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dxu2atlassian committed Dec 23, 2024
1 parent cee6cf0 commit 86c12a8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
2 changes: 1 addition & 1 deletion crates/forge_analyzer/src/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ impl FunctionAnalyzer<'_> {
[PropPath::Def(def), ref authn @ .., PropPath::Static(ref last)]
if (*last == *"requestJira"
|| *last == *"requestConfluence"
|| *last == *"requestBitbucket")
|| *last == *"requestBitbucket") // TODO: so here JSM (and likely JS) is bundled inside Jira
&& Some(&ImportKind::Default)
== self.res.is_imported_from(def, "@forge/api") =>
{
Expand Down
2 changes: 0 additions & 2 deletions crates/forge_analyzer/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ pub struct Interp<'cx, C: Runner<'cx>> {
pub jira_service_management_permission_resolver: &'cx PermissionHashMap,
pub jira_permission_resolver: &'cx PermissionHashMap,
pub confluence_permission_resolver: &'cx PermissionHashMap,
pub jira_service_management_regex_map: &'cx HashMap<String, Regex>,
pub bitbucket_permission_resolver: &'cx PermissionHashMap,
pub jira_software_regex_map: &'cx HashMap<String, Regex>,
pub jira_service_management_regex_map: &'cx HashMap<String, Regex>,
Expand Down Expand Up @@ -553,7 +552,6 @@ impl<'cx, C: Runner<'cx>> Interp<'cx, C> {
jira_service_management_permission_resolver,
jira_permission_resolver,
confluence_permission_resolver,
jira_service_management_regex_map,
bitbucket_permission_resolver,
jira_software_regex_map,
jira_service_management_regex_map,
Expand Down
69 changes: 45 additions & 24 deletions crates/forge_permission_resolver/src/permissions_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ struct RequestDetails {
default
)]
permission: Vec<PermissionData>,

// For parsing Jira Software as that swagger doesn't follow "x-atlassian-oauth2-scopes" scope style
#[serde(default)]
security: Vec<SecurityData>,
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize)]
Expand All @@ -44,6 +48,12 @@ struct PermissionData {
scopes: Vec<String>,
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Deserialize)]
struct SecurityData {
#[serde(default, rename = "OAuth2")]
oauth2: Vec<String>,
}

#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
pub enum RequestType {
Get,
Expand Down Expand Up @@ -204,12 +214,25 @@ fn get_request_type(
}

fn get_scopes(endpoint_data: &RequestDetails) -> Vec<String> {
endpoint_data
let mut scopes = endpoint_data
.permission
.iter()
.flat_map(|data| &*data.scopes)
.cloned()
.collect()
.collect::<Vec<_>>();

if scopes.is_empty() {
// For Jira Software if the initial scopes are empty, try the scopes from the security field
scopes.extend(
endpoint_data
.security
.iter()
.flat_map(|sec| &sec.oauth2)
.cloned(),
);
}

scopes
}

#[cfg(test)]
Expand Down Expand Up @@ -369,9 +392,9 @@ mod test {
}

#[test]
fn test_get_organization() {
let (permission_map, regex_map) = get_permission_resolver_jira_service_management();
let url = "/rest/servicedeskapi/organization";
fn test_get_issues_for_epic() {
let (permission_map, regex_map) = get_permission_resolver_jira_software();
let url = "/rest/agile/1.0/sprint/23";
let request_type = RequestType::Get;
let result = check_url_for_permissions(&permission_map, &regex_map, request_type, url);

Expand All @@ -380,30 +403,28 @@ mod test {

assert!(!result.is_empty(), "Should have parsed permissions");
assert!(
result.contains(&String::from("manage:servicedesk-customer")),
"Should require manage:servicedesk-customer permission"
result.contains(&String::from("read:sprint:jira-software")),
"Should require read:sprint:jira-software permission"
);
}

// TODO: this fails right now as the Jira Software swagger does not have the "x-atlassian-oauth2-scopes" in it that we parse for with serde
// #[test]
// fn test_get_issues_for_epic() {
// let (permission_map, regex_map) = get_permission_resolver_jira_software();
// let url = "/rest/agile/1.0/sprint/23";
// let request_type = RequestType::Get;
// let result = check_url_for_permissions(&permission_map, &regex_map, request_type, url);
#[test]
fn test_get_all_boards() {
let (permission_map, regex_map) = get_permission_resolver_jira_software();
let url = "/rest/agile/1.0/board";
let request_type = RequestType::Get;
let result = check_url_for_permissions(&permission_map, &regex_map, request_type, url);

// println!("Permission Map: {:?}", permission_map); // TODO: this does not give back any scopes?
// println!("Regex Map: {:?}", regex_map);
println!("Permission Map: {:?}", permission_map);
println!("Regex Map: {:?}", regex_map);

// assert!(!result.is_empty(), "Should have parsed permissions");
assert!(!result.is_empty(), "Should have parsed permissions");

// // let expected_permission: Vec<String> = vec![
// // String::from("read:epic:jira-software"),
// // String::from("read:issue-details:jira"),
// // String::from("read:jql:jira"),
// // ];
let expected_permission: Vec<String> = vec![
String::from("read:board-scope:jira-software"),
String::from("read:project:jira"),
];

// // assert_eq!(result, expected_permission);
// }
assert_eq!(result, expected_permission);
}
}
4 changes: 2 additions & 2 deletions crates/fsrt/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ mod test;
use clap::{Parser, ValueHint};
use forge_permission_resolver::permissions_resolver::{
get_permission_resolver_bitbucket, get_permission_resolver_confluence,
get_permission_resolver_jira,
get_permission_resolver_jira_service_management, get_permission_resolver_jira_software,
get_permission_resolver_jira, get_permission_resolver_jira_service_management,
get_permission_resolver_jira_software,
};

use std::{
Expand Down

0 comments on commit 86c12a8

Please sign in to comment.