From 78780fea07496327af35599f4d523ed319dfcce1 Mon Sep 17 00:00:00 2001 From: venetrius Date: Thu, 10 Oct 2024 14:56:43 +0200 Subject: [PATCH] use oktokit form actions/github --- .github/workflows/set-version-labels-test.yml | 1 + common/dist/index.js | 116 +++++++++--------- common/src/set-version-labels.js | 108 ++++++++-------- 3 files changed, 111 insertions(+), 114 deletions(-) diff --git a/.github/workflows/set-version-labels-test.yml b/.github/workflows/set-version-labels-test.yml index abb6389..90b2226 100644 --- a/.github/workflows/set-version-labels-test.yml +++ b/.github/workflows/set-version-labels-test.yml @@ -17,3 +17,4 @@ jobs: uses: camunda/automation-platform-github-actions/version-label-automation-manual@Version-Labels-Automation with: issue: ${{ github.event.inputs.issue }} + repo-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/common/dist/index.js b/common/dist/index.js index b2aa245..7c14caf 100644 --- a/common/dist/index.js +++ b/common/dist/index.js @@ -163772,80 +163772,82 @@ module.exports = SBOMParser; const core = __nccwpck_require__(42186); const github = __nccwpck_require__(95438); -const octokit = __nccwpck_require__(72785)(); + module.exports = async function () { + const repoToken = core.getInput('repo-token'); + const octokit = github.getOctokit(repoToken); const repo = github.context.payload.repository; const owner = repo.owner.login; - const issueNumber = core.getInput('issue'); + const issueNumber = core.getInput('issue-number'); const potentialLabels = await getLabelsWithPrefix(owner, repo, issueNumber, `potential`); const versionLabels = replacePrefix(potentialLabels, "potential", "version"); await removeLabels(owner, repo, issueNumber, potentialLabels); await setLabels(owner, repo, issueNumber, versionLabels); -} -const replacePrefix = function(strings, oldPrefix, newPrefix) { - return strings.map(str => { - if (str.startsWith(oldPrefix)) { - return newPrefix + str.slice(oldPrefix.length); + const replacePrefix = function (strings, oldPrefix, newPrefix) { + return strings.map(str => { + if (str.startsWith(oldPrefix)) { + return newPrefix + str.slice(oldPrefix.length); + } + return str; + }); + } + + const removeLabels = async function (owner, repo, issueNumber, labels) { + console.log(`Remove labels for issue #${issueNumber}:`, labels); + try { + for (const label of labels) { + await octokit.issues.removeLabel({ + owner, + repo, + issue_number: issueNumber, + name: label // Use the current label in the iteration + }); + console.log(`Label "${label}" removed from issue #${issueNumber}`); + } + } catch (error) { + console.error('Error removing labels:', error); } - return str; - }); -} + } -const removeLabels = async function (owner, repo, issueNumber, labels) { - console.log(`Remove labels for issue #${issueNumber}:`, labels); - try { - for (const label of labels) { - await octokit.issues.removeLabel({ + const getLabelsWithPrefix = async function (owner, repo, issueNumber, prefix) { + console.log(`Get labels with prefix for issue: #${issueNumber}`); + try { + // Get issue details, which includes labels + const { data: issue } = await octokit.issues.get({ owner, repo, issue_number: issueNumber, - name: label // Use the current label in the iteration }); - console.log(`Label "${label}" removed from issue #${issueNumber}`); - } - } catch (error) { - console.error('Error removing labels:', error); - } -} -const getLabelsWithPrefix = async function (owner, repo, issueNumber, prefix) { - console.log(`Get labels with prefix for issue: #${issueNumber}`); - try { - // Get issue details, which includes labels - const { data: issue } = await octokit.issues.get({ - owner, - repo, - issue_number: issueNumber, - }); - - // Filter labels that start with the specified prefix - const matchingLabels = issue.labels - .map(label => label.name) - .filter(label => label.startsWith(prefix)); + // Filter labels that start with the specified prefix + const matchingLabels = issue.labels + .map(label => label.name) + .filter(label => label.startsWith(prefix)); - console.log(`Labels starting with "${prefix}":`, matchingLabels); - return matchingLabels; - } catch (error) { - console.error('Error fetching issue labels:', error); + console.log(`Labels starting with "${prefix}":`, matchingLabels); + return matchingLabels; + } catch (error) { + console.error('Error fetching issue labels:', error); + } } -} -const setLabels = async function (owner, repo, issueNumber, labels) { - console.log(`Set labels for issue: #${issueNumber}:`, labels); - try { - await octokit.issues.addLabels({ - owner, - repo, - issue_number: issueNumber, - labels: labels - }); - console.log(`Labels set to issue #${issueNumber}:`, labels); - } catch (error) { - console.error('Error setting labels:', error); + const setLabels = async function (owner, repo, issueNumber, labels) { + console.log(`Set labels for issue: #${issueNumber}:`, labels); + try { + await octokit.issues.addLabels({ + owner, + repo, + issue_number: issueNumber, + labels: labels + }); + console.log(`Labels set to issue #${issueNumber}:`, labels); + } catch (error) { + console.error('Error setting labels:', error); + } } } @@ -164022,14 +164024,6 @@ exports.formatLinkList = function(links, prefix) { } -/***/ }), - -/***/ 72785: -/***/ ((module) => { - -module.exports = eval("require")("@octokit/rest"); - - /***/ }), /***/ 71269: diff --git a/common/src/set-version-labels.js b/common/src/set-version-labels.js index bf1c943..f3e39bf 100644 --- a/common/src/set-version-labels.js +++ b/common/src/set-version-labels.js @@ -1,78 +1,80 @@ const core = require('@actions/core'); const github = require('@actions/github'); -const octokit = require("@octokit/rest")(); + module.exports = async function () { + const repoToken = core.getInput('repo-token'); + const octokit = github.getOctokit(repoToken); const repo = github.context.payload.repository; const owner = repo.owner.login; - const issueNumber = core.getInput('issue'); + const issueNumber = core.getInput('issue-number'); const potentialLabels = await getLabelsWithPrefix(owner, repo, issueNumber, `potential`); const versionLabels = replacePrefix(potentialLabels, "potential", "version"); await removeLabels(owner, repo, issueNumber, potentialLabels); await setLabels(owner, repo, issueNumber, versionLabels); -} -const replacePrefix = function(strings, oldPrefix, newPrefix) { - return strings.map(str => { - if (str.startsWith(oldPrefix)) { - return newPrefix + str.slice(oldPrefix.length); + const replacePrefix = function (strings, oldPrefix, newPrefix) { + return strings.map(str => { + if (str.startsWith(oldPrefix)) { + return newPrefix + str.slice(oldPrefix.length); + } + return str; + }); + } + + const removeLabels = async function (owner, repo, issueNumber, labels) { + console.log(`Remove labels for issue #${issueNumber}:`, labels); + try { + for (const label of labels) { + await octokit.issues.removeLabel({ + owner, + repo, + issue_number: issueNumber, + name: label // Use the current label in the iteration + }); + console.log(`Label "${label}" removed from issue #${issueNumber}`); + } + } catch (error) { + console.error('Error removing labels:', error); } - return str; - }); -} + } -const removeLabels = async function (owner, repo, issueNumber, labels) { - console.log(`Remove labels for issue #${issueNumber}:`, labels); - try { - for (const label of labels) { - await octokit.issues.removeLabel({ + const getLabelsWithPrefix = async function (owner, repo, issueNumber, prefix) { + console.log(`Get labels with prefix for issue: #${issueNumber}`); + try { + // Get issue details, which includes labels + const { data: issue } = await octokit.issues.get({ owner, repo, issue_number: issueNumber, - name: label // Use the current label in the iteration }); - console.log(`Label "${label}" removed from issue #${issueNumber}`); - } - } catch (error) { - console.error('Error removing labels:', error); - } -} - -const getLabelsWithPrefix = async function (owner, repo, issueNumber, prefix) { - console.log(`Get labels with prefix for issue: #${issueNumber}`); - try { - // Get issue details, which includes labels - const { data: issue } = await octokit.issues.get({ - owner, - repo, - issue_number: issueNumber, - }); - // Filter labels that start with the specified prefix - const matchingLabels = issue.labels - .map(label => label.name) - .filter(label => label.startsWith(prefix)); + // Filter labels that start with the specified prefix + const matchingLabels = issue.labels + .map(label => label.name) + .filter(label => label.startsWith(prefix)); - console.log(`Labels starting with "${prefix}":`, matchingLabels); - return matchingLabels; - } catch (error) { - console.error('Error fetching issue labels:', error); + console.log(`Labels starting with "${prefix}":`, matchingLabels); + return matchingLabels; + } catch (error) { + console.error('Error fetching issue labels:', error); + } } -} -const setLabels = async function (owner, repo, issueNumber, labels) { - console.log(`Set labels for issue: #${issueNumber}:`, labels); - try { - await octokit.issues.addLabels({ - owner, - repo, - issue_number: issueNumber, - labels: labels - }); - console.log(`Labels set to issue #${issueNumber}:`, labels); - } catch (error) { - console.error('Error setting labels:', error); + const setLabels = async function (owner, repo, issueNumber, labels) { + console.log(`Set labels for issue: #${issueNumber}:`, labels); + try { + await octokit.issues.addLabels({ + owner, + repo, + issue_number: issueNumber, + labels: labels + }); + console.log(`Labels set to issue #${issueNumber}:`, labels); + } catch (error) { + console.error('Error setting labels:', error); + } } } \ No newline at end of file