Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add rust buck tests #3803

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ pay:
- pnpm-lock.yaml

core:
- core/**/*
- core/api/*
- core/api-cron/*
- core/api-exporter/*
- core/api-trigger/*
- core/api-ws-server/*
- flake.lock
- pnpm-lock.yaml

api-keys:
- third-party/rust/*
- core/api-keys/*
- flake.lock
- Cargo.lock
9 changes: 6 additions & 3 deletions .github/workflows/buck2-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
${{ toJSON(github.event.pull_request.labels.*.name) }}
EOF

DEFAULT_LABELS=("dashboard" "consent" "pay" "core")
DEFAULT_LABELS=("dashboard" "consent" "pay" "core", "api-keys")
LABELS=($(jq -r '.[]' < labels.json))
if [ ${#LABELS[@]} -eq 0 ]; then
LABELS=("${DEFAULT_LABELS[@]}")
Expand All @@ -32,10 +32,13 @@ jobs:
for LABEL in "${LABELS[@]}"; do
case "$LABEL" in
dashboard|consent|pay)
ARGS+=" //apps/$LABEL:test-unit"
ARGS+=" //apps/$LABEL:test"
;;
core)
ARGS+=" //core/api:test-unit"
ARGS+=" //core/api:test"
;;
api-keys)
ARGS+=" //core/$LABEL:test"
;;
esac
done
Expand Down
5 changes: 5 additions & 0 deletions BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ pnpm_workspace(
],
visibility = ["PUBLIC"],
)

export_file(
name = "rustfmt.toml",
visibility = ["PUBLIC"],
)
2 changes: 1 addition & 1 deletion apps/consent/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ eslint(
)

test_suite(
name = "test-unit",
name = "test",
tests = [
":audit",
":lint",
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ eslint(
)

test_suite(
name = "test-unit",
name = "test",
tests = [
":audit",
":lint",
Expand Down
2 changes: 1 addition & 1 deletion apps/pay/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ eslint(
)

test_suite(
name = "test-unit",
name = "test",
tests = [
":audit",
":lint",
Expand Down
4 changes: 2 additions & 2 deletions ci/apps/app-template.lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ plan:
- get: #@ app_src_resource_name(app)
trigger: true
- { get: pipeline-tasks }
- task: buck-test-unit
- task: buck-test
config:
platform: linux
image_resource: #@ task_image_config()
Expand All @@ -36,7 +36,7 @@ plan:
- name: #@ app_src_resource_name(app)
path: repo
params:
BUCK_TARGET: #@ "//apps/" + app + ":test-unit"
BUCK_TARGET: #@ "//apps/" + app + ":test"
BUCK_CMD: test
run:
path: pipeline-tasks/ci/apps/tasks/buck-task.sh
Expand Down
17 changes: 17 additions & 0 deletions ci/core/pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#@ load("@ytt:data", "data")

#@ load("template.lib.yml",
#@ "component_src_resource",
#@ "component_src_resource_name",
#@ "core_bundle_src_resource",
#@ "buck_test_name",
Expand All @@ -26,6 +27,8 @@ source:
repository: #@ release_pipeline_image()
#@ end

#@ components = ["api-keys"]

groups:
- name: core-bundle
jobs:
Expand All @@ -41,8 +44,17 @@ groups:
- quickstart
- release-core-bundle
- bump-core-bundle-images-in-chart
#@ for component in components:
- name: #@ component
jobs:
- #@ buck_test_name(component)
#@ end

jobs:
#@ for component in components:
- #@ buck_test(component)
#@ end

- #@ buck_test("api")
#@ for component in core_bundle_components:
- #@ build_edge_image(component)
Expand Down Expand Up @@ -246,6 +258,11 @@ jobs:

resources:
- #@ core_bundle_src_resource()

#@ for component in components:
- #@ component_src_resource(component)
#@ end

- name: repo-out
type: git
source:
Expand Down
25 changes: 23 additions & 2 deletions ci/core/template.lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ plan:
- get: #@ component_src_resource_name(component)
trigger: true
- { get: pipeline-tasks }
- task: buck-test-unit
- task: buck-test
config:
platform: linux
image_resource: #@ task_image_config()
Expand All @@ -35,7 +35,7 @@ plan:
- name: #@ component_src_resource_name(component)
path: repo
params:
BUCK_TARGET: #@ "//core/" + component + ":test-unit"
BUCK_TARGET: #@ "//core/" + component + ":test"
BUCK_CMD: test
run:
path: pipeline-tasks/ci/apps/tasks/buck-task.sh
Expand Down Expand Up @@ -155,6 +155,27 @@ source:
webhook_token: ((webhook.secret))
#@ end

#@ def component_src_resource(component):
name: #@ component_src_resource_name(component)
type: git
source:
paths:
- #@ "core/" + component
- #@ "core/" + component + "/*"
- #@ "core/" + component + "/**/*"
- flake.nix
- flake.lock
- toolchains/
- toolchains/*
- toolchains/**/*
- third-party/rust/**/*
- Cargo.lock
fetch_tags: true
uri: #@ data.values.git_uri
branch: #@ data.values.git_branch
private_key: #@ data.values.github_private_key
#@ end

#@ def edge_image_resource_name(component):
#@ return component + "-edge-image"
#@ end
Expand Down
62 changes: 62 additions & 0 deletions core/api-keys/BUCK
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@toolchains//rover:macros.bzl", "sdl", "diff_check", "dev_update_file")
load("@toolchains//rust:macros.bzl", "rustfmt_check", "clippy_check")

sdl(
name = "sdl",
Expand Down Expand Up @@ -74,3 +75,64 @@ rust_library(
"SQLX_OFFLINE": "true",
}
)

rust_test(
name = "test-unit",
edition = "2021",
srcs = glob([
"src/**/*.rs",
".sqlx/*",
"migrations/*",
"api-keys.yml"
]),
crate_root = "src/lib.rs",
deps = [
"//lib/tracing-rs:tracing",
"//third-party/rust:tokio",
"//third-party/rust:anyhow",
"//third-party/rust:async-graphql",
"//third-party/rust:async-graphql-axum",
"//third-party/rust:axum",
"//third-party/rust:jsonwebtoken",
"//third-party/rust:clap",
"//third-party/rust:reqwest",
"//third-party/rust:serde",
"//third-party/rust:serde_yaml",
"//third-party/rust:serde_json",
"//third-party/rust:thiserror",
"//third-party/rust:chrono",
"//third-party/rust:sqlx",
"//third-party/rust:rand",
"//third-party/rust:uuid",
"//third-party/rust:serde_with",
],
env = {
"CARGO_MANIFEST_DIR": ".",
"SQLX_OFFLINE": "true",
}
)

rustfmt_check(
name = "check-format-rust",
srcs = glob([
"src/**/*.rs",
".sqlx/*",
"migrations/*",
"api-keys.yml"
]),
crate_root = "src/lib.rs",
)

clippy_check(
name = "check-lint-rust-lib",
clippy_txt_dep = ":lib-api-keys[clippy.txt]",
)

test_suite(
name = "test",
tests = [
":check-format-rust",
":check-lint-rust-lib",
":test-unit"
],
)
2 changes: 1 addition & 1 deletion core/api-keys/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl ApiKeysApp {
&self,
key: &str,
) -> Result<(IdentityApiKeyId, String, bool), ApplicationError> {
Ok(self.identities.find_subject_by_key(&key).await?)
Ok(self.identities.find_subject_by_key(key).await?)
}

#[tracing::instrument(name = "app.create_api_key_for_subject", skip_all)]
Expand Down
6 changes: 3 additions & 3 deletions core/api-keys/src/graphql/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ impl From<DateTime<Utc>> for Timestamp {
}
}

impl Into<DateTime<Utc>> for Timestamp {
fn into(self) -> DateTime<Utc> {
self.0
impl From<Timestamp> for DateTime<Utc> {
fn from(Timestamp(dt): Timestamp) -> Self {
dt
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/api-keys/src/identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl Identities {

match record {
Some(record) => Ok(IdentityApiKey {
id: IdentityApiKeyId::from(key_id),
id: key_id,
name: record.name,
identity_id: IdentityId::from(record.identity_id),
created_at: record.created_at,
Expand Down
4 changes: 2 additions & 2 deletions core/api-keys/src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ pub const READ_SCOPE: &str = "read";
pub const WRITE_SCOPE: &str = "write";

pub fn read_only_scope() -> String {
format!("{READ_SCOPE}")
READ_SCOPE.to_string()
}

pub fn read_write_scope() -> String {
format!("{READ_SCOPE} {WRITE_SCOPE}")
}

pub fn is_read_only(scope: &String) -> bool {
!(scope.as_str().split(" ").any(|s| s == WRITE_SCOPE) || scope.is_empty())
!(scope.as_str().split(' ').any(|s| s == WRITE_SCOPE) || scope.is_empty())
}
2 changes: 1 addition & 1 deletion core/api/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ jest_test(
)

test_suite(
name = "test-unit",
name = "test",
tests = [
":audit",
":check-lint",
Expand Down
2 changes: 2 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
edition = "2021"
newline_style = "Unix"
7 changes: 7 additions & 0 deletions toolchains/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ rover_toolchain(
name = "rover",
visibility = ["PUBLIC"],
)

load("@toolchains//rust:toolchain.bzl", "galoy_rust_toolchain")

galoy_rust_toolchain(
name = "galoy_rust",
visibility = ["PUBLIC"],
)
14 changes: 14 additions & 0 deletions toolchains/rust/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export_file(
name = "clippy_output.py",
visibility = ["PUBLIC"],
)

export_file(
name = "crate_context.py",
visibility = ["PUBLIC"],
)

export_file(
name = "rustfmt_check.py",
visibility = ["PUBLIC"],
)
35 changes: 35 additions & 0 deletions toolchains/rust/clippy_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
"""
Prints output of a Clippy compilation and exits non-zero if appropriate.
"""
import argparse
import os
import sys


def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"clippy_txt",
help="The file from a Clippy compilation with its output")

return parser.parse_args()


def main() -> int:
args = parse_args()

# If output is empty, there are no errors/warnings and we can exit `0`
if os.path.getsize(args.clippy_txt) == 0:
return 0

# Otherwise print output and exit non-zero
with open(args.clippy_txt, encoding="utf-8") as f:
print(f.read())

return 1


if __name__ == "__main__":
sys.exit(main())

Loading
Loading