|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +################################################################################################# |
| 4 | +# |
| 5 | +# creattach.py -- Associate a file with an Artifact as an Attachment |
| 6 | +# |
| 7 | +USAGE = """ |
| 8 | +Usage: py creattach.py <ArtifactIdentifier> <filename> |
| 9 | +""" |
| 10 | +################################################################################################# |
| 11 | + |
| 12 | +import sys |
| 13 | +import re |
| 14 | +import base64 |
| 15 | + |
| 16 | +from pyral import Rally, rallySettings |
| 17 | + |
| 18 | +################################################################################################# |
| 19 | + |
| 20 | +errout = sys.stderr.write |
| 21 | + |
| 22 | +ARTY_FOR_PREFIX = {'S' : 'UserStory', 'US' : 'UserStory', 'DE' : 'Defect', 'TC' : 'TestCase'} |
| 23 | + |
| 24 | +COMMON_ATTRIBUTES = ['_type', 'oid', '_ref', '_CreatedAt', '_hydrated', 'Name'] |
| 25 | + |
| 26 | +ATTACHMENT_ATTRIBUTES = ['oid', 'ObjectID', '_type', '_ref', '_CreatedAt', 'Name', |
| 27 | + 'CreationDate', 'Description', |
| 28 | + 'Content', 'ContentType', 'Size', |
| 29 | + 'Subscription', |
| 30 | + 'Workspace', |
| 31 | + 'Artifact', |
| 32 | + 'User' |
| 33 | + ] |
| 34 | + |
| 35 | +ATTACHMENT_CONTENT_ATTRS = """ |
| 36 | + Workspace ref |
| 37 | +
|
| 38 | + Content base64 content |
| 39 | +""" |
| 40 | + |
| 41 | +ATTACHMENT_IMPORTANT_ATTRS = """ |
| 42 | + Subscription ref (supplied at creation) |
| 43 | + Workspace ref (supplied at creation) |
| 44 | +
|
| 45 | + Name STRING Required (name of the file, like foo.txt or ThePlan.doc) |
| 46 | + User ref to User Required Settable (User who added the object) |
| 47 | +
|
| 48 | + Content ref to AttachmentContent |
| 49 | + Size INTEGER Required |
| 50 | + ContentType STRING Required |
| 51 | +
|
| 52 | +
|
| 53 | + Artifact ref to Artifact (optional field) |
| 54 | +
|
| 55 | + Description TEXT Optional |
| 56 | +
|
| 57 | +""" |
| 58 | + |
| 59 | +ATTACHMENT_CREATION_ATTACHING_SEQUENCE = """ |
| 60 | + 1) Create the AttachmentContent item with content in base64 encoding |
| 61 | + Get the oid of this created item back |
| 62 | +
|
| 63 | + 2) Create the Attachment item |
| 64 | + using the ref to newly created AttachmentContent item. |
| 65 | + you also have to set |
| 66 | + the User ref (from the person who "uploaded" the AttachmentContent |
| 67 | + the Name of the file associated with the AttachmentContent.Content |
| 68 | + the Size of the base64 encoded stuff |
| 69 | + the ContentType (mime type, like text/xml, text/html, etc.) |
| 70 | + |
| 71 | + unclear at this time if you can set the Artifact ref ... |
| 72 | +
|
| 73 | + you could also provide Description text |
| 74 | + |
| 75 | +""" |
| 76 | + |
| 77 | +################################################################################################# |
| 78 | + |
| 79 | +def main(args): |
| 80 | + options = [opt for opt in args if opt.startswith('--')] |
| 81 | + args = [arg for arg in args if arg not in options] |
| 82 | + server, user, password, workspace, project = rallySettings(options) |
| 83 | + print " ".join(["|%s|" % item for item in [server, user, '********', workspace, project]]) |
| 84 | + rally = Rally(server, user, password, workspace=workspace, version="1.30") # specify the Rally server and credentials |
| 85 | + rally.enableLogging('rally.hist.creattach') # name of file you want logging to go to |
| 86 | + |
| 87 | + if len(args) != 2: |
| 88 | + errout('ERROR: You must supply an Artifact identifier and an attachment file name') |
| 89 | + errout(USAGE) |
| 90 | + sys.exit(1) |
| 91 | + |
| 92 | + target, attachment_file_name = args |
| 93 | + artifact = validateTarget(rally, target) |
| 94 | + |
| 95 | + me = rally.getUserInfo(username=user).pop(0) |
| 96 | + #print "%s user oid: %s" % (user, me.oid) |
| 97 | + |
| 98 | + att_content = base64.encodestring(open(attachment_file_name, 'r').read()) |
| 99 | + ac_size = len(att_content) |
| 100 | + ac_info = {"Content" : att_content} |
| 101 | + |
| 102 | + ac = rally.create('AttachmentContent', ac_info, project=None) |
| 103 | + #print "created AttachmentContent: %s with size of %d" % (ac.oid, ac_size) |
| 104 | + |
| 105 | + attachment_info = { |
| 106 | + #"Subscription" : subs.ref , (will default to current) |
| 107 | + #"Workspace" : wksp.ref , (will default to current) |
| 108 | + |
| 109 | + "Name" : attachment_file_name, |
| 110 | + "Content" : ac.ref, #ref to AttachmentContent |
| 111 | + "Size" : ac_size, |
| 112 | + "ContentType" : 'text/plain', |
| 113 | + |
| 114 | + "User" : me.ref, |
| 115 | + |
| 116 | + "Artifact" : artifact.ref # (optional field) |
| 117 | + } |
| 118 | + |
| 119 | + att = rally.create('Attachment', attachment_info, project=None) |
| 120 | + print "created Attachment: %s with Name: %s" % (att.oid, att.Name) |
| 121 | + |
| 122 | +################################################################################################# |
| 123 | + |
| 124 | +def validateTarget(rally, target): |
| 125 | + mo = re.match('^(S|US|DE|TC)\d+$', target) |
| 126 | + if not mo: |
| 127 | + errout("Target artifact identification flawed, invalid FormattedID value\n") |
| 128 | + sys.exit(2) |
| 129 | + prefix = mo.group(1) |
| 130 | + entity = ARTY_FOR_PREFIX.get(prefix, None) |
| 131 | + if not entity: |
| 132 | + errout("Target artifact identification flawed, unknown prefix: %s\n" % prefix) |
| 133 | + sys.exit(3) |
| 134 | + |
| 135 | + ident_query = 'FormattedID = %s' % target |
| 136 | + response = rally.get(entity, fetch=True, query=ident_query, project=None) |
| 137 | + |
| 138 | + if response.errors: |
| 139 | + errout("Request could not be successfully serviced, error code: %d\n" % response.status_code) |
| 140 | + errout("\n".join(response.errors)) |
| 141 | + sys.exit(4) |
| 142 | + |
| 143 | + if response.resultCount == 0: |
| 144 | + errout('No item found for %s %s\n' % (entity, target)) |
| 145 | + sys.exit(5) |
| 146 | + elif response.resultCount > 1: |
| 147 | + errout('ERROR: more than 1 item returned matching your criteria for the target\n') |
| 148 | + sys.exit(6) |
| 149 | + |
| 150 | + artifact = response.next() |
| 151 | + |
| 152 | + return artifact |
| 153 | + |
| 154 | +################################################################################################# |
| 155 | +################################################################################################# |
| 156 | + |
| 157 | +if __name__ == '__main__': |
| 158 | + main(sys.argv[1:]) |
| 159 | + |
0 commit comments