Skip to content

Commit

Permalink
Merge pull request #4 from Ry0taK/master
Browse files Browse the repository at this point in the history
Add database feature
  • Loading branch information
yanorei32 authored Apr 23, 2024
2 parents 0c62adc + a0a1c01 commit cada6c9
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 16 deletions.
98 changes: 98 additions & 0 deletions .github/workflows/issue.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Workflow file for registering dad jokes from issues
name: Register dad jokes from issues

on:
issues:
types: [opened]

permissions:
issues: write
contents: write

jobs:
register-dad-jokes:
runs-on: ubuntu-latest
steps:
- name: Check permissions
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
with:
script: |
const { data: permissions } = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: context.payload.issue.user.login
});
if (permissions.permission !== 'write' && permissions.permission !== 'admin') {
throw new Error('User does not have write permissions');
}
- name: Checkout
uses: actions/checkout@1d96c772d19495a3b5c517cd2bc0cb401ea0529f

- name: Write dad joke to file
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
with:
script: |
const fs = require('fs');
const path = require('path');
if (!fs.existsSync('database')) {
fs.mkdirSync('database');
}
const issue = context.payload.issue;
const id = `CRE-${new Date().getFullYear()}-${36000+fs.readdirSync('database').length}`;
const body = JSON.parse(issue.body);
if(!body.author || !body.text || !body.vector_string) {
throw new Error('Invalid issue body');
}
body.id = id;
const issuePath = path.join('database', `${id}.json`);
fs.writeFileSync(issuePath, JSON.stringify(body, null, 2));
console.log(`Wrote issue to ${issuePath}`);
- name: Sync database to main branch
run: |
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git add database
git commit -m "Sync database"
git push
- name: Comment and close valid issue
if: success()
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
with:
script: |
const fs = require('fs');
const id = `CRE-${new Date().getFullYear()}-${36000+fs.readdirSync('database').length - 1}`;
const issue = context.payload.issue;
github.rest.issues.createComment({
issue_number: issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Registered dad joke with ID ${id}`
});
github.rest.issues.update({
issue_number: issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed'
});
- name: Comment and close invalid issue
if: failure()
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea
with:
script: |
const issue = context.payload.issue;
github.rest.issues.createComment({
issue_number: issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Closing issue due to the missing permission or invalid body.`
});
github.rest.issues.update({
issue_number: issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed'
});
84 changes: 68 additions & 16 deletions root/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,19 @@
a {
color: unset;
}

#save_button {
padding: 4px 25px;
border: #eaeded 1px solid;
margin: 3px 0;
float: right;
cursor: pointer;
user-select: none;
color: white;
font-weight: bold;
background-color: #004273;
border-color: #166ea9;
}
</style>
</head>
<body>
Expand All @@ -109,11 +122,11 @@ <h2>Common RyotaK Scoring System Calculator</h2>
<div class=infos>
<div class=info>
発言者:
<input type=text placeholder="発言者">
<input id=author type=text placeholder="発言者">
</div>
<div class=info>
該当テキスト:
<input type=text placeholder="該当テキスト">
<input id=text type=text placeholder="該当テキスト">
</div>
</div>
<div>
Expand All @@ -122,6 +135,7 @@ <h2>Common RyotaK Scoring System Calculator</h2>
<tr><td>Impact Subscore:</td><td id=impact_subscore></td></tr>
<tr><td>Exploitability Subscore:</td><td id=exploitability_subscore></td></tr>
<tr><td><b>Overall CRSS Score:</b></td><td id=overall_score></td></tr>
<tr><td><b>Vector String:</b></td><td id=vector_string></td></tr>
</table>
</div>
<section>
Expand All @@ -133,36 +147,37 @@ <h3>Exploitability Metrics</h3>
<form>
<div>
<h4>寒さへの影響 (SI)*</h4>
<label><input value=0.000 type=radio name=SI><div class=selector>低: Low (SI:L)</div></label
><label><input value=0.275 type=radio name=SI><div class=selector>中: Medium (SI:M)</div></label
><label><input value=0.660 type=radio name=SI><div class=selector>高: High (SI:H)</div></label>
<label><input value=0.000 type=radio name=SI data-text=L><div class=selector>低: Low (SI:L)</div></label
><label><input value=0.275 type=radio name=SI data-text=M><div class=selector>中: Medium (SI:M)</div></label
><label><input value=0.660 type=radio name=SI data-text=H><div class=selector>高: High (SI:H)</div></label>
</div>
<div>
<h4>ギャグの安直さ (GC)*</h4>
<label><input value=0.000 type=radio name=GC><div class=selector>低: Low (GC:L)</div></label
><label><input value=0.275 type=radio name=GC><div class=selector>中: Medium (GC:M)</div></label
><label><input value=0.660 type=radio name=GC><div class=selector>高: High (GC:H)</div></label>
<label><input value=0.000 type=radio name=GC data-text=L><div class=selector>低: Low (GC:L)</div></label
><label><input value=0.275 type=radio name=GC data-text=M><div class=selector>中: Medium (GC:M)</div></label
><label><input value=0.660 type=radio name=GC data-text=H><div class=selector>高: High (GC:H)</div></label>
</div>
<div>
<h4>ユーザーの予備知識 (UK)*</h4>
<label><input value=0.704 type=radio name=UK><div class=selector>低: Low (UK:L)</div></label
><label><input value=0.560 type=radio name=UK><div class=selector>中: Medium (UK:M)</div></label
><label><input value=0.450 type=radio name=UK><div class=selector>高: High (UK:H)</div></label>
<label><input value=0.704 type=radio name=UK data-text=L><div class=selector>低: Low (UK:L)</div></label
><label><input value=0.560 type=radio name=UK data-text=M><div class=selector>中: Medium (UK:M)</div></label
><label><input value=0.450 type=radio name=UK data-text=H><div class=selector>高: High (UK:H)</div></label>
</div>
<div>
<h4>伝達区分の制約 (TL)*</h4>
<label><input value=0.710 type=radio name=TL><div class=selector>なし: None (TL:N)</div></label
><label><input value=0.350 type=radio name=TL><div class=selector>あり: Required (TL:R)</div></label>
<label><input value=0.710 type=radio name=TL data-text=N><div class=selector>なし: None (TL:N)</div></label
><label><input value=0.350 type=radio name=TL data-text=R><div class=selector>あり: Required (TL:R)</div></label>
</div>
<div>
<h4>継続的なユーザーへの影響 (CA)*</h4>
<label><input value=0.0 type=radio name=CA><div class=selector>低: Low (CA:L)</div></label
><label><input value=0.3 type=radio name=CA><div class=selector>中: Medium (CA:M)</div></label
><label><input value=0.5 type=radio name=CA><div class=selector>高: High (CA:H)</div></label>
<label><input value=0.0 type=radio name=CA data-text=L><div class=selector>低: Low (CA:L)</div></label
><label><input value=0.3 type=radio name=CA data-text=M><div class=selector>中: Medium (CA:M)</div></label
><label><input value=0.5 type=radio name=CA data-text=H><div class=selector>高: High (CA:H)</div></label>
</div>
</form>
</div>
</section>
<div id=save_button>Save</div>
</div>
</main>
<footer class=owsc>
Expand All @@ -187,6 +202,22 @@ <h1>OWSC</h1>
return '[Critical 緊急]';
};

const get_radio_text = name => {
const e = document.querySelector(`input[name=${name}]:checked`);
return e ? e.getAttribute('data-text') : '';
};

const vector = () => {
const f = document.getElementsByTagName('form')[0];
const SI = get_radio_text('SI');
const GC = get_radio_text('GC');
const UK = get_radio_text('UK');
const TL = get_radio_text('TL');
const CA = get_radio_text('CA');

return `CRSS:1.0/SI:${SI}/GC:${GC}/UK:${UK}/TL:${TL}/CA:${CA}`;
};

function calc() {
const f = document.getElementsByTagName('form')[0];
const SI = f.SI.value;
Expand All @@ -207,11 +238,32 @@ <h1>OWSC</h1>

const overall_score = base_score + (10 - base_score) * f.CA.value;
document.getElementById('overall_score').textContent = format(overall_score) + ' ' + rate(round(overall_score));

const vector_string = vector();
document.getElementById('vector_string').textContent = vector_string;
}

function save() {
const author = document.getElementById('author').value;
const text = document.getElementById('text').value;
const vector_string = document.getElementById('vector_string').textContent;
const body = JSON.stringify({
author,
text,
vector_string,
});
const params = new URLSearchParams();
params.append('title', `Add ${text}`);
params.append('body', body);

const url = `https://github.com/gnknzm/crss.gnknzm.net/issues/new?${params.toString()}`;
window.open(url, '_blank');
}

document.addEventListener('DOMContentLoaded', calc);
for (e of document.getElementsByTagName('input'))
e.addEventListener('change', calc);
document.getElementById('save_button').addEventListener('click', save);
</script>
</body>
</html>

0 comments on commit cada6c9

Please sign in to comment.