Skip to content

Commit

Permalink
Merge pull request #46 from seed-of-apricot/update-summary
Browse files Browse the repository at this point in the history
Update summary
  • Loading branch information
seed-of-apricot committed Oct 12, 2020
2 parents ad91be8 + 72c975c commit 64e88fd
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 36 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,12 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
summaryPath: example/example.csv
flagPath: example/flags/*.csv
- name: pull-request
uses: peter-evans/create-pull-request@master
- name: add-and-commit
uses: EndBug/add-and-commit@v4
with:
commit-message: update summary
title: Update Summary
body: summary updated
base: main
message: README.md has been re-written
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
1. Checkout
Expand All @@ -78,12 +77,13 @@ jobs:
The parameters that this action refers to are:
| parameter | required | default | What is this |
| ------------ | -------- | ------- | -------------------------------------- |
| GITHUB_TOKEN | Yes | | GitHub token |
| summaryPath | Yes | | Path to the summary file |
| flagPath | Yes | | Path to the flag files to be retrieved |
| id | | `'id'` | Column name of id |
| parameter | required | default | What is this |
| ------------ | -------- | -------- | --------------------------------------------------------------------------------------------------------------- |
| GITHUB_TOKEN | Yes | | GitHub token |
| summaryPath | Yes | | Path to the summary file |
| flagPath | Yes | | Path to the flag files to be retrieved |
| id | | `id` | Column name of id |
| mode | | `single` | `single` uses the file name as the column name <br /> `multiple` uses filename + column name as the column name |

## License

Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ inputs:
required: false
description: 'column name for the id'
default: 'id'
mode:
required: false
description: 'compilation mode'
default: 'single'
runs:
using: 'node12'
main: 'dist/index.js'
25 changes: 21 additions & 4 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions example/example.csv
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
id, flag3
00001, 1
00002, 1
00003,
00004,
00005, 1
00006,
00007,
00008,
00009,
id, flag3,example1,example3,test,example2
00001, 1, 1, 1, 1,
00002, 1, 1, 1, 1, 1
00003,,,,,
00004,,,,,
00005, 1,, 1,, 1
00006,,,, 1,
00007,, 1,,,
00008,,,,, 1
00009,,,,,
2 changes: 1 addition & 1 deletion example/flags/example1.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id, flag1
id, flag001
00002, 1
00001, 1
00007, 1
2 changes: 1 addition & 1 deletion example/flags/example2.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id, flag2
id, flag002
00002, 1
00008, 1
00005, 1
2 changes: 1 addition & 1 deletion example/flags/example3.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id, flag3
id, flag003
00001, 1
00002, 1
00005, 1
2 changes: 1 addition & 1 deletion example/flags/test.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id, test
id, testtest2
00006, 1
00002, 1
00001, 1
1 change: 0 additions & 1 deletion src/getFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const getFiles = async (
...prev,
octokit.repos.getContent({
...github.context.repo,
headers: { accept: 'application/vnd.github.v3.raw' },
path: file.filename,
ref: item.data.sha,
}),
Expand Down
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const main = async (): Promise<void> => {
console.log('files have been retrieved');
const newSummary = processFiles(
((await summary).data as unknown) as string,
(await files).map(item => (item.data as unknown) as string),
(await files).map(item => ({
data: Buffer.from(item.data.content, 'base64').toString(),
title: item.data.path.replace(/^.*\//g, '').split('.')[0],
})),
);
console.log('new summary has been compiled');
writeNewSummary(await newSummary);
Expand Down
20 changes: 17 additions & 3 deletions src/processFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,37 @@ import parse from 'csv-parse/lib/sync';

export const processFiles = async (
summary: string,
files: string[],
files: { data: string; title: string }[],
): Promise<{ [key in string]: string }[]> => {
const mode = core.getInput('mode');
const idColumn = core.getInput('id') || 'id';
const summaryObject: { [key in string]: string }[] = parse(summary, {
columns: true,
});
files.map(file => {
const data = parse(file, {
console.log(file);
const data = parse(file.data, {
columns: true,
});
data.map((row: { [key in string]: string }) => {
const keys = Object.keys(row).slice(1);
const id = row[idColumn];
keys.map(key => {
const name = () => {
switch (mode) {
case 'single':
return file.title;
case 'multiple':
return `${file.title}_${key}`;
default:
return 'null';
}
};
const index = summaryObject.findIndex(item => item[idColumn] === id);
if (index > -1) {
summaryObject[index][key] = row[key];
summaryObject[index][name()] = row[key];
} else {
core.setFailed('no id found!');
}
});
});
Expand Down

0 comments on commit 64e88fd

Please sign in to comment.