|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Hannes Wellmann and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Hannes Wellmann - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | + |
| 15 | +import java.time.LocalDate |
| 16 | +import java.time.ZonedDateTime |
| 17 | + |
| 18 | +@groovy.transform.Field |
| 19 | +def boolean _CALENDAR_API_IS_DRY_RUN = true |
| 20 | + |
| 21 | +def setDryRun(boolean isDryRun) { |
| 22 | + _CALENDAR_API_IS_DRY_RUN = isDryRun |
| 23 | +} |
| 24 | + |
| 25 | +def initialize(Object utilities) { |
| 26 | + def gcloudInstall = utilities.installDownloadableTool('gcloud', 'https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-linux-x86_64.tar.gz') |
| 27 | + env.GCLOUD = "${gcloudInstall}/bin/gcloud" |
| 28 | + // https://docs.cloud.google.com/sdk/docs/configurations |
| 29 | + env.CLOUDSDK_CONFIG = "${gcloudInstall}/../config" |
| 30 | + // https://docs.cloud.google.com/sdk/docs/initializing |
| 31 | + sh '''#!/bin/bash -xe |
| 32 | + # Skip default initialization since this is a non-interactive session |
| 33 | + $GCLOUD config set disable_prompts true |
| 34 | + $GCLOUD auth activate-service-account --key-file=${GCLOULD_KEY_FILE} |
| 35 | + ''' |
| 36 | +} |
| 37 | + |
| 38 | +// Calendar events: https://developers.google.com/workspace/calendar/api/v3/reference/events#resource |
| 39 | + |
| 40 | +def createEvent(String title, LocalDate start, LocalDate end = null) { |
| 41 | + if (end == null) { |
| 42 | + end = start.plusDays(1) // Assume one day event |
| 43 | + } |
| 44 | + def parameters = [summary: title, start: [date: formatDate(start)], end: [date: formatDate(end)] ] |
| 45 | + insertEvent(parameters) |
| 46 | +} |
| 47 | + |
| 48 | +// Recurrence Rules: https://datatracker.ietf.org/doc/html/rfc5545#section-3.3.10 |
| 49 | + |
| 50 | +def createRecurrenceRRule(String frequency, LocalDate until) { |
| 51 | + // See https://datatracker.ietf.org/doc/html/rfc5545#section-3.8.5.3 |
| 52 | + return "RRULE:FREQ=${frequency.toUpperCase()};UNTIL=${java.time.format.DateTimeFormatter.BASIC_ISO_DATE.format(until)}" |
| 53 | +} |
| 54 | +// createRecurrenceExRule(), createRecurrenceRDate() and createRecurrenceExDate() currently not needed |
| 55 | + |
| 56 | +def createEvent(String title, ZonedDateTime start, ZonedDateTime end, recurrenceRules = null) { |
| 57 | + def parameters = [summary: title] |
| 58 | + parameters.start = [dateTime: formatDateTime(start), timeZone: start.zone.id] |
| 59 | + parameters.end = [dateTime: formatDateTime(end), timeZone: end.zone.id] |
| 60 | + parameters.recurrence = recurrenceRules |
| 61 | + insertEvent(parameters) |
| 62 | +} |
| 63 | + |
| 64 | +private String formatDate(LocalDate date) { |
| 65 | + return java.time.format.DateTimeFormatter.ISO_LOCAL_DATE.format(date) |
| 66 | +} |
| 67 | + |
| 68 | +private String formatDateTime(ZonedDateTime dateTime) { |
| 69 | + return java.time.format.DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(dateTime) |
| 70 | +} |
| 71 | + |
| 72 | +// https://developers.google.com/workspace/calendar/api/v3/reference/events/insert |
| 73 | +private void insertEvent(Map<String, Object> parameters) { |
| 74 | + def response = queryGoogleCalendarAPI('-X POST', 'events', parameters) |
| 75 | + if (response != null && !isConfirmedEvent(response)) { |
| 76 | + error "Response contains errors:\n${response}" |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +private boolean isConfirmedEvent(Map response) { |
| 81 | + return response.kind == 'calendar#event' && response.status == 'confirmed' |
| 82 | +} |
| 83 | + |
| 84 | +// See documentation of the Google Calendar API |
| 85 | +// - https://developers.google.com/workspace/calendar/api/guides/overview |
| 86 | + |
| 87 | +private Map<String, String> queryGoogleCalendarAPI(String method, String endpoint, Map<String, Object> parameters = null) { |
| 88 | + def calendarId = 'prfk26fdmpru1mptlb06p0jh4s@group.calendar.google.com' |
| 89 | + def query = """\ |
| 90 | + GC_API_TOKEN=\$(\$GCLOUD auth print-access-token --scopes=https://www.googleapis.com/auth/calendar) |
| 91 | + curl -L ${method} \ |
| 92 | + 'https://www.googleapis.com/calendar/v3/calendars/${calendarId}/${endpoint}' \ |
| 93 | + -H "Authorization: Bearer \${GC_API_TOKEN}" \ |
| 94 | + -H 'Accept: application/json' \ |
| 95 | + -H 'Content-Type: application/json' |
| 96 | + """.replace('\t','').trim() |
| 97 | + if (parameters) { |
| 98 | + def data = writeJSON(json: parameters, returnText: true) |
| 99 | + data = data.replace("'", "'\\''") // Escape single quotes by '\'' |
| 100 | + query += " --data '${data}'" |
| 101 | + } |
| 102 | + if (_CALENDAR_API_IS_DRY_RUN && !method.isEmpty()) { |
| 103 | + echo "Query (not send):\n${query}" |
| 104 | + return null |
| 105 | + } |
| 106 | + echo "Execute:\n${query}" |
| 107 | + // Ensure the commands are not printed to prevent exposure of (temporary credentials) |
| 108 | + def response = sh(script: "#!/bin/bash -e\n${query}", returnStdout: true) |
| 109 | + if (response == null || response.isEmpty()) { |
| 110 | + error 'Response is null or empty. This commonly indicates: HTTP/1.1 500 Internal Server Error' |
| 111 | + } |
| 112 | + return readJSON(text: response) |
| 113 | +} |
| 114 | + |
| 115 | +return this |
0 commit comments