-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
192 lines (173 loc) · 7.07 KB
/
action.yml
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions
---
name: Get Repositories
description: 🗄️ Get organization or user repos based on the topics filter (with logic AND/OR) to use in the matrix job or another action.
author: Dariusz Porowski
branding:
icon: list # https://feathericons.com
color: gray-dark
inputs:
owner:
description: GitHub Organization or User name, default to `github.repository_owner`
required: false
default: ${{ github.repository_owner }}
topics:
description: Comma-separated list of repository topics
required: false
operator:
description: Operator to use when filtering repositories by topics, `OR` or `AND`, default to `OR`
required: false
default: OR
matrix-use:
description: Output to use in the matrix job?, `true` or `false`
required: false
default: ${{ true }}
format:
description: Output format, `json` or `flat`, default to `json`
required: false
default: json
delimiter:
description: Delimiter to use when `format` is `flat`, default to `\n`
required: false
github-token:
description: GitHub Token with `repo` scope, default to `github.token`
required: false
default: ${{ github.token }}
outputs:
count:
description: Number of found repositories
value: ${{ steps.repos.outputs.count }}
repos:
description: Repositories
value: ${{ steps.repos.outputs.repos }}
format:
description: Output format
value: ${{ steps.repos.outputs.format }}
runs:
using: composite
steps:
- uses: actions/github-script@v7
id: repos
with:
github-token: ${{ inputs.github-token || env.GITHUB_TOKEN }}
script: |
// constraints
const choiceBool = ['true', 'false']
// inputs
const { INPUT_OWNER, INPUT_TOPICS, INPUT_OPERATOR, INPUT_MATRIX_USE, INPUT_OUTPUT_FORMAT, INPUT_FLAT_DELIMITER } = process.env
// owner
let inputOwner = ''
if (INPUT_OWNER) {
inputOwner = INPUT_OWNER.trim()
} else {
core.setFailed('owner is required (organization or user name)')
process.exit(core.ExitCode.Failure)
}
// topics
const inputTopics = INPUT_TOPICS ? INPUT_TOPICS.trim().split(',').map(element => {
element = element.trim()
if (element.includes(' ')) {
return element.split(' ')
} else {
return element
}
}).flat().filter(element => element !== '') : []
// operator
const inputOperator = INPUT_OPERATOR ? INPUT_OPERATOR.trim().toUpperCase() : 'OR'
const choiceInputOperator = ['AND', 'OR']
if (!choiceInputOperator.includes(inputOperator)) {
core.setFailed(`Invalid operator: ${inputOperator}, accepted values: ${choiceInputOperator.join(', ')}`)
process.exit(core.ExitCode.Failure)
}
// matrix use
const inputMatrixUse = INPUT_MATRIX_USE ? INPUT_MATRIX_USE.trim().toLowerCase() : 'true'
if (!choiceBool.includes(inputMatrixUse)) {
core.setFailed(`Invalid matrix-use: ${inputMatrixUse}, accepted values: ${choiceBool.join(', ')}`)
process.exit(core.ExitCode.Failure)
}
// format
const inputOutputFormat = INPUT_OUTPUT_FORMAT ? INPUT_OUTPUT_FORMAT.trim().toLowerCase() : 'json'
const choiceInputOutputFormat = ['json', 'flat']
if (!choiceInputOutputFormat.includes(inputOutputFormat)) {
core.setFailed(`Invalid format: ${inputOutputFormat}, accepted values: ${choiceInputOutputFormat.join(', ')}`)
process.exit(core.ExitCode.Failure)
}
// delimiter
const inputDelimiter = INPUT_FLAT_DELIMITER ? INPUT_FLAT_DELIMITER.trim() : `\n`
if (inputOutputFormat === 'flat' && inputDelimiter === '') {
core.setFailed('delimiter is required when format is `flat`')
process.exit(core.ExitCode.Failure)
}
core.info(`Owner: ${inputOwner}`)
core.info(`Topics: ${JSON.stringify(inputTopics)}`)
core.info(`Operator: ${inputOperator}`)
core.info(`Output Format: ${inputOutputFormat}`)
core.info(`Flat format delimiter: ${inputDelimiter}`)
// construct search query
let query = `user:${inputOwner}`
if (inputTopics.length) {
query += `+${inputTopics.map(element => element).join(`+${inputOperator}+`)}+in:topics`
}
core.info(`Search query: ${query}`)
// check total count
const checkTotalCount = await github.rest.search.repos({
q: query,
per_page: 1,
page: 1
})
const totalCount = checkTotalCount.data.total_count
core.info(`Found repo(s): ${totalCount}`)
// check matrix limit
if (inputOutputFormat === 'json' && inputMatrixUse === 'true' && totalCount > 256) {
core.setFailed('Found more than 256 repos. Please adjust the filter. 256 repos is a hard limit for matrix job! ref: https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs')
process.exit(core.ExitCode.Failure)
}
// check gh api limit
if (totalCount > 1000) {
core.setFailed('Found more than 1000 repos. Please adjust the filter. 1000 repos is a hard limit for GitHub API query return!')
process.exit(core.ExitCode.Failure)
}
// get repos
const getRepos = (await github.paginate(github.rest.search.repos, {
q: query,
per_page: 100,
order: 'desc'
}))
// construct output
let repos = []
if (totalCount) {
getRepos.map(element => {
const repo = {
owner: element.owner.login,
name: element.name,
full_name: element.full_name,
private: element.private,
html_url: element.html_url,
fork: element.fork,
archived: element.archived,
disabled: element.disabled,
is_template: element.is_template,
visibility: element.visibility,
default_branch: element.default_branch
}
repos.push(repo)
})
}
// format output
if (inputOutputFormat === 'flat') {
repos = repos.map(element => {
return element.full_name
})
repos = repos.join(inputDelimiter)
}
// set outputs
core.setOutput('count', totalCount)
core.setOutput('repos', repos)
core.setOutput('format', inputOutputFormat)
env:
INPUT_OWNER: ${{ inputs.owner }}
INPUT_TOPICS: ${{ inputs.topics }}
INPUT_OPERATOR: ${{ inputs.operator || 'OR' }}
INPUT_MATRIX_USE: ${{ inputs.matrix-use || true }}
INPUT_OUTPUT_FORMAT: ${{ inputs.format || 'json' }}
INPUT_FLAT_DELIMITER: ${{ inputs.delimiter }}