rustc_commits.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. use anyhow::Context as _;
  2. use chrono::{DateTime, FixedOffset};
  3. use tokio_postgres::Client as DbClient;
  4. /// A bors merge commit.
  5. #[derive(Debug, serde::Serialize)]
  6. pub struct Commit {
  7. pub sha: String,
  8. pub parent_sha: String,
  9. pub time: DateTime<FixedOffset>,
  10. pub pr: Option<u32>,
  11. }
  12. pub async fn record_commit(db: &DbClient, commit: Commit) -> anyhow::Result<()> {
  13. let pr = commit.pr.expect("commit has pr");
  14. db.execute(
  15. "INSERT INTO rustc_commits (sha, parent_sha, time, pr) VALUES ($1, $2, $3, $4) ON CONFLICT DO NOTHING",
  16. &[&commit.sha, &commit.parent_sha, &commit.time, &(pr as i32)],
  17. )
  18. .await
  19. .context("inserting commit")?;
  20. Ok(())
  21. }
  22. pub async fn has_commit(db: &DbClient, sha: &str) -> bool {
  23. !db.query("SELECT 1 FROM rustc_commits WHERE sha = $1", &[&sha])
  24. .await
  25. .unwrap()
  26. .is_empty()
  27. }
  28. pub async fn get_missing_commits(db: &DbClient) -> Vec<String> {
  29. let missing = db
  30. .query(
  31. "
  32. SELECT parent_sha
  33. FROM rustc_commits
  34. WHERE parent_sha NOT IN (
  35. SELECT sha
  36. FROM rustc_commits
  37. )",
  38. &[],
  39. )
  40. .await
  41. .unwrap();
  42. missing.into_iter().map(|row| row.get(0)).collect()
  43. }
  44. pub async fn get_commits_with_artifacts(db: &DbClient) -> anyhow::Result<Vec<Commit>> {
  45. let commits = db
  46. .query(
  47. "
  48. select sha, parent_sha, time, pr
  49. from rustc_commits
  50. where time >= current_date - interval '168 days'
  51. order by time desc;",
  52. &[],
  53. )
  54. .await
  55. .context("Getting commit data")?;
  56. let mut data = Vec::with_capacity(commits.len());
  57. for commit in commits {
  58. let sha: String = commit.get(0);
  59. let parent_sha: String = commit.get(1);
  60. let time: DateTime<FixedOffset> = commit.get(2);
  61. let pr: Option<i32> = commit.get(3);
  62. data.push(Commit {
  63. sha,
  64. parent_sha,
  65. time,
  66. pr: pr.map(|n| n as u32),
  67. });
  68. }
  69. Ok(data)
  70. }