summaryrefslogtreecommitdiff
path: root/post-build-report.py
blob: 868e737ae75bb6a987f618ca8ae3fa76aaee2b84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
import os
import sys
import json
import httplib
import logging

from urlparse import urljoin, urlsplit


logging.basicConfig(format='%(levelname)s:  %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
httplib.HTTPConnection.debuglevel = 1


RESULT_ENDPOINT = "/api/result/"


def _push_object(auth_pw, backend_url, endpoint, params):
    usplit = urlsplit(backend_url)
    url = urljoin(backend_url, endpoint)

    logger.info("Submitting to URL: %s" % url)

    headers = {
        "Content-type": "application/json",
        "Accept": "application/json",
        "Authorization": "Token %s" % auth_pw
    }

    conn = None
    if usplit.scheme.lower() == "http":
        conn = httplib.HTTPConnection(usplit.netloc)
    if usplit.scheme.lower() == "https":
        conn = httplib.HTTPSConnection(usplit.netloc)

    if conn is None:
        print "Unknown scheme: %s" % usplit.scheme
        sys.exit(1)

    conn.request("POST", endpoint, json.dumps(params), headers)

    response = conn.getresponse()
    if response.status < 300:
        return response.read()
    else:
        logger.warn(response.status)
        logger.warn(response.reason)
        logger.warn(response.read())
    return []


def _get_manifest(workspace_path):
    manifest_path = os.path.join(workspace_path, "pinned-manifest.xml")
    print "Searching for: %s" % manifest_path
    if os.path.exists(manifest_path):
        with open(manifest_path, "r") as manifest_file:
            return manifest_file.read()
    print "Manifest not found"
    return None



if __name__ == '__main__':
    jenkins_project_name = os.environ.get("SOURCE_PROJECT_NAME")

    jenkins_build_number = os.environ.get("SOURCE_BUILD_NUMBER")
    jenkins_build_id = os.environ.get("SOURCE_BUILD_ID")
    jenkins_build_url = os.environ.get("SOURCE_BUILD_URL")

    branch_name = os.environ.get("SOURCE_BRANCH_NAME", "")

    art_url = os.environ.get("ART_URL", "http://localhost:8000/")
    art_token = os.environ.get("ART_TOKEN")

    manifest = _get_manifest(os.environ.get("WORKSPACE"))
    test_jobs = os.environ.get("LAVA_JOB_IDS", "")

    if jenkins_build_number is None:
        print "Build number not set. Exiting!"
        sys.exit(1)
    if jenkins_project_name is None:
        print "Project name not set. Exiting!"
        sys.exit(1)
    if jenkins_build_url is None:
        print "Build URL not set. Exiting!"
        sys.exit(1)
    if art_token is None:
        print "ART token not set. Exiting!"
        sys.exit(1)
    if not manifest:
        print "Manifest missing. Exiting!"
        sys.exit(1)

    print "Registered test jobs: %s" % test_jobs

    params = {
        'name': jenkins_project_name,

        'build_id': jenkins_build_id,
        'build_url': jenkins_build_url,
        'build_number': jenkins_build_number,

        'test_jobs': test_jobs,
        'manifest': manifest,
        'branch_name': branch_name,

        "gerrit_change_number": os.environ.get("SOURCE_GERRIT_CHANGE_NUMBER"),
        "gerrit_patchset_number":os.environ.get("SOURCE_GERRIT_PATCHSET_NUMBER"),
        "gerrit_change_url": os.environ.get("SOURCE_GERRIT_CHANGE_URL"),
        "gerrit_change_id": os.environ.get("SOURCE_GERRIT_CHANGE_ID", "")
    }

    print params

    _push_object(art_token, art_url, RESULT_ENDPOINT, params)