Skip to content
Draft
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
9c89286
Regalia 向けの サービスを実装
nishinoyama Mar 7, 2025
3f4ab31
contestant サービスの定義
nishinoyama Mar 7, 2025
ba17942
contestants サービスをBotに持たせる
nishinoyama Mar 7, 2025
f12bc82
regalia の contestant と team のAPIの型に対応
nishinoyama Mar 10, 2025
78211a2
コードの順番を調整
nishinoyama Mar 10, 2025
9a5aeb7
configファイルもしくは、regaliaのチームを取得する方式に
nishinoyama Mar 10, 2025
ebd4dcb
configファイルもしくは、regaliaの問題群を取得する方式に
nishinoyama Mar 10, 2025
d0652e8
serviceの命名の修正
nishinoyama Mar 10, 2025
ef01d9f
環境変数を読んで起動するコンテナイメージを作る
tosuke Mar 11, 2025
323dae0
feature/regalia-service でもビルドする
tosuke Mar 11, 2025
6ce327d
Merge pull request #48 from ictsc/feature/containerize
tosuke Mar 11, 2025
d1df503
特に regalia の興味のない部分の dead_code を allow
nishinoyama Mar 11, 2025
fa00187
log や err ハンドルを調整
nishinoyama Mar 11, 2025
fcccaa3
helpチャンネルを破壊しない
nishinoyama Mar 11, 2025
414ff0d
.env を gitignore
nishinoyama Mar 11, 2025
ca8a40c
clippy した
nishinoyama Mar 11, 2025
808be51
api URL の修正
nishinoyama Mar 11, 2025
c5003f7
syncコマンド実行時の文面の修正
nishinoyama Mar 12, 2025
d9e14d2
regaliaが無を返す場合に対応
nishinoyama Mar 12, 2025
882368c
Regalia の ListDeployments を読めるように
nishinoyama Mar 13, 2025
e508ae2
Regalia の Deploy を叩けるようにした
nishinoyama Mar 13, 2025
2aa0b64
RegaliaRedeployment から botのRedeployStatus への変換を修正
nishinoyama Mar 13, 2025
7b1115f
スコアサーバー未登録に対するエラーの追加
nishinoyama Mar 13, 2025
3546893
SIGTERM を受け取れるようにし、そのまま終了するようにした
nishinoyama Mar 14, 2025
661f6c5
SIGINT でも落ちるようにした
nishinoyama Mar 14, 2025
425bf0e
join コマンドですっとぼける
nishinoyama Mar 14, 2025
e19cbe8
redeploy コマンドそのものを無効化
nishinoyama Mar 14, 2025
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
179 changes: 139 additions & 40 deletions src/services/regalia.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::models::{Contestant, Problem, Team};
use crate::services::contestant::{ContestantServiceError, ContestantService};
use crate::services::contestant::{ContestantService, ContestantServiceError};
use crate::services::problem::{ProblemError, ProblemService};
use crate::services::redeploy::{
RedeployJob, RedeployResult, RedeployService, RedeployStatusList, RedeployTarget,
RedeployError, RedeployJob, RedeployResult, RedeployService, RedeployStatus,
RedeployStatusList, RedeployTarget,
};
use crate::services::team::{TeamError, TeamService};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use reqwest::header::HeaderMap;
use reqwest::{Client, ClientBuilder};
use serde_derive::{Deserialize, Serialize};
Expand Down Expand Up @@ -79,7 +81,10 @@ impl TeamService for Regalia {
async fn get_teams(&self) -> anyhow::Result<Vec<Team>, TeamError> {
let response = self
.client
.post(format!("{}admin.v1.TeamService/ListTeams", self.config.baseurl))
.post(format!(
"{}admin.v1.TeamService/ListTeams",
self.config.baseurl
))
.json(&RegaliaPostListAllTeamsRequest {})
.send()
.await
Expand Down Expand Up @@ -136,6 +141,42 @@ impl ProblemService for Regalia {
}
}

#[async_trait]
impl RedeployService for Regalia {
#[tracing::instrument(skip_all, fields(target = ?target))]
async fn redeploy(&self, target: &RedeployTarget) -> RedeployResult<RedeployJob> {
todo!()
}

#[tracing::instrument(skip_all)]
async fn get_status(&self, team_id: &str) -> RedeployResult<RedeployStatusList> {
let team_code = team_id.to_string();
let response = self
.client
.post(format!(
"{}admin.v1.DeploymentService/ListDeployments",
self.config.baseurl
))
.json(&RegaliaPostListDeploymentsRequest { team_code })
.send()
.await
.map_err(|e| RedeployError::Unexpected(Box::new(e)))?;
match response.status() {
reqwest::StatusCode::OK => Ok(response
.json::<RegaliaPostListDeploymentsResponse>()
.await
.map_err(|e| RedeployError::Unexpected(Box::new(e)))?
.deployments
.into_iter()
.map(Into::into)
.collect()),
_ => Err(RedeployError::Unexpected(Box::new(
response.error_for_status().unwrap_err(),
))),
}
}
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RegaliaContestant {
Expand Down Expand Up @@ -216,37 +257,6 @@ enum RegaliaProblemType {
ProblemTypeDescriptive,
}

impl From<RegaliaContestant> for Contestant {
fn from(value: RegaliaContestant) -> Self {
Self {
name: value.name,
display_name: value.display_name,
team_id: value.team.code,
discord_id: value.discord_id,
}
}
}

impl From<RegaliaTeam> for Team {
fn from(value: RegaliaTeam) -> Self {
Self {
id: value.code,
role_name: value.name,
invitation_code: "".to_string(),
user_group_id: "".to_string(),
}
}
}

impl From<RegaliaProblem> for Problem {
fn from(value: RegaliaProblem) -> Self {
Self {
code: value.code,
name: value.title,
}
}
}

#[derive(Debug, Serialize)]
struct RegaliaPostListAllContestantsRequest {}

Expand Down Expand Up @@ -277,13 +287,102 @@ struct RegaliaPostListProblemsResponse {
problems: Vec<RegaliaProblem>,
}

#[async_trait]
impl RedeployService for Regalia {
async fn redeploy(&self, _target: &RedeployTarget) -> RedeployResult<RedeployJob> {
todo!()
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct RegaliaPostListDeploymentsRequest {
team_code: String,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RegaliaPostListDeploymentsResponse {
#[serde(default)]
deployments: Vec<RegaliaDeployment>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RegaliaDeployment {
team_code: String,
problem_code: String,
revision: i64,
latest_event: RegaliaDeploymentEventType,
#[serde(default)]
events: Vec<RegaliaDeploymentEvent>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
enum RegaliaDeploymentEventType {
DeploymentEventTypeUnspecified,
DeploymentEventTypeQueued,
DeploymentEventTypeCreating,
DeploymentEventTypeFinished,
DeploymentEventTypeError,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
struct RegaliaDeploymentEvent {
occurred_at: DateTime<Utc>,
#[serde(rename = "type")]
typ: RegaliaDeploymentEventType,
}
impl From<RegaliaContestant> for Contestant {
fn from(value: RegaliaContestant) -> Self {
Self {
name: value.name,
display_name: value.display_name,
team_id: value.team.code,
discord_id: value.discord_id,
}
}
}

async fn get_status(&self, _team_id: &str) -> RedeployResult<RedeployStatusList> {
todo!()
impl From<RegaliaTeam> for Team {
fn from(value: RegaliaTeam) -> Self {
Self {
id: value.code,
role_name: value.name,
invitation_code: "".to_string(),
user_group_id: "".to_string(),
}
}
}

impl From<RegaliaProblem> for Problem {
fn from(value: RegaliaProblem) -> Self {
Self {
code: value.code,
name: value.title,
}
}
}

impl From<RegaliaDeployment> for RedeployStatus {
fn from(value: RegaliaDeployment) -> Self {
use RegaliaDeploymentEventType::*;
let team_id = value.team_code.clone();
let problem_code = value.problem_code.clone();
let is_redeploying = matches!(value.latest_event, DeploymentEventTypeCreating);
let events = {
let mut events = value.events.iter().collect::<Vec<_>>();
events.sort_by(|a, b| a.occurred_at.cmp(&b.occurred_at));
events.reverse();
events
};
let last_redeploy_started_at = events
.iter()
.find_map(|e| matches!(e.typ, DeploymentEventTypeCreating).then(|| e.occurred_at));
let last_redeploy_completed_at = events
.iter()
.find_map(|e| matches!(e.typ, DeploymentEventTypeFinished).then(|| e.occurred_at));
Self {
team_id,
problem_code,
is_redeploying,
last_redeploy_started_at,
last_redeploy_completed_at,
}
}
}