new-github-issue-url
Version:
Generate a URL for opening a new GitHub issue with prefilled title, body, and other fields
43 lines (35 loc) • 890 B
JavaScript
export default function newGithubIssueUrl(options = {}) {
let repoUrl;
if (options.repoUrl) {
repoUrl = options.repoUrl;
} else if (options.user && options.repo) {
repoUrl = `https://github.com/${options.user}/${options.repo}`;
} else {
throw new Error('You need to specify either the `repoUrl` option or both the `user` and `repo` options');
}
const url = new URL(`${repoUrl}/issues/new`);
const types = [
'body',
'title',
'labels',
'template',
'milestone',
'assignee',
'projects',
'type',
];
for (const type of types) {
let value = options[type];
if (value === undefined) {
continue;
}
if (type === 'labels' || type === 'projects') {
if (!Array.isArray(value)) {
throw new TypeError(`The \`${type}\` option should be an array`);
}
value = value.join(',');
}
url.searchParams.set(type, value);
}
return url.toString();
}