rustc_commits.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 get_commits_with_artifacts(db: &DbClient) -> anyhow::Result<Vec<Commit>> {
  23. let commits = db
  24. .query(
  25. "
  26. select sha, parent_sha, time, pr
  27. from rustc_commits
  28. where time >= current_date - interval '168 days'
  29. order by time desc;",
  30. &[],
  31. )
  32. .await
  33. .context("Getting commit data")?;
  34. let mut data = Vec::with_capacity(commits.len());
  35. for commit in commits {
  36. let sha: String = commit.get(0);
  37. let parent_sha: String = commit.get(1);
  38. let time: DateTime<FixedOffset> = commit.get(2);
  39. let pr: Option<i32> = commit.get(3);
  40. data.push(Commit {
  41. sha,
  42. parent_sha,
  43. time,
  44. pr: pr.map(|n| n as u32),
  45. });
  46. }
  47. Ok(data)
  48. }