Skip to content

Commit c8479d0

Browse files
committed
feat: initial commit
0 parents  commit c8479d0

File tree

11 files changed

+273
-0
lines changed

11 files changed

+273
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
package-lock.json
3+
test/fixture/output.md

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Patch my Code of Conduct
2+
3+
This action adds a prefix to the default Code of Conduct template and applies the necessary templates.
4+
5+
## Example usage
6+
7+
```yaml
8+
uses: @anonrig/patch-my-code-of-conduct@v1
9+
with:
10+
base_path: './base.md'
11+
patch_path: './patch_file'
12+
output_path: '../../CODE_OF_CONDUCT.md'
13+
```

action.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: 'Code of Conduct'
2+
description: 'Generates a Code of Conduct with a prefix and apply necessary patches'
3+
inputs:
4+
base_template_path:
5+
description: 'Path of the base markdown file'
6+
required: true
7+
patch_file_path:
8+
description: 'Path of the patch to be applied to the output file'
9+
required: true
10+
output_template_path:
11+
description: 'Path of the generated markdown file'
12+
required: true
13+
runs:
14+
using: 'node18'
15+
main: 'lib/index.js'

base.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Code of Conduct
2+
3+
The OpenJS Foundation and its member projects use [Contributor Covenant v2.0](https://contributor-covenant.org/version/2/0/code_of_conduct) as their code of conduct. The full text is included [below](#contributor-covenant-code-of-conduct-v20) in English, and [translations](https://www.contributor-covenant.org/translations) are available on the Contributor Covenant website.
4+
5+
## Commitment
6+
7+
All recipients of reports commit to:
8+
9+
- maintain the confidentiality with regard to the reporter and victim of an incident, and
10+
- participate in the path for escalation as outlined in the section on Escalation when required.
11+
12+
## Report an issue in a project
13+
14+
1. To report an issue in one of the projects listed below, please use the method provided. The project itself is responsible for managing these reports.
15+
* **AMP Project:** <[email protected]>
16+
* **Appium:** email maintainers
17+
* **Electron:** <[email protected]>
18+
* **Express.js:** <[email protected]>
19+
* **Fastify:** <[email protected]> or <[email protected]>
20+
* **HospitalRun:** <[email protected]>
21+
* **LoopBack** <[email protected]>
22+
* **Node.js:** <[email protected]>
23+
* **Node-RED:** <[email protected]>
24+
* **Webdriver.io:** [contact TSC reps](https://github.com/webdriverio/webdriverio/blob/HEAD/AUTHORS.md)
25+
* **Webhint:** <[email protected]>
26+
2. For every other OpenJS Foundation project, please email <[email protected]>. The Cross Project Council (CPC) is responsible for managing these reports.
27+
28+
29+
## Report an issue in a space managed by the foundation
30+
31+
For reporting issues in spaces managed by the OpenJS Foundation, for example, repositories within the OpenJS organization or an live event such as a conferences, email <[email protected]>. The Cross Project Council (CPC) is responsible for managing these reports.
32+
33+
## Escalate an issue
34+
35+
The OpenJS Foundation maintains a [Code of Conduct Panel (CoCP)](https://github.com/openjs-foundation/cross-project-council/blob/HEAD/conduct/FOUNDATION_CODE_OF_CONDUCT_REQUIREMENTS.md#code-of-conduct-panel). This is a foundation-wide team established to manage escalation when a reporter believes that a report to a member project or the CPC has not been properly handled.
36+
37+
In order to escalate to the CoCP, email <[email protected]>.
38+
39+
## More Info
40+
41+
For more information, refer to the full
42+
[Code of Conduct governance document](https://github.com/openjs-foundation/cross-project-council/blob/HEAD/conduct/FOUNDATION_CODE_OF_CONDUCT_REQUIREMENTS.md).
43+
44+
---

lib/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import core from '@actions/core'
2+
import { apply_patch } from "./runner.js";
3+
4+
try {
5+
const base_path = core.getInput('base_path');
6+
const patch_path = core.getInput('patch_path');
7+
const output_path = core.getInput('output_path');
8+
9+
await apply_patch(base_path, patch_path, output_path);
10+
} catch (error) {
11+
core.setFailed(error);
12+
}
13+

lib/runner.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import fs from 'node:fs/promises';
2+
import f from 'node:fs'
3+
import { execSync } from 'node:child_process'
4+
5+
export async function apply_patch(base_path, patch_path, output_path) {
6+
if (!f.existsSync(base_path)) {
7+
throw new Error(`base_path (${base_path}) does not exist`)
8+
}
9+
const base_text = await fs.readFile(base_path, 'utf-8');
10+
11+
const template_url = 'https://www.contributor-covenant.org/version/2/1/code_of_conduct/code_of_conduct.md';
12+
const template_response = await fetch(template_url);
13+
14+
if (template_response.status !== 200 && template_response.status !== 201) {
15+
throw new Error(`template response got ${template_response.status} status code. expected 200 or 201.`)
16+
}
17+
const template_text = await template_response.text();
18+
19+
const output_text = `${base_text}${template_text}`;
20+
await fs.writeFile(output_path, output_text);
21+
22+
if (!f.existsSync(patch_path)) {
23+
throw new Error(`patch_path (${patch_path}) does not exist`)
24+
}
25+
26+
await execSync(`patch ${output_path} ${patch_path} -R`);
27+
}
28+

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "code-of-conduct",
3+
"version": "1.0.0",
4+
"description": "A tool for generating a code of conduct with patches",
5+
"main": "lib/index.js",
6+
"type": "module",
7+
"scripts": {
8+
"start": "node lib/index.js",
9+
"test": "vitest"
10+
},
11+
"keywords": [
12+
"Code of Conduct"
13+
],
14+
"author": "Yagiz Nizipli <[email protected]>",
15+
"license": "MIT",
16+
"devDependencies": {
17+
"@types/node": "^18.11.9",
18+
"vitest": "^0.25.1"
19+
},
20+
"dependencies": {
21+
"@actions/core": "^1.10.0",
22+
"@actions/github": "^5.1.1"
23+
}
24+
}

test/apply.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {assert, beforeEach, test} from 'vitest';
2+
import path from 'node:path'
3+
import {random_filename, write_file, remove_file, read_file, __dirname} from "./file-operations";
4+
import {apply_patch} from '../lib/runner.js'
5+
6+
beforeEach(async (context) => {
7+
context.base_path = random_filename()
8+
context.patch_path = random_filename()
9+
context.output_path = random_filename()
10+
11+
await write_file(context.base_path)
12+
await write_file(context.patch_path)
13+
await write_file(context.output_path)
14+
15+
return async () => {
16+
await remove_file(context.base_path)
17+
await remove_file(context.patch_path)
18+
await remove_file(context.output_path)
19+
}
20+
})
21+
22+
test('should add base as prefix', async (context) => {
23+
await write_file(context.base_path, 'this is a prefix')
24+
await apply_patch(context.base_path, context.patch_path, context.output_path)
25+
const output = await read_file(context.output_path)
26+
assert.isTrue(output.startsWith('this is a prefix\n'))
27+
})
28+
29+
test('should apply the patch', async (context) => {
30+
const base_path = path.join(__dirname, '../test/fixture/base.md');
31+
const patch_path = path.join(__dirname, '../test/fixture/patch.txt');
32+
const output_path = path.join(__dirname, '../test/fixture/output.md');
33+
34+
await remove_file(output_path);
35+
36+
const title = `CHANGED THE TITLE OF COC`
37+
const patch_text = `--- /dev/null 2022-11-11 18:00:48
38+
+++ output.md 2022-11-11 18:00:32
39+
@@ -43,7 +43,7 @@
40+
41+
---
42+
43+
-# ${title}
44+
+# Contributor Covenant Code of Conduct
45+
46+
## Our Pledge
47+
48+
`
49+
await write_file(patch_path, patch_text)
50+
51+
await apply_patch(base_path, patch_path, output_path)
52+
const output = await read_file(output_path)
53+
assert.isTrue(output.includes(title))
54+
})

test/file-operations.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import crypto from 'node:crypto'
2+
import fs from 'node:fs/promises'
3+
4+
import { dirname } from 'path'
5+
import { fileURLToPath } from 'url'
6+
7+
export const __filename = fileURLToPath(import.meta.url)
8+
export const __dirname = dirname(__filename)
9+
10+
export function random_filename() {
11+
return "/tmp/" + crypto.randomUUID() + ".txt";
12+
}
13+
14+
export function read_file(filename) {
15+
return fs.readFile(filename, 'utf-8');
16+
}
17+
18+
export function write_file(filename = random_filename(), data = '') {
19+
return fs.writeFile(filename, data, 'utf-8');
20+
}
21+
22+
export function remove_file(filename) {
23+
return fs.rm(filename, { force: true });
24+
}

test/fixture/base.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Code of Conduct
2+
3+
The OpenJS Foundation and its member projects use [Contributor Covenant v2.0](https://contributor-covenant.org/version/2/0/code_of_conduct) as their code of conduct. The full text is included [below](#contributor-covenant-code-of-conduct-v20) in English, and [translations](https://www.contributor-covenant.org/translations) are available on the Contributor Covenant website.
4+
5+
## Commitment
6+
7+
All recipients of reports commit to:
8+
9+
- maintain the confidentiality with regard to the reporter and victim of an incident, and
10+
- participate in the path for escalation as outlined in the section on Escalation when required.
11+
12+
## Report an issue in a project
13+
14+
1. To report an issue in one of the projects listed below, please use the method provided. The project itself is responsible for managing these reports.
15+
* **AMP Project:** <[email protected]>
16+
* **Appium:** email maintainers
17+
* **Electron:** <[email protected]>
18+
* **Express.js:** <[email protected]>
19+
* **Fastify:** <[email protected]> or <[email protected]>
20+
* **HospitalRun:** <[email protected]>
21+
* **LoopBack** <[email protected]>
22+
* **Node.js:** <[email protected]>
23+
* **Node-RED:** <[email protected]>
24+
* **Webdriver.io:** [contact TSC reps](https://github.com/webdriverio/webdriverio/blob/HEAD/AUTHORS.md)
25+
* **Webhint:** <[email protected]>
26+
2. For every other OpenJS Foundation project, please email <[email protected]>. The Cross Project Council (CPC) is responsible for managing these reports.
27+
28+
29+
## Report an issue in a space managed by the foundation
30+
31+
For reporting issues in spaces managed by the OpenJS Foundation, for example, repositories within the OpenJS organization or an live event such as a conferences, email <[email protected]>. The Cross Project Council (CPC) is responsible for managing these reports.
32+
33+
## Escalate an issue
34+
35+
The OpenJS Foundation maintains a [Code of Conduct Panel (CoCP)](https://github.com/openjs-foundation/cross-project-council/blob/HEAD/conduct/FOUNDATION_CODE_OF_CONDUCT_REQUIREMENTS.md#code-of-conduct-panel). This is a foundation-wide team established to manage escalation when a reporter believes that a report to a member project or the CPC has not been properly handled.
36+
37+
In order to escalate to the CoCP, email <[email protected]>.
38+
39+
## More Info
40+
41+
For more information, refer to the full
42+
[Code of Conduct governance document](https://github.com/openjs-foundation/cross-project-council/blob/HEAD/conduct/FOUNDATION_CODE_OF_CONDUCT_REQUIREMENTS.md).
43+
44+
---

0 commit comments

Comments
 (0)