1#!/usr/local/bin/python3.8
2
3#################################################################################################
4#
5#  repoitems.py - show changesets associated with a particular repository
6#
7USAGE = """
8Usage: python repoitems.py <repository_name>
9"""
10#################################################################################################
11
12__doc__ = """
13    A Changeset item has these attributes:
14        ObjectID
15        CreationDate
16        Name
17        Subscription
18        Workspace
19        SCMRepository
20        Revision
21        CommitTimestamp
22        Author
23        Message
24        Changes
25        Artifacts
26        Builds
27"""
28
29#################################################################################################
30
31import sys, os
32import time
33
34from pyral import Rally, rallyWorkset
35
36#################################################################################################
37
38ITEM_LIMIT = 1000
39
40errout = sys.stderr.write
41
42oid_cache = {}
43
44#################################################################################################
45
46def main(args):
47
48    options = [opt for opt in args if opt.startswith('--')]
49    args    = [arg for arg in args if arg not in options]
50    if not args:
51        print(USAGE)
52        sys.exit(9)
53    server, username, password, apikey, workspace, project = rallyWorkset(options)
54    if apikey:
55        rally = Rally(server, apikey=apikey, workspace=workspace, project=project)
56    else:
57        rally = Rally(server, user=username, password=password, workspace=workspace, project=project)
58    rally.enableLogging('rally.hist.chgsets')  # name of file you want the logging to go to
59
60    repo_name = args.pop(0)
61    since = None
62    if args:
63        since = args.pop(0)
64        try:
65            days = int(since)
66            now = time.time()
67            since_ts = now - (days * 86400)
68            since = time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime(since_ts))
69        except:
70            since = None
71
72    showRepoItems(rally, repo_name, workspace=workspace, limit=ITEM_LIMIT, since_date=since)
73
74#################################################################################################
75
76def showRepoItems(rally, repo_name, workspace=None, limit=200, order="ASC", since_date=None):
77
78    by_repo = 'SCMRepository.Name = "%s"' % repo_name
79    criteria = by_repo
80    if since_date:
81        date_cond = "CommitTimestamp >= %s" % since_date
82        criteria = [by_repo, date_cond]
83
84    try:
85        response = rally.get('Changeset', fetch=True,
86                             order="CommitTimestamp %s" % order,
87                             query=criteria,
88                             workspace=workspace, project=None,
89                             pagesize=200, limit=limit)
90    except Exception as msg:
91        print(msg)
92        return None
93
94    if response.errors:
95        errout("Changeset request could not be successfully serviced, error code: %d\n" % \
96               response.status_code)
97        return None
98
99    print("Workspace: %s  SCMRepository: %s  Changesets: %s " % \
100          (workspace, repo_name, response.resultCount))
101    for cs in response:
102        author    = cs.Author.UserName if cs.Author else "-None-"
103        committed = cs.CommitTimestamp.replace('T', ' ')
104
105        print("%-12.12s  %-42.42s %-19.19s Z %s  %s" % \
106              (cs.SCMRepository.Name, cs.Revision, committed, author, cs.oid))
107        print("    |%s|" % cs.Message)
108
109        if len(cs.Artifacts) == 0:
110            print "    changeset %s - %s has no artifacts"  % (cs.SCMRepository.Name, cs.Revision)
111            continue
112
113        artifact_idents = [art.FormattedID for art in cs.Artifacts]
114        if artifact_idents:
115            print("         artifacts mentioned: %s" % " ".join(artifact_idents))
116##
117##        for artifact in cs.Artifacts:
118##            print("    %s  |%s|  |%s|" % (artifact.FormattedID, artifact.Workspace.Name, artifact.Project.Name))
119##
120
121        # If we iterate over change items via cs.Changes, then we later have to do lazy load
122        # for the change attributes on a per Change basis, which is relatively slow
123        # So, instead we go get all Change items associated with the Changeset
124        # and get the Change attributes populated, so we don't do a lazy load
125#        changes = rally.get('Change', fetch='Action,PathAndFilename,Changeset',
126#                             query="Changeset = %s" % cs.ref,
127#                             workspace=workspace, project=None,
128#                             pagesize=200, limit=limit)
129#        for change in changes:
130#            print "      %s  %s" % (change.Action, change.PathAndFilename)
131
132#################################################################################################
133#################################################################################################
134
135if __name__ == '__main__':
136    main(sys.argv[1:])
137