Skip to content

Commit

Permalink
Set build description and revision (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
ca-johnson authored Jul 25, 2019
1 parent 2d5b851 commit 35dd400
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
11 changes: 7 additions & 4 deletions python/buildkite.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
__LOCAL_RUN__ = os.environ['BUILDKITE_AGENT_NAME'] == 'local'

__REVISION_METADATA__ = 'buildkite:perforce:revision'
__REVISION_ANNOTATION__ = "Revision: %s"
__SHELVED_METADATA__ = 'buildkite:perforce:shelved'
__SHELVED_ANNOTATION__ = "Saved shelved change {original} as {copy}"

Expand Down Expand Up @@ -80,7 +79,8 @@ def set_build_changelist(changelist):
'original': get_users_changelist(),
'copy': changelist,
}),
'--context', __SHELVED_METADATA__
'--context', __SHELVED_METADATA__,
'--style', 'info',
])

def get_build_revision():
Expand All @@ -92,5 +92,8 @@ def get_build_revision():

def set_build_revision(revision):
"""Set the p4 revision for following jobs in this build"""
if set_metadata(__REVISION_METADATA__, revision):
subprocess.call(['buildkite-agent', 'annotate', __REVISION_ANNOTATION__ % revision, '--context', __REVISION_METADATA__])
set_metadata(__REVISION_METADATA__, revision)

def set_build_info(revision, description):
"""Set the description and commit number in the UI for this build by mimicking a git repo"""
set_metadata('buildkite:git:commit', 'commit %s\n\n\t%s' % (revision, description))
8 changes: 6 additions & 2 deletions python/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

def main():
"""Main"""
Expand All @@ -19,7 +19,8 @@ def main():
revision = get_build_revision()
if revision == 'HEAD':
# Resolve HEAD to a concrete revision
revision = repo.head()
head = repo.head()
revision = '@%s' % head
set_build_revision(revision)

repo.sync(revision=revision)
Expand All @@ -37,6 +38,9 @@ def main():

repo.unshelve(changelist)

description = repo.description(get_users_changelist() or head)
set_build_info(head, description)


if __name__ == "__main__":
main()
6 changes: 5 additions & 1 deletion python/perforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ def info(self):

def head(self):
"""Get current head revision"""
return '@%s' % self.perforce.run_counter("maxCommitChange")[0]['value']
return self.perforce.run_counter("maxCommitChange")[0]['value']

def description(self, changelist):
"""Get description of a given changelist"""
return self.perforce.run_describe(str(changelist))[0]['desc']

def sync(self, revision=None):
"""Sync the workspace"""
Expand Down
2 changes: 1 addition & 1 deletion python/test_perforce.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_fixture(capsys):
# Returns [metadata, contents]
content = repo.perforce.run_print("//depot/file.txt")[1]
assert content == "Hello World\n"
assert repo.head() == "@2", "Unexpected head revision"
assert repo.head() == "2", "Unexpected head revision"

shelved_change = repo.perforce.run_describe('-sS', '3')
assert len(shelved_change) > 0, "Shelved changelist was missing"
Expand Down

0 comments on commit 35dd400

Please sign in to comment.