Skip to content

Commit dc80a1d

Browse files
committed
feat: add template_url and get rid of base file
1 parent bd10a83 commit dc80a1d

File tree

8 files changed

+29
-97
lines changed

8 files changed

+29
-97
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Start Patch
2626
uses: pkgjs/[email protected]
2727
with:
28-
base_file_path: './base.md'
28+
base_url: 'https://raw.githubusercontent.com/openjs-foundation/cross-project-council/240d047d4f2cb135e6c1ce64b036af7aaf639a01/meetings/2023/2023-01-03.md'
2929
patch_file_path: './patch'
3030
output_file_path: './CODE_OF_CONDUCT.md'
3131
- uses: gr2m/create-or-update-pull-request-action@v1

Diff for: action.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
name: 'Code of Conduct'
22
description: 'Generates a Code of Conduct with a prefix and apply necessary patches'
33
inputs:
4-
base_file_path:
5-
description: 'Path of the base markdown file'
4+
base_url:
5+
description: 'The url to be used for the base'
66
required: true
7+
template_url:
8+
description: URL to be used for the template of the CoC
9+
default: https://www.contributor-covenant.org/version/2/1/code_of_conduct/code_of_conduct.md
10+
required: false
711
patch_file_path:
812
description: 'Path of the patch to be applied to the output file'
913
required: true

Diff for: lib/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const core = require('@actions/core')
22
const { apply_patch } = require('./runner.js')
33

4-
const base_path = core.getInput('base_file_path');
4+
const base_url = core.getInput('base_url');
5+
const template_url = core.getInput('template_url');
56
const patch_path = core.getInput('patch_file_path');
67
const output_path = core.getInput('output_file_path');
78

8-
apply_patch(base_path, patch_path, output_path)
9+
apply_patch(base_url, template_url, patch_path, output_path)
910
.then(() => core.setOutput('state', 'success'))
1011
.catch(error => core.setFailed(error))
1112

Diff for: lib/runner.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ const fs = require('node:fs')
22
const { execSync } = require('node:child_process')
33
const { fetch } = require('undici')
44

5-
async function apply_patch(base_path, patch_path, output_path) {
6-
if (!fs.existsSync(base_path)) {
7-
throw new Error(`base_path (${base_path}) does not exist`)
5+
async function apply_patch(base_url, template_url, patch_path, output_path) {
6+
const base_response = await fetch(base_url);
7+
8+
if (base_response.status !== 200 && base_response.status !== 201) {
9+
throw new Error(`base url response got ${base_url.status} status code. expected 200 or 201.`)
810
}
9-
const base_text = fs.readFileSync(base_path, 'utf-8');
11+
const base_text = await base_response.text();
1012

11-
const template_url = 'https://www.contributor-covenant.org/version/2/1/code_of_conduct/code_of_conduct.md';
1213
const template_response = await fetch(template_url);
1314

1415
if (template_response.status !== 200 && template_response.status !== 201) {

Diff for: package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"author": "Yagiz Nizipli <[email protected]>",
1515
"license": "MIT",
1616
"devDependencies": {
17-
"@types/node": "^18.11.13",
17+
"@types/node": "^18.11.18",
1818
"@vercel/ncc": "^0.36.0",
19-
"vitest": "^0.25.7"
19+
"vitest": "^0.26.3"
2020
},
2121
"dependencies": {
2222
"@actions/core": "^1.10.0",

Diff for: test/apply.test.ts

+4-33
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,22 @@ import {random_filename, write_file, remove_file, read_file, __dirname} from "./
44
import {apply_patch} from '../lib/runner.js'
55

66
beforeEach(async (context) => {
7-
context.base_path = random_filename()
7+
context.base_url = 'https://raw.githubusercontent.com/pkgjs/patch-my-code-of-conduct/cd136b70909ad4c14d738d0399e09559ca2a3ecd/template/base.md'
8+
context.template_url = 'https://www.contributor-covenant.org/version/2/1/code_of_conduct/code_of_conduct.md';
89
context.patch_path = random_filename()
910
context.output_path = random_filename()
1011

11-
await write_file(context.base_path)
1212
await write_file(context.patch_path)
1313
await write_file(context.output_path)
1414

1515
return async () => {
16-
await remove_file(context.base_path)
1716
await remove_file(context.patch_path)
1817
await remove_file(context.output_path)
1918
}
2019
})
2120

2221
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)
22+
await apply_patch(context.base_url, context.template_url, context.patch_path, context.output_path)
2523
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))
24+
assert.isTrue(output.startsWith('This is the beginning of our Code of Conduct.\n'))
5425
})

Diff for: test/fixture/base.md

-44
This file was deleted.

Diff for: test/fixture/patch.txt

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
--- /dev/null 2022-11-11 18:00:48
22
+++ output.md 2022-11-11 18:00:32
3-
@@ -43,7 +43,7 @@
4-
5-
---
6-
7-
-# CHANGED THE TITLE OF COC
8-
+# Contributor Covenant Code of Conduct
9-
3+
@@ -1,6 +1,6 @@
4+
This is the beginning of our Code of Conduct.
5+
6+
-# Contributor Covenant Code of Conduct
7+
+# CHANGED_THE_TITLE
8+
109
## Our Pledge
11-
10+

0 commit comments

Comments
 (0)