Skip to content

Commit

Permalink
Save a copy of shelved changelists to run buildkite builds against (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
ca-johnson authored Jun 4, 2019
1 parent c09601b commit 105af22
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
34 changes: 27 additions & 7 deletions python/buildkite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
"""
import os
import subprocess
from datetime import datetime

__ACCESS_TOKEN__ = os.environ['BUILDKITE_AGENT_ACCESS_TOKEN']
# https://github.com/buildkite/cli/blob/e8aac4bedf34cd8084a3ae7a4ab7812c611d0310/local/run.go#L403
__LOCAL_RUN__ = os.environ['BUILDKITE_AGENT_NAME'] == 'local'

__REVISION_METADATA__ = 'buildkite:perforce:revision'
__REVISION_ANNOTATION__ = "Revision: %s"
__SHELVED_METADATA__ = 'buildkite:perforce:shelved'
__SHELVED_ANNOTATION__ = "[%(timestamp)s] Saved shelved change %(original)s as %(copy)s"

def get_env():
"""Get env vars passed in via plugin config"""
Expand Down Expand Up @@ -36,13 +40,6 @@ def get_config():
conf['view'] = ['%s %s' % (v, next(view_iter)) for v in view_iter]
return conf

def get_shelved_change():
"""Get a shelved changelist that will be patched into each workspace"""
branch = os.environ.get('BUILDKITE_BRANCH', '')
if branch.isdigit():
return branch
return None

def get_metadata(key):
"""If it exists, retrieve metadata from buildkite for a given key"""
if not __ACCESS_TOKEN__ or __LOCAL_RUN__:
Expand All @@ -62,6 +59,29 @@ def set_metadata(key, value, overwrite=False):
subprocess.call(['buildkite-agent', 'meta-data', 'set', key, value])
return True

def get_users_changelist():
"""Get the shelved changelist supplied by the user, if applicable"""
branch = os.environ.get('BUILDKITE_BRANCH', '')
if branch.isdigit():
return branch

def get_build_changelist():
"""Get a saved version of the users originally supplied changelist, if available"""
return get_metadata(__SHELVED_METADATA__)

def set_build_changelist(changelist):
"""Set a shelved change that should be used instead of the user-supplied one"""
if set_metadata(__SHELVED_METADATA__, changelist):
subprocess.call([
'buildkite-agent', 'annotate',
__SHELVED_ANNOTATION__.format(**{
'timestamp': datetime.now().strftime("%m/%d/%Y %H:%M:%S"),
'original': get_users_changelist(),
'copy': changelist,
}),
'--context', __SHELVED_METADATA__
])

def get_build_revision():
"""Get a p4 revision for the build to sync to"""
if __LOCAL_RUN__:
Expand Down
17 changes: 13 additions & 4 deletions python/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import subprocess

from perforce import P4Repo
from buildkite import get_env, get_config, get_build_revision, set_build_revision, get_shelved_change

from buildkite import (get_env, get_config, get_build_revision, set_build_revision,
get_users_changelist, get_build_changelist, set_build_changelist)

def main():
"""Main"""
Expand All @@ -18,16 +18,25 @@ def main():

revision = get_build_revision()
if revision == 'HEAD':
# Resolve HEAD to a concrete revision
revision = repo.head()
set_build_revision(revision)

repo.sync(revision=revision)

if os.environ.get('BUILDKITE_CLEAN_CHECKOUT'):
repo.clean()

changelist = get_shelved_change()
if changelist:
user_changelist = get_users_changelist()
if user_changelist:
# Use existing or make a copy of the users changelist for this build
changelist = get_build_changelist()
if not changelist:
changelist = repo.backup(user_changelist)
set_build_changelist(changelist)

repo.unshelve(changelist)


if __name__ == "__main__":
main()

0 comments on commit 105af22

Please sign in to comment.