diff options
author | Daniel Smith <[email protected]> | 2024-10-18 16:16:13 +0200 |
---|---|---|
committer | Daniel Smith <[email protected]> | 2025-02-24 10:53:33 +0000 |
commit | eb3adfa163fb5134c897b0bce214ee5641a2af03 (patch) | |
tree | a5dbe3f5caaf346a602df1aed91b7478c14d5760 /gerritRESTTools.js | |
parent | ca936d04a499b24e53c3ef144deb9a92628fa889 (diff) |
Change-Id: I54b21d67d8338a120f933fd102d2e5ae0a88e55f
Reviewed-by: Daniel Smith <[email protected]>
Diffstat (limited to 'gerritRESTTools.js')
-rw-r--r-- | gerritRESTTools.js | 104 |
1 files changed, 45 insertions, 59 deletions
diff --git a/gerritRESTTools.js b/gerritRESTTools.js index e3c1414..d844a9d 100644 --- a/gerritRESTTools.js +++ b/gerritRESTTools.js @@ -1,5 +1,5 @@ // /* eslint-disable no-unused-vars */ -// Copyright (C) 2020 The Qt Company Ltd. +// Copyright (C) 2024 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only @@ -14,7 +14,7 @@ const config = require("./config.json"); axiosRetry(axios, { retries: 3, // Random delay in ms between 1 and 6 sec. Helps reduce load on gerrit. - retryDelay: function() {Math.floor(Math.random() * 5 * 1000) + 1}, + retryDelay: function () { Math.floor(Math.random() * 5 * 1000) + 1 }, shouldResetTimeout: true, retryCondition: (error) => { let status = error.response.status; @@ -70,36 +70,33 @@ function trimResponse(response) { // Post a comment to the change on the latest revision. exports.postGerritComment = postGerritComment; function postGerritComment( - parentUuid, fullChangeID, revision, message, reviewers, + fullChangeID, revision, message, reviewers, notifyScope, customAuth, callback ) { function _postComment() { - let url = `${gerritBaseURL("changes")}/${fullChangeID}/revisions/${ - revision || "current"}/review`; + let url = `${gerritBaseURL("changes")}/${fullChangeID}/revisions/${revision || "current"}/review`; let data = { message: message, notify: notifyScope || "OWNER_REVIEWERS" }; if (reviewers) { // format reviewers as a list of ReviewInput entities data.reviewers = reviewers.map((reviewer) => { return { reviewer: reviewer }; }); } - logger.log( + logger.info( `POST request to: ${url}\nRequest Body: ${safeJsonStringify(data)}`, - "debug", parentUuid ); axios({ method: "post", url: url, data: data, auth: customAuth || gerritAuth }) .then(function (response) { - logger.log(`Posted comment "${message}" to change "${fullChangeID}"`, "info", parentUuid); + logger.info(`Posted comment "${message}" to change "${fullChangeID}"`); callback(true, undefined); }) .catch(function (error) { if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx - logger.log( - `An error occurred in POST (gerrit comment) to "${url}". Error ${ - error.response.status}: ${error.response.data}`, - "error", parentUuid + logger.info( + `An error occurred in POST (gerrit comment) to "${url}". Error ${error.response.status}: ${error.response.data}`, + ); callback(false, error.response); } else if (error.request) { @@ -107,9 +104,9 @@ function postGerritComment( callback(false, "retry"); } else { // Something happened in setting up the request that triggered an Error - logger.log( + logger.info( `Error in HTTP request while posting comment. Error: ${safeJsonStringify(error)}`, - "error", parentUuid + ); callback(false, error.message); } @@ -118,7 +115,7 @@ function postGerritComment( // Query the change first to see if we've posted the same comment on the current revision before const message_url = `${gerritBaseURL("changes")}/${fullChangeID}/messages`; - logger.log(`GET request to: ${message_url}`, "debug", parentUuid); + logger.info(`GET request to: ${message_url}`); axios({ method: "get", url: message_url, auth: customAuth || gerritAuth }) .then(function (response) { let parsedResponse = JSON.parse(trimResponse(response.data)); @@ -135,9 +132,8 @@ function postGerritComment( // Then iterate and check for message in the current patchset. for (let i = messages.length - 1; i >= 0; i--) { if (messages[i].includes(message) && messages[i].includes(`Patch Set ${patchset}:`)) { - logger.log( - `Comment "${message}" already posted on patchset ${patchset} of ${fullChangeID}`, - "verbose", parentUuid + logger.info( + `Comment "${message}" already posted on patchset ${patchset} of ${fullChangeID}` ); callback(true, undefined); return; @@ -150,16 +146,16 @@ function postGerritComment( // Query gerrit for a change and return it along with the current revision if it exists. exports.queryChange = queryChange; -function queryChange(parentUuid, fullChangeID, fields, customAuth, callback) { +function queryChange(fullChangeID, fields, customAuth, callback) { let url = `${gerritBaseURL("changes")}/${fullChangeID}/?o=CURRENT_COMMIT&o=CURRENT_REVISION`; // Tack on any additional fields requested if (fields) fields.forEach((field) => url = `${url}&o=${field}`); - logger.log(`Querying gerrit for ${url}`, "debug", parentUuid); + logger.info(`Querying gerrit for ${url}`); axios.get(url, { auth: customAuth || gerritAuth }) .then(function (response) { // Execute callback and return the list of changes - logger.log(`Raw response: ${response.data}`, "debug", parentUuid); + logger.info(`Raw response: ${response.data}`); callback(true, JSON.parse(trimResponse(response.data))); }) .catch(function (error) { @@ -171,10 +167,9 @@ function queryChange(parentUuid, fullChangeID, fields, customAuth, callback) { callback(false, { statusCode: 404 }); } else { // Some other error was returned - logger.log( - `An error occurred in GET "${url}". Error ${error.response.status}: ${ - error.response.data}`, - "error", parentUuid + logger.info( + `An error occurred in GET "${url}". Error ${error.response.status}: ${error.response.data}`, + ); callback(false, { statusCode: error.response.status, statusDetail: error.response.data }); } @@ -183,9 +178,9 @@ function queryChange(parentUuid, fullChangeID, fields, customAuth, callback) { callback(false, "retry"); } else { // Something happened in setting up the request that triggered an Error - logger.log( + logger.info( `Error in HTTP request while trying to query ${fullChangeID}. ${error}`, - "error", parentUuid + ); callback(false, error.message); } @@ -194,36 +189,34 @@ function queryChange(parentUuid, fullChangeID, fields, customAuth, callback) { // Add a user to the attention set of a change exports.addToAttentionSet = addToAttentionSet; -function addToAttentionSet(parentUuid, changeJSON, user, reason, customAuth, callback) { +function addToAttentionSet(changeJSON, user, reason, customAuth, callback) { let project = changeJSON.project.name ? changeJSON.project.name : changeJSON.project; checkAccessRights( - parentUuid, project, changeJSON.branch || changeJSON.change.branch, + project, changeJSON.branch || changeJSON.change.branch, user, "push", customAuth || gerritAuth, function (success, data) { if (!success) { let msg = `User "${user}" cannot push to ${project}:${changeJSON.branch}.` - logger.log(msg, "warn", parentUuid); + logger.info(msg); callback(false, msg); let botAssignee = envOrConfig("GERRIT_USER"); if (botAssignee && user != botAssignee) { - logger.log(`Falling back to GERRIT_USER (${botAssignee}) as assignee...`); + logger.info(`Falling back to GERRIT_USER (${botAssignee}) as assignee...`); addToAttentionSet( - parentUuid, changeJSON, botAssignee, "fallback to bot", customAuth, - function () {} + changeJSON, botAssignee, "fallback to bot", customAuth, + function () { } ); } } else { let url = `${gerritBaseURL("changes")}/${changeJSON.fullChangeID || changeJSON.id}/attention`; let data = { user: user, "reason": reason || "Update Attention Set" }; - logger.log( - `POST request to: ${url}\nRequest Body: ${safeJsonStringify(data)}`, - "debug", parentUuid + logger.info( + `POST request to: ${url}\nRequest Body: ${safeJsonStringify(data)}` ); axios({ method: "POST", url: url, data: data, auth: customAuth || gerritAuth }) .then(function (response) { - logger.log( - `Added Attention Set user: "${user}" on "${changeJSON.fullChangeID || changeJSON.id}"`, - "info", parentUuid + logger.info( + `Added Attention Set user: "${user}" on "${changeJSON.fullChangeID || changeJSON.id}"` ); callback(true, undefined); }) @@ -231,10 +224,9 @@ function addToAttentionSet(parentUuid, changeJSON, user, reason, customAuth, cal if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx - logger.log( - `An error occurred in POST to "${url}". Error: ${error.response.status}: ${ - error.response.data}`, - "error", parentUuid + logger.info( + `An error occurred in POST to "${url}". Error: ${error.response.status}: ${error.response.data}` + ); callback(false, { status: error.response.status, data: error.response.data }); } else if (error.request) { @@ -242,9 +234,8 @@ function addToAttentionSet(parentUuid, changeJSON, user, reason, customAuth, cal callback(false, "retry"); } else { // Something happened in setting up the request that triggered an Error - logger.log( - `Error in HTTP request while trying to add to attention set. Error: ${error}`, - "error", parentUuid + logger.info( + `Error in HTTP request while trying to add to attention set. Error: ${error}` ); callback(false, error.message); } @@ -255,21 +246,20 @@ function addToAttentionSet(parentUuid, changeJSON, user, reason, customAuth, cal } // Check permissions for a branch. Returns Bool. -function checkAccessRights(uuid, repo, branch, user, permission, customAuth, callback) { +function checkAccessRights(repo, branch, user, permission, customAuth, callback) { // Decode and re-encode to be sure we don't double-encode something that was already // passed to us in URI encoded format. repo = encodeURIComponent(decodeURIComponent(repo)); branch = encodeURIComponent(decodeURIComponent(branch)); - let url = `${gerritBaseURL("projects")}/${repo}/check.access?account=${ - user}&ref=${encodeURIComponent('refs/for/refs/heads/')}${branch}&perm=${permission}`; - logger.log(`GET request for ${url}`, "debug", uuid); + let url = `${gerritBaseURL("projects")}/${repo}/check.access?account=${user}&ref=${encodeURIComponent('refs/for/refs/heads/')}${branch}&perm=${permission}`; + logger.info(`GET request for ${url}`); axios .get(url, { auth: customAuth || gerritAuth }) .then(function (response) { // A successful response's JSON object has a status field (independent // of the HTTP response's status), that tells us whether this user // does (200) or doesn't (403) have the requested permissions. - logger.log(`Raw Response: ${response.data}`, "debug", uuid); + logger.info(`Raw Response: ${response.data}`); callback(JSON.parse(trimResponse(response.data)).status == 200, undefined) }) .catch(function (error) { @@ -282,17 +272,13 @@ function checkAccessRights(uuid, repo, branch, user, permission, customAuth, cal // to check permissions of other users, a much bigger problem. data = "retry"; } - logger.log( - `An error occurred in GET to "${url}". Error ${error.response.status}: ${ - error.response.data}`, - "error", uuid + logger.info( + `An error occurred in GET to "${url}". Error ${error.response.status}: ${error.response.data}` ); } else { data = `${error.status}:${error.message}`; - logger.log( - `Failed to get ${permission} access rights on ${repo}:${branch}\n${ - safeJsonStringify(error)}`, - "error", uuid + logger.info( + `Failed to get ${permission} access rights on ${repo}:${branch}\n${safeJsonStringify(error)}` ); } callback(false, data); |