|
| 1 | +const core = require('@actions/core'); |
| 2 | +const github = require('@actions/github'); |
| 3 | +const asana = require('asana'); |
| 4 | + |
| 5 | +async function moveSection(client, taskId, targets) { |
| 6 | + const task = await client.tasks.findById(taskId); |
| 7 | + |
| 8 | + targets.forEach(async target => { |
| 9 | + const targetProject = task.projects.find(project => project.name === target.project); |
| 10 | + if (!targetProject) { |
| 11 | + core.info(`This task does not exist in "${target.project}" project`); |
| 12 | + return; |
| 13 | + } |
| 14 | + let targetSection = await client.sections.findByProject(targetProject.gid) |
| 15 | + .then(sections => sections.find(section => section.name === target.section)); |
| 16 | + if (targetSection) { |
| 17 | + await client.sections.addTask(targetSection.gid, { task: taskId }); |
| 18 | + core.info(`Moved to: ${target.project}/${target.section}`); |
| 19 | + } else { |
| 20 | + core.error(`Asana section ${target.section} not found.`); |
| 21 | + } |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +async function findComment(client, taskId, commentId) { |
| 26 | + let stories; |
| 27 | + try { |
| 28 | + const storiesCollection = await client.tasks.stories(taskId); |
| 29 | + stories = await storiesCollection.fetch(200); |
| 30 | + } catch (error) { |
| 31 | + throw error; |
| 32 | + } |
| 33 | + |
| 34 | + return stories.find(story => story.text.indexOf(commentId) !== -1); |
| 35 | +} |
| 36 | + |
| 37 | +async function addComment(client, taskId, commentId, text, isPinned) { |
| 38 | + if(commentId){ |
| 39 | + text += '\n'+commentId+'\n'; |
| 40 | + } |
| 41 | + try { |
| 42 | + const comment = await client.tasks.addComment(taskId, { |
| 43 | + text: text, |
| 44 | + is_pinned: isPinned, |
| 45 | + }); |
| 46 | + return comment; |
| 47 | + } catch (error) { |
| 48 | + console.error('rejecting promise', error); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +module.exports = async function action() { |
| 53 | + const |
| 54 | + ASANA_PAT = core.getInput('asana-pat', {required: true}), |
| 55 | + ACTION = core.getInput('action', {required: true}), |
| 56 | + TRIGGER_PHRASE = core.getInput('trigger-phrase') || '', |
| 57 | + PULL_REQUEST = github.context.payload.pull_request, |
| 58 | + REGEX_STRING = `${TRIGGER_PHRASE}(?:\s*)https:\\/\\/app.asana.com\\/(\\d+)\\/(?<project>\\d+)\\/(?<task>\\d+)`, |
| 59 | + REGEX = new RegExp(REGEX_STRING,'g') |
| 60 | + ; |
| 61 | + |
| 62 | + console.log('pull_request', PULL_REQUEST); |
| 63 | + |
| 64 | + const client = await asana.Client.create({ |
| 65 | + defaultHeaders: { 'asana-enable': 'new-sections,string_ids' }, |
| 66 | + logAsanaChangeWarnings: false |
| 67 | + }).useAccessToken(ASANA_PAT).authorize(); |
| 68 | + |
| 69 | + if(client === null){ |
| 70 | + throw new Error('client authorization failed'); |
| 71 | + } |
| 72 | + |
| 73 | + console.info('looking in body', PULL_REQUEST.body, 'regex', REGEX_STRING); |
| 74 | + let foundAsanaTasks = []; |
| 75 | + while ((parseAsanaURL = REGEX.exec(PULL_REQUEST.body)) !== null) { |
| 76 | + const taskId = parseAsanaURL.groups.task; |
| 77 | + if (!taskId) { |
| 78 | + core.error(`Invalid Asana task URL after the trigger phrase ${TRIGGER_PHRASE}`); |
| 79 | + continue; |
| 80 | + } |
| 81 | + foundAsanaTasks.push(taskId); |
| 82 | + } |
| 83 | + console.info(`found ${foundAsanaTasks.length} taskIds:`, foundAsanaTasks.join(',')); |
| 84 | + |
| 85 | + console.info('calling', ACTION); |
| 86 | + switch(ACTION){ |
| 87 | + case 'assert-link': { |
| 88 | + const githubToken = core.getInput('github-token', {required: true}); |
| 89 | + const octokit = new github.GitHub(githubToken); |
| 90 | + const statusState = (foundAsanaTasks.length > 0) ? 'success' : 'error'; |
| 91 | + core.info(`setting ${statusState} for ${github.context.payload.pull_request.head.sha}`); |
| 92 | + octokit.repos.createStatus({ |
| 93 | + ...github.context.repo, |
| 94 | + 'context': 'asana-link-presence', |
| 95 | + 'state': statusState, |
| 96 | + 'description': 'asana link not found', |
| 97 | + 'sha': github.context.payload.pull_request.head.sha, |
| 98 | + }); |
| 99 | + break; |
| 100 | + } |
| 101 | + case 'add-comment': { |
| 102 | + const commentId = core.getInput('comment-id'), |
| 103 | + htmlText = core.getInput('text', {required: true}), |
| 104 | + isPinned = core.getInput('is-pinned') === 'true'; |
| 105 | + const comments = []; |
| 106 | + for(const taskId of foundAsanaTasks) { |
| 107 | + if(commentId){ |
| 108 | + const comment = await findComment(client, taskId, commentId); |
| 109 | + if(comment){ |
| 110 | + console.info('found existing comment', comment.gid); |
| 111 | + continue; |
| 112 | + } |
| 113 | + } |
| 114 | + const comment = await addComment(client, taskId, commentId, htmlText, isPinned); |
| 115 | + comments.push(comment); |
| 116 | + }; |
| 117 | + return comments; |
| 118 | + } |
| 119 | + case 'remove-comment': { |
| 120 | + const commentId = core.getInput('comment-id', {required: true}); |
| 121 | + const removedCommentIds = []; |
| 122 | + for(const taskId of foundAsanaTasks) { |
| 123 | + const comment = await findComment(client, taskId, commentId); |
| 124 | + if(comment){ |
| 125 | + console.info("removing comment", comment.gid); |
| 126 | + try { |
| 127 | + await client.stories.delete(comment.gid); |
| 128 | + } catch (error) { |
| 129 | + console.error('rejecting promise', error); |
| 130 | + } |
| 131 | + removedCommentIds.push(comment.gid); |
| 132 | + } |
| 133 | + } |
| 134 | + return removedCommentIds; |
| 135 | + } |
| 136 | + case 'move-section': { |
| 137 | + const targetJSON = core.getInput('targets', {required: true}); |
| 138 | + const targets = JSON.parse(targetJSON); |
| 139 | + const movedTasks = []; |
| 140 | + for(const taskId of foundAsanaTasks) { |
| 141 | + await moveSection(client, taskId, targets); |
| 142 | + movedTasks.push(taskId); |
| 143 | + } |
| 144 | + return movedTasks; |
| 145 | + } |
| 146 | + default: |
| 147 | + console.info('lame'); |
| 148 | + core.setFailed("unexpected action ${ACTION}"); |
| 149 | + } |
| 150 | +} |
0 commit comments