Skip to content

Commit 16b5a79

Browse files
committed
chore: separate discussions logic
Signed-off-by: Sebastian Beltran <bjohansebas@gmail.com>
1 parent 3d13e7c commit 16b5a79

5 files changed

Lines changed: 131 additions & 90 deletions

File tree

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ inputs:
4848
orgs:
4949
description: 'An optional list of org in the format: <org>,<org>'
5050
required: false
51+
discussions:
52+
description: 'Disable/Active discussions fetching'
53+
default: 'true'
54+
required: false
5155
outputs:
5256
issueNumber:
5357
description: 'If an issue was created, this will be its number'

lib/agenda.js

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const { graphql } = require('@octokit/graphql')
4+
35
/**
46
* get agenda issues and PRs from repositories
57
* @param {Object} client - GitHub client
@@ -38,7 +40,7 @@ async function fetchAgendaItems (client, repos, agendaLabel) {
3840
labels: agendaLabel,
3941
per_page: 100
4042
})).filter(pr => pr.labels.find(label => label.name === agendaLabel) &&
41-
!(agendaIssues.find((i) => i.url === pr.url))) // workaround for flaky GH API/SDK behavior where sometimes the issue endpoint loads PRs
43+
!(agendaIssues.find((i) => i.url === pr.url))) // workaround for flaky GH API/SDK behavior where sometimes the issue endpoint loads PRs
4244

4345
console.log(`Fetching PRs for ${r.owner}/${r.repo}: Found ${_agendaPrs.length}`)
4446

@@ -52,6 +54,80 @@ async function fetchAgendaItems (client, repos, agendaLabel) {
5254
return agendaIssues
5355
}
5456

57+
async function fetchDiscussionsItems (repos, agendaLabel, token) {
58+
const agendaDiscussions = []
59+
for (const r of repos) {
60+
let hasNextPage = true
61+
let endCursor = null
62+
do {
63+
const query = `
64+
query($owner: String!, $name: String!, $after: String) {
65+
repository(owner: $owner, name: $name) {
66+
discussions(first: 100, after: $after) {
67+
pageInfo {
68+
endCursor
69+
hasNextPage
70+
}
71+
edges {
72+
cursor
73+
node {
74+
id
75+
title
76+
url
77+
labels(first: 10) {
78+
nodes {
79+
color
80+
name
81+
}
82+
}
83+
}
84+
}
85+
}
86+
}
87+
}
88+
`
89+
const variables = {
90+
owner: r.owner,
91+
name: r.repo,
92+
after: endCursor
93+
}
94+
95+
const _agendaDiscussions = await graphql(query, {
96+
...variables,
97+
headers: {
98+
authorization: `token ${token}`
99+
}
100+
})
101+
102+
const discussions = _agendaDiscussions?.repository?.discussions
103+
104+
if (discussions) {
105+
const { edges, pageInfo } = discussions
106+
for (const edge of edges) {
107+
const labels = edge.node?.labels.nodes
108+
if (Array.isArray(labels) && labels.some(label => label.name === agendaLabel)) {
109+
console.log(`Adding Discussion: ${edge.node.url}`)
110+
agendaDiscussions.push({
111+
id: edge.node.id,
112+
html_url: edge.node.url,
113+
title: edge.node.title
114+
})
115+
}
116+
}
117+
hasNextPage = pageInfo.hasNextPage
118+
endCursor = pageInfo.endCursor
119+
} else {
120+
hasNextPage = false
121+
}
122+
} while (hasNextPage)
123+
}
124+
125+
console.log(`Found ${agendaDiscussions.length} total discussions for agenda`)
126+
127+
return agendaDiscussions
128+
}
129+
55130
module.exports = {
56-
fetchAgendaItems
131+
fetchAgendaItems,
132+
fetchDiscussionsItems
57133
}

package-lock.json

Lines changed: 38 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@actions/github": "^6.0.1",
2828
"@hackmd/api": "^2.5.0",
2929
"@js-temporal/polyfill": "^0.5.1",
30-
"@octokit/graphql": "^9.0.1",
30+
"@octokit/graphql": "^8.2.2",
3131
"ejs": "^3.1.10",
3232
"safe-parse-list": "^0.1.1"
3333
},

run.js

Lines changed: 10 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22
const core = require('@actions/core')
33
const { getOctokit, context } = require('@actions/github')
4-
const { graphql } = require('@octokit/graphql')
54
const list = require('safe-parse-list')
65
const ejs = require('ejs')
76
const meetings = require('./lib/meetings')
@@ -32,6 +31,8 @@ const pkg = require('./package.json')
3231
const issueTitle = core.getInput('issueTitle')
3332
const issueTemplate = core.getInput('issueTemplate')
3433

34+
const discussions = core.getBooleanInput('discussions')
35+
3536
// variables we use for notes
3637
const createNotes = core.getInput('createNotes')
3738
const notesUserTemplate = core.getInput('notesTemplate')
@@ -97,72 +98,15 @@ const pkg = require('./package.json')
9798
}
9899
}
99100

100-
const agendaIssues = await agenda.fetchAgendaItems(client, repos, agendaLabel)
101-
102-
for (const r of repos) {
103-
let hasNextPage = true
104-
let endCursor = null
105-
do {
106-
const query = `
107-
query($owner: String!, $name: String!, $after: String) {
108-
repository(owner: $owner, name: $name) {
109-
discussions(first: 100, after: $after) {
110-
pageInfo {
111-
endCursor
112-
hasNextPage
113-
}
114-
edges {
115-
cursor
116-
node {
117-
id
118-
title
119-
url
120-
labels(first: 10) {
121-
nodes {
122-
color
123-
name
124-
}
125-
}
126-
}
127-
}
128-
}
129-
}
130-
}
131-
`
132-
const variables = {
133-
owner: r.owner,
134-
name: r.repo,
135-
after: endCursor
136-
}
137-
const _agendaDiscussions = await graphql(query, {
138-
...variables,
139-
headers: {
140-
authorization: `token ${token}`
141-
}
142-
})
143-
const discussions = _agendaDiscussions?.repository?.discussions
144-
if (discussions) {
145-
const { edges, pageInfo } = discussions
146-
for (const edge of edges) {
147-
const labels = edge.node?.labels.nodes
148-
if (Array.isArray(labels) && labels.some(label => label.name === agendaLabel)) {
149-
console.log(`Adding Discussion: ${edge.node.url}`)
150-
agendaIssues.push({
151-
id: edge.node.id,
152-
html_url: edge.node.url,
153-
title: edge.node.title
154-
})
155-
}
156-
}
157-
hasNextPage = pageInfo.hasNextPage
158-
endCursor = pageInfo.endCursor
159-
} else {
160-
hasNextPage = false
161-
}
162-
} while (hasNextPage)
101+
const agendaItems = []
102+
agendaItems.push(...await agenda.fetchAgendaItems(client, repos, agendaLabel))
103+
104+
if (discussions) {
105+
const agendaDiscussions = await agenda.fetchDiscussionsItems(repos, agendaLabel, token)
106+
agendaItems.push(...agendaDiscussions)
163107
}
164108

165-
console.log(`Found ${agendaIssues.length} total issues for agenda`)
109+
console.log(`Found ${agendaItems.length} total items for agenda`)
166110

167111
const opts = {
168112
...repo,
@@ -172,7 +116,7 @@ const pkg = require('./package.json')
172116
createWithin,
173117
agendaLabel,
174118
meetingLink,
175-
agendaIssues,
119+
agendaIssues: agendaItems,
176120
issueTitle: titleTemplate
177121
}
178122

0 commit comments

Comments
 (0)