Skip to content

Commit ae6698b

Browse files
committed
feat: Update Extractor to set permissions for files
1 parent be759dc commit ae6698b

File tree

3 files changed

+83
-38
lines changed

3 files changed

+83
-38
lines changed

Cargo.lock

Lines changed: 26 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/extractors.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::path::PathBuf;
1+
use std::{os::unix::fs::PermissionsExt, path::PathBuf};
22

33
use anyhow::{Context, Result};
4-
use ghactions_core::repository::reference::RepositoryReference as Repository;
4+
use ghactions_core::{repository::reference::RepositoryReference as Repository, toolcache::tool};
55
use octocrab::models::repos::{Asset, Release};
66

77
async fn fetch_releases(client: &octocrab::Octocrab, repository: &Repository) -> Result<Release> {
@@ -104,7 +104,14 @@ pub async fn fetch_extractor(
104104
match glob {
105105
Ok(path) => {
106106
log::debug!("Extractor Path :: {path:?}");
107-
return Ok(path.parent().unwrap().to_path_buf().canonicalize()?);
107+
let full_path = path.parent().unwrap().to_path_buf().canonicalize()?;
108+
// Linux and Macos
109+
#[cfg(unix)]
110+
{
111+
update_tools_permisisons(&full_path)?;
112+
}
113+
114+
return Ok(full_path);
108115
}
109116
Err(e) => {
110117
log::error!("Failed to find extractor: {e}");
@@ -114,3 +121,43 @@ pub async fn fetch_extractor(
114121
}
115122
Ok(extractor_pack)
116123
}
124+
125+
/// Update the permissions for tool scripts (*.sh) and the extractor (extractor)
126+
fn update_tools_permisisons(path: &PathBuf) -> Result<()> {
127+
let tools_path = path.join("tools");
128+
log::info!("Tools :: {tools_path:?}");
129+
130+
if tools_path.exists() {
131+
log::debug!("Found tools directory at {tools_path:?}");
132+
133+
// Linux
134+
let linux_extractor = tools_path.join("linux64").join("extractor");
135+
if linux_extractor.exists() {
136+
set_permissions(&linux_extractor)?;
137+
}
138+
// Macos
139+
let macos_extractor = tools_path.join("osx64").join("extractor");
140+
if macos_extractor.exists() {
141+
set_permissions(&macos_extractor)?;
142+
}
143+
144+
for file in std::fs::read_dir(&tools_path)? {
145+
let file = file?;
146+
let path = file.path();
147+
148+
if path.is_file() && path.extension().map_or(false, |ext| ext == "sh") {
149+
log::debug!("Setting executable permissions for {path:?}");
150+
set_permissions(&path)?;
151+
}
152+
}
153+
}
154+
Ok(())
155+
}
156+
157+
/// Sets the file permissions to be executable
158+
fn set_permissions(path: &PathBuf) -> Result<()> {
159+
log::info!("Setting permissions for :: {:?}", path);
160+
let perms = std::fs::Permissions::from_mode(0o555);
161+
std::fs::set_permissions(&path, perms)?;
162+
Ok(())
163+
}

src/main.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::path::PathBuf;
2-
31
use anyhow::{Context, Result};
42
use ghactions::{ActionTrait, group, groupend};
53
use ghactions_core::RepositoryReference;
@@ -22,6 +20,12 @@ async fn main() -> Result<()> {
2220

2321
let client = action.octocrab()?;
2422

23+
let cwd = action
24+
.working_directory()
25+
.context("Failed to get working directory")?;
26+
let databases = cwd.join(".codeql");
27+
let sarif_output = databases.join("results");
28+
2529
group!("Setting up CodeQL");
2630

2731
let mut codeql = CodeQL::init()
@@ -56,7 +60,7 @@ async fn main() -> Result<()> {
5660
.extractor_repository()
5761
.context("Failed to get extractor repository")?;
5862

59-
let extractor_path = PathBuf::from("./extractors");
63+
let extractor_path = cwd.join(".codeql").join("extractors");
6064
if !extractor_path.exists() {
6165
std::fs::create_dir(&extractor_path)
6266
.with_context(|| format!("Failed to create directory {extractor_path:?}"))?;
@@ -106,12 +110,6 @@ async fn main() -> Result<()> {
106110

107111
groupend!();
108112

109-
let cwd = action
110-
.working_directory()
111-
.context("Failed to get working directory")?;
112-
let databases = cwd.join(".codeql");
113-
let sarif_output = databases.join("results");
114-
115113
std::fs::create_dir_all(&sarif_output).context("Failed to create results directory")?;
116114

117115
for (extractor, reporef) in extractors {

0 commit comments

Comments
 (0)