Search for issues and PR labeled 'agenda' and add them to the agenda #76
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Search for issues and PR labeled 'agenda' and add them to the agenda | |
on: | |
schedule: | |
- cron: '0 1 * * 1' # Runs at 1:00 AM every Monday | |
repository_dispatch: | |
types: add-issues-ocwm | |
jobs: | |
search_and_add: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Node 20 | |
uses: actions/setup-node@v4 | |
with: | |
node-version: '20' | |
- name: Get Token | |
uses: actions/create-github-app-token@v1 | |
id: get_workflow_token | |
with: | |
app-id: ${{ vars.APP_ID }} | |
private-key: ${{ secrets.PRIVATE_KEY }} | |
- name: Install dependencies | |
run: npm install @octokit/[email protected] | |
# Step to check if today is the third Monday | |
- name: Check if today is the third Monday | |
id: check-third-monday | |
run: | | |
day=$(date +%d) | |
dow=$(date +%u) # Day of the week (1 = Monday, ..., 7 = Sunday) | |
# Check if the day is between 15th and 21st, and if it's Monday (1) | |
if [ "$dow" -ne 1 ]; then | |
echo "Not a Monday, exiting..." | |
echo "::set-output name=is-third-monday::false" | |
exit 0 | |
fi | |
if [ "$day" -ge 15 ] && [ "$day" -le 21 ]; then | |
echo "This is the third Monday of the month!" | |
echo "::set-output name=is-third-monday::true" | |
else | |
echo "Not the third Monday, exiting..." | |
echo "::set-output name=is-third-monday::false" | |
exit 0 | |
fi | |
- name: Adding Issues | |
id: add-issues | |
if: steps.check-third-monday.outputs.is-third-monday == 'true' | |
uses: actions/github-script@v7 | |
env: | |
PLACEHOLDER: '<!-- | [TOPIC] [IssuePRDiscussion] | [owner] | -->' | |
OWNER: ${{ vars.ORGANISATION }} | |
REPO: 'community' | |
REPO_NAMES: ${{ vars.REPOSITORIES }} | |
AGENDA_LABEL: ${{ vars.AGENDA_LABEL }} | |
OCWM_LABEL: ${{ vars.OCWM_LABEL }} | |
MY_TOKEN: ${{ steps.get_workflow_token.outputs.token }} | |
with: | |
script: | | |
const octokit = require('@octokit/core').Octokit | |
const mygithub = new octokit({ | |
request: { fetch: fetch,}, | |
auth: process.env.MY_TOKEN | |
}); | |
const repoNames = process.env.REPO_NAMES; | |
const placeholder = process.env.PLACEHOLDER; | |
console.log("Placeholder:" + placeholder); | |
let repositories = repoNames.split(",") | |
let targetLabel = encodeURIComponent(process.env.OCWM_LABEL); | |
let appendLabel = encodeURIComponent(process.env.AGENDA_LABEL); | |
console.log("Repositories:" + repositories); | |
console.log(`GET /repos/${process.env.OWNER}/${process.env.REPO}/issues?labels=${targetLabel}&per_page=1`); | |
const { data: workMeetings } = await mygithub.request(`GET /repos/${process.env.OWNER}/${process.env.REPO}/issues?labels=${targetLabel}&per_page=1`, { | |
}) | |
console.log("workMeetings:" + JSON.stringify(workMeetings)); | |
for (let r = 0; r < repositories.length; r++) { | |
const { data: items2add } = await mygithub.request(`GET /repos/${process.env.OWNER}/${repositories[r]}/issues?labels=${appendLabel}`); | |
console.log("Issues to add:" + JSON.stringify(items2add)); | |
const query = ` | |
query { | |
search(query: "repo:${process.env.OWNER}/${repositories[r]} is:open label:${appendLabel}", type: DISCUSSION, first: 5) { | |
edges { | |
node { | |
... on Discussion { | |
id | |
title | |
body | |
resourcePath | |
author { | |
login | |
} | |
} | |
} | |
} | |
} | |
}`; | |
const response = await mygithub.graphql(query, {}); | |
console.log("Response:" + JSON.stringify(response)); | |
const discussions = response.search.edges; | |
console.log("Discussions to add:" + JSON.stringify(discussions)); | |
try { | |
let body = workMeetings[0].body; | |
let changesFlag = false; | |
// Loop through issues to add them to the agenda | |
for (let i = 0; i < items2add.length; i++) { | |
let url = items2add[i].html_url; | |
let author = "@" + items2add[i].user.login; | |
let title = items2add[i].title; | |
let search = parseInt(JSON.stringify(body).search(url)); | |
if (search === -1) { | |
let startIndex = parseInt(JSON.stringify(body.indexOf(placeholder))) | |
let json_text = JSON.stringify(body.substring(0, startIndex - 1) + `\n| ${url} - ${title} | ${author} |\r\n` + body.substring(startIndex, body.length)); | |
body = JSON.parse(json_text); | |
changesFlag = true; | |
await mygithub.request(`DELETE /repos/${process.env.OWNER}/${repositories[r]}/issues/${items2add[i].number}/labels/${appendLabel}`); | |
} | |
} | |
// Loop through discussions to add them to the agenda | |
for (let i = 0; i < discussions.length; i++) { | |
let url = "https://github.com/" + discussions[i].node.resourcePath; | |
let author = "@" + discussions[i].node.author.login; | |
let title = discussions[i].node.title; | |
let search = parseInt(JSON.stringify(body).search(url)); | |
if (search === -1) { | |
let startIndex = parseInt(JSON.stringify(body.indexOf(placeholder))) | |
let json_text = JSON.stringify(body.substring(0, startIndex - 1) + `\n| ${url} - ${title} | ${author} |\r\n` + body.substring(startIndex, body.length)); | |
body = JSON.parse(json_text); | |
changesFlag = true; | |
} | |
} | |
if (changesFlag){ | |
let template = JSON.stringify(JSON.stringify(body)); | |
let parsed = JSON.parse(JSON.parse(template)); | |
console.log(`PATCH /repos/${process.env.OWNER}/${process.env.REPO}/issues/${workMeetings[0].number}`); | |
const { data: label } = await mygithub.request(`GET /repos/${process.env.OWNER}/${process.env.REPO}/labels/${process.env.AGENDA_LABEL}`); | |
await mygithub.request(`PATCH /repos/${process.env.OWNER}/${process.env.REPO}/issues/${workMeetings[0].number}`, { | |
body: parsed, | |
milestone: null, | |
state: 'open', | |
}) | |
for (let i = 0; i < discussions.length; i++) { | |
await mygithub.graphql(` | |
mutation { | |
removeLabelsFromLabelable(input: {labelIds: ["${label.node_id}"], labelableId: "${discussions[i].node.id}"}) { | |
clientMutationId | |
} | |
} | |
`); | |
} | |
} | |
} | |
catch (err) { | |
console.error("Error:"+err.message); | |
console.log("There is no OCWM available"); | |
} | |
} |