Skip to content

Commit d73b981

Browse files
committed
docs: Enhance documentation with detailed comments and descriptions for functions and modules
1 parent e472216 commit d73b981

4 files changed

Lines changed: 128 additions & 6 deletions

File tree

src/action.rs

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
#![allow(dead_code)]
2+
//! Action module for defining and handling the GitHub Action's inputs, outputs, and core functionality
3+
//!
4+
//! This module contains the Action struct which represents the GitHub Action and implements
5+
//! the necessary functionality to process inputs, validate configurations, and manage outputs.
26
use std::path::PathBuf;
37

48
use anyhow::{Context, Result};
59
use ghactions::prelude::*;
610
use ghactions_core::repository::reference::RepositoryReference as Repository;
711
use ghastoolkit::{CodeQL, CodeQLPack, codeql::CodeQLLanguage};
812

13+
/// ASCII art banner for the CodeQL Extractor Action
914
pub const BANNER: &str = r#" ___ _ ____ __ __ _ _ _
1015
/ __\___ __| | ___ /___ \/ / /__\_ _| |_ /_\ ___| |_
1116
/ / / _ \ / _` |/ _ \// / / / /_\ \ \/ / __|//_\\ / __| __|
1217
/ /__| (_) | (_| | __/ \_/ / /___//__ > <| |_/ _ \ (__| |_
1318
\____/\___/ \__,_|\___\___,_\____/\__/ /_/\_\\__\_/ \_/\___|\__|"#;
19+
20+
/// Version of the CodeQL Extractor Action, pulled from Cargo.toml
1421
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
22+
23+
/// Authors of the CodeQL Extractor Action, pulled from Cargo.toml
1524
pub const AUTHORS: &str = env!("CARGO_PKG_AUTHORS");
1625

1726
/// This action is for 3rd party CodeQL extractors to be used in GitHub Actions
@@ -94,6 +103,13 @@ pub struct Action {
94103
}
95104

96105
impl Action {
106+
/// Returns the working directory for the action
107+
///
108+
/// If no working directory is provided, the current directory is used.
109+
/// Otherwise, the provided directory is resolved to an absolute path.
110+
///
111+
/// # Returns
112+
/// - `Result<PathBuf>`: The resolved working directory path
97113
pub fn working_directory(&self) -> Result<PathBuf> {
98114
if self.working_directory.is_empty() {
99115
log::debug!("No working directory provided, using the current directory");
@@ -108,8 +124,13 @@ impl Action {
108124
))
109125
}
110126

111-
/// Gets the repository to use for the extractor. If the repository is not provided,
112-
/// it will use the repository that the action is running in.
127+
/// Gets the repository references for the extractors
128+
///
129+
/// If no extractor repositories are provided, the current repository is used.
130+
/// Otherwise, the provided repositories are parsed into Repository objects.
131+
///
132+
/// # Returns
133+
/// - `Result<Vec<Repository>>`: A list of parsed repository references
113134
pub fn extractor_repository(&self) -> Result<Vec<Repository>> {
114135
if self.extractors.is_empty() {
115136
log::debug!("No extractor repository provided, using the current repository");
@@ -129,13 +150,19 @@ impl Action {
129150
.collect::<Vec<Repository>>())
130151
}
131152

153+
/// Returns the list of languages to use for CodeQL analysis.
132154
pub fn languages(&self) -> Vec<CodeQLLanguage> {
133155
self.languages
134156
.iter()
135157
.map(|lang| CodeQLLanguage::from(lang.as_str()))
136158
.collect()
137159
}
138160

161+
/// Returns the directory to use for CodeQL operations.
162+
///
163+
/// # Errors
164+
///
165+
/// Returns an error if the directory cannot be created.
139166
pub fn get_codeql_dir(&self) -> Result<PathBuf> {
140167
let paths = vec![
141168
// Local CodeQL directory in the working directory
@@ -162,6 +189,11 @@ impl Action {
162189
Err(anyhow::anyhow!("Failed to create CodeQL directory",))
163190
}
164191

192+
/// Validates the provided languages against the supported CodeQL languages.
193+
///
194+
/// # Errors
195+
///
196+
/// Returns an error if any of the provided languages are not supported.
165197
pub fn validate_languages(&self, codeql_languages: &Vec<CodeQLLanguage>) -> Result<()> {
166198
for lang in self.languages() {
167199
let mut supported = false;
@@ -187,6 +219,9 @@ impl Action {
187219
Ok(())
188220
}
189221

222+
/// Returns the CodeQL version to use.
223+
///
224+
/// If the CodeQL version is not provided, it defaults to "latest".
190225
pub fn codeql_version(&self) -> &str {
191226
if self.codeql_version.is_empty() {
192227
log::debug!("No CodeQL version provided, using the latest version");
@@ -195,6 +230,11 @@ impl Action {
195230
&self.codeql_version
196231
}
197232

233+
/// Installs the specified CodeQL packs.
234+
///
235+
/// # Errors
236+
///
237+
/// Returns an error if any of the packs cannot be installed.
198238
pub async fn install_packs(&self, codeql: &CodeQL) -> Result<()> {
199239
log::info!("Installing CodeQL Packs");
200240
for pack in &self.packs {
@@ -227,10 +267,12 @@ impl Action {
227267
Ok(())
228268
}
229269

270+
/// Returns whether attestation is enabled.
230271
pub fn attestation(&self) -> bool {
231272
self.attestation
232273
}
233274

275+
/// Returns whether empty databases are allowed.
234276
pub fn allow_empty_database(&self) -> bool {
235277
self.allow_empty_database
236278
}
@@ -240,6 +282,12 @@ impl Action {
240282
mod tests {
241283
use super::*;
242284

285+
/// Helper function to create a test Action instance with predefined values
286+
///
287+
/// Creates an Action with:
288+
/// - A single extractor repository "owner/repo"
289+
/// - A single language "iac"
290+
/// - Default values for all other fields
243291
fn action() -> Action {
244292
Action {
245293
extractors: vec!["owner/repo".to_string()],
@@ -248,6 +296,11 @@ mod tests {
248296
}
249297
}
250298

299+
/// Test that language validation works correctly
300+
///
301+
/// Tests two scenarios:
302+
/// 1. When a language is specified that isn't supported by CodeQL (should error)
303+
/// 2. When a language is specified that is supported by CodeQL (should pass)
251304
#[test]
252305
fn test_validate_languages() {
253306
let action = action();

src/codeql.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
//! CodeQL installation and management utilities
2+
//!
3+
//! This module provides helper functions for downloading and installing CodeQL,
4+
//! particularly through alternative methods like GitHub CLI when the standard
5+
//! installation process fails.
6+
17
use anyhow::{Context, Result};
28
use ghastoolkit::CodeQL;
39

4-
/// Download the CodeQL CLI using the GitHub CLI
10+
/// Download and install the CodeQL CLI using the GitHub CLI
11+
///
12+
/// This function serves as a fallback installation method when the standard CodeQL
13+
/// installation process fails. It uses the GitHub CLI to:
14+
/// 1. Install the gh-codeql extension
15+
/// 2. Set the specified CodeQL version
16+
/// 3. Install the CodeQL stub for command-line access
17+
///
18+
/// # Arguments
19+
/// * `codeql_version` - The version of CodeQL to download (e.g., "latest" or a specific version)
20+
///
21+
/// # Returns
22+
/// * `Result<String>` - Path to the installed CodeQL binary or an error
523
pub async fn gh_codeql_download(codeql_version: &str) -> Result<String> {
624
log::info!("Downloading CodeQL Extension for GitHub CLI...");
725
tokio::process::Command::new("gh")

src/extractors.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ use ghactions_core::repository::reference::RepositoryReference as Repository;
44
use octocrab::models::repos::{Asset, Release};
55
use std::{os::unix::fs::PermissionsExt, path::PathBuf};
66

7+
/// Fetches a release from a GitHub repository
8+
///
9+
/// If the repository reference includes a specific tag, it fetches that release.
10+
/// Otherwise, it fetches the latest release.
11+
///
12+
/// # Arguments
13+
/// * `client` - The Octocrab client to use for API requests
14+
/// * `repository` - The repository reference containing owner, name, and optional tag
15+
///
16+
/// # Returns
17+
/// * `Result<Release>` - The fetched release or an error
718
async fn fetch_releases(client: &octocrab::Octocrab, repository: &Repository) -> Result<Release> {
819
let release = if let Some(rel) = &repository.reference {
920
log::info!("Fetching release by tag: {}", rel);
@@ -164,7 +175,16 @@ pub async fn fetch_extractor(
164175

165176
/// Update the SARIF file with the extractor information (CodeQL ${language})
166177
///
167-
/// Update only the `runs.0.tool.driver` section of the SARIF file
178+
/// Updates only the `runs.0.tool.driver` section of the SARIF file to include
179+
/// information about which extractor was used. This helps in distinguishing
180+
/// results from different CodeQL extractors when analyzing multiple languages.
181+
///
182+
/// # Arguments
183+
/// * `path` - Path to the SARIF file that needs to be updated
184+
/// * `extractor` - Name of the extractor to be added to the SARIF metadata
185+
///
186+
/// # Returns
187+
/// * `Result<()>` - Success or an error if the SARIF file couldn't be updated
168188
pub fn update_sarif(path: &PathBuf, extractor: String) -> Result<()> {
169189
let sarif_content =
170190
std::fs::read_to_string(path).context(format!("Failed to read SARIF file: {:?}", path))?;
@@ -194,7 +214,16 @@ pub fn update_sarif(path: &PathBuf, extractor: String) -> Result<()> {
194214
Ok(())
195215
}
196216

197-
/// Update the permissions for tool scripts (*.sh) and the extractor (extractor)
217+
/// Update the permissions for tool scripts (*.sh) and the extractor executables
218+
///
219+
/// Makes shell scripts and extractor binaries executable by setting appropriate permissions.
220+
/// Looks for tools in standard locations for Linux (linux64/extractor) and macOS (osx64/extractor).
221+
///
222+
/// # Arguments
223+
/// * `path` - The base path where tools are located
224+
///
225+
/// # Returns
226+
/// * `Result<()>` - Success or an error if permissions couldn't be set
198227
fn update_tools_permisisons(path: &PathBuf) -> Result<()> {
199228
let tools_path = path.join("tools");
200229
log::info!("Tools :: {tools_path:?}");
@@ -226,7 +255,16 @@ fn update_tools_permisisons(path: &PathBuf) -> Result<()> {
226255
Ok(())
227256
}
228257

229-
/// Sets the file permissions to be executable
258+
/// Sets the file permissions to be executable (read and execute for all users)
259+
///
260+
/// Sets the permissions to 0o555 (r-xr-xr-x) which allows reading and
261+
/// execution by all users, but no write permissions.
262+
///
263+
/// # Arguments
264+
/// * `path` - The path to the file whose permissions should be set
265+
///
266+
/// # Returns
267+
/// * `Result<()>` - Success or an error if permissions couldn't be set
230268
fn set_permissions(path: &PathBuf) -> Result<()> {
231269
log::info!("Setting permissions for :: {:?}", path);
232270
let perms = std::fs::Permissions::from_mode(0o555);

src/main.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
//! Main module for the CodeQL Extractor Action
2+
//!
3+
//! This module contains the main function and orchestrates the entire workflow
4+
//! for setting up CodeQL, fetching and configuring extractors, and running analyses.
15
use anyhow::{Context, Result};
26
use ghactions::{ActionTrait, group, groupend};
37
use ghactions_core::RepositoryReference;
@@ -13,6 +17,15 @@ use action::{AUTHORS, Action, BANNER, VERSION};
1317

1418
use crate::codeql::gh_codeql_download;
1519

20+
/// Main function that drives the CodeQL Extractor Action workflow
21+
///
22+
/// This function:
23+
/// 1. Initializes the Action
24+
/// 2. Sets up CodeQL
25+
/// 3. Fetches and configures the extractors
26+
/// 4. Creates databases for each language
27+
/// 5. Runs analyses on the databases
28+
/// 6. Processes and updates the SARIF results
1629
#[tokio::main]
1730
async fn main() -> Result<()> {
1831
let mut action = Action::init()?;

0 commit comments

Comments
 (0)