-
Notifications
You must be signed in to change notification settings - Fork 6
/
run.js
160 lines (143 loc) · 4.79 KB
/
run.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict'
const core = require('@actions/core')
const github = require('@actions/github')
const list = require('safe-parse-list')
const ejs = require('ejs')
const meetings = require('./lib/meetings')
const issues = require('./lib/issues')
const notes = require('./lib/notes')
const defaultTemplate = require('./lib/default-template')
const defaultNotesTemplate = require('./lib/default-notes-template')
const conversions = require('./lib/conversions')
const pkg = require('./package.json')
;(async function run () {
console.log(`Version: ${pkg.version}`)
try {
const token = core.getInput('token')
const client = new github.GitHub(token)
// variables we use for timing
const schedules = list(core.getInput('schedules'))
const createWithin = core.getInput('createWithin')
// variables we use for labels
const agendaLabel = core.getInput('agendaLabel')
const meetingLabels = core.getInput('meetingLabels')
// variables we use for content
const issueTitle = core.getInput('issueTitle')
const issueTemplate = core.getInput('issueTemplate')
// variables we use for notes
const createNotes = core.getInput('createNotes')
const notesUserTemplate = core.getInput('notesTemplate')
// Get list of repos
let repos = core.getInput('repos')
const repo = github.context.repo
if (repos) {
repos = repos.split(',').map((str) => {
const parts = str.trim().split('/')
return {
owner: parts[0],
repo: parts[1]
}
})
} else {
repos = [github.context.repo]
}
// Get repos from orgs
let orgs = core.getInput('orgs')
if (orgs) {
orgs = orgs.split(',').map((o) => o.trim())
for (const org of orgs) {
console.log(`Fetching repos for ${org}`)
const resp = await client.paginate('GET /orgs/{org}/repos', { org })
resp.forEach((r) => {
repos.push({
owner: org,
repo: r.name
})
})
}
}
let template = defaultTemplate
// if we have a user-provided issue template, try to get it
// and then if it exists reassign template variable from the
// default to the user-provided template
if (issueTemplate) {
try {
const userProvidedIssueTemplate = await issues.stringifiedIssueTemplate(client, {
...repo,
template: issueTemplate,
ref: github.context.payload?.pull_request?.head?.ref || github.context.payload?.repository?.default_branch || 'main'
})
template = conversions.convert(userProvidedIssueTemplate)
template = ejs.compile(userProvidedIssueTemplate)
} catch (error) {
console.error(`template missing or invalid (${issueTemplate}): ${error.message}`)
}
}
let titleTemplate = issueTitle
if (issueTemplate) {
try {
titleTemplate = ejs.compile(titleTemplate)
} catch (e) {
// ignore title template
}
}
const agendaIssues = []
for (const r of repos) {
console.log(`Fetching issues for ${r.owner}/${r.repo}`)
const _agendaIssues = await client.paginate('GET /repos/{owner}/{repo}/issues', {
owner: r.owner,
repo: r.repo,
state: 'open',
labels: agendaLabel
})
for (const i of _agendaIssues) {
if (!agendaIssues.find((ii) => ii.url === i.url)) {
agendaIssues.push(i)
break
}
}
}
const opts = {
...repo,
schedules,
meetingLabels,
createWithin,
agendaLabel,
agendaIssues,
issueTitle: titleTemplate
}
const issue = await meetings.createNextMeeting(client, { ...opts, template })
if (!issue) {
return console.log('No issues to create')
}
const issueNumber = issue.data.number
core.setOutput('issueNumber', issueNumber.toString())
console.log(`Issue created: (#${issueNumber}) ${issue.data.title}`)
opts.issue = issue.data
if (createNotes === true || createNotes === 'true') {
let notesTemplate = defaultNotesTemplate
if (notesUserTemplate) {
try {
const tmpl = await notes.getNotesTemplate(client, {
...repo,
notesUserTemplate
})
notesTemplate = ejs.compile(tmpl)
} catch (e) {
console.error(`notesTemplate missing or invalid (${notesUserTemplate}): ${e.message}`)
}
}
opts.meetingNotes = await notes.create(notesTemplate, opts)
console.log(`Notes created: ${opts.meetingNotes}`)
}
const updatedIssue = await meetings.setMeetingIssueBody(client, { ...opts, template })
if (!updatedIssue) {
return console.log('No issues to update')
} else {
return console.log('Issue updated successfully')
}
} catch (e) {
console.error(e)
core.setFailed(e.message)
}
})()