1 (* Poll Bugzilla, find new tasks, and add them to the database.
2 * This runs from a cron job.
9 let quote = Filename.quote
12 "Red Hat Enterprise Linux 6",
13 Printer.Date.from_string "2016-11-14";
14 "Red Hat Enterprise Linux 7",
15 Printer.Date.from_string "2017-04-03";
18 let assigned_to = "rjones@redhat.com"
19 let states = ["NEW";"ASSIGNED";"ON_DEV";"POST";"MODIFIED"]
22 let rex = Pcre.regexp "^(\\d+) (\\S+) (.*)$" in
25 let dbh = PGOCaml.connect ~database:"todo" () in
28 fun (product, my_deadline) ->
32 sprintf "bugzilla query -p %s -a %s -t %s --outputformat='%%{bug_id} %%{component} %%{short_desc}'"
33 (quote product) (quote assigned_to)
34 (quote (String.concat "," states)) in
35 let chan = open_process_in cmd in
37 (try while true do lines := input_line chan :: !lines done
38 with End_of_file -> ());
39 let stat = close_process_in chan in
43 eprintf "error: bugzilla command: exited with error %d\n%!" i;
46 eprintf "error: bugzilla command: killed by signal %d\n%!" i;
49 eprintf "error: bugzilla command: stopped by signal %d\n%!" i;
55 (* The output is <BUG> <COMPONENT> <DESCRIPTION>.
56 * The component is turned into a tag.
61 let subs = Pcre.exec ~rex line in
62 let bugid = Int32.of_string (Pcre.get_substring subs 1) in
63 let component = Pcre.get_substring subs 2 in
64 let description = Pcre.get_substring subs 3 in
66 PGOCaml.begin_work dbh;
68 (* If the bug doesn't exist in the tasks table, add it. *)
69 let rows = PGSQL(dbh) "select id from tasks
70 where rhbz = $bugid" in
75 printf "new task: RHBZ#%ld %s %s\n%!"
76 bugid component description;
77 PGSQL(dbh) "insert into tasks (description, rhbz)
78 values ($description, $bugid)";
79 PGOCaml.serial4 dbh "tasks_id_seq"
80 | _ -> assert false in
82 (* If the component doesn't exist as a tag, create it. *)
84 PGSQL(dbh) "select id from tags where name = $component" in
89 printf "new tag: %s\n%!" component;
90 PGSQL(dbh) "insert into tags (name, colour)
91 values ($component, 'blue')";
92 PGOCaml.serial4 dbh "tags_id_seq"
93 | _ -> assert false in
95 (* If the bug is not tagged with the component, tag it.
96 * If the bug changes component, this creates a second
97 * tag, which is intentional.
100 PGSQL(dbh) "select 1 from tags_tasks, tasks, tags
101 where tags_tasks.taskid = tasks.id
102 and tags_tasks.tagid = tags.id
103 and tasks.rhbz = $bugid
104 and tags.name = $component" in
105 if rows <> [Some 1_l] then
106 PGSQL(dbh) "insert into tags_tasks (tagid, taskid)
107 values ($tagid, $taskid)";
109 (* Add the bug to the todo table with the appropriate
110 * deadline. But first check if it is present in any
114 PGSQL(dbh) "select 1 from imminent where taskid = $taskid" in
116 PGSQL(dbh) "select 1 from todo where taskid = $taskid" in
118 PGSQL(dbh) "select 1 from ideas where taskid = $taskid" in
120 PGSQL(dbh) "select 1 from retired where taskid = $taskid" in
121 if r1 <> [Some 1_l] && r2 <> [Some 1_l] &&
122 r3 <> [Some 1_l] && r4 <> [Some 1_l] then
123 PGSQL(dbh) "insert into todo (taskid, deadline)
124 values ($taskid, $my_deadline::date)";
129 eprintf "error: line did not match regular expression: %s\n%!"