-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (42 loc) · 1.34 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
52
53
54
55
56
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios');
const BASE_URI = 'https://api.todoist.com/rest/v1';
const http = axios.create({
baseURL: BASE_URI,
headers: {
Authorization: `Bearer ${core.getInput('token')}`,
},
});
const fetchProjectIdFromName = async (name) => {
const response = await http.get('/projects');
const projects = response.data;
const project = projects.filter((project) => project.name === name).pop();
return project ? project.id : null;
};
const createTask = async ({ content, projectId }) => {
const body = {
content,
project_id: projectId,
};
const response = await http.post('/tasks', body);
return response.data;
};
const run = async () => {
try {
const { context } = github;
if (context.eventName !== 'issues') {
throw new Error('Supported event issues only.');
}
const projectName = core.getInput('project-name');
core.debug(`project-name: ${projectName}`);
const projectId = await fetchProjectIdFromName(projectName);
const { issue } = context.payload;
const content = `#${issue.number} ${issue.title}`;
const task = await createTask({ content, projectId });
core.setOutput('message', JSON.stringify(task, null, 2));
} catch (error) {
core.setFailed(error.message);
}
};
run();