-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (40 loc) · 1.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import core from '@actions/core';
import github from '@actions/github';
import { Octokit } from '@octokit/core'; // eslint-disable-line import/no-extraneous-dependencies
import { paginateGraphQL } from '@octokit/plugin-paginate-graphql';
import { ApiWrapper } from './apiwrapper.js'; // eslint-disable-line import/extensions
import { RepositoryProjectsManager } from './projects.js'; // eslint-disable-line import/extensions
const GraphQlOctokit = Octokit.plugin(paginateGraphQL);
// https://github.com/octokit/authentication-strategies.js
// example: https://github.com/octokit/graphql.js/issues/61#issuecomment-542399763
// for token type installation, pass only the token
const octokit = new GraphQlOctokit({ auth: process.env.GITHUB_TOKEN });
const apiWrapper = new ApiWrapper({ octokit });
const hasDuplicates = (array) => new Set(array).size !== array.length;
try {
const titlesInput = core.getInput('project-titles');
const titles = titlesInput.split(/\s+/);
if (hasDuplicates(titles)) {
throw new Error(`Duplicate project titles are not allowed: ${titles}`);
}
// requries a github action event of type pull_request
const {
context: {
payload: {
pull_request: {
node_id, // eslint-disable-line camelcase
},
repository,
},
},
} = github;
const rpm = new RepositoryProjectsManager({
apiWrapper,
ownerName: repository.owner.login,
repositoryName: repository.name,
});
const assignedProjectTitles = await rpm.assign(node_id, titles);
core.setOutput('project-titles', assignedProjectTitles.map((p) => p.title).join(' '));
} catch (error) {
core.setFailed(error.message);
}