Skip to content

Commit

Permalink
use oktokit form actions/github
Browse files Browse the repository at this point in the history
  • Loading branch information
venetrius committed Oct 10, 2024
1 parent e54bb78 commit 78780fe
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 114 deletions.
1 change: 1 addition & 0 deletions .github/workflows/set-version-labels-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
116 changes: 55 additions & 61 deletions common/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down Expand Up @@ -164022,14 +164024,6 @@ exports.formatLinkList = function(links, prefix) {
}


/***/ }),

/***/ 72785:
/***/ ((module) => {

module.exports = eval("require")("@octokit/rest");


/***/ }),

/***/ 71269:
Expand Down
108 changes: 55 additions & 53 deletions common/src/set-version-labels.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 78780fe

Please sign in to comment.