Skip to content

Commit 4d524b7

Browse files
authored
feat: Initial version (#1)
1 parent 013cde5 commit 4d524b7

19 files changed

+39997
-7
lines changed

Diff for: .github/workflows/release.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Release
2+
on:
3+
push:
4+
branches:
5+
- main
6+
7+
jobs:
8+
release:
9+
name: release
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
- uses: actions/setup-node@v1
14+
with:
15+
node-version: "12.x"
16+
- uses: actions/cache@v1
17+
with:
18+
path: ~/.npm
19+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
20+
restore-keys: |
21+
${{ runner.os }}-node-
22+
- run: npm ci
23+
- run: npm run build
24+
- run: npx semantic-release
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
NPM_TOKEN: ${{ secrets.PROBOTBOT_NPM_TOKEN }}

Diff for: .github/workflows/test.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Test
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
types: [opened, synchronize]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
node_version: ["10", "12", "14"]
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Use Node.js ${{ matrix.node_version }}
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: ${{ matrix.node_version }}
22+
- uses: actions/cache@v1
23+
with:
24+
path: ~/.npm
25+
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
26+
restore-keys: |
27+
${{ runner.os }}-node-
28+
- name: Install
29+
run: npm ci
30+
- name: Test
31+
run: npm test

Diff for: .github/workflows/update-prettier.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Update Prettier
2+
on:
3+
push:
4+
branches:
5+
- "dependabot/npm_and_yarn/prettier-*"
6+
jobs:
7+
update_prettier:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v2
11+
- uses: actions/setup-node@v1
12+
with:
13+
version: 12
14+
- run: npm ci
15+
- run: npm run lint:fix
16+
- uses: gr2m/[email protected]
17+
env:
18+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
with:
20+
title: "Prettier updated"
21+
body: "An update to prettier required updates to your code."
22+
branch: ${{ github.ref }}
23+
commit-message: "style: prettier"

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
coverage/
2+
node_modules/
3+
pkg/

Diff for: LICENSE

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
MIT License Copyright (c) 2020 Probot Contributors
1+
ISC License
22

3-
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
3+
Copyright (c) 2020 Probot Contributors
44

5-
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
5+
Permission to use, copy, modify, and/or distribute this software for any
6+
purpose with or without fee is hereby granted, provided that the above
7+
copyright notice and this permission notice appear in all copies.
68

7-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Diff for: README.md

+258-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,268 @@
1-
# 🚧 WORK IN PROGRESS. See [#1](https://github.com/probot/octokit-plugin-config/pull/1)
2-
31
# octokit-plugin-config
42

53
> Get/set persisted configuration using YAML/JSON files in repositories
64
5+
[![@latest](https://img.shields.io/npm/v/@probot/octokit-plugin-config.svg)](https://www.npmjs.com/package/@probot/octokit-plugin-config)
6+
[![Build Status](https://github.com/probot/octokit-plugin-config/workflows/Test/badge.svg)](https://github.com/probot/octokit-plugin-config/actions?query=workflow%3ATest+branch%3Amain)
7+
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=probot/octokit-plugin-config)](https://dependabot.com/)
8+
9+
By default, this plugin loads configuration from a given repository file. If the file doesn't exist, it loads configuration from the same path in the same owner's `.github` repository.
10+
11+
Configuration can be extended across multiple files using [the `_extends` key](#extends).
12+
13+
## Usage
14+
15+
<table>
16+
<tbody valign=top align=left>
17+
<tr><th>
18+
19+
Browsers
20+
21+
</th><td width=100%>
22+
23+
Load `@probot/octokit-plugin-config` and [`@octokit/core`](https://github.com/octokit/core.js) (or core-compatible module) directly from [cdn.pika.dev](https://cdn.pika.dev)
24+
25+
```html
26+
<script type="module">
27+
import { Octokit } from "https://cdn.pika.dev/@octokit/core";
28+
import {
29+
config,
30+
composeConfigGet,
31+
} from "https://cdn.pika.dev/@probot/octokit-plugin-config";
32+
</script>
33+
```
34+
35+
</td></tr>
36+
<tr><th>
37+
38+
Node
39+
40+
</th><td>
41+
42+
Install with `npm install @octokit/core @probot/octokit-plugin-config`. Optionally replace `@octokit/core` with a compatible module
43+
44+
```js
45+
const { Octokit } = require("@octokit/core");
46+
const { config, composeConfigGet } = require("@probot/octokit-plugin-config");
47+
```
48+
49+
</td></tr>
50+
</tbody>
51+
</table>
52+
53+
```js
54+
// given that `.github/my-app.yml` in `octocat/hello-world` has the following ocntent
55+
//
56+
// comment: 'Thank you for creating the issue!'
57+
//
58+
const { config } = await octokit.config.get({
59+
owner: "octocat",
60+
repo: "hello-world",
61+
path: ".github/my-app.yml",
62+
});
63+
// config is now { comment: "Thank you for creating the issue!" }
64+
65+
// all options and returns
66+
const { config, files } = await octokit.config.get({
67+
owner: "octocat",
68+
repo: "hello-world",
69+
path: ".github/my-app.yml",
70+
defaults: {
71+
comment: "Thank you for creating the issue!",
72+
},
73+
branch: "develop",
74+
});
75+
// files is an array of { owner, repo, path, config } objects
76+
```
77+
78+
## Options
79+
80+
<table>
81+
<thead align=left>
82+
<tr>
83+
<th>
84+
option
85+
</th>
86+
<th>
87+
type
88+
</th>
89+
<th width=100%>
90+
description
91+
</th>
92+
</tr>
93+
</thead>
94+
<tbody align=left valign=top>
95+
<tr>
96+
<th><code>owner</code></th>
97+
<td>String</td>
98+
<td>
99+
<strong>Required.</strong> Repository owner login.
100+
</td>
101+
</tr>
102+
<tr>
103+
<th><code>repo</code></th>
104+
<td>String</td>
105+
<td>
106+
<strong>Required.</strong> Repository name.
107+
</td>
108+
</tr>
109+
<tr>
110+
<th><code>path</code></th>
111+
<td>String</td>
112+
<td>
113+
<strong>Required.</strong> Path of the configuration file. Supported file extensions are <code>.yml</code>, <code>.yaml</code>, and <code>.json</code>.
114+
</td>
115+
</tr>
116+
<tr>
117+
<th><code>defaults</code></th>
118+
<td>String</td>
119+
<td>
120+
Default options that are returned if the configuration file does not exist, or merged with the contents if it does exist. Defaults are merged using shallowly using <code>Object.assign</code>. For custom merge strategies, you can set <code>defaults</code> to a function, see <a href="#custom-configuration-merging">Merging configuration</a> below for more information. Defaults to <code>{}</code>.
121+
</td>
122+
</tr>
123+
<tr>
124+
<th><code>branch</code></th>
125+
<td>String</td>
126+
<td>
127+
Defaults to the repository's default branch. The branch is only used for the provided repository, not for the <code>.github<code> repository or other configurations linked using <a href="extends">the <code>_extends</code> key</a>.
128+
</td>
129+
</tr>
130+
</tbody>
131+
</table>
132+
133+
<a name="extends"></a>
134+
135+
### The `_extends` key
136+
137+
`octokit.config.get()` supports sharing configs between repositories. If configuration for your app is not available in the target repository, it will be loaded from the `.github` directory of the same owner's `.github` repository.
138+
139+
You can choose own shared location. Use the `_extends` option in the configuration file to extend settings from another repository.
140+
141+
For example, given `.github/test.yml`:
142+
143+
```yml
144+
_extends: github-settings
145+
# Override values from the extended config or define new values
146+
name: myrepo
147+
```
148+
149+
This configuration will be merged with the `.github/test.yml` file from the `github-settings` repository, which might look like this:
150+
151+
```yml
152+
shared1: will be merged
153+
shared2: will also be merged
154+
```
155+
156+
Just put common configuration keys in a repository within your organization. Then reference this repository from config files with the same name.
157+
158+
You can also reference configurations from other owners:
159+
160+
```yml
161+
_extends: other/probot-settings
162+
other: DDD
163+
```
164+
165+
Additionally, you can specify a specific path for the configuration by appending a colon after the project.
166+
167+
```yml
168+
_extends: probot-settings:.github/other_test.yml
169+
other: FFF
170+
```
171+
172+
<a name="custom-configuration-merging"></a>
173+
174+
### Merging configuration
175+
176+
Given `.github/test.yml`:
177+
178+
```yml
179+
settings:
180+
one: value from configuration
181+
```
182+
183+
And
184+
185+
```js
186+
const { config } = await octokit.config.get({
187+
owner,
188+
repo,
189+
path: ".github/test.yml",
190+
defaults: {
191+
settings: {
192+
one: "default value",
193+
two: "default value",
194+
},
195+
},
196+
});
197+
```
198+
199+
The resulting `config` object is
200+
201+
```js
202+
{
203+
settings: {
204+
one: "value from configuration";
205+
}
206+
}
207+
```
208+
209+
And not as you might expect
210+
211+
```js
212+
{
213+
settings: {
214+
one: "value from configuration";
215+
two: "default value";
216+
}
217+
}
218+
```
219+
220+
The reason for that behavior is that merging objects deeply is not supported in JavaScript by default, and there are different strategies and many pitfals. There are many libraries that support deep merging in different ways, but instead making that decision for and significantly increasing the bundle size of this plugin, we let you pass a custom merge strategy instead.
221+
222+
In order to achive the deeply merged configuration, the `defaults` option can be set to a function. The function receives one `configs` argument, which is an array of configurations loaded from files in reverse order, so that the latter items should take precedence over the former items. The `configs` array can have more than one object if [the `_extends` key](#extends) is used.
223+
224+
```js
225+
const defaults = {
226+
settings: {
227+
one: "default value",
228+
two: "default value",
229+
},
230+
};
231+
const { config } = await octokit.config.get({
232+
owner,
233+
repo,
234+
path: ".github/test.yml",
235+
defaults(configs) {
236+
const allConfigs = [defaults, ...configs];
237+
const fileSettingsConfigs = allConfigs.map(
238+
(config: Configuration) => config.settings
239+
);
240+
return Object.assign({}, ...allConfigs, {
241+
settings: Object.assign({}, ...fileSettingsConfigs),
242+
});
243+
},
244+
});
245+
```
246+
247+
Or simpler, using a library such as [deepmerge](https://github.com/TehShrike/deepmerge)
248+
249+
```js
250+
const { config } = await octokit.config.get({
251+
owner,
252+
repo,
253+
path: ".github/test.yml",
254+
defaults: (configs) => deepmerge([defaults, ...configs]),
255+
});
256+
```
257+
7258
## Contributing
8259

9260
See [CONTRIBUTING.md](CONTRIBUTING.md)
10261

262+
## Credits
263+
264+
The idea for this plugin and some of its code was extracted from [Probot](https://probot.github.io/). It originated as [probot-config](https://github.com/probot/probot-config), created by [Jan Michael Auer](https://github.com/jan-auer) and was later merged into [`probot`](https://github.com/probot/probot).
265+
11266
## License
12267

13-
[MIT](LICENSE)
268+
[ISC](LICENSE)

0 commit comments

Comments
 (0)