Skip to content
Clone

refactor(repo): refresh crate across features, pull_request and metrics

zhenyi committed
parent f8bd2c7 commit a83f492 Browse files

5 files changed

+1 −49

File tree 5
Modified lib/repo/features/pull_request/mod.rs View lib/repo/features/pull_request/mod.rs
@@ -1,5 +1,3 @@
1//! Pull request feature module.
2
31pub use export::repo::pull_request::*;
42
53pub mod usecase;
No newline at end of file
Modified lib/repo/features/pull_request/usecase.rs View lib/repo/features/pull_request/usecase.rs
@@ -1,5 +1,3 @@
1//! Pull request feature use cases.
2
31use std::collections::HashMap;
42
53use db::conn::sea_orm::*;
@@ -400,8 +398,6 @@
400398 result
401399}
402400
403/// Heal the half-state where the base reference advanced to the merged commit
404/// but the database transaction failed before recording `has_merged`.
405401async fn heal_merged(svc: &RepoService, repo_id: Uuid, pr: &pr_entity::Model) -> pr_entity::Model {
406402 if pr.has_merged {
407403 return pr.clone();
Modified lib/repo/handler/middleware/metrics.rs View lib/repo/handler/middleware/metrics.rs
@@ -2,12 +2,6 @@
22use axum::middleware::Next;
33use axum::response::Response;
44
5/// HTTP-level metrics layer for the `repo` crate.
6///
7/// Emits `repo_http_requests_total{route,method,status_class}` (counter) and
8/// `repo_http_request_duration_seconds{route,method,status_class}` (histogram)
9/// on every request that traverses the router. Uses `MatchedPath` so the
10/// `route` label stays low-cardinality (no repo-id path params).
115pub async fn metrics_layer(req: Request, next: Next) -> Response {
126 crate::metrics::ensure_described();
137
Modified lib/repo/error.rs View lib/repo/error.rs
@@ -80,8 +80,7 @@
8080 )
8181 }
8282
83 /// Bounded label for metrics. Keep stable and low-cardinality.
84 pub fn error_kind(&self) -> &'static str {
83 pub fn error_kind(&self) -> &'static str {
8584 match self {
8685 RepoError::BadRequest(_) => "bad_request",
8786 RepoError::Unauthorized(_) => "unauthorized",
Modified lib/repo/metrics.rs View lib/repo/metrics.rs
@@ -1,21 +1,12 @@
1//! Tracing layer for the `repo` crate.
2//!
3//! Emits metrics through the `metrics` facade. No exporter is installed here;
4//! the host binary is responsible for binding a recorder. All labels are
5//! bounded to keep cardinality low (no `repo_id`/`user_id`/`branch`).
6
71use std::time::Instant;
82
93use metrics::{counter, describe_counter, describe_histogram, histogram};
104
115use crate::error::RepoError;
126
13// ── Feature constants (one per usecase) ───────────────────────────────────
147
15// archive
168pub const FEATURE_ARCHIVES_GET: &str = "archives_get";
179
18// branch_protection
1910pub const FEATURE_BRANCH_PROTECTIONS_LIST: &str = "branch_protections_list";
2011pub const FEATURE_BRANCH_PROTECTIONS_CREATE: &str = "branch_protections_create";
2112pub const FEATURE_BRANCH_PROTECTIONS_GET: &str = "branch_protections_get";
@@ -23,7 +14,6 @@
2314pub const FEATURE_BRANCH_PROTECTIONS_DELETE: &str = "branch_protections_delete";
2415pub const FEATURE_BRANCH_PROTECTIONS_UPDATE_PRIORITIES: &str = "branch_protections_update_priorities";
2516
26// branch
2717pub const FEATURE_BRANCHES_LIST: &str = "branches_list";
2818pub const FEATURE_BRANCHES_GET: &str = "branches_get";
2919pub const FEATURE_BRANCHES_CREATE: &str = "branches_create";
@@ -32,23 +22,19 @@
3222pub const FEATURE_BRANCHES_RENAME: &str = "branches_rename";
3323pub const FEATURE_BRANCHES_MERGE_UPSTREAM: &str = "branches_merge_upstream";
3424
35// collab
3625pub const FEATURE_COLLAB_LIST: &str = "collab_list";
3726pub const FEATURE_COLLAB_IS_COLLABORATOR: &str = "collab_is_collaborator";
3827pub const FEATURE_COLLAB_ADD: &str = "collab_add";
3928pub const FEATURE_COLLAB_DELETE: &str = "collab_delete";
4029pub const FEATURE_COLLAB_GET_REPO_PERMISSIONS: &str = "collab_get_repo_permissions";
4130
42// commit_status
4331pub const FEATURE_COMMIT_STATUSES_LIST: &str = "commit_statuses_list";
4432pub const FEATURE_COMMIT_STATUSES_CREATE: &str = "commit_statuses_create";
4533pub const FEATURE_COMMIT_STATUSES_LIST_BY_REF: &str = "commit_statuses_list_by_ref";
4634pub const FEATURE_COMMIT_STATUSES_COMBINED_BY_REF: &str = "commit_statuses_combined_by_ref";
4735
48// compare
4936pub const FEATURE_COMPARE: &str = "compare";
5037
51// contents
5238pub const FEATURE_CONTENTS_LIST: &str = "contents_list";
5339pub const FEATURE_CONTENTS_GET: &str = "contents_get";
5440pub const FEATURE_CONTENTS_CREATE_FILE: &str = "contents_create_file";
@@ -57,13 +43,11 @@
5743pub const FEATURE_CONTENTS_CHANGE_FILES: &str = "contents_change_files";
5844pub const FEATURE_CONTENTS_APPLY_DIFF_PATCH: &str = "contents_apply_diff_patch";
5945
60// deploy_key
6146pub const FEATURE_DEPLOY_KEYS_LIST: &str = "deploy_keys_list";
6247pub const FEATURE_DEPLOY_KEYS_GET: &str = "deploy_keys_get";
6348pub const FEATURE_DEPLOY_KEYS_CREATE: &str = "deploy_keys_create";
6449pub const FEATURE_DEPLOY_KEYS_DELETE: &str = "deploy_keys_delete";
6550
66// extras
6751pub const FEATURE_EXTRAS_SEARCH_REPOS: &str = "extras_search_repos";
6852pub const FEATURE_EXTRAS_GET_REPO_BY_ID: &str = "extras_get_repo_by_id";
6953pub const FEATURE_EXTRAS_UPDATE_REPO_AVATAR: &str = "extras_update_repo_avatar";
@@ -100,7 +84,6 @@
10084pub const FEATURE_EXTRAS_GET_SIGNING_KEY_GPG: &str = "extras_get_signing_key_gpg";
10185pub const FEATURE_EXTRAS_GET_SIGNING_KEY_SSH: &str = "extras_get_signing_key_ssh";
10286
103// git_ref
10487pub const FEATURE_GIT_REFS_LIST: &str = "git_refs_list";
10588pub const FEATURE_GIT_REFS_FILTER: &str = "git_refs_filter";
10689pub const FEATURE_GIT_REFS_GET_COMMIT: &str = "git_refs_get_commit";
@@ -109,7 +92,6 @@
10992pub const FEATURE_GIT_REFS_GET_BLOB: &str = "git_refs_get_blob";
11093pub const FEATURE_GIT_REFS_GET_NOTE: &str = "git_refs_get_note";
11194
112// pull_request
11395pub const FEATURE_PULL_REQUESTS_LIST: &str = "pull_requests_list";
11496pub const FEATURE_PULL_REQUESTS_GET: &str = "pull_requests_get";
11597pub const FEATURE_PULL_REQUESTS_CREATE: &str = "pull_requests_create";
@@ -125,7 +107,6 @@
125107pub const FEATURE_PULL_REQUESTS_LIST_EVENTS: &str = "pull_requests_list_events";
126108pub const FEATURE_PULL_REQUESTS_MERGE: &str = "pull_requests_merge";
127109
128// release
129110pub const FEATURE_RELEASES_LIST: &str = "releases_list";
130111pub const FEATURE_RELEASES_GET: &str = "releases_get";
131112pub const FEATURE_RELEASES_GET_LATEST: &str = "releases_get_latest";
@@ -135,34 +116,28 @@
135116pub const FEATURE_RELEASES_DELETE: &str = "releases_delete";
136117pub const FEATURE_RELEASES_DELETE_BY_TAG: &str = "releases_delete_by_tag";
137118
138// subscriber
139119pub const FEATURE_SUBSCRIBERS_LIST: &str = "subscribers_list";
140120
141// sync
142121pub const FEATURE_SYNC_GET_STATUS: &str = "sync_get_status";
143122pub const FEATURE_SYNC_REPO_METADATA: &str = "sync_repo_metadata";
144123
145// tag_protection
146124pub const FEATURE_TAG_PROTECTIONS_LIST: &str = "tag_protections_list";
147125pub const FEATURE_TAG_PROTECTIONS_CREATE: &str = "tag_protections_create";
148126pub const FEATURE_TAG_PROTECTIONS_GET: &str = "tag_protections_get";
149127pub const FEATURE_TAG_PROTECTIONS_EDIT: &str = "tag_protections_edit";
150128pub const FEATURE_TAG_PROTECTIONS_DELETE: &str = "tag_protections_delete";
151129
152// tag
153130pub const FEATURE_TAGS_LIST: &str = "tags_list";
154131pub const FEATURE_TAGS_GET: &str = "tags_get";
155132pub const FEATURE_TAGS_CREATE: &str = "tags_create";
156133pub const FEATURE_TAGS_DELETE: &str = "tags_delete";
157134
158// topic
159135pub const FEATURE_TOPICS_LIST: &str = "topics_list";
160136pub const FEATURE_TOPICS_UPDATE: &str = "topics_update";
161137pub const FEATURE_TOPICS_ADD: &str = "topics_add";
162138pub const FEATURE_TOPICS_DELETE: &str = "topics_delete";
163139pub const FEATURE_TOPICS_SEARCH: &str = "topics_search";
164140
165// lifecycle (repos)
166141pub const FEATURE_REPOS_CREATE: &str = "repos_create";
167142pub const FEATURE_REPOS_GET: &str = "repos_get";
168143pub const FEATURE_REPOS_LIST: &str = "repos_list";
@@ -174,11 +149,9 @@
174149pub const FEATURE_REPOS_FORK: &str = "repos_fork";
175150pub const FEATURE_REPOS_GENERATE: &str = "repos_generate";
176151
177// ── Outcome labels (bounded enums) ────────────────────────────────────────
178152
179153pub const OUTCOME_SUCCESS: &str = "success";
180154
181// ── Metric description registration (Once) ────────────────────────────────
182155
183156static DESCRIBE_ONCE: std::sync::Once = std::sync::Once::new();
184157
@@ -215,9 +188,7 @@
215188 );
216189}
217190
218// ── Timer (RAII) ──────────────────────────────────────────────────────────
219191
220/// Records the usecase execution duration on drop.
221192pub struct Timer {
222193 pub feature: &'static str,
223194 pub start: Instant,
@@ -244,10 +215,7 @@
244215 }
245216}
246217
247// ── Generic outcome counter ───────────────────────────────────────────────
248218
249/// Bump invocation counter (always) and, on `Err`, an error counter labeled
250/// by `RepoError::error_kind()`. Drop semantics of `_timer` capture latency.
251219pub fn record_outcome<T>(feature: &'static str, result: &Result<T, RepoError>) {
252220 match result {
253221 Ok(_) => {
@@ -276,7 +244,6 @@
276244 }
277245}
278246
279// ── Repo create helper ────────────────────────────────────────────────────
280247
281248#[derive(Debug, Clone, Copy)]
282249pub enum RepoCreateOutcome {
@@ -308,7 +275,6 @@
308275 .increment(1);
309276}
310277
311// ── Branch create helper ──────────────────────────────────────────────────
312278
313279#[derive(Debug, Clone, Copy)]
314280pub enum BranchCreateOutcome {
@@ -340,7 +306,6 @@
340306 .increment(1);
341307}
342308
343// ── Pull request merge helper ─────────────────────────────────────────────
344309
345310#[derive(Debug, Clone, Copy)]
346311pub enum PullRequestMergeOutcome {