-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIntegration.js
More file actions
169 lines (149 loc) · 6.06 KB
/
Integration.js
File metadata and controls
169 lines (149 loc) · 6.06 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Original author: MattN
/* exported SETTINGS, createSpreadsheetEditTrigger, editTrigger, loadSettings */
/* global Bug, lookForBugNumberChange */
var SETTINGS = {
HEADER_ROWS: 1, // Rows to ignore
BUG_ID_COLUMN: 1, // = A
BUG_COLUMNS: ["summary", "status", "nickname:assigned_to_detail"],
ADDITIONAL_FETCHED_FIELDS: ["assigned_to", "resolution", "whiteboard", "flags", "cf_qa_whiteboard"]
};
// TODO: Be careful of performance and quota with update all. Perhaps just a batch instead of all. e.g. with last update column deletion.
// TODO: deal with existing BG colours and conditional formatting
// TODO: maybe don't assign same BUG_ID_COLUMN for the whole document, only sheet
// TODO: support clock triggers
function createSpreadsheetEditTrigger() {
// Only one per type per spreadsheet is allowed so remove any existing first.
removeSpreadsheetEditTrigger();
var ss = SpreadsheetApp.getActive();
// TODO: only active sheet
ScriptApp.newTrigger('editTrigger')
.forSpreadsheet(ss)
.onEdit()
.create();
}
function removeSpreadsheetEditTrigger() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var triggers = ScriptApp.getUserTriggers(ss);
for (var i = 0; i < triggers.length; i++) {
if (triggers[i].getEventType() != ScriptApp.EventType.ON_EDIT) {
continue;
}
ScriptApp.deleteTrigger(triggers[i]);
}
}
// Event object described at https://developers.google.com/apps-script/guides/triggers/events
function editTrigger(e) {
lookForBugNumberChange(e);
}
/**
* Runs when the add-on is installed.
*/
/* eslint-disable-next-line no-unused-vars */
function onInstall() {
onOpen();
}
/**
* Adds a custom menu to the active spreadsheet.
* The onOpen() function, when defined, is automatically invoked whenever the
* spreadsheet is opened.
*/
function onOpen() {
var addonMenu = SpreadsheetApp.getUi().createAddonMenu();
addonMenu
.addItem("Update active sheet", "updateActiveSheet")
.addItem("Update selection", "updateActiveRange")
.addSeparator()
.addSubMenu(SpreadsheetApp.getUi().createMenu('Setup')
.addItem("Activate edit trigger", "createSpreadsheetEditTrigger")
.addItem("Deactivate edit trigger", "removeSpreadsheetEditTrigger")
.addItem("Set # of header rows to ignore…", "promptHeaderRows")
.addItem("Set bugzilla columns…", "promptBugColumns")
.addItem("Set watch column…", "promptWatchColumn")
.addItem("View current settings…", "viewSettings")
).addToUi();
}
function loadSettings() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var documentProperties = PropertiesService.getDocumentProperties();
var settings = Object.keys(SETTINGS);
for (var i = 0; i < settings.length; i++) {
var setting = settings[i];
Logger.log("Loading " + setting);
var value = documentProperties.getProperty(setting);
Logger.log("value: " + value);
if (value) {
if (setting == "BUG_COLUMNS") {
SETTINGS[setting] = value.trim().split(",");
} else {
SETTINGS[setting] = parseInt(value);
}
}
}
var triggers = ScriptApp.getUserTriggers(ss);
Logger.log(triggers);
for (var j = 0; j < triggers.length; j++) {
if (triggers[j].getEventType() != ScriptApp.EventType.ON_EDIT) {
continue;
}
SETTINGS["TRIGGER_ON_EDIT"] = triggers[j].getUniqueId();
}
}
/* eslint-disable-next-line no-unused-vars */
function viewSettings() {
loadSettings();
var settings = Object.keys(SETTINGS);
var output = "";
for (var i = 0; i < settings.length; i++) {
var setting = settings[i];
output += setting + ": " + SETTINGS[setting] + "\n";
}
SpreadsheetApp.getUi().alert(output);
}
/* eslint-disable-next-line no-unused-vars */
function promptBugColumns() {
return promptSetting("BUG_COLUMNS", "Bugzilla columns to display",
"Enter an ordered, comma-separated list of Bugzilla REST API fields to display.\n" +
"Advanced examples: cf_status_firefox44, flags:qe-verify:status, flags:firefox-backlog:setter, flags:sec-review\n" +
"Special non-Bugzilla supported values: type, qe-verify (also looks in whiteboards for [qa…]), nickname:assigned_to_detail");
}
/* eslint-disable-next-line no-unused-vars */
function promptWatchColumn() {
return promptSetting("BUG_ID_COLUMN", "Bug ID watch column",
"Enter the column number (e.g. 1=A, 2=B, 3=C, etc.) to watch for bug IDs.\n" +
"Fetched data will be displayed in columns to the right of this (overwriting existing values).\n" +
"Note: this applies to the whole document, not just the active sheet");
}
/* eslint-disable-next-line no-unused-vars */
function promptHeaderRows() {
return promptSetting("HEADER_ROWS", "Number of header rows to ignore",
"Enter the number of header rows at the top of the sheet to ignore:");
}
function promptSetting(key, title, description) {
var ui = SpreadsheetApp.getUi();
var documentProperties = PropertiesService.getDocumentProperties();
var defaultValue = SETTINGS[key];
loadSettings();
var currentValue = SETTINGS[key];
var message = description + "\n\n" +
"Default value: " + defaultValue + "\n" +
"Current value: " + currentValue + "\n";
var result = ui.prompt(title, message, ui.ButtonSet.OK_CANCEL);
// Process the user's response.
var button = result.getSelectedButton();
var text = result.getResponseText();
if (button == ui.Button.OK) {
Logger.log("Setting " + key + " to " + text);
documentProperties.setProperty(key, text);
SETTINGS[key] = text;
Logger.log(SETTINGS[key]);
}
}
/* eslint-disable-next-line no-unused-vars */
function _test() {
var bug = new Bug(445639);
bug.fetch(SETTINGS.BUG_COLUMNS.concat(SETTINGS.ADDITIONAL_FETCHED_FIELDS).concat(["keywords"]));
Logger.log(bug.getField("keywords"));
}