source: mergebot/trunk/mergebot/BranchActor.py @ 16

Last change on this file since 16 was 16, checked in by retracile, 15 years ago

Mergebot: Codebase as released with permission from CommProve?, plus cleanups to remove traces of that environment.

File size: 2.9 KB
Line 
1#!/usr/bin/env python
2"""Module for creating new branches for tickets"""
3# Syntax: BranchActor.py ticketnum component version requestor
4
5import os
6import sys
7import time
8import trac.env
9
10import SvnOps
11from WorkQueue import MergeBotActor, VersionToDir
12from TrackerTools import GetRepositoryPublicUrl, GetRepositoryLocalUrl, Task, \
13    GetLogFile
14
15def branch_action(trac_env, ticketnum, component, version, requestor):
16    "Create the branch"
17    task_obj = Task(trac_env, ticketnum)
18
19    # Setup logging
20    logfile = GetLogFile(trac_env, ticketnum)
21    open(logfile, "a").write("%s: branching ticket %s\n" % (time.asctime(),
22        ticketnum))
23
24    # Determine the URLS to copy from and to
25    branchdir = "branches/ticket-%s" % (ticketnum)
26    copyfrom = os.path.join(GetRepositoryLocalUrl(trac_env), component,
27        VersionToDir(version))
28    copyto = os.path.join(GetRepositoryLocalUrl(trac_env), component, branchdir)
29
30    commit_header = "Ticket #%s: %s" % (ticketnum, task_obj.GetSummary())
31
32    # Delete the branch if it already exists.  This can happen if the branch
33    # was merged, but we're still working on it.
34    if SvnOps.get_branch_info(copyto, logfile):
35        # This branch already exists.
36        commit_message = "\n".join([commit_header,
37            "    Delete old branch",
38        ])
39        new_rev = SvnOps.delete_branch(copyto, commit_message, logfile)
40        if new_rev == -1:
41            status = "branchfailed"
42            return status, task_obj
43
44    # Do the branch creationg
45    commit_message = "\n".join([commit_header,
46        "    Create branch from %s for %s." % (version, requestor),
47    ])
48    retval = SvnOps.create_branch(copyfrom, copyto, commit_message, logfile)
49    if retval:
50        # Failed for some reason.
51        status = "branchfailed"
52    else:
53        publiccopyto = os.path.join(GetRepositoryPublicUrl(trac_env), component,
54            branchdir)
55        comment = "\n".join([
56            "Created branch from %s for %s." % (version, requestor),
57            "",
58            "Browse branch [source:%s source code] and [log:%s commit log]." %
59                (os.path.join(component, branchdir),
60                os.path.join(component, branchdir)),
61            "",
62            "To checkout, run:",
63            "{{{",
64            "svn checkout %s %s-%s" % (publiccopyto, component, ticketnum),
65            "}}}",
66        ])
67        task_obj.AddComment(comment)
68        status = "branched"
69    return status, task_obj
70
71class BranchActor(MergeBotActor):
72    "Actor for creating a new branch."
73    def __init__(self, trac_env):
74        MergeBotActor.__init__(self, trac_env, "branch", branch_action)
75
76def main():
77    tracdir = sys.argv[1]
78    trac_env = trac.env.open_environment(tracdir)
79    branchingActor = BranchActor(trac_env)
80    branchingActor.AddTask(sys.argv[2:])
81    branchingActor.Run()
82
83if __name__ == "__main__":
84    main()
85
86# vim:foldcolumn=4 foldmethod=indent
87# vim:tabstop=4 shiftwidth=4 softtabstop=4 expandtab
Note: See TracBrowser for help on using the repository browser.