Skip to content

Commit

Permalink
worker/jobs/git: Push squash snapshot branches to `GIT_ARCHIVE_REPO_U…
Browse files Browse the repository at this point in the history
…RL` too (#7648)
  • Loading branch information
Turbo87 authored Dec 3, 2023
1 parent d925502 commit 3d4113e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
40 changes: 16 additions & 24 deletions crates_io_index/repo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::credentials::Credentials;
use anyhow::{anyhow, Context};
use base64::{engine::general_purpose, Engine};
use crates_io_env_vars::var;
use crates_io_env_vars::{required_var, required_var_parsed, var};
use secrecy::{ExposeSecret, SecretString};
use std::path::{Path, PathBuf};
use std::process::Command;
Expand All @@ -15,54 +15,46 @@ pub struct RepositoryConfig {

impl RepositoryConfig {
pub fn from_environment() -> anyhow::Result<Self> {
let repo_url: Url = required_var_parsed("GIT_REPO_URL")?;
let is_ssh = repo_url.scheme() == "ssh";

let username = var("GIT_HTTP_USER")?;
let password = var("GIT_HTTP_PWD")?.map(SecretString::from);
let http_url = var("GIT_REPO_URL")?;

let ssh_key = var("GIT_SSH_KEY")?.map(SecretString::from);
let ssh_url = var("GIT_SSH_REPO_URL")?;
match (is_ssh, username, password) {
(true, username, password) => {
let ssh_key = SecretString::from(required_var("GIT_SSH_KEY")?);

match (username, password, http_url, ssh_key, ssh_url) {
(extra_user, extra_pass, extra_http_url, Some(encoded_key), Some(ssh_url)) => {
if let (Some(_), Some(_), Some(_)) = (extra_user, extra_pass, extra_http_url) {
if username.is_some() || password.is_some() {
warn!("both http and ssh credentials to authenticate with git are set");
info!("note: ssh credentials will take precedence over the http ones");
}

let index_location =
Url::parse(&ssh_url).expect("failed to parse GIT_SSH_REPO_URL");

let key = general_purpose::STANDARD
.decode(encoded_key.expose_secret())
.decode(ssh_key.expose_secret())
.expect("failed to base64 decode the ssh key");
let key =
String::from_utf8(key).expect("failed to convert the ssh key to a string");
let credentials = Credentials::Ssh { key: key.into() };

Ok(Self {
index_location,
index_location: repo_url,
credentials,
})
}
(Some(username), Some(password), Some(http_url), None, None) => {
let index_location = Url::parse(&http_url).expect("failed to parse GIT_REPO_URL");
(false, Some(username), Some(password)) => {
let credentials = Credentials::Http { username, password };

Ok(Self {
index_location,
index_location: repo_url,
credentials,
})
}
(_, _, Some(http_url), _, _) => {
let index_location = Url::parse(&http_url).expect("failed to parse GIT_REPO_URL");
let credentials = Credentials::Missing;

Ok(Self {
index_location,
credentials,
})
}
_ => Err(anyhow!("must have `GIT_REPO_URL` defined")),
(false, _, _) => Ok(Self {
index_location: repo_url,
credentials: Credentials::Missing,
}),
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/worker/jobs/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::worker::Environment;
use anyhow::Context;
use async_trait::async_trait;
use chrono::Utc;
use crates_io_env_vars::var_parsed;
use crates_io_index::{Crate, Repository};
use crates_io_worker::BackgroundJob;
use diesel::prelude::*;
Expand All @@ -12,6 +13,7 @@ use std::fs::{self, File};
use std::io::{BufRead, BufReader, ErrorKind, Write};
use std::process::Command;
use std::sync::Arc;
use url::Url;

#[derive(Serialize, Deserialize)]
pub struct SyncToGitIndex {
Expand Down Expand Up @@ -203,6 +205,14 @@ impl BackgroundJob for SquashIndex {
&format!("{original_head}:refs/heads/snapshot-{now}"),
]))?;

if let Some(archive_url) = var_parsed::<Url>("GIT_ARCHIVE_REPO_URL")? {
repo.run_command(Command::new("git").args([
"push",
archive_url.as_str(),
&format!("{original_head}:snapshot-{now}"),
]))?;
}

info!("The index has been successfully squashed.");

Ok(())
Expand Down

0 comments on commit 3d4113e

Please sign in to comment.