Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .github/workflows/activity-trigger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,16 @@ permissions:
jobs:
Gather-Activity-Event-Information:
runs-on: ubuntu-latest
if: github.repository == 'hackforla/website'
if: ${{ github.repository == 'hackforla/website' }}
steps:
- uses: actions/checkout@v5

- name: Gather Event Details
id: gather-event-details
uses: actions/github-script@v7
with:
github-token: ${{ secrets.HACKFORLA_GRAPHQL_TOKEN }}
# github-token defaults to `github.token` for 'pull_request_review'
github-token: ${{ secrets.HACKFORLA_GRAPHQL_TOKEN != '' && secrets.HACKFORLA_GRAPHQL_TOKEN || github.token }}
script: |
const script = require('./github-actions/activity-trigger/activity-trigger.js');
const activities = script({github, context});
Expand All @@ -41,7 +42,7 @@ jobs:
id: post-to-skills-issue
uses: actions/github-script@v7
with:
github-token: ${{ secrets.HACKFORLA_GRAPHQL_TOKEN }}
github-token: ${{ secrets.HACKFORLA_GRAPHQL_TOKEN != '' && secrets.HACKFORLA_GRAPHQL_TOKEN || github.token }}
script: |
const activities = JSON.parse(${{ steps.gather-event-details.outputs.result }});
const script = require('./github-actions/activity-trigger/post-to-skills-issue.js');
Expand Down
50 changes: 33 additions & 17 deletions github-actions/activity-trigger/activity-trigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,33 @@ async function activityTrigger({github, context}) {
let activities = [];

// Exclude all bot actors from being recorded as a guardrail against infinite loops
const EXCLUDED_ACTORS = ['HackforLABot', 'elizabethhonest', 'github-actions', 'github-advanced-security', 'github-pages', 'dependabot[bot]', 'dependabot-preview[bot]', 'dependabot', 'dependabot-preview'];
const EXCLUDED_ACTORS = [
"HackforLABot",
"elizabethhonest",
"dependabot",
"dependabot[bot]",
"github-actions",
"github-actions[bot]",
"github-advanced-security",
"github-advanced-security[bot]"
];

if (eventName === 'issues') {
issueNum = context.payload.issue.number;
eventUrl = context.payload.issue.html_url;
timeline = context.payload.issue.updated_at;
// If issue action is not opened and an assignee exists, then change
// If issue action is closed and an assignee exists, then change
// the eventActor to the issue assignee, else retain issue author
assignee = context.payload.assignee?.login;
if (eventAction != 'opened' && assignee != null ) {
console.log(`Issue is ${eventAction}. Change eventActor => ${assignee}`);
eventActor = assignee;
} else {
eventActor = context.payload.issue.user.login;
}
if (eventAction === 'closed') {
if (context.payload.issue.assignees?.length > 0) {
eventActor = context.payload.issue.assignees[0].login;
} else {
eventActor = context.payload.issue.user.login;
}
let reason = context.payload.issue.state_reason;
eventActor = context.payload.issue.user.login;
eventAction = 'Closed-' + reason;
} else if (eventAction === 'assigned' || eventAction === 'unassigned') {
eventActor = context.payload.assignee.login;
}
} else if (eventName === 'issue_comment') {
// Check if the comment is on an issue or a pull request
Expand All @@ -61,6 +69,11 @@ async function activityTrigger({github, context}) {
issueNum = context.payload.pull_request.number;
eventUrl = context.payload.review.html_url;
timeline = context.payload.review.updated_at;
eventActor = context.payload.review.user.login;
} else if (eventName === 'pull_request_review_comment') {
issueNum = context.payload.pull_request.number;
eventUrl = context.payload.comment.html_url;
timeline = context.payload.comment.updated_at;
}

// Return immediately if the issueNum is a Skills Issue- to discourage
Expand All @@ -81,12 +94,13 @@ async function activityTrigger({github, context}) {
'issues.assigned': 'assigned',
'issues.unassigned': 'unassigned',
'issue_comment.created': 'commented',
'pull_request_review.created': 'submitted review',
'pull_request_review.submitted': 'submitted review',
'pull_request_review_comment.created': 'commented',
'pull_request_comment.created': 'commented',
'pull_request.opened': 'opened',
'pull_request.PRclosed': 'closed',
'pull_request.PRmerged': 'merged',
'pull_request.reopened': 'reopened'
'pull_request_target.opened': 'opened',
'pull_request_target.PRclosed': 'closed',
'pull_request_target.PRmerged': 'merged',
'pull_request_target.reopened': 'reopened'
};

let localTime = getDateTime(timeline);
Expand All @@ -96,8 +110,10 @@ async function activityTrigger({github, context}) {
// Check to confirm the eventActor isn't a bot
const isExcluded = (eventActor) => EXCLUDED_ACTORS.includes(eventActor);
if (!isExcluded(eventActor)) {
console.log(`Not a bot. Message to post: ${message}`);
console.log(`Not a bot. Message to post: "${message}"`);
activities.push([eventActor, message]);
} else {
console.log(`eventActor: ${eventActor} likely a bot. Do not post`);
}

// Only if issue is closed, and eventActor != assignee, return assignee and message
Expand All @@ -109,7 +125,7 @@ async function activityTrigger({github, context}) {
if ((eventAction === 'PRclosed' || eventAction === 'PRmerged') && (eventActor != eventPRAuthor)) {
let messagePRAuthor = `- ${eventPRAuthor} PR was ${action}: ${eventUrl} at ${localTime}`;
if (!isExcluded(eventPRAuthor)) {
console.log(`Not a bot. Message to post: ${messagePRAuthor}`);
console.log(`Not a bot. Message to post: "${messagePRAuthor}"`);
activities.push([eventPRAuthor, messagePRAuthor]);
}
}
Expand Down
26 changes: 15 additions & 11 deletions github-actions/activity-trigger/post-to-skills-issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,20 @@

// If eventActor undefined, exit
if (!eventActor) {
console.log(`eventActor is undefined (likely a bot). Cannot post message.`);
console.log(`eventActor is undefined (likely a bot). Cannot post message...`);
return;
}

// Get eventActor's Skills Issue number, nodeId, current statusId (all null if no Skills Issue found)

const skillsInfo = await querySkillsIssue(github, context, eventActor, SKILLS_LABEL);
const skillsIssueNum = skillsInfo.issueNum;
const skillsIssueNodeId = skillsInfo.issueId;
const skillsStatusId = skillsInfo.statusId;

// Return immediately if Skills Issue not found
if (skillsIssueNum) {
console.log(`Found Skills Issue for ${eventActor}: #${skillsIssueNum}`);
} else {
console.log(`Did not find Skills Issue for ${eventActor}. Cannot post message.`);
if (!skillsIssueNum) {
console.log(` ⮡ Did not find Skills Issue for ${eventActor}. Cannot post message.`);
return;
}

Expand All @@ -58,8 +57,9 @@
per_page: 100,
issue_number: skillsIssueNum,
});
console.log(` ⮡ Found comment with MARKER...`);
} catch (err) {
console.error(`GET comments failed for issue #${skillsIssueNum}:`, err);
console.error(`GET comments failed for issue #${skillsIssueNum}:`, err);
return;
}

Expand All @@ -68,7 +68,6 @@
const commentFoundId = commentFound ? commentFound.id : null;

if (commentFound) {
console.log(`Found comment with MARKER: ${MARKER}`);
const commentId = commentFoundId;
const originalBody = commentFound.body;
const updatedBody = `${originalBody}\n${message}`;
Expand All @@ -80,14 +79,18 @@
commentId,
body: updatedBody
});
console.log(` ✅ Success. Entry posted to Skills Issue`);
} catch (err) {
console.error(`Something went wrong updating comment:`, err);
console.error(`Something went wrong posting entry:`, err);
}

} else {
console.log(`MARKER not found in comments, creating new comment with MARKER...`);
console.log(`MARKER not found, creating new comment entry with MARKER...`);
const body = `${MARKER}\n## Activity Log: ${eventActor}\n### Repo: https://github.com/hackforla/website\n\n##### ⚠ Important note: The bot updates this comment automatically - do not edit\n\n${message}`;
await postComment(skillsIssueNum, body, github, context);
const commentPosted = await postComment(skillsIssueNum, body, github, context);
if (commentPosted) {
console.log(` ✅ Success. Entry posted to Skills Issue`);
}
}

// If eventActor is team member, open issue and move to "In progress". Else, close issue
Expand All @@ -108,8 +111,9 @@
issue_number: skillsIssueNum,
state: skillsIssueState,
});
console.log(` ⮡ Re-opened issue #${skillsIssueNum}`)
} catch (err) {
console.error(`Failed to update issue #${skillsIssueNum} state:`, err);
console.error(`Failed to update issue #${skillsIssueNum} state:`, err);
}
}

Expand Down
1 change: 1 addition & 0 deletions github-actions/utils/post-issue-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async function postComment(issueNum, comment, github, context) {
issue_number: issueNum,
body: comment,
});
return true;
} catch (err) {
throw new Error(err);
}
Expand Down