Files
freeCodeCamp/tools/crowdin/actions/pr-creator/index.js
Randell Dawson 75dc001aea feat(tools): Add Crowdin PR creator action (#41383)
Co-authored-by: Nicholas Carrigan (he/him) <nhcarrigan@gmail.com>
2021-03-09 22:27:40 +05:30

91 lines
2.4 KiB
JavaScript

/* eslint-disable import/no-unresolved */
/* eslint-disable camelcase */
const core = require('@actions/core');
const githubRoot = require('@actions/github');
(async () => {
try {
const token = core.getInput('github-token');
const branch = core.getInput('branch');
const [owner, repo] = core.getInput('owner-repo').split('/');
if (!owner || !repo) {
core.setFailed('Must specify a valid ownerName/repoName');
}
const base = core.getInput('base');
const title = core.getInput('title');
const body = core.getInput('body');
const labelsStr = core.getInput('labels');
const labels = labelsStr.trim().split(/,\s+/);
const reviewersStr = core.getInput('reviewers');
const reviewers = reviewersStr.trim().split(/,\s+/);
const github = githubRoot.getOctokit(token);
const branchExists = await github.repos
.getBranch({
owner,
repo,
branch
})
.catch(() => {
console.info('Branch does not exist. Likely no changes in download?');
});
if (!branchExists || branchExists.status !== 200) {
return;
}
const pullRequestExists = await github.pulls.list({
owner,
repo,
head: `${owner}:${branch}`
});
if (pullRequestExists.data.length) {
console.info(
'It looks like a pull request already exists for this branch.'
);
return;
}
const PR = await github.pulls
.create({
owner,
repo,
head: branch,
base,
title,
body
})
.catch(err => {
console.info(
'Unpredicted error occurred when trying to create the PR.'
);
console.error(err);
});
if (!PR || PR.status !== 201) {
return;
}
const prNumber = PR.data.number;
console.log(
`https://github.com/freeCodeCamp/freeCodeCamp/pull/${prNumber} created`
);
if (labels && labels.length) {
await github.issues.addLabels({
owner,
repo,
issue_number: prNumber,
labels
});
console.log(`Labels ${labels} added to PR`);
}
if (reviewers && reviewers.length) {
await github.pulls.requestReviewers({
owner,
repo,
pull_number: prNumber,
reviewers
});
console.log(`Requested Reviewers ${reviewers} added to PR`);
}
} catch (error) {
core.setFailed(error.message);
}
})();