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

Add logging to project automations #3404

Merged
merged 19 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e356aab
Add detailed `console.log` statements for enhanced debugging in GitHu…
Proibito04 Nov 27, 2023
680d771
temp update of console.log
Proibito04 Nov 28, 2023
77b75a6
Refactor logging to use `@actions/core` and remove redundant logs
Proibito04 Nov 28, 2023
ce70e6f
Remove `debug` import from issues.mjs
Proibito04 Nov 28, 2023
723183f
Merge branch 'main' into issue_3396
Proibito04 Nov 28, 2023
f1d96e9
Merge branches 'issue_3396' and 'issue_3396' of github.com:Proibito04…
Proibito04 Nov 28, 2023
c05900f
Merge branch 'main' into issue_3396
Proibito04 Nov 30, 2023
df83e81
Pass `core` to from YAML to `main` to `Project`
dhruvkb Dec 1, 2023
8742c21
Use `this.core` to log info, error and debug lines
dhruvkb Dec 1, 2023
e2394a9
Refactored Project class, updated moveCard and YAML files
Proibito04 Dec 2, 2023
ed58894
Applied suggested changes in issue.mjs and projects.mjs.
Proibito04 Dec 2, 2023
ec5fea3
Merge branch 'main' into issue_3396
Proibito04 Dec 4, 2023
917929e
Merge branch 'main' into issue_3396
Proibito04 Dec 5, 2023
9556346
Applied suggested changes in issue.mjs and projects.mjs.
Proibito04 Dec 11, 2023
12a56a9
Merge branch 'issue_3396' of github.com:Proibito04/openverse into iss…
Proibito04 Dec 11, 2023
51cccf6
Merge branch 'main' of https://github.com/WordPress/openverse into is…
dhruvkb Dec 14, 2023
b99d5ed
Add more logging
dhruvkb Dec 14, 2023
e7ecf6c
Dedent code and increase logging
dhruvkb Dec 14, 2023
9c58a2d
Add more `info` and `debug` logs
dhruvkb Dec 14, 2023
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
37 changes: 29 additions & 8 deletions automations/js/src/project_automation/issues.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getBoard } from '../utils/projects.mjs'

import { debug } from '@actions/core'

Proibito04 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Set the "Priority" custom field based on the issue's labels. Also move
* the card for critical issues directly to the "📅 To Do" column.
Expand All @@ -9,14 +11,18 @@ import { getBoard } from '../utils/projects.mjs'
* @param card {import('../utils/projects.mjs').Card}
*/
async function syncPriority(issue, board, card) {
debug('Starting syncPriority for issue:', issue.number)
const priority = issue.labels.find((label) =>
label.name.includes('priority')
)?.name

if (priority) {
await board.setCustomChoiceField(card.id, 'Priority', priority)
}
if (priority === '🟥 priority: critical') {
console.log("::debug::Moving card to 'To Do' for critical priority issue")
await board.moveCard(card.id, board.columns.ToDo)
console.log("::debug::Card moved to 'To Do' column")
Proibito04 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -28,60 +34,75 @@ async function syncPriority(issue, board, card) {
*/
export const main = async (octokit, context) => {
Proibito04 marked this conversation as resolved.
Show resolved Hide resolved
const { EVENT_ACTION: eventAction } = process.env
console.log('::debug::Event action received:', eventAction)
Proibito04 marked this conversation as resolved.
Show resolved Hide resolved

const issue = context.payload.issue
const label = context.payload.label
console.log('::debug::Issue details:', issue)

if (issue.labels.some((label) => label.name === '🧭 project: thread')) {
// Do not add project threads to the Backlog board.
console.log('::debug::Issue is a project thread. Exiting.')
process.exit(0)
}

const backlogBoard = await getBoard(octokit, 'Backlog')
console.log('::debug::Backlog board fetched')

// Create new, or get the existing, card for the current issue.
const card = await backlogBoard.addCard(issue.node_id)
console.log('::debug::Card created or fetched for the issue:', card.id)

switch (eventAction) {
case 'opened':
case 'reopened': {
console.log('::debug::Issue opened or reopened')
if (issue.labels.some((label) => label.name === '⛔ status: blocked')) {
console.log(
"::debug::Issue is blocked. Moving card to 'Blocked' column"
)
await backlogBoard.moveCard(card.id, backlogBoard.columns.Blocked)
} else {
console.log("::debug::Moving card to 'Backlog'")
await backlogBoard.moveCard(card.id, backlogBoard.columns.Backlog)
}

await syncPriority(issue, backlogBoard, card)
break
}

case 'closed': {
console.log('::debug::Issue closed')
if (issue.state_reason === 'completed') {
console.log("::debug::Issue completed. Moving card to 'Done'")
await backlogBoard.moveCard(card.id, backlogBoard.columns.Done)
} else {
console.log("::debug::Issue not completed. Moving card to 'Discarded'")
await backlogBoard.moveCard(card.id, backlogBoard.columns.Discarded)
}
break
}

case 'assigned': {
console.log('::debug::Issue assigned')
if (card.status === backlogBoard.columns.Backlog) {
console.log("::debug::Moving card to 'To Do'")
await backlogBoard.moveCard(card.id, backlogBoard.columns.ToDo)
}
break
}

case 'labeled': {
console.log('::debug::Issue labeled:', label.name)
if (label.name === '⛔ status: blocked') {
console.log(
"::debug::Issue is blocked. Moving card to 'Blocked' column"
)
await backlogBoard.moveCard(card.id, backlogBoard.columns.Blocked)
}
await syncPriority(issue, backlogBoard, card)
break
}

case 'unlabeled': {
console.log('::debug::Label removed:', label.name)
if (label.name === '⛔ status: blocked') {
// TODO: Move back to the column it came from.
console.log(
"::debug::'Blocked' label removed. Moving card to 'Backlog'"
)
await backlogBoard.moveCard(card.id, backlogBoard.columns.Backlog)
}
await syncPriority(issue, backlogBoard, card)
Expand Down
39 changes: 32 additions & 7 deletions automations/js/src/project_automation/prs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ import { PullRequest } from '../utils/pr.mjs'
* @param prCard {Card}
*/
async function syncReviews(pr, prBoard, prCard) {
console.log('::debug::Synchronizing reviews for PR:', pr.nodeId)
const reviewDecision = pr.reviewDecision
const reviewCounts = pr.reviewCounts
console.log('::debug::Review decision:', reviewDecision)

if (reviewDecision === 'APPROVED') {
console.log("::debug::Moving PR to 'Approved'")
await prBoard.moveCard(prCard.id, prBoard.columns.Approved)
} else if (reviewDecision === 'CHANGES_REQUESTED') {
console.log(
"::debug::Changes requested for PR. Moving to 'ChangesRequested'"
)
await prBoard.moveCard(prCard.id, prBoard.columns.ChangesRequested)
} else if (reviewCounts.APPROVED === 1) {
} else if (pr.reviewCounts.APPROVED === 1) {
console.log("::debug::PR needs 1 more review. Moving to 'Needs1Review'")
await prBoard.moveCard(prCard.id, prBoard.columns.Needs1Review)
} else {
console.log("::debug::PR needs 2 more reviews. Moving to 'Needs2Reviews'")
await prBoard.moveCard(prCard.id, prBoard.columns.Needs2Reviews)
}
}
Expand All @@ -33,8 +40,11 @@ async function syncReviews(pr, prBoard, prCard) {
* @param destColumn {string}
*/
async function syncIssues(pr, backlogBoard, destColumn) {
console.log('::debug::Synchronizing linked issues for PR:', pr.nodeId)
for (let linkedIssue of pr.linkedIssues) {
console.log('::debug::Processing linked issue:', linkedIssue)
const issueCard = await backlogBoard.addCard(linkedIssue)
console.log('::debug::Moving linked issue to column:', destColumn)
await backlogBoard.moveCard(issueCard.id, backlogBoard.columns[destColumn])
}
}
Expand All @@ -45,51 +55,66 @@ async function syncIssues(pr, backlogBoard, destColumn) {
* @param octokit {import('@octokit/rest').Octokit} the Octokit instance to use
*/
export const main = async (octokit) => {
console.log('::debug::Starting PR script')

const { eventName, eventAction, prNodeId } = JSON.parse(
readFileSync('/tmp/event.json', 'utf-8')
)
console.log(
'::debug::Event details - Name:',
eventName,
', Action:',
eventAction,
', PR Node ID:',
prNodeId
)

const pr = new PullRequest(octokit, prNodeId)
await pr.init()

const prBoard = await getBoard(octokit, 'PRs')
const backlogBoard = await getBoard(octokit, 'Backlog')

// Create new, or get the existing, card for the current pull request.
const prCard = await prBoard.addCard(pr.nodeId)
console.log('::debug::PR card created or fetched:', prCard.id)

if (eventName === 'pull_request_review') {
await syncReviews(pr, prBoard, prCard)
} else {
switch (eventAction) {
case 'opened':
case 'reopened': {
console.log('::debug::PR opened or reopened')
if (pr.isDraft) {
console.log("::debug::PR is a draft. Moving to 'Draft'")
await prBoard.moveCard(prCard.id, prBoard.columns.Draft)
} else {
await syncReviews(pr, prBoard, prCard)
}
await syncIssues(pr, backlogBoard, 'InProgress')
break
}

case 'edited': {
console.log('::debug::PR edited')
await syncIssues(pr, backlogBoard, 'InProgress')
break
}

case 'converted_to_draft': {
console.log('::debug::PR converted to draft')
await prBoard.moveCard(prCard.id, prBoard.columns.Draft)
break
}

case 'ready_for_review': {
console.log('::debug::PR ready for review')
await syncReviews(pr, prBoard, prCard)
break
}

case 'closed': {
console.log('::debug::PR closed')
if (!pr.isMerged) {
console.log(
"::debug::PR not merged. Moving linked issues to 'Backlog'"
)
await syncIssues(pr, backlogBoard, 'Backlog')
}
break
Expand Down
4 changes: 4 additions & 0 deletions automations/js/src/utils/projects.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @typedef {{projectId: string, fields: {[p: string]: Field}}} ProjectDetails
*/

import { debug } from '@actions/core'

const PROJECT_NUMBERS = {
Backlog: 75,
PRs: 98,
Expand Down Expand Up @@ -164,6 +166,7 @@ class Project {
* @returns {Promise<string>} the ID of the card that was updated
*/
async setCustomChoiceField(cardId, fieldName, optionName) {
debug('Setting priority:', priority, 'for card:', card.id)
// Preliminary validation
if (!this.fields[fieldName]) {
throw new Error(`Unknown field name "${fieldName}".`)
Expand Down Expand Up @@ -194,6 +197,7 @@ class Project {
optionId: this.fields[fieldName].options[optionName],
}
)
debug('Priority set for card:', card.id)
return res.updateProjectV2ItemFieldValue.projectV2Item.id
}

Expand Down