Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GHA add comments to prework issue 4820 #6873

Open
wants to merge 10 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 13 additions & 0 deletions .github/workflows/issue-comment-trigger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Issue Comment Trigger
on:
issue_comment:
types:
- created

jobs:
Add-Comment-To-Prework-Issue:
if: ${{ github.event_name == 'issue_comment' && github.event.action == 'created'}}
permissions:
issues: write
uses: ./.github/workflows/prework-issue-comment-reusable-workflow.yaml
secrets: inherit
10 changes: 9 additions & 1 deletion .github/workflows/issue-trigger.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Issue Trigger
on:
issues:
types: [opened, transferred, assigned, labeled, unlabeled]
types: [opened, closed, transferred, assigned, unassigned, labeled, unlabeled]

jobs:
# Adds newly created issues onto project board in the default column 'New Issue Approval'
Expand Down Expand Up @@ -111,3 +111,11 @@ jobs:
script: |
const script = require('./github-actions/trigger-issue/feature-branch-comment/hide-feature-branch-comment.js')
script({g: github, c: context})

# Adds a comment to actor's prework issue
Add-Comment-To-Prework-Issue:
if: "${{ github.event.action == 'opened' || github.event.action == 'closed' || github.event.action == 'assigned' || github.event.action == 'unassigned'}}"
permissions:
issues: write
uses: ./.github/workflows/prework-issue-comment-reusable-workflow.yaml
secrets: inherit
72 changes: 72 additions & 0 deletions .github/workflows/prework-issue-comment-reusable-workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Find Prework Issue and Comment User Activity

on:
workflow_call:

jobs:
Add-Comment-To-Prework-Issue-Reusable:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- uses: actions/checkout@v4

- name: Get activity detail
id: get-activity-detail
uses: actions/github-script@v7
with:
script: |
const script = require('./github-actions/prework-issue-reusable-workflow/get-activity-detail.js')
const contributor = script({g: github, c: context})
return contributor

- name: Get prework issue
id: get-prework-issue
uses: actions/github-script@v7
with:
script: |
const script = require('./github-actions/prework-issue-reusable-workflow/get-prework-issue.js')
const activityDetail = ${{steps.get-activity-detail.outputs.result}}
if (!activityDetail) {
return false
}
return script({g: github, c: context}, activityDetail)

- name: Update prework issue status
id: update-prework-issue-status
uses: actions/github-script@v7
with:
github-token: ${{ secrets.HACKFORLA_BOT_PA_TOKEN }}
script: |
const script = require('./github-actions/prework-issue-reusable-workflow/update-prework-issue-status.js')
const issue = ${{steps.get-prework-issue.outputs.result}}
if (issue) {
return script({g: github, c: context}, issue)
}
return false

# Only move issue if issue was closed and is reopened
- name: Move issue
uses: actions/github-script@v7
with:
github-token: ${{ secrets.HACKFORLA_BOT_PA_TOKEN }}
# NOTE: The projectColumnId is hard-coded
script: |
const projectColumnId = "MDEzOlByb2plY3RDb2x1bW43MTk4MjI4"
const issue = ${{steps.update-prework-issue-status.outputs.result}}
const script = require('./github-actions/utils/update-issue-project-card.js')
if (issue) {
const issueCardId = issue.projectCards.nodes[0].id
script(issueCardId, projectColumnId, github)
}

- name: Leave comment on issue
uses: actions/github-script@v7
with:
script: |
const script = require('./github-actions/prework_issue_comment/add-prework-comment.js')
const issue = ${{steps.get-prework-issue.outputs.result}}
const activityDetail = ${{steps.get-activity-detail.outputs.result}}
if (issue && activityDetail) {
script({ g: github, c: context}, issue.number, activityDetail)
}
12 changes: 12 additions & 0 deletions .github/workflows/pull-request-review-comment-trigger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Pull Request Review Comment Trigger
on:
pull_request_review_comment:
types:
- created
jobs:
Add-Comment-To-Prework-Issue:
if: ${{ github.event_name == 'pull_request_review_comment' && github.event.action == 'created'}}
permissions:
issues: write
uses: ./.github/workflows/prework-issue-comment-reusable-workflow.yaml
secrets: inherit
12 changes: 12 additions & 0 deletions .github/workflows/pull-request-review-trigger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: Pull Request Review Trigger
on:
pull_request_review:
types:
- submitted
jobs:
Add-Comment-To-Prework-Issue:
if: ${{ github.event_name == 'pull_request_review' && github.event.action == 'submitted'}}
permissions:
issues: write
uses: ./.github/workflows/prework-issue-comment-reusable-workflow.yaml
secrets: inherit
9 changes: 8 additions & 1 deletion .github/workflows/pull-request-trigger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,11 @@ jobs:
project: Project Board
column: 'PR Needs review (Automated Column, do not place items here manually)'
repo-token: ${{ secrets.HACKFORLA_BOT_PA_TOKEN }}
action: delete
action: delete

Add-Comment-To-Prework-Issue:
if: ${{ github.event_name == 'pull_request' && (github.event.action == 'opened' || github.event.action == 'closed')}}
permissions:
issues: write
uses: ./.github/workflows/prework-issue-comment-reusable-workflow.yaml
secrets: inherit
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Import modules
const postComment = require("../utils/post-issue-comment")

// Global variables
var github
var context

/**
* @description - This function is the entry point into the javascript file, it leaves a comment on the prework issue
* @param {Object} g - github object
* @param {Object} c - context object
* @param {Number} issueNumber - issue
* @param {Object} activityDetail - details of the activity performed
*/
async function main({ g, c }, issueNumber, activityDetail) {
github = g
context = c
const comment = await makeComment(activityDetail)
if (comment !== null) {
await postComment(issueNumber, comment, github, context)
}
}

/**
* @description - This function makes the comment
* @param {Object} activityDetail - details of the activity performed
* @returns {Promise<string>} - Comment to be posted with the issue label event actor's name
*/

async function makeComment(activityDetail) {
const comment = `${activityDetail.activityObject} has been ${activityDetail.action} by @${activityDetail.contributor}
`
return comment
}

module.exports = main
136 changes: 136 additions & 0 deletions github-actions/prework-issue-reusable-workflow/get-activity-detail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Import modules
var fs = require("fs")

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable fs.

// Global variables
var github
var context

/**
* @description - This function is the entry point into the javascript file,
* It will determine the eventName and return the proper activity detail
* @param {Object} g - github object
* @param {Object} c - context object
*/
async function main({ g, c }) {
github = g
context = c

switch (context.eventName) {
case "issue_comment":
return await getIssueCommentEventType(context)
case "issues":
return await getIssueEventType(context)
case "pull_request":
return await getPullRequestEventType(context)
case "pull_request_review":
return await getPullRequestReviewEventType(context)
case "pull_request_review_comment":
return await getPullRequestReviewCommentEventType(context)
default:
// Handle cases where eventName doesn't match any known event type
console.log(`Unknown event name: ${context.eventName}`)
}
}

/**
* Retrieves detailed information about an issue event based on the action performed.
* @param {*} context
* @returns - An object containing the activity detail:
* - contributor
* - action
* - object the activity was performed on (issue, issue_comment, pr, pr_comment, pr_review)
*
*/
async function getIssueEventType(context) {
const activityDetail = {
contributor: "",
action: context.payload.action,
activityObject: `Issue #${context.payload.issue.number}`,
}
if (context.payload.action == "opened") {
activityDetail.contributor = context.payload.issue.user.login
} else if (context.payload.action == "closed") {
// NOTE: context.payload.issue.assignee.login can be null if issue is not assigned and marked closed
activityDetail.contributor = context.payload.issue.assignee.login
} else {
// on unassigned event, the `issue.assignee` is null.
activityDetail.contributor = context.payload.assignee.login
}
return activityDetail
}

/**
* Retrieves detailed information about an issue event based on the action performed.
* @param {*} context
* @returns - An object containing the activity detail:
* - contributor
* - action
* - object the activity was performed on (issue, issue_comment, pr, pr_comment, pr_review)
*
*/
async function getIssueCommentEventType(context) {
const activityDetail = {
contributor: context.payload.comment.user.login,
action: context.payload.action,
activityObject: context.payload.comment.url,
}
return activityDetail
}

/**
* Retrieves detailed information about an issue event based on the action performed.
* @param {*} context
* @returns - An object containing the activity detail:
* - contributor
* - action
* - object the activity was performed on (issue, issue_comment, pr, pr_comment, pr_review)
*
*/
async function getPullRequestEventType(context) {
const activityDetail = {
contributor: context.payload.pull_request.user.login,
action: context.payload.action,
activityObject: `PR #${context.payload.pull_request.number}`,
}
return activityDetail
}

/**
* Retrieves detailed information about an issue event based on the action performed.
* @param {*} context
* @returns - An object containing the activity detail:
* - contributor
* - action
* - object the activity was performed on (issue, issue_comment, pr, pr_comment, pr_review)
*
*/
async function getPullRequestReviewEventType(context) {
// also achievable by the context.sender - user who did this event
const activityDetail = {
contributor: context.payload.review.user.login,
action: context.payload.action,
activityObject: `PR #${context.payload.pull_request.number}`,
}
return activityDetail
}

/**
* Retrieves detailed information about an issue event based on the action performed.
* @param {*} context
* @returns - An object containing the activity detail:
* - contributor
* - action
* - object the activity was performed on (issue, issue_comment, pr, pr_comment, pr_review)
*
*/
async function getPullRequestReviewCommentEventType(context) {
// also achievable by the context.sender - user who did this event
const activityDetail = {
contributor: context.payload.comment.user.login,
action: context.payload.action,
activityObject: `${context.payload.comment.url}`,
}
return activityDetail
}

module.exports = main
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Import modules
var getIssueByLabel = require("../utils/get-issue-by-label")

// Global variables
var github
var context

/**
* @description - This function is the entry point into the javascript file,
* it will find the prework issue
*
* @param {Object} g - github object
* @param {Object} c - context object
* @param {Object} activityDetail - person who triggered the workflow
*/
async function main({ g, c }, activityDetail) {
github = g
context = c
let labels = ["Complexity: Prework"]
const issue = await getIssueByLabel(
activityDetail.contributor,
labels,
github,
context
)
return issue
}

module.exports = main
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Import modules
var reopenIssue = require("../utils/reopen-issue")

// Global variables
var github
var context

/**
* @description - This function is the entry point into the javascript file, it will reopen the issue
* @param {Object} g - github object
* @param {Object} c - context object
* @param {Object} issue - the issue to update
*/
async function main({ g, c }, issue) {
github = g
context = c

// TODO value is hardcoded
// Project Number =1 , for HFLA project number = 7
console.log(issue)
if (issue.closed == true) {
const result = await reopenIssue(issue.id, github)
console.log(result)
return result.reopenIssue.issue
}
return false
}

module.exports = main
Loading