Skip to content

Commit

Permalink
Merge pull request #6 from weecology/weather
Browse files Browse the repository at this point in the history
Require version tags [no version bump]
  • Loading branch information
gmyenni authored Feb 19, 2020
2 parents 28cd8a6 + cf8238f commit 98bdc4e
Showing 1 changed file with 64 additions and 38 deletions.
102 changes: 64 additions & 38 deletions archive.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#' Update the version number
#'
#' By default update the minor version number since most changes are new data.
#' If [major] or [patch] is in the last commit summary increment the matching
#' version instead.

config <- yaml::yaml.load_file("config.yml")

Expand All @@ -16,52 +13,81 @@ git2r::config(repo,
user.email = config$deploy_email,
user.name = config$deploy_username)

# Check the most recent commit for version instructions
last_commit <- git2r::commits(repo)[[1]]
current_ver <- semver::parse_version(readLines("version.txt"))
if (grepl("Merge pull request", last_commit['summary'])){
last_commit <- git2r::commits(repo)[[2]]
}

if (grepl("\\[no version bump\\]", last_commit['summary'])) {
new_ver <- current_ver
} else if (grepl("\\[major\\]", last_commit['summary'])) {
new_ver <- semver::increment_version(current_ver, "major", 1L)
} else if (grepl("\\[patch\\]", last_commit['summary'])) {
new_ver <- semver::increment_version(current_ver, "patch", 1L)
} else {
new_ver <- semver::increment_version(current_ver, "minor", 1L)
}

writeLines(as.character(new_ver), "version.txt")

# set up the new commit
travis_build <- Sys.getenv("TRAVIS_BUILD_NUMBER")
git2r::checkout(repo, branch = "master")
commit_message <- paste("Update data and trigger archive: Travis Build",
commit_message <- paste("Update data and trigger release: Travis Build",
travis_build,
"[skip ci]")

# detect if this is a Travis CI build triggered by Cron
travis_event <- Sys.getenv("TRAVIS_EVENT_TYPE")
if (travis_event == "cron") {
# handle changes to the version number
current_ver <- semver::parse_version(readLines("version.txt"))

if (Sys.getenv("TRAVIS_EVENT_TYPE") == "cron") # is this a build triggered by Cron
{
commit_message <- paste(commit_message, "[cron]")

# check for changes to files
git_status <- git2r::status(repo)
if (length(git_status$staged) == 0 &&
length(git_status$unstaged) == 0 &&
length(git_status$untracked) == 0) # no changes to any files
{
new_ver <- current_ver
} else if (length(git_status$staged) == 0 &&
length(git_status$unstaged) > 0 && # changes to data files
length(git_status$untracked) == 0) {
new_ver <- semver::increment_version(current_ver, "minor", 1L)
} else {
stop(paste("Encountered an unexpected git status during the Cron update."))
}
} else { # this is triggered by an update to Master or by a PR on a branch
# parse the most recent commit for version instructions
last_commit <- git2r::commits(repo)[[1]]
if (grepl("Merge", last_commit['summary'], ignore.case = TRUE))
{
last_commit <- git2r::commits(repo)[[2]]
}

if (grepl("\\[no version bump\\]", last_commit['summary'], ignore.case = TRUE))
{
new_ver <- current_ver
} else if (grepl("\\[major\\]", last_commit['summary'], ignore.case = TRUE)) {
new_ver <- semver::increment_version(current_ver, "major", 1L)
} else if (grepl("\\[minor\\]", last_commit['summary'], ignore.case = TRUE)) {
new_ver <- semver::increment_version(current_ver, "minor", 1L)
} else if (grepl("\\[patch\\]", last_commit['summary'], ignore.case = TRUE)) {
new_ver <- semver::increment_version(current_ver, "patch", 1L)
} else {
stop(paste("The final commit message in a set of changes must be tagged",
"with version increment information.\nOptions include",
"[major], [minor], [patch], and [no version bump].\n",
"The last commit in this set of changes is:\n",
last_commit['summary']))
}
}

# write out the new version and add the commit
git2r::add(repo, "*")
git2r::commit(repo, message = commit_message)
writeLines(as.character(new_ver), "version.txt")

# Create a new release to trigger Zenodo archiving

github_token = Sys.getenv("GITHUB_TOKEN")
pull_request = Sys.getenv("TRAVIS_PULL_REQUEST")
branch = Sys.getenv("TRAVIS_BRANCH")

# If the version has been incremented, this is not a pull request,
# and it is the master branch of the repo, then push the updated data,
# create a new tag, push the tag and trigger a release.
if (new_ver > current_ver & branch == 'master' & pull_request == 'false'){
# If the version has been incremented (i.e. there are changes to be committed),
# this is the master branch of the repo, and
# this is not a pull request, then:
# 1. add a new commit with the update data to master branch
# 2. push the changes
# 3. create a new tag
# 4. push the tag
# 5. trigger a release.
if (new_ver > current_ver &&
Sys.getenv("TRAVIS_BRANCH") == 'master' &&
Sys.getenv("TRAVIS_PULL_REQUEST") == 'false')
{
# write out the new version and add the commit
github_token <- Sys.getenv("GITHUB_TOKEN")
git2r::checkout(repo, branch = "master")
git2r::add(repo, "*")
git2r::commit(repo, message = commit_message)

git2r::push(repo,
name = "deploy",
refspec = "refs/heads/master",
Expand Down

0 comments on commit 98bdc4e

Please sign in to comment.