• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright 2019 The ChromiumOS Authors
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7"""A custom script example that utilizes the .JSON contents of the tryjob."""
8
9
10import json
11import sys
12
13from update_tryjob_status import TryjobStatus
14
15
16def main():
17    """Determines the exit code based off of the contents of the .JSON file."""
18
19    # Index 1 in 'sys.argv' is the path to the .JSON file which contains
20    # the contents of the tryjob.
21    #
22    # Format of the tryjob contents:
23    #   {
24    #     "status" : [TRYJOB_STATUS],
25    #     "buildbucket_id" : [BUILDBUCKET_ID],
26    #     "extra_cls" : [A_LIST_OF_EXTRA_CLS_PASSED_TO_TRYJOB],
27    #     "url" : [GERRIT_URL],
28    #     "builder" : [TRYJOB_BUILDER_LIST],
29    #     "rev" : [REVISION],
30    #     "link" : [LINK_TO_TRYJOB],
31    #     "options" : [A_LIST_OF_OPTIONS_PASSED_TO_TRYJOB]
32    #   }
33    abs_path_json_file = sys.argv[1]
34
35    with open(abs_path_json_file) as f:
36        tryjob_contents = json.load(f)
37
38    CUTOFF_PENDING_REVISION = 369416
39
40    SKIP_REVISION_CUTOFF_START = 369420
41    SKIP_REVISION_CUTOFF_END = 369428
42
43    if tryjob_contents["status"] == TryjobStatus.PENDING.value:
44        if tryjob_contents["rev"] <= CUTOFF_PENDING_REVISION:
45            # Exit code 0 means to set the tryjob 'status' as 'good'.
46            sys.exit(0)
47
48        # Exit code 124 means to set the tryjob 'status' as 'bad'.
49        sys.exit(124)
50
51    if tryjob_contents["status"] == TryjobStatus.BAD.value:
52        # Need to take a closer look at the contents of the tryjob to then decide
53        # what that tryjob's 'status' value should be.
54        #
55        # Since the exit code is not in the mapping, an exception will occur which
56        # will save the file in the directory of this custom script example.
57        sys.exit(1)
58
59    if tryjob_contents["status"] == TryjobStatus.SKIP.value:
60        # Validate that the 'skip value is really set between the cutoffs.
61        if (
62            SKIP_REVISION_CUTOFF_START
63            < tryjob_contents["rev"]
64            < SKIP_REVISION_CUTOFF_END
65        ):
66            # Exit code 125 means to set the tryjob 'status' as 'skip'.
67            sys.exit(125)
68
69        if tryjob_contents["rev"] >= SKIP_REVISION_CUTOFF_END:
70            sys.exit(124)
71
72
73if __name__ == "__main__":
74    main()
75