From e40cf477d37bca5409996a97d5588c24665b6fe8 Mon Sep 17 00:00:00 2001 From: Tsiry Sandratraina Date: Wed, 1 Nov 2023 09:53:25 +0000 Subject: [PATCH] feat: add `clippy` function --- README.md | 17 +++++++---- ci.ts | 2 +- gen/nexus.ts | 5 ++++ schema.graphql | 1 + src/dagger/index.ts | 4 +-- src/dagger/jobs.ts | 65 +++++++++++++++++++++++++++++++++++++++++-- src/dagger/queries.ts | 6 ++++ src/dagger/schema.ts | 8 +++++- 8 files changed, 97 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index bcaaa2d..c857f70 100644 --- a/README.md +++ b/README.md @@ -37,17 +37,24 @@ fluentci run . ## Jobs -| Job | Description | -| ----- | ------------------ | -| build | build your project | -| test | Run your tests | +| Job | Description | +| ------ | ------------------------------- | +| clippy | Run Rust Clippy on your project | +| build | Build your project | +| test | Run your tests | + +```graphql +build(src: String!): String +clippy(src: String!): String +test(src: String!): String +``` ## Programmatic usage You can also use this pipeline programmatically: ```ts -import { build, test } from "https://pkg.fluentci.io/rust_pipeline@v0.6.1/mod.ts"; +import { build, test } from "https://pkg.fluentci.io/rust_pipeline@v0.6.2/mod.ts"; await test(); await build(); diff --git a/ci.ts b/ci.ts index 01e02a6..340b897 100644 --- a/ci.ts +++ b/ci.ts @@ -1,7 +1,7 @@ import { build, test, -} from "https://pkg.fluentci.io/rust_pipeline@v0.6.1/mod.ts"; +} from "https://pkg.fluentci.io/rust_pipeline@v0.6.2/mod.ts"; await test(); await build(); diff --git a/gen/nexus.ts b/gen/nexus.ts index 40c2c83..0847fe3 100644 --- a/gen/nexus.ts +++ b/gen/nexus.ts @@ -44,6 +44,7 @@ export type NexusGenAllTypes = NexusGenRootTypes & NexusGenScalars export interface NexusGenFieldTypes { Query: { // field return type build: string | null; // String + clippy: string | null; // String test: string | null; // String } } @@ -51,6 +52,7 @@ export interface NexusGenFieldTypes { export interface NexusGenFieldTypeNames { Query: { // field return type name build: 'String' + clippy: 'String' test: 'String' } } @@ -60,6 +62,9 @@ export interface NexusGenArgTypes { build: { // args src: string; // String! } + clippy: { // args + src: string; // String! + } test: { // args src: string; // String! } diff --git a/schema.graphql b/schema.graphql index 33883ca..9b75e92 100644 --- a/schema.graphql +++ b/schema.graphql @@ -4,5 +4,6 @@ type Query { build(src: String!): String + clippy(src: String!): String test(src: String!): String } \ No newline at end of file diff --git a/src/dagger/index.ts b/src/dagger/index.ts index b915f31..6aa8a77 100644 --- a/src/dagger/index.ts +++ b/src/dagger/index.ts @@ -1,4 +1,4 @@ import pipeline from "./pipeline.ts"; -import { build, test } from "./jobs.ts"; +import { clippy, build, test } from "./jobs.ts"; -export { pipeline, build, test }; +export { pipeline, clippy, build, test }; diff --git a/src/dagger/jobs.ts b/src/dagger/jobs.ts index 42b7b85..2205760 100644 --- a/src/dagger/jobs.ts +++ b/src/dagger/jobs.ts @@ -1,12 +1,45 @@ import Client, { connect } from "../../deps.ts"; export enum Job { + clippy = "clippy", test = "test", build = "build", } export const exclude = ["target", ".git", ".devbox", ".fluentci"]; +export const clippy = async (src = ".") => { + await connect(async (client: Client) => { + const context = client.host().directory(src); + const ctr = client + .pipeline(Job.test) + .container() + .from("rust:1.73-bookworm") + .withExec(["apt-get", "update"]) + .withExec(["apt-get", "install", "-y", "build-essential", "pkg-config"]) + .withExec(["rustup", "component", "add", "clippy"]) + .withExec(["cargo", "install", "clippy-sarif", "--version", "0.3.0"]) + .withExec(["cargo", "install", "sarif-fmt", "--version", "0.3.0"]) + .withDirectory("/app", context, { exclude }) + .withWorkdir("/app") + .withMountedCache("/app/target", client.cacheVolume("target")) + .withMountedCache("/root/cargo/registry", client.cacheVolume("registry")) + .withExec([ + "sh", + "-c", + "cargo clippy \ + --all-features \ + --message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt", + ]); + + await ctr + .file("/app/rust-clippy-results.sarif") + .export("./rust-clippy-results.sarif"); + await ctr.stdout(); + }); + return "Done"; +}; + export const test = async (src = ".", options: string[] = []) => { await connect(async (client: Client) => { const context = client.host().directory(src); @@ -27,7 +60,12 @@ export const test = async (src = ".", options: string[] = []) => { return "done"; }; -export const build = async (src = ".", options: string[] = []) => { +export const build = async ( + src = ".", + packageName?: string, + target = "x86_64-unknown-linux-gnu", + options: string[] = [] +) => { await connect(async (client: Client) => { const context = client.host().directory(src); const ctr = client @@ -38,7 +76,20 @@ export const build = async (src = ".", options: string[] = []) => { .withWorkdir("/app") .withMountedCache("/app/target", client.cacheVolume("target")) .withMountedCache("/root/cargo/registry", client.cacheVolume("registry")) - .withExec(["cargo", "build", "--release", ...options]); + .withExec( + packageName + ? [ + "cargo", + "build", + "--release", + "-p", + packageName, + "--target", + target, + ...options, + ] + : ["cargo", "build", "--release", "--target", target, ...options] + ); const result = await ctr.stdout(); @@ -49,6 +100,14 @@ export const build = async (src = ".", options: string[] = []) => { export type JobExec = (src?: string) => | Promise + | (( + src?: string, + packageName?: string, + target?: string, + options?: { + ignore: string[]; + } + ) => Promise) | (( src?: string, options?: { @@ -57,11 +116,13 @@ export type JobExec = (src?: string) => ) => Promise); export const runnableJobs: Record = { + [Job.clippy]: clippy, [Job.test]: test, [Job.build]: build, }; export const jobDescriptions: Record = { + [Job.clippy]: "Run clippy", [Job.test]: "Run tests", [Job.build]: "Build the project", }; diff --git a/src/dagger/queries.ts b/src/dagger/queries.ts index ea0b1d0..fd17ae0 100644 --- a/src/dagger/queries.ts +++ b/src/dagger/queries.ts @@ -1,5 +1,11 @@ import { gql } from "../../deps.ts"; +export const clippy = gql` + query clippy($src: String!) { + clippy(src: $src) + } +`; + export const test = gql` query test($src: String!) { test(src: $src) diff --git a/src/dagger/schema.ts b/src/dagger/schema.ts index 320b948..c999cfd 100644 --- a/src/dagger/schema.ts +++ b/src/dagger/schema.ts @@ -8,10 +8,16 @@ import { nonNull, } from "../../deps.ts"; -import { test, build } from "./jobs.ts"; +import { clippy, test, build } from "./jobs.ts"; const Query = queryType({ definition(t) { + t.string("clippy", { + args: { + src: nonNull(stringArg()), + }, + resolve: async (_root, args, _ctx) => await clippy(args.src), + }); t.string("test", { args: { src: nonNull(stringArg()),