| # Copyright 2017 The Fuchsia Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style license that can be |
| # found in the LICENSE file. |
| |
| import base64 |
| import hashlib |
| import re |
| |
| from recipe_engine import recipe_test_api |
| |
| |
| class GitilesTestApi(recipe_test_api.RecipeTestApi): |
| def hash(self, *bases): |
| return hashlib.sha1(":".join(bases).encode()).hexdigest() |
| |
| def log( |
| self, |
| step_name, |
| s, |
| n=3, |
| add_change_id=False, |
| extra_footers=None, |
| committer_email=None, |
| ): |
| commits = [] |
| for i in range(n): |
| commit = "fake %s hash %d" % (s, i) |
| name = "Fake %s" % (s) |
| email = committer_email or "fake_%s@fake_%i.email.com" % (s, i) |
| |
| message_lines = ["fake %s msg %d" % (s, i)] |
| |
| footers = {} |
| if extra_footers: |
| footers.update(extra_footers) |
| if add_change_id: |
| # Using commit from above as Change-Id, but removing whitespace |
| # and possibly future punctuation. |
| alphanumeric_only_commit = re.sub(r"\W+", "", commit) |
| footers["Change-Id"] = "I%s" % alphanumeric_only_commit |
| |
| for key, value in sorted(footers.items()): |
| message_lines.append("%s: %s" % (key, value)) |
| |
| commits.append( |
| { |
| "id": self.hash(commit), |
| "tree": self.hash("tree", commit), |
| "parents": [self.hash("parent", commit)], |
| "author": { |
| "name": name, |
| "email": email, |
| "time": "Mon Jan 01 00:00:00 2015", |
| }, |
| "committer": { |
| "name": name, |
| "email": email, |
| "time": "Mon Jan 01 00:00:00 2015", |
| }, |
| "message": "\n".join(message_lines), |
| "tree_diff": [ |
| { |
| "type": "add", |
| "old_id": 40 * "0", |
| "old_mode": 0, |
| "new_id": self.hash("file", f, commit), |
| "new_mode": 33188, |
| "new_path": f, |
| } |
| for f in ["%s.py" % (chr(i + ord("a")))] |
| ], |
| } |
| ) |
| return self.step_data(step_name, self.m.json.output(commits)) |
| |
| def fetch(self, step_name, data): |
| return self.m.url.text(step_name, base64.b64encode(data.encode())) |