From 6749e8a3fe0cb208c4b4f20d98f81986f4969cbd Mon Sep 17 00:00:00 2001 From: Thomas Gerbet Date: Tue, 28 Oct 2025 12:15:02 +0100 Subject: [PATCH] chore: Setup linting/formatting Similar to: https://github.com/Enalean/tuleap-documentation-en/pull/1732 https://github.com/Enalean/tuleap-documentation-en/pull/1730 --- .github/workflows/CI.yml | 21 ++- artifact-post-action/auto-assign/javy.nix | 13 +- artifact-post-action/auto-assign/shell.nix | 4 +- .../compute-risk-and-residual/shell.nix | 10 +- .../compute-risk-and-residual/src/main.rs | 123 ++++++++++-------- artifact-post-action/compute-risk/shell.nix | 10 +- artifact-post-action/compute-risk/src/main.rs | 35 ++--- .../post-action-add-comment/shell.nix | 10 +- .../post-action-add-comment/src/main.rs | 2 +- .../tools => build-support}/nixpkgs-pin.json | 0 .../oxalica-rust-overlay-pin.json | 0 .../pinned-nixpkgs.nix | 7 +- build-support/pinned-treefmt-nix.nix | 9 ++ build-support/treefmt-nix-pin.json | 12 ++ shell.nix | 19 +++ 15 files changed, 183 insertions(+), 92 deletions(-) rename {artifact-post-action/tools => build-support}/nixpkgs-pin.json (100%) rename {artifact-post-action/tools => build-support}/oxalica-rust-overlay-pin.json (100%) rename {artifact-post-action/tools => build-support}/pinned-nixpkgs.nix (88%) create mode 100644 build-support/pinned-treefmt-nix.nix create mode 100644 build-support/treefmt-nix-pin.json create mode 100644 shell.nix diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index c6da457..47f6d0d 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -1,6 +1,6 @@ name: Tests -permissions: read-all +permissions: {} on: push: @@ -16,17 +16,30 @@ jobs: - "artifact-post-action/auto-assign" - "artifact-post-action/compute-risk" - "artifact-post-action/post-action-add-comment" - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 name: Tests (${{ matrix.tuleap_functions }}) defaults: run: working-directory: ${{ matrix.tuleap_functions }} steps: - - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - - uses: cachix/install-nix-action@3715ab1a11cac9e991980d7b4a28d80c7ebdd8f9 # v27 + - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 + with: + persist-credentials: false + - uses: cachix/install-nix-action@fd24c48048070c1be9acd18c9d369a83f0fe94d7 # v31.8.1 - name: Prepare run: nix-shell --run "make prepare" - name: Tests run: nix-shell --pure --run "make tests" - name: Build run: nix-shell --pure --run "make build" + formatting_linting: + name: Check for formatting or linting issues + runs-on: ubuntu-24.04 + steps: + - name: Checkout + uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 + with: + persist-credentials: false + - uses: cachix/install-nix-action@fd24c48048070c1be9acd18c9d369a83f0fe94d7 # v31.8.1 + - name: Build + run: nix-shell --pure --run 'treefmt --ci' diff --git a/artifact-post-action/auto-assign/javy.nix b/artifact-post-action/auto-assign/javy.nix index 0de3a8a..0612780 100644 --- a/artifact-post-action/auto-assign/javy.nix +++ b/artifact-post-action/auto-assign/javy.nix @@ -1,11 +1,12 @@ # TODO: # * build from sources # * upstream to nixpkgs -{ stdenv -, lib -, fetchurl -, autoPatchelfHook -, gzip +{ + stdenv, + lib, + fetchurl, + autoPatchelfHook, + gzip, }: stdenv.mkDerivation rec { @@ -25,7 +26,7 @@ stdenv.mkDerivation rec { buildInputs = [ stdenv.cc.cc.lib ]; - + unpackPhase = '' runHook preUnpack gzip -cd "${src}" > javy diff --git a/artifact-post-action/auto-assign/shell.nix b/artifact-post-action/auto-assign/shell.nix index 543c436..b58a689 100644 --- a/artifact-post-action/auto-assign/shell.nix +++ b/artifact-post-action/auto-assign/shell.nix @@ -1,4 +1,6 @@ -{ pkgs ? (import ../tools/pinned-nixpkgs.nix) {} }: +{ + pkgs ? (import ../../build-support/pinned-nixpkgs.nix) { }, +}: pkgs.mkShellNoCC { buildInputs = [ diff --git a/artifact-post-action/compute-risk-and-residual/shell.nix b/artifact-post-action/compute-risk-and-residual/shell.nix index bf71966..f524762 100644 --- a/artifact-post-action/compute-risk-and-residual/shell.nix +++ b/artifact-post-action/compute-risk-and-residual/shell.nix @@ -1,10 +1,16 @@ -{ pkgs ? (import ./../tools/pinned-nixpkgs.nix) {} }: +{ + pkgs ? (import ./../../build-support/pinned-nixpkgs.nix) { }, +}: pkgs.mkShellNoCC { buildInputs = [ (pkgs.rust-bin.stable.latest.default.override { targets = [ "wasm32-wasip1" ]; - extensions = [ "cargo" "rustc" "rust-src" ]; + extensions = [ + "cargo" + "rustc" + "rust-src" + ]; }) pkgs.gnumake ]; diff --git a/artifact-post-action/compute-risk-and-residual/src/main.rs b/artifact-post-action/compute-risk-and-residual/src/main.rs index 47bc989..e24ac0c 100644 --- a/artifact-post-action/compute-risk-and-residual/src/main.rs +++ b/artifact-post-action/compute-risk-and-residual/src/main.rs @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use serde_json::json; use std::error::Error; use std::io::stdin; @@ -51,17 +51,11 @@ enum ListValue { #[serde(tag = "type")] enum FieldValue { #[serde(rename = "string")] - String { - field_id: u32, - }, + String { field_id: u32 }, #[serde(rename = "text")] - Text { - field_id: u32, - }, + Text { field_id: u32 }, #[serde(rename = "art_link")] - Link { - field_id: u32, - }, + Link { field_id: u32 }, #[serde(rename = "sb")] SelectBox { field_id: u32, @@ -70,15 +64,9 @@ enum FieldValue { bind_value_ids: Vec, }, #[serde(rename = "aid")] - ArtifactId { - field_id: u32, - label: String, - }, + ArtifactId { field_id: u32, label: String }, #[serde(rename = "rb")] - RadioButton { - field_id: u32, - label: String, - }, + RadioButton { field_id: u32, label: String }, } #[derive(Serialize, Deserialize)] @@ -103,7 +91,7 @@ struct TrackerField { #[derive(Serialize, Deserialize)] struct Tracker { id: u32, - fields: Vec + fields: Vec, } #[derive(Serialize, Deserialize)] @@ -120,32 +108,51 @@ fn convert_label_to_integer(label: String) -> Result> { } } -fn find_select_box_by_label<'a>(changeset: &'a Changeset, target_label: &str) -> Option<&'a FieldValue> { - changeset.values.iter().find(|field_value| match field_value { - FieldValue::SelectBox { label, .. } if label == target_label => true, - _ => false, - }) +fn find_select_box_by_label<'a>( + changeset: &'a Changeset, + target_label: &str, +) -> Option<&'a FieldValue> { + changeset + .values + .iter() + .find(|field_value| match field_value { + FieldValue::SelectBox { label, .. } if label == target_label => true, + _ => false, + }) } fn find_value_matching_label(field_value: &FieldValue) -> Option { match field_value { - FieldValue::SelectBox { values, .. } => values.first().and_then(|first_value| match first_value { - ListValue::StaticValue { label, .. } => Some(label.to_string()), - _ => None, - }), + FieldValue::SelectBox { values, .. } => { + values.first().and_then(|first_value| match first_value { + ListValue::StaticValue { label, .. } => Some(label.to_string()), + _ => None, + }) + } _ => None, } } -fn find_select_box_value_by_label(artifact: &Artifact, target_label: &str) -> Result, Box> { +fn find_select_box_value_by_label( + artifact: &Artifact, + target_label: &str, +) -> Result, Box> { find_select_box_by_label(&artifact.current, target_label) - .and_then(|field_value| find_value_matching_label(field_value) ) - .and_then(|label| Some(convert_label_to_integer(label))) - .transpose() + .and_then(|field_value| find_value_matching_label(field_value)) + .and_then(|label| Some(convert_label_to_integer(label))) + .transpose() } -fn find_risk_value(artifact: &Artifact, risk_field_label: &str, product: u32) -> Result, Box> { - let risk_field_option = artifact.tracker.fields.iter().find(|field| field.label == risk_field_label); +fn find_risk_value( + artifact: &Artifact, + risk_field_label: &str, + product: u32, +) -> Result, Box> { + let risk_field_option = artifact + .tracker + .fields + .iter() + .find(|field| field.label == risk_field_label); if risk_field_option.is_none() { return Err(format!("Cannot find field {}", risk_field_label).into()); @@ -157,10 +164,9 @@ fn find_risk_value(artifact: &Artifact, risk_field_label: &str, product: u32) -> } let risk_values = risk_field.values.as_ref().unwrap(); - let matching_value = risk_values.iter() - .find(|value| { - value.label.as_str() == product.to_string() - }); + let matching_value = risk_values + .iter() + .find(|value| value.label.as_str() == product.to_string()); if let Some(matching_value) = matching_value { Ok(Some(FieldValueBinding { @@ -172,7 +178,12 @@ fn find_risk_value(artifact: &Artifact, risk_field_label: &str, product: u32) -> } } -fn process_risk_values(artifact: &Artifact, severity_field_label: &str, probability_field_label: &str, risk_field_label: &str) -> Result, Box> { +fn process_risk_values( + artifact: &Artifact, + severity_field_label: &str, + probability_field_label: &str, + risk_field_label: &str, +) -> Result, Box> { let severity = find_select_box_value_by_label(&artifact, severity_field_label)?; if severity.is_none() { return Ok(None); @@ -184,7 +195,7 @@ fn process_risk_values(artifact: &Artifact, severity_field_label: &str, probabil return Ok(None); } let probability_value = probability.unwrap(); - + let product = severity_value * probability_value; find_risk_value(artifact, risk_field_label, product) @@ -198,26 +209,30 @@ fn main() -> Result<(), Box> { let mut bindings: Vec = Vec::new(); match process_risk_values(&artifact, "Severity", "Probability", "Risk") { - Ok(possible_binding) => { - match possible_binding { - Some(binding) => bindings.push(binding), - None => () - } + Ok(possible_binding) => match possible_binding { + Some(binding) => bindings.push(binding), + None => (), }, Err(e) => return Err(e), } - match process_risk_values(&artifact, "Residual severity", "Residual probability", "Residual risk level") { - Ok(possible_binding) => { - match possible_binding { - Some(binding) => bindings.push(binding), - None => () - } + match process_risk_values( + &artifact, + "Residual severity", + "Residual probability", + "Residual risk level", + ) { + Ok(possible_binding) => match possible_binding { + Some(binding) => bindings.push(binding), + None => (), }, Err(e) => return Err(e), } - println!("{}", json!({ - "values": bindings - })); + println!( + "{}", + json!({ + "values": bindings + }) + ); Ok(()) } diff --git a/artifact-post-action/compute-risk/shell.nix b/artifact-post-action/compute-risk/shell.nix index 0505944..472a113 100644 --- a/artifact-post-action/compute-risk/shell.nix +++ b/artifact-post-action/compute-risk/shell.nix @@ -1,10 +1,16 @@ -{ pkgs ? (import ../tools/pinned-nixpkgs.nix) {} }: +{ + pkgs ? (import ../../build-support/pinned-nixpkgs.nix) { }, +}: pkgs.mkShellNoCC { buildInputs = [ (pkgs.rust-bin.stable.latest.default.override { targets = [ "wasm32-wasip1" ]; - extensions = [ "cargo" "rustc" "rust-src" ]; + extensions = [ + "cargo" + "rustc" + "rust-src" + ]; }) pkgs.gnumake ]; diff --git a/artifact-post-action/compute-risk/src/main.rs b/artifact-post-action/compute-risk/src/main.rs index 1c8a571..4539828 100644 --- a/artifact-post-action/compute-risk/src/main.rs +++ b/artifact-post-action/compute-risk/src/main.rs @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -use serde_json::{json, Value}; +use serde_json::{Value, json}; use std::error::Error; use std::io::stdin; @@ -63,11 +63,7 @@ fn main() -> Result<(), Box> { let field_risk = json["tracker"]["fields"] .as_array() - .and_then(|fields| { - fields - .iter() - .find(|&field| field["label"] == "Risk") - }); + .and_then(|fields| fields.iter().find(|&field| field["label"] == "Risk")); if field_risk.is_none() { return Err("Cannot find field_risk")?; @@ -79,25 +75,30 @@ fn main() -> Result<(), Box> { return Err("Cannot find Risk values")?; } - if let (Some(severity_value), Some(probability_value)) = (field_severity_value, field_probability_value) { + if let (Some(severity_value), Some(probability_value)) = + (field_severity_value, field_probability_value) + { let product = severity_value * probability_value; - let matching_value = risk_values.unwrap().into_iter() - .find(|&value| { + let matching_value = risk_values.unwrap().into_iter().find(|&value| { let value_label = value["label"].as_str().unwrap_or_default(); value_label == product.to_string() }); if let Some(matching_value) = matching_value { let field_id = field_risk.unwrap()["field_id"].as_i64().unwrap_or(0); - println!("{}", json!({ - "values": [{ - "field_id": field_id, - "bind_value_ids": [ - matching_value["id"] - ] - }] - }).to_string()); + println!( + "{}", + json!({ + "values": [{ + "field_id": field_id, + "bind_value_ids": [ + matching_value["id"] + ] + }] + }) + .to_string() + ); Ok(()) } else { diff --git a/artifact-post-action/post-action-add-comment/shell.nix b/artifact-post-action/post-action-add-comment/shell.nix index 0505944..472a113 100644 --- a/artifact-post-action/post-action-add-comment/shell.nix +++ b/artifact-post-action/post-action-add-comment/shell.nix @@ -1,10 +1,16 @@ -{ pkgs ? (import ../tools/pinned-nixpkgs.nix) {} }: +{ + pkgs ? (import ../../build-support/pinned-nixpkgs.nix) { }, +}: pkgs.mkShellNoCC { buildInputs = [ (pkgs.rust-bin.stable.latest.default.override { targets = [ "wasm32-wasip1" ]; - extensions = [ "cargo" "rustc" "rust-src" ]; + extensions = [ + "cargo" + "rustc" + "rust-src" + ]; }) pkgs.gnumake ]; diff --git a/artifact-post-action/post-action-add-comment/src/main.rs b/artifact-post-action/post-action-add-comment/src/main.rs index 10d4fdb..1ca9d0d 100644 --- a/artifact-post-action/post-action-add-comment/src/main.rs +++ b/artifact-post-action/post-action-add-comment/src/main.rs @@ -21,7 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -use serde_json::{json, Value}; +use serde_json::{Value, json}; use std::error::Error; use std::io::stdin; diff --git a/artifact-post-action/tools/nixpkgs-pin.json b/build-support/nixpkgs-pin.json similarity index 100% rename from artifact-post-action/tools/nixpkgs-pin.json rename to build-support/nixpkgs-pin.json diff --git a/artifact-post-action/tools/oxalica-rust-overlay-pin.json b/build-support/oxalica-rust-overlay-pin.json similarity index 100% rename from artifact-post-action/tools/oxalica-rust-overlay-pin.json rename to build-support/oxalica-rust-overlay-pin.json diff --git a/artifact-post-action/tools/pinned-nixpkgs.nix b/build-support/pinned-nixpkgs.nix similarity index 88% rename from artifact-post-action/tools/pinned-nixpkgs.nix rename to build-support/pinned-nixpkgs.nix index 87bec53..948d04a 100644 --- a/artifact-post-action/tools/pinned-nixpkgs.nix +++ b/build-support/pinned-nixpkgs.nix @@ -1,4 +1,4 @@ -{}: +{ }: let oxalicaRustOverlayJson = builtins.fromJSON (builtins.readFile ./oxalica-rust-overlay-pin.json); @@ -10,5 +10,6 @@ let pinnedNixpkgs = import (fetchTarball { url = "https://github.com/NixOS/nixpkgs/archive/${nixpkgsJson.rev}.tar.gz"; sha256 = nixpkgsJson.sha256; - } ) { overlays = [ oxalicaRustOverlay ]; }; -in pinnedNixpkgs + }) { overlays = [ oxalicaRustOverlay ]; }; +in +pinnedNixpkgs diff --git a/build-support/pinned-treefmt-nix.nix b/build-support/pinned-treefmt-nix.nix new file mode 100644 index 0000000..aac0ad5 --- /dev/null +++ b/build-support/pinned-treefmt-nix.nix @@ -0,0 +1,9 @@ +{ }: + +let + treefmt-nix-pin = builtins.fromJSON (builtins.readFile ./treefmt-nix-pin.json); +in +import (fetchTarball { + url = "https://github.com/numtide/treefmt-nix/archive/${treefmt-nix-pin.rev}.tar.gz"; + sha256 = treefmt-nix-pin.sha256; +}) diff --git a/build-support/treefmt-nix-pin.json b/build-support/treefmt-nix-pin.json new file mode 100644 index 0000000..5c31482 --- /dev/null +++ b/build-support/treefmt-nix-pin.json @@ -0,0 +1,12 @@ +{ + "url": "https://github.com/numtide/treefmt-nix.git", + "rev": "f56b1934f5f8fcab8deb5d38d42fd692632b47c2", + "date": "2025-10-20T09:26:31+02:00", + "path": "/nix/store/7jxjmwvjc6lf2ikdh9cjnzv99q3nvm19-treefmt-nix", + "sha256": "1klcfmqb4q4vvy9kdm5i9ddl26rhlyhf1mrd5aw1d4529bqnq5b5", + "hash": "sha256-ZRVs8UqikBa4Ki3X4KCnMBtBW0ux1DaT35tgsnB1jM4=", + "fetchLFS": false, + "fetchSubmodules": false, + "deepClone": false, + "leaveDotGit": false +} diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..314fc56 --- /dev/null +++ b/shell.nix @@ -0,0 +1,19 @@ +{ + pkgs ? (import ./build-support/pinned-nixpkgs.nix) { }, + treefmt-nix ? (import ./build-support/pinned-treefmt-nix.nix { }), +}: + +pkgs.mkShellNoCC { + buildInputs = [ + (treefmt-nix.mkWrapper pkgs { + programs.gofmt.enable = true; + programs.rustfmt.enable = true; + programs.nixfmt = { + enable = true; + package = pkgs.nixfmt; + }; + programs.actionlint.enable = true; + programs.zizmor.enable = true; + }) + ]; +}