way_of_working 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -1
  3. data/CLAUDE.md +48 -0
  4. data/CODE_OF_CONDUCT.md +55 -104
  5. data/CONTRIBUTING.md +62 -0
  6. data/README.md +240 -23
  7. data/exe/way_of_working +6 -1
  8. data/lib/way_of_working/audit/github/auditor.rb +47 -0
  9. data/lib/way_of_working/audit/github/generators/exec.rb +214 -0
  10. data/lib/way_of_working/audit/github/rules/base.rb +116 -0
  11. data/lib/way_of_working/audit/github/rules/registry.rb +44 -0
  12. data/lib/way_of_working/audit/github/rules/unknown.rb +19 -0
  13. data/lib/way_of_working/audit/github.rb +25 -0
  14. data/lib/way_of_working/changelog/keepachangelog/generators/init.rb +118 -0
  15. data/lib/way_of_working/changelog/keepachangelog/github_audit_rule.rb +32 -0
  16. data/lib/way_of_working/changelog/keepachangelog/templates/docs/way_of_working/changelog.md +73 -0
  17. data/lib/way_of_working/changelog/keepachangelog.rb +31 -0
  18. data/lib/way_of_working/cli.rb +13 -0
  19. data/lib/way_of_working/code_of_conduct/contributor_covenant/generators/init.rb +69 -0
  20. data/lib/way_of_working/code_of_conduct/contributor_covenant/github_audit_rule.rb +33 -0
  21. data/lib/way_of_working/code_of_conduct/contributor_covenant/templates/CODE_OF_CONDUCT.md.tt +83 -0
  22. data/lib/way_of_working/code_of_conduct/contributor_covenant/templates/docs/way_of_working/code-of-conduct.md +79 -0
  23. data/lib/way_of_working/code_of_conduct/contributor_covenant.rb +32 -0
  24. data/lib/way_of_working/completion.rb +60 -0
  25. data/lib/way_of_working/config.rb +45 -0
  26. data/lib/way_of_working/decision_record/madr/generators/init.rb +41 -0
  27. data/lib/way_of_working/decision_record/madr/generators/new.rb +69 -0
  28. data/lib/way_of_working/decision_record/madr/github_audit_rule.rb +37 -0
  29. data/lib/way_of_working/decision_record/madr/templates/.github/ISSUE_TEMPLATE/decision-record.md +26 -0
  30. data/lib/way_of_working/decision_record/madr/templates/docs/decisions/0000-use-markdown-any-decision-records.md +30 -0
  31. data/lib/way_of_working/decision_record/madr/templates/docs/decisions/README.md +7 -0
  32. data/lib/way_of_working/decision_record/madr/templates/docs/decisions/adr-template.md.tt +82 -0
  33. data/lib/way_of_working/decision_record/madr/templates/docs/decisions/index.md +48 -0
  34. data/lib/way_of_working/decision_record/madr/templates/docs/way_of_working/decision-records.md +195 -0
  35. data/lib/way_of_working/decision_record/madr.rb +49 -0
  36. data/lib/way_of_working/git/repo_reader.rb +2 -2
  37. data/lib/way_of_working/inclusive_language/alex/generators/exec.rb +51 -0
  38. data/lib/way_of_working/inclusive_language/alex/generators/init.rb +59 -0
  39. data/lib/way_of_working/inclusive_language/alex/github_audit_rule.rb +42 -0
  40. data/lib/way_of_working/inclusive_language/alex/templates/.alexignore +2 -0
  41. data/lib/way_of_working/inclusive_language/alex/templates/.alexrc +3 -0
  42. data/lib/way_of_working/inclusive_language/alex/templates/.github/workflows/inclusive-language.yml +16 -0
  43. data/lib/way_of_working/inclusive_language/alex/templates/docs/way_of_working/inclusive-language.md +28 -0
  44. data/lib/way_of_working/inclusive_language/alex.rb +46 -0
  45. data/lib/way_of_working/pull_request_template/hdi/generators/init.rb +26 -0
  46. data/lib/way_of_working/pull_request_template/hdi/github_audit_rule.rb +32 -0
  47. data/lib/way_of_working/pull_request_template/hdi/templates/.github/pull_request_template.md +51 -0
  48. data/lib/way_of_working/pull_request_template/hdi/templates/docs/way_of_working/pull-request-template-and-guidelines.md +50 -0
  49. data/lib/way_of_working/pull_request_template/hdi.rb +31 -0
  50. data/lib/way_of_working/readme_badge/generators/init.rb +11 -1
  51. data/lib/way_of_working/readme_badge/paths.rb +5 -2
  52. data/lib/way_of_working/readme_badge.rb +2 -6
  53. data/lib/way_of_working/sub_commands/base.rb +12 -0
  54. data/lib/way_of_working/sub_commands/config.rb +25 -0
  55. data/lib/way_of_working/sub_commands/init.rb +1 -1
  56. data/lib/way_of_working/version.rb +1 -1
  57. data/lib/way_of_working/versioning/semver/generators/init.rb +20 -0
  58. data/lib/way_of_working/versioning/semver/github_audit_rule.rb +38 -0
  59. data/lib/way_of_working/versioning/semver/templates/docs/way_of_working/versioning.md +71 -0
  60. data/lib/way_of_working/versioning/semver.rb +31 -0
  61. data/lib/way_of_working.rb +22 -0
  62. data/way_of_working.gemspec +5 -1
  63. metadata +108 -9
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Way of Working
2
2
 
3
3
  <!-- Way of Working: Main Badge Holder Start -->
4
- ![Way of Working Badge](https://img.shields.io/badge/Way_of_Working-v2.0.1-%238169e3?labelColor=black)
4
+ ![Way of Working Badge](https://img.shields.io/badge/Way_of_Working-v2.2.0-%238169e3?labelColor=black)
5
5
  <!-- Way of Working: Additional Badge Holder Start -->
6
6
  <!-- Way of Working: Badge Holder End -->
7
7
 
@@ -14,21 +14,28 @@
14
14
 
15
15
  This tool underpins the Way of Working framework, used to describe a consistent, high-quality approach to Software Engineering. It is intended as an opinionated approach supporting rapid development, across numerous projects, by a fungible workforce who can swap between assignments with the minimum friction.
16
16
 
17
- It consists command line tool that uses a modular, plugin-based system to gain functionality, allowing for organisations to pick and choose different solutions for the same feature.
17
+ It consists of a command line tool that uses a modular, plugin-based system to gain functionality, allowing for organisations to pick and choose different solutions for the same feature.
18
18
  Organisations can create their own gem which defines the specific plugin dependencies which form their way of working. See the [Way of Working for HDI](https://github.com/HealthDataInsight/way_of_working-for-hdi) as an example.
19
19
 
20
20
  There are subcommands that plugins hook into which enable them to add GitHub actions, configuration and other files to a project and GitHub pages outlining the tooling choice and thinking behind it.
21
21
 
22
22
  Below is a list of plugins that have been implemented so far:
23
23
 
24
- | Feature | Plugin | Description |
25
- | --------------------- | -------------------------------------- | ------------------------------------------------------------------------------ |
26
- | Changelog | [changelog-keepachangelog] | Implements [keepachangelog v1.1] |
27
- | Code Linting | [code_linting-hdi] | Implements a combination of [MegaLinter] and [RuboCop] built on NDRS standards |
28
- | Code of Conduct | [code_of_conduct-contributor_covenant] | Implements [Contributor Covenant v2.1] |
29
- | Decision Records | [decision_record-madr] | Implements [MADR v3] |
30
- | Inclusive Language | [inclusive_language-alex] | Implements [alex] |
31
- | Pull Request Template | [pull_request_template-hdi] | Implements a bespoke PR template |
24
+ | Feature | Plugin | Description |
25
+ |-----------------------|-------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------|
26
+ | Audit | Built-in (audit/github) | A framework for rules to check for incorrect content and configuration of GitHub repos — bundled, opt in by `require` (see [Built-in Features](#built-in-features)) |
27
+ | Changelog | Built-in (changelog/keepachangelog) | Implements [keepachangelog v1.1] — bundled, opt in by `require` (see [Built-in Features](#built-in-features)) |
28
+ | Code Linting | [code_linting-hdi] | Implements a combination of [MegaLinter] and [RuboCop] built on NDRS standards |
29
+ | Code of Conduct | Built-in (code_of_conduct/contributor_covenant) | Implements [Contributor Covenant v3] — bundled, opt in by `require` (see [Built-in Features](#built-in-features)) |
30
+ | Decision Records | Built-in (decision_record/madr) | Implements [MADR v3] — bundled, opt in by `require` (see [Built-in Features](#built-in-features)) |
31
+ | Inclusive Language | Built-in (inclusive_language/alex) | Implements [alex] — bundled, opt in by `require` (see [Built-in Features](#built-in-features)) |
32
+ | Pull Request Template | Built-in (pull_request_template/hdi) | Implements a bespoke PR template — bundled, opt in by `require` (see [Built-in Features](#built-in-features)) |
33
+ | README Badge | Built-in (readme_badge) | Adds a README badge panel and a Way of Working badge — bundled and always loaded, no `require` needed (see [README Badge](#readme-badge)) |
34
+ | Versioning | Built-in (versioning/semver) | Implements [Semantic Versioning v2.0.0] — bundled, opt in by `require` (see [Built-in Features](#built-in-features)) |
35
+
36
+ Some features are **Built-in** — they ship inside this gem and are enabled by requiring them (see [Built-in Features](#built-in-features)); the others are separate plugin gems that you add as dependencies.
37
+
38
+ The standard versions cited above (Contributor Covenant v3, MADR v3, and so on) describe the content a feature generates, not the gem's interface. They advance through **minor** releases — see [Versioning Policy](#versioning-policy).
32
39
 
33
40
  ## Installation
34
41
 
@@ -54,7 +61,195 @@ To add all the Way of Working components to your project, run:
54
61
  way_of_working init all --contact-method [CONTACT METHOD]
55
62
  ```
56
63
 
57
- You will need to provide the Code of Conduct `[CONTACT METHOD]`, usually an email address, for community leaders to receive reports of unacceptable behavior.
64
+ You will need to provide the Code of Conduct `[CONTACT METHOD]`, usually an email address, for community leaders to receive reports of unacceptable behavior. Once you've provided it, you'll be offered the chance to remember it (see [Config](#config)) so you don't need to pass it again next time.
65
+
66
+ ### Config
67
+
68
+ Some options, like the Code of Conduct `--contact-method`, are more about you or your organisation than about the specific project you're running the command in. Rather than typing them in every time, `way_of_working` can remember them in `~/.config/way_of_working/config.yaml` (or `$XDG_CONFIG_HOME/way_of_working/config.yaml`):
69
+
70
+ ```bash
71
+ # Store a value once
72
+ way_of_working config set code_of_conduct.contact_method conduct@example.org
73
+
74
+ # Read it back
75
+ way_of_working config get code_of_conduct.contact_method
76
+ ```
77
+
78
+ Keys are dotted paths namespaced by feature (`code_of_conduct.contact_method`), so unrelated features — a future security contact, say — can store their own values (`security.contact_method`) without clashing.
79
+ When a feature that supports this looks for an option and it isn't passed on the command line, it falls back to the stored value; if you pass the option explicitly and it's new or different, you'll be asked whether to remember it for next time.
80
+
81
+ ### README Badge
82
+
83
+ The README badge panel is a set of HTML comments at the top of your README that other generators find and add their badges to. It also shows a Way of Working badge with the version of the way of working your project has adopted. This feature is always loaded, so no `require` is needed:
84
+
85
+ ```bash
86
+ # Add the badge panel, Way of Working badge and documentation to your project
87
+ way_of_working init readme_badge
88
+ ```
89
+
90
+ Re-running it after upgrading updates the Way of Working badge to the new version. Features such as [Inclusive Language](#inclusive-language) add their own badges to the panel when you initialise them.
91
+
92
+ ### Built-in Features
93
+
94
+ Some features are bundled with this gem rather than shipped as separate plugins. These built-in features are **opt in**: enable one by requiring it wherever you load Way of Working — for example in your organisation's Way of Working gem, or in your project's `Rakefile`.
95
+
96
+ #### Audit
97
+
98
+ Auditing your GitHub repositories against a consistent set of rules catches missing or misconfigured files and repository settings across an organisation, keeping projects aligned with your way of working. Many of the other built-in features register their own rules to check that they have been adopted properly.
99
+
100
+ This feature provides a registry and auditing tool for GitHub repositories; rules can check for both missing/incorrect files and mis-configuration of the repository itself. Enable it by requiring it:
101
+
102
+ ```ruby
103
+ require 'way_of_working/audit/github'
104
+ ```
105
+
106
+ The audit needs two environment variables:
107
+
108
+ - `GITHUB_ORGANISATION`: the name of your organisation being scanned (as used in the GitHub URL)
109
+ - `GITHUB_TOKEN`: a PAT token with sufficient permission to access repositories and their configuration
110
+
111
+ Once required, a subcommand becomes available. By default it runs only against repositories configured as git remotes in your current project:
112
+
113
+ ```bash
114
+ # Audit this project's GitHub repositories
115
+ way_of_working exec audit_github
116
+ ```
117
+
118
+ Flags let you widen or narrow the scope:
119
+
120
+ ```bash
121
+ # Audit every repository in the organisation
122
+ way_of_working exec audit_github --all
123
+
124
+ # Filter to repositories carrying a topic
125
+ way_of_working exec audit_github --all --topic way-of-working
126
+
127
+ # Audit one or more repositories by name (implies the whole organisation)
128
+ way_of_working exec audit_github --name structured_store other_repo
129
+
130
+ # Audit only public repositories
131
+ way_of_working exec audit_github --all --public
132
+
133
+ # Attempt to automatically fix issues where rules support it
134
+ way_of_working exec audit_github --fix
135
+
136
+ # List the rule IDs and tags, then audit (and fix) only some of them
137
+ way_of_working exec audit_github --list-rules
138
+ way_of_working exec audit_github --rule versioning/semver changelog/keepachangelog --fix
139
+ way_of_working exec audit_github --tag cis_level1 --fix
140
+ ```
141
+
142
+ #### Changelog
143
+
144
+ Keeping a curated, human-readable changelog helps users and contributors quickly see what has changed between releases without trawling through the commit history.
145
+
146
+ This feature uses [Keep a Changelog v1.1][keepachangelog v1.1] to generate a `CHANGELOG.md`, seeding it with sections derived from your git tags and commit messages where possible. Enable it by requiring it:
147
+
148
+ ```ruby
149
+ require 'way_of_working/changelog/keepachangelog'
150
+ ```
151
+
152
+ Once required, a subcommand becomes available:
153
+
154
+ ```bash
155
+ # Add a Keep a Changelog CHANGELOG.md and documentation to your project
156
+ way_of_working init changelog
157
+ ```
158
+
159
+ #### Code of Conduct
160
+
161
+ A code of conduct sets clear expectations for acceptable behaviour within a community or project, helping to create a safer, more welcoming and inclusive environment and giving maintainers a basis for addressing inappropriate behaviour.
162
+
163
+ This feature installs the [Contributor Covenant v3] code of conduct along with its accompanying documentation. Enable it by requiring it:
164
+
165
+ ```ruby
166
+ require 'way_of_working/code_of_conduct/contributor_covenant'
167
+ ```
168
+
169
+ Previous Contributor Covenant versions (e.g. v2.1) are no longer generated; re-running the generator replaces them with v3.
170
+
171
+ Once required, a subcommand becomes available:
172
+
173
+ ```bash
174
+ # Add the Contributor Covenant code of conduct and documentation to your project
175
+ way_of_working init code_of_conduct --contact-method [CONTACT METHOD]
176
+ ```
177
+
178
+ You will need to provide a `[CONTACT METHOD]`, usually an email address, for community leaders to receive reports of unacceptable behavior — or store one under `code_of_conduct.contact_method` (see [Config](#config)) so you don't need to pass it every time.
179
+
180
+ #### Decision Records
181
+
182
+ Recording the context and reasoning behind significant decisions helps current and future contributors understand why a project is the way it is, avoids relitigating settled questions, and onboards newcomers faster.
183
+
184
+ This feature uses [Markdown Any Decision Records (MADR)][MADR v3] to capture decisions as version-controlled Markdown alongside your code. Enable it by requiring it:
185
+
186
+ ```ruby
187
+ require 'way_of_working/decision_record/madr'
188
+ ```
189
+
190
+ Once required, two subcommands become available:
191
+
192
+ ```bash
193
+ # Add the decision records index, template and first record to your project
194
+ way_of_working init decision_record
195
+
196
+ # Create a new decision record
197
+ way_of_working new decision_record "Title of the decision"
198
+ ```
199
+
200
+ #### Inclusive Language
201
+
202
+ Using insensitive and inconsiderate language can cause harm to others, create barriers to communication, and damage relationships. It can make people feel excluded, disrespected, and devalued, and may perpetuate negative stereotypes and biases.
203
+
204
+ This feature uses [alex] for automated testing of inclusive language, both at the command line and as a GitHub workflow. Enable it by requiring it:
205
+
206
+ ```ruby
207
+ require 'way_of_working/inclusive_language/alex'
208
+ ```
209
+
210
+ Once required, two subcommands become available:
211
+
212
+ ```bash
213
+ # Add the alex config, .alexignore, GitHub Action and documentation to your project
214
+ way_of_working init inclusive_language
215
+
216
+ # Run the inclusive language checks
217
+ way_of_working exec inclusive_language
218
+ ```
219
+
220
+ #### Pull Request Template
221
+
222
+ A consistent pull request template standardises PR submissions, giving reviewers the context they need, improving review efficiency, and keeping a searchable project history.
223
+
224
+ This feature installs a bespoke HDI pull request template along with its accompanying guidelines documentation. Enable it by requiring it:
225
+
226
+ ```ruby
227
+ require 'way_of_working/pull_request_template/hdi'
228
+ ```
229
+
230
+ Once required, a subcommand becomes available:
231
+
232
+ ```bash
233
+ # Add the pull request template and documentation to your project
234
+ way_of_working init pull_request_template
235
+ ```
236
+
237
+ #### Versioning
238
+
239
+ A shared versioning standard makes software changes clear to communicate and releases predictable to manage across projects, so consumers can reason about compatibility at a glance.
240
+
241
+ This feature documents [Semantic Versioning v2.0.0] as the project's versioning standard. Enable it by requiring it:
242
+
243
+ ```ruby
244
+ require 'way_of_working/versioning/semver'
245
+ ```
246
+
247
+ Once required, a subcommand becomes available:
248
+
249
+ ```bash
250
+ # Add the Semantic Versioning documentation to your project
251
+ way_of_working init versioning
252
+ ```
58
253
 
59
254
  ### Help
60
255
 
@@ -64,36 +259,58 @@ More help on using the command line tool is found by using:
64
259
  way_of_working help
65
260
  ```
66
261
 
262
+ ### Shell Completion
263
+
264
+ Tab completion of commands and options is available for bash and zsh. Add the matching line to your shell's startup file:
265
+
266
+ ```bash
267
+ # ~/.bashrc
268
+ eval "$(way_of_working completion bash)"
269
+
270
+ # ~/.zshrc (after compinit)
271
+ eval "$(way_of_working completion zsh)"
272
+ ```
273
+
274
+ Completions come from the installed commands each time you press Tab, so commands added by plugins appear without regenerating anything.
275
+
67
276
  ## Development
68
277
 
69
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
278
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
279
  This RubyGem was created using `bundle gem --exe way_of_working`.
71
280
 
72
281
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
73
282
 
74
- When creating plugins, the convention is to create them within the WayOfWorking and feature namespaces. E.g.
283
+ When creating plugins, the convention is to create them within the WayOfWorking and feature namespaces. E.g. the [code_linting-hdi] plugin is the gem `way_of_working-code_linting-hdi`, defines its generators under `WayOfWorking::CodeLinting::Hdi`, and is loaded automatically from `lib/way_of_working/code_linting/hdi/plugin.rb` (the gem name with `-` replaced by `/`).
284
+
285
+ ### Versioning Policy
286
+
287
+ This project follows [Semantic Versioning][Semantic Versioning v2.0.0], but SemVer governs the gem's **public interface** — the CLI commands and their options (including GitHub audit rule IDs), the require paths of built-in features, and the plugin registration API (including `Rules::Base.rule_id`) — not the content of the artefacts its generators emit.
288
+
289
+ Consequently:
290
+
291
+ - Refreshing the standard a feature embeds (e.g. a new Contributor Covenant or MADR version) is **new behaviour → MINOR**.
292
+ - A bug fix in generation is **PATCH**.
293
+ - **MAJOR** is reserved for breaking the interface above (renaming or removing a command or option, moving a require path, or changing the plugin API).
294
+
295
+ For a standard update drastic enough to surprise existing users, we add a **new variant** under the feature's `category/variant` namespace rather than mutating the existing one, so both coexist and consumers opt in by require path. This keeps the change additive (MINOR) and leaves existing users untouched.
296
+
297
+ See [ADR-0001](docs/decisions/0001-version-the-gem-interface-not-generated-content.md) for the full rationale and [`CONTRIBUTING.md`](CONTRIBUTING.md) for the version-bump checklist.
75
298
 
76
299
  ## Contributing
77
300
 
78
301
  Bug reports and pull requests are welcome on GitHub at <https://github.com/HealthDataInsight/way_of_working>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/HealthDataInsight/way_of_working/blob/main/CODE_OF_CONDUCT.md).
79
302
 
303
+ See [`CONTRIBUTING.md`](CONTRIBUTING.md) for development setup, the versioning policy and the release process.
304
+
80
305
  ## License
81
306
 
82
307
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
83
308
 
84
- ## Code of Conduct
85
-
86
- Everyone interacting in the WayOfWorking project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/HealthDataInsight/way_of_working/blob/main/CODE_OF_CONDUCT.md).
87
-
88
309
  [alex]:https://alexjs.com
89
- [Contributor Covenant v2.1]: https://www.contributor-covenant.org
310
+ [Contributor Covenant v3]: https://www.contributor-covenant.org/version/3/0/code_of_conduct/
90
311
  [keepachangelog v1.1]: https://keepachangelog.com/en/1.1.0/
91
312
  [MADR v3]: https://adr.github.io/madr/
92
313
  [MegaLinter]: https://megalinter.io/
93
314
  [RuboCop]: https://rubocop.org
94
- [changelog-keepachangelog]: https://github.com/HealthDataInsight/way_of_working-changelog-keepachangelog
315
+ [Semantic Versioning v2.0.0]: https://semver.org/spec/v2.0.0.html
95
316
  [code_linting-hdi]: https://github.com/HealthDataInsight/way_of_working-code_linting-hdi
96
- [code_of_conduct-contributor_covenant]: https://github.com/HealthDataInsight/way_of_working-code_of_conduct-contributor_covenant
97
- [decision_record-madr]: https://github.com/HealthDataInsight/way_of_working-decision_record-madr
98
- [inclusive_language-alex]: https://github.com/HealthDataInsight/way_of_working-inclusive_language-alex
99
- [pull_request_template-hdi]: https://github.com/HealthDataInsight/way_of_working-pull_request_template-hdi
data/exe/way_of_working CHANGED
@@ -2,4 +2,9 @@
2
2
 
3
3
  require 'way_of_working'
4
4
 
5
- WayOfWorking::Cli.start(ARGV)
5
+ # Called by the shell completion scripts on every Tab (see WayOfWorking::Completion)
6
+ if ARGV.first == '__complete'
7
+ puts WayOfWorking::Completion.candidates(ARGV.drop(1))
8
+ else
9
+ WayOfWorking::Cli.start(ARGV)
10
+ end
@@ -0,0 +1,47 @@
1
+ require 'active_support'
2
+ # Rules (including plugin rules) may use any core extension while they run
3
+ require 'active_support/core_ext'
4
+ require 'octokit'
5
+ require_relative 'rules/registry'
6
+
7
+ module WayOfWorking
8
+ module Audit
9
+ module Github
10
+ # This auditor runs all the rules against any given GitHub repository
11
+ class Auditor
12
+ def initialize(access_token, organisation_name, fix = false)
13
+ @access_token = access_token
14
+ @organisation_name = organisation_name
15
+ @fix = fix
16
+ end
17
+
18
+ def audit(repository)
19
+ # Get all the rules once, rather than repeatedly in individual rules
20
+ rulesets = @client.get("repos/#{repository.full_name}/rulesets").map do |rule|
21
+ @client.get("repos/#{repository.full_name}/rulesets/#{rule[:id]}")
22
+ end
23
+
24
+ Array(Rules::Registry.rules).each do |rule_name, klass|
25
+ rule = klass.new(client, rule_name, repository, rulesets, @fix)
26
+
27
+ yield rule
28
+ end
29
+ end
30
+
31
+ def repositories
32
+ @repositories ||= client.org_repos(@organisation_name)
33
+ end
34
+
35
+ private
36
+
37
+ def client
38
+ @client ||= begin
39
+ client = Octokit::Client.new(access_token: @access_token) # , per_page: 1_000
40
+ client.auto_paginate = true
41
+ client
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,214 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'git'
4
+ require 'rainbow'
5
+ require 'thor'
6
+ require 'way_of_working/audit/github/auditor'
7
+ # require 'way_of_working/github_audit/rules/all'
8
+
9
+ module WayOfWorking
10
+ module Audit
11
+ module Github
12
+ module Generators
13
+ # This generator runs the github audit
14
+ class Exec < Thor::Group
15
+ class_option :all, type: :boolean, default: false,
16
+ desc: 'Audit all repositories in the organisation (not just this repo)'
17
+
18
+ class_option :fix, type: :boolean, default: false,
19
+ desc: 'Attempt to automatically fix issues where possible'
20
+
21
+ class_option :name, type: :array, default: nil,
22
+ desc: 'Filter repositories by name (e.g., structured_store)'
23
+
24
+ class_option :public, type: :boolean, default: false,
25
+ desc: 'Filter to only public repositories'
26
+
27
+ class_option :topic, type: :string, default: nil,
28
+ desc: 'Filter repositories by topic (e.g., way-of-working)'
29
+
30
+ class_option :rule, type: :array, default: nil,
31
+ desc: 'Only run (and --fix) these rules (e.g., versioning/semver)'
32
+
33
+ class_option :tag, type: :array, default: nil,
34
+ desc: 'Only run (and --fix) rules with any of these tags (e.g., cis_level1)'
35
+
36
+ class_option :list_rules, type: :boolean, default: false,
37
+ desc: 'List the rule IDs and tags that --rule and --tag accept, and exit'
38
+
39
+ desc 'This runs the github audit on this project'
40
+
41
+ def list_rules_if_requested
42
+ return unless options[:list_rules]
43
+
44
+ width = rule_catalogue.keys.map(&:length).max.to_i
45
+ rule_catalogue.each { |id, (name, tags)| say "#{id.ljust(width)} #{name} #{tags.inspect}" }
46
+ exit
47
+ end
48
+
49
+ def check_rule_option
50
+ unknown = Array(options[:rule]) - rule_catalogue.keys
51
+ return if unknown.empty?
52
+
53
+ abort(Rainbow("\nUnknown rule: #{unknown.join(', ')}. Valid rules: #{rule_catalogue.keys.join(', ')}").red)
54
+ end
55
+
56
+ def check_tag_option
57
+ known = rule_catalogue.values.flat_map(&:last).uniq.map(&:to_s)
58
+ unknown = Array(options[:tag]) - known
59
+ return if unknown.empty?
60
+
61
+ abort(Rainbow("\nUnknown tag: #{unknown.join(', ')}. Valid tags: #{known.join(', ')}").red)
62
+ end
63
+
64
+ def check_for_github_token_environment_variables
65
+ @github_token = ENV.fetch('GITHUB_TOKEN', nil)
66
+ return unless @github_token.nil?
67
+
68
+ abort(Rainbow("\nMissing GITHUB_TOKEN environment variable").red)
69
+ end
70
+
71
+ def check_for_github_organisation_environment_variables
72
+ @github_organisation = ENV.fetch('GITHUB_ORGANISATION', nil)
73
+ return unless @github_organisation.nil?
74
+
75
+ abort(Rainbow("\nMissing GITHUB_ORGANISATION environment variable").red)
76
+ end
77
+
78
+ def start_timer
79
+ @start_time = Time.now
80
+ end
81
+
82
+ def check_github_organisation_remotes
83
+ abort(Rainbow("\nGitHub is not an upstream repository.").red) if github_organisation_remotes.empty?
84
+
85
+ # say("Limiting audit to #{path}\n", :yellow) if path
86
+ say "\nRunning..."
87
+ end
88
+
89
+ def prep_audit
90
+ @auditor = ::WayOfWorking::Audit::Github::Auditor.new(@github_token, @github_organisation, options[:fix])
91
+
92
+ # Loop though all the repos
93
+ @repositories = @auditor.repositories
94
+ rescue Octokit::Unauthorized
95
+ abort(Rainbow("\nGITHUB_TOKEN has expired or does not have sufficient permission").red)
96
+ end
97
+
98
+ def filter_all_if_specified
99
+ return if options[:all] || options[:name]
100
+
101
+ @repositories = @repositories.select do |repo|
102
+ github_organisation_remotes.include?(repo.name)
103
+ end
104
+ end
105
+
106
+ def filter_by_name_array_if_specified
107
+ return unless options[:name]
108
+
109
+ @repositories = @repositories.select do |repo|
110
+ options[:name].include?(repo.name)
111
+ end
112
+ end
113
+
114
+ def filter_by_topic_if_specified
115
+ return unless options[:topic]
116
+
117
+ @repositories = @repositories.select do |repo|
118
+ repo.topics.include?(options[:topic])
119
+ end
120
+ end
121
+
122
+ def filter_by_visibility_if_specified
123
+ return unless options[:public]
124
+
125
+ @repositories = @repositories.reject(&:private?)
126
+ end
127
+
128
+ def run_audit
129
+ @audit_ok = true
130
+ unarchived_repos.each do |repo|
131
+ @auditor.audit(repo) do |rule|
132
+ id = Rules::Registry.id_for(rule.class, rule.name)
133
+ report(rule, id) if rule_selected?(rule, id)
134
+ end
135
+ end
136
+ end
137
+
138
+ def run_last
139
+ say("\nTotal time taken: #{(Time.now - @start_time).to_i} seconds", :yellow)
140
+
141
+ if @audit_ok
142
+ say("\nGitHub audit succeeded!", :green)
143
+ else
144
+ abort(Rainbow("\nGitHub audit failed!").red)
145
+ end
146
+ end
147
+
148
+ private
149
+
150
+ # Maps each registered rule's ID to its display name and tags.
151
+ # ponytail: allocate skips initialize (which needs a repo), so tags must not depend on instance state
152
+ def rule_catalogue
153
+ @rule_catalogue ||= Array(Rules::Registry.rules).to_h do |name, klass|
154
+ [Rules::Registry.id_for(klass, name), [name, klass.allocate.tags]]
155
+ end
156
+ end
157
+
158
+ # ponytail: rules are still built for every repo, only status (the API calls) is skipped
159
+ def rule_selected?(rule, id)
160
+ (options[:rule].nil? || options[:rule].include?(id)) &&
161
+ (options[:tag].nil? || rule.tags.map(&:to_s).intersect?(options[:tag]))
162
+ end
163
+
164
+ def report(rule, id)
165
+ case rule.status
166
+ when :not_applicable
167
+ # Do nothing
168
+ when :passed
169
+ say " ✅ #{rule.tags.inspect} Passed #{rule.name} (#{id})"
170
+ when :failed
171
+ say " ❌ #{rule.tags.inspect} Failed #{rule.name} (#{id}): #{rule.errors.to_sentence}"
172
+ @audit_ok = false
173
+ else
174
+ abort(Rainbow("Unknown response #{rule.status.inspect}").red)
175
+ end
176
+ end
177
+
178
+ def unarchived_repos(&block)
179
+ return to_enum(__method__) unless block_given?
180
+
181
+ @repositories.each do |repo|
182
+ next if repo.archived?
183
+
184
+ say
185
+ say("#{repo.name} #{'🔒' if repo.private?}", :bold)
186
+ say("#{repo.description} [#{repo.language}]")
187
+ say(repo.topics.join(' '), :cyan) unless repo.topics.empty?
188
+
189
+ block.call(repo)
190
+ end
191
+ end
192
+
193
+ # This method returns the repo names for all upstream repositories
194
+ # hosted on GitHub, for the given organisation
195
+ def github_organisation_remotes
196
+ @github_organisation_remotes ||= begin
197
+ all_remote_urls = ::Git.open('.').remotes.map(&:url)
198
+
199
+ organisation_remote_urls = all_remote_urls.select do |url|
200
+ url.start_with?("https://github.com/#{@github_organisation}")
201
+ end
202
+
203
+ organisation_remote_urls.map do |url|
204
+ matchdata = url.match(%r{\Ahttps://github.com/([^/]+)/([^/]+)\.git})
205
+
206
+ matchdata[2]
207
+ end
208
+ end
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end