Skip to content
This repository was archived by the owner on Aug 20, 2024. It is now read-only.

Commit

Permalink
semantic version releases (issue #463) (#575)
Browse files Browse the repository at this point in the history
* Added .editorconfig

Idea runs editorconfig by default, all other editors support this via extensions

* Add `X-App-Version` header to all responses

* Added cargo version bumping to travis

* Rather have one commit for version bumping

* init-reset script should fail loudly

* Cargo fmt fixes

* #rustnoob caching the header name and header value

Not necessary but wanted to try it out, also this is run on every request so may as well be efficient

* Only tag for master builds
  • Loading branch information
sdbondi authored and stringhandler committed Nov 21, 2018
1 parent 9693be0 commit 802fe63
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org

root = true

[*]
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
indent_style = space
indent_size = 4
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ before_install:

before_script:

after_script:

after_script:
- ./scripts/bump-version.sh --with-git

37 changes: 37 additions & 0 deletions api/src/middleware/app_version_header.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use actix_web::http::header::{HeaderName, HeaderValue};
use actix_web::http::HttpTryFrom;
use actix_web::middleware::{Middleware, Response};
use actix_web::{HttpRequest, HttpResponse, Result};

use server::AppState;

const SEMVER_HEADER_NAME: &'static str = "X-App-Version";
const APP_VERSION: &'static str = env!("CARGO_PKG_VERSION");

pub struct AppVersionHeader {
header_name: HeaderName,
app_version: HeaderValue,
}

impl AppVersionHeader {
pub fn new() -> AppVersionHeader {
AppVersionHeader {
header_name: HeaderName::try_from(SEMVER_HEADER_NAME).unwrap(),
app_version: HeaderValue::from_static(APP_VERSION),
}
}
}

impl Middleware<AppState> for AppVersionHeader {
fn response(
&self,
_request: &HttpRequest<AppState>,
mut response: HttpResponse,
) -> Result<Response> {
response
.headers_mut()
.insert(&self.header_name, self.app_version.clone());

Ok(Response::Done(response))
}
}
2 changes: 2 additions & 0 deletions api/src/middleware/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub use self::app_version_header::*;
pub use self::database_transaction::*;

mod app_version_header;
mod database_transaction;
3 changes: 2 additions & 1 deletion api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use actix_web::middleware::Logger;
use actix_web::{server, App};
use config::Config;
use db::*;
use middleware::*;
use middleware::{AppVersionHeader, DatabaseTransaction};
use routing;
use utils::ServiceLocator;

Expand Down Expand Up @@ -35,6 +35,7 @@ impl Server {
move || {
App::with_state(AppState::new(config.clone()))
.middleware(DatabaseTransaction::new())
.middleware(AppVersionHeader::new())
.middleware(Logger::new(
r#"{\"remote_ip\":\"%a\", \"user_agent\": \"%{User-Agent}i\", \"request\": \"%r\", \"status_code\": %s, \"response_time\": %D}"#,
)).configure(|a| {
Expand Down
1 change: 1 addition & 0 deletions db/scripts/init-reset.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env bash

set -e
set -o allexport
source .env
set +o allexport
Expand Down
38 changes: 38 additions & 0 deletions scripts/bump-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

set -e

if [[ "$TRAVIS_BRANCH" != "master" ]];then
echo "Skipping tag-version because branch is not master (but is '$TRAVIS_BRANCH')"
exit 0
fi

new_version=""

function bump_patch {
local file="$1"
local version=`sed -En 's/version[[:space:]]*=[[:space:]]*"([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)"/\1/p' < $file`
new_version=`echo $version | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{$NF=sprintf("%0*d", length($NF), ($NF+1)); print}'`
local search='^(version[[:space:]]*=[[:space:]]*).+'
local replace="\1\"${new_version}\""

sed -i ".tmp" -E "s/${search}/${replace}/g" "$1"
echo "$file bumped from $version to $new_version"
rm "$1.tmp"
}

FILES=( "db/Cargo.toml" "api/Cargo.toml" )

for target in "${FILES[@]}"; do
bump_patch "$target"
if [[ $1 == "--with-git" ]]; then
git add "$target"
fi
done

if [[ $1 == "--with-git" ]]; then
git commit -m "Version bump to ${new_version}"
git tag ${new_version}
git push origin
git push --tags
fi

0 comments on commit 802fe63

Please sign in to comment.