|
| 1 | +// Import modules |
| 2 | +const fs = require('fs'); |
| 3 | + |
| 4 | +const retrieveSkillsId = require('./retrieve-skills-id'); |
| 5 | +const postComment = require('../utils/post-issue-comment'); |
| 6 | +const checkTeamMembership = require('../utils/check-team-membership'); |
| 7 | +const statusFieldIds = require('../utils/_data/status-field-ids'); |
| 8 | +const mutateIssueStatus = require('../utils/mutate-issue-status'); |
| 9 | + |
| 10 | +// Global variables |
| 11 | +var github; |
| 12 | +var context; |
| 13 | + |
| 14 | +/** |
| 15 | + * Function to retrieve Skills Issue and add comments |
| 16 | + * @param {Object} github - GitHub object |
| 17 | + * @param {Object} context - Context object |
| 18 | + * |
| 19 | + */ |
| 20 | +async function firstPostToSkillsIssue({g, c}) { |
| 21 | + |
| 22 | + github = g; |
| 23 | + context = c; |
| 24 | + |
| 25 | + const owner = context.repo.owner; |
| 26 | + const repo = context.repo.repo; |
| 27 | + const team = 'website-write'; |
| 28 | + |
| 29 | + |
| 30 | + try { |
| 31 | + const csvPath = 'github-actions/activity-trigger/member_activity_history_bot.csv'; |
| 32 | + const csvContent = fs.readFileSync(csvPath, 'utf-8'); |
| 33 | + |
| 34 | + // Parse CSV assuming |
| 35 | + const rows = csvContent |
| 36 | + .trim() |
| 37 | + .split('\n') |
| 38 | + .map(line => line.split(',')); |
| 39 | + |
| 40 | + const processed = processCsvForSkillsIssue(rows); |
| 41 | + |
| 42 | + console.log(JSON.stringify(processed, null, 2)); // For testing only |
| 43 | + |
| 44 | + processed.forEach(async entry => { |
| 45 | + let username = entry.username; |
| 46 | + let skillsIssueNum = entry.issueNum; |
| 47 | + let message = entry.postToSkillsIssue; |
| 48 | + const MARKER = '<!-- Skills Issue Activity Record -->'; |
| 49 | + |
| 50 | + // Since we know this is the first run and no matching issue comments exist yet, we can post immediately |
| 51 | + const body = `${MARKER}\n## Activity Log: ${username}\n\n##### ⚠ Important note: The bot updates this issue automatically - do not edit\n\n${message}`; |
| 52 | + await postComment(github, context, skillsIssueNum, body); |
| 53 | + |
| 54 | + // Check whether eventActor is team member; if so open issue and move to "In progress" |
| 55 | + const isActiveMember = await checkTeamMembership(github, username, team); |
| 56 | + |
| 57 | + if (isActiveMember) { |
| 58 | + // If isActiveMember, make sure Skills Issue is open, and... |
| 59 | + await github.request('PATCH /repos/{owner}/{repo}/issues/{issueNum}', { |
| 60 | + owner, |
| 61 | + repo, |
| 62 | + issueNum: skillsIssueNum, |
| 63 | + state: "open", |
| 64 | + }); |
| 65 | + // update issue's status to "In progress (actively working)" |
| 66 | + // Needs skillsIssueNodeId first. |
| 67 | + let skillsIssueNodeId = await retrieveSkillsId(github, context, skillsIssueNum); |
| 68 | + let statusValue = statusFieldIds('In_Progress'); |
| 69 | + await mutateIssueStatus(github, context, skillsIssueNodeId, statusValue); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + } catch (error) { |
| 74 | + console.error('Error processing CSV:', error); |
| 75 | + process.exit(1); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +function processCsvForSkillsIssue(rows) { |
| 80 | + |
| 81 | + const results = []; |
| 82 | + let currentUser = null; |
| 83 | + let skillsIssueNum = null; |
| 84 | + let postToSkillsIssue = null; |
| 85 | + let collecting = false; |
| 86 | + |
| 87 | + for (const row of rows) { |
| 88 | + const username = row[0]; |
| 89 | + const issueNum = row[1]; |
| 90 | + const col3 = row[2]; |
| 91 | + |
| 92 | + if (username !== currentUser) { |
| 93 | + if (collecting && postToSkillsIssue !== null) { |
| 94 | + results.push({ username: currentUser, issueNum: skillsIssueNum, postToSkillsIssue }); |
| 95 | + } |
| 96 | + |
| 97 | + currentUser = username; |
| 98 | + |
| 99 | + if (col3 === "SKILLS ISSUE") { |
| 100 | + postToSkillsIssue = ""; |
| 101 | + skillsIssueNum = issueNum; |
| 102 | + collecting = true; |
| 103 | + } else { |
| 104 | + postToSkillsIssue = null; |
| 105 | + collecting = false; |
| 106 | + } |
| 107 | + } else { |
| 108 | + if (collecting) { |
| 109 | + postToSkillsIssue += col3 + "\n"; |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (collecting && postToSkillsIssue !== null) { |
| 115 | + results.push({ username: currentUser, issueNum: skillsIssueNum, postToSkillsIssue }); |
| 116 | + } |
| 117 | + |
| 118 | + return results; |
| 119 | +} |
| 120 | + |
| 121 | +module.exports = firstPostToSkillsIssue; |
0 commit comments