Skip to content

Commit

Permalink
Add newPaper-updatedPaper separation #117
Browse files Browse the repository at this point in the history
  • Loading branch information
tarepan committed Feb 29, 2020
1 parent c8ff5dd commit 0be80b9
Show file tree
Hide file tree
Showing 9 changed files with 674 additions and 189 deletions.
660 changes: 510 additions & 150 deletions arXivSearches.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
preset: "ts-jest/presets/js-with-ts",
testEnvironment: "node"
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/", "<rootDir>/dist/"]
};
6 changes: 3 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@actions/core": "^1.2.0",
"@actions/github": "^2.0.0",
"@octokit/webhooks": "^7.0.0",
"immer": "^5.3.6",
"immer": "^5.1.0",
"node-fetch": "^2.6.0",
"oauth-1.0a": "^2.2.6",
"strict-uri-encode": "^2.0.0",
Expand Down
26 changes: 16 additions & 10 deletions src/ConfirmationAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { searchArXivByID } from "./arXivSearch";
import { ArXivStorage } from "./domain";
import { tweet } from "./twitter";
import * as WebhooksApi from "@octokit/webhooks";
import { updateArticleStatus } from "./updateArticles";
import { updateArticleStatus, arXivID2identity } from "./updateArticles";

async function run(): Promise<void> {
//@ts-ignore
Expand All @@ -16,7 +16,7 @@ async function run(): Promise<void> {
const regResult = idRegExp.exec(issueCommentPayload.issue.body);
// regResult == null means the issue is not for article confirmation
if (regResult != null) {
const id = regResult[1];
const arXivID = regResult[1];

// extract judge
const c = /\[vclab::confirmed\]|\[confirmed\]|vclab::confirmed/;
Expand All @@ -25,7 +25,7 @@ async function run(): Promise<void> {
const isC = c.exec(issueCommentPayload.comment.body);
const isE = e.exec(issueCommentPayload.comment.body);

// make thunks toward contribution
// make thanks toward contribution
if (isC !== null || isE !== null) {
const octokit = new github.GitHub(core.getInput("token"));
// reply thunks in comment
Expand Down Expand Up @@ -61,29 +61,35 @@ async function run(): Promise<void> {
);
//// update storage
const judgeResult = isC !== null ? "confirmed" : "excluded";
const newStorage = updateArticleStatus(storage, id, judgeResult);
const identity = arXivID2identity(arXivID);
const newStorage = updateArticleStatus(
storage,
identity.article,
judgeResult
);
//// commit storage update
const blob = Buffer.from(JSON.stringify(newStorage, undefined, 2));
await octokit.repos
.createOrUpdateFile({
...github.context.repo,
path: "arXivSearches.json",
message: `Add arXiv paper confirmation ${id}`,
message: `Add arXiv paper confirmation ${identity.repository}-${identity.article}`,
content: blob.toString("base64"),
// @ts-ignore
sha: contents.data.sha
})
.catch(err => core.setFailed(err));

// Tweet if "confirmed" (== VC paper)
if (isC !== null) {
console.log("is [vclab::confirmed]");
const arXivIDRegResult = /http:\/\/arxiv.org\/abs\/([a-z.0-9]+)/.exec(
id
arXivID
);
if (arXivIDRegResult !== null) {
const arXivID = arXivIDRegResult[1];
const paper = await searchArXivByID(arXivID);
const content = `[[VC paper]]\n"${paper.title}"\narXiv: ${id}`;
const arXivSearchID = arXivIDRegResult[1];
const paper = await searchArXivByID(arXivSearchID);
const content = `[[VC paper]]\n"${paper.title}"\narXiv: ${arXivSearchID}`;
// tweet confirmed paper
await tweet(
content,
Expand All @@ -101,7 +107,7 @@ async function run(): Promise<void> {
});
console.log("tweet created.");
} else {
core.setFailed(`arXiv ID search failure for ${id}`);
core.setFailed(`arXiv ID search failure for ${arXivID}`);
}
} else if (isE !== null) {
console.log("is [vclab::excluded]");
Expand Down
132 changes: 116 additions & 16 deletions src/PullShareAction.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,64 @@
import * as core from "@actions/core";
import * as github from "@actions/github";
import { searchArXiv } from "./arXivSearch";
import { ArXivStorage } from "./domain";
import {
ArXivStorage,
ArXivSearchResults,
ArXivRecord,
SearchedPaper,
Identity
} from "./domain";
import { tweet } from "./twitter";
import { arXivID2identity } from "./updateArticles";

function findNewPaper(
searchResults: ArXivSearchResults,
storage: ArXivStorage
): [SearchedPaper, ArXivRecord] | undefined {
// find non-match (==new) arXivPaper (version update is excluded)
const newPapers = searchResults.filter(paper => {
const articleID = arXivID2identity(paper.id).article;
return storage.every(record => record.id.article !== articleID);
});
if (newPapers.length === 0) {
return undefined;
} else {
const theNewPaper = newPapers[0];
const identity = arXivID2identity(theNewPaper.id);
return [
theNewPaper,
{
id: identity,
status: "candidate"
}
];
}
}

/**
* Find versionUp-ed paper in arXiv
*/
function findUpdatedPaper(
searchResults: ArXivSearchResults,
storage: ArXivStorage
): [SearchedPaper, Identity] | undefined {
//
const updatedPaperCand = searchResults.find(paper => {
const paperID = arXivID2identity(paper.id);
// There is a record which match the articleID but not match version in storage
return storage.some(
record =>
record.id.article === paperID.article &&
record.id.version !== paperID.version
);
});
if (updatedPaperCand === undefined) {
return undefined;
} else {
const identity = arXivID2identity(updatedPaperCand.id);
return [updatedPaperCand, identity];
}
}

async function run(): Promise<void> {
// fetch search result
Expand All @@ -23,26 +79,19 @@ async function run(): Promise<void> {
).toString()
);

// find non-match (==new) arXivPaper
const newPapers = searchResults.filter(paper =>
storage.every(record => record.id !== paper.id)
);

// add to storage with [candidate] status
if (newPapers.length > 0) {
const theNewPaper = newPapers[0];
storage.push({
id: theNewPaper.id,
status: "candidate"
});

const newPaperCand = findNewPaper(searchResults, storage);
if (newPaperCand !== undefined) {
const theNewPaper = newPaperCand[0];
const newRecord = newPaperCand[1];
// storage update
storage.push(newPaperCand[1]);
// commit storage update
const blob = Buffer.from(JSON.stringify(storage, undefined, 2));
await octokit.repos
.createOrUpdateFile({
...github.context.repo,
path: "arXivSearches.json",
message: `Add new arXiv search result ${theNewPaper.id}`,
message: `Add new arXiv search result ${newRecord.id.article}`,
content: blob.toString("base64"),
// @ts-ignore
sha: contents.data.sha
Expand All @@ -54,7 +103,7 @@ async function run(): Promise<void> {
await octokit.issues
.create({
...github.context.repo,
title: `'Voice Conversion' paper candidate ${theNewPaper.id}`,
title: `'Voice Conversion' paper candidate ${newRecord.id.article}`,
body: `Please check whether this paper is about 'Voice Conversion' or not.\n## article info.\n- title: **${theNewPaper.title}**\n- summary: ${theNewPaper.summary}\n- id: ${theNewPaper.id}\n## judge\nWrite [vclab::confirmed] or [vclab::excluded] in comment.`
})
.catch(err => core.setFailed(err));
Expand All @@ -76,6 +125,57 @@ async function run(): Promise<void> {
core.setFailed(err);
});
console.log("tweet created.");
return;
}

// paper update (if newPaper, already returned after Tweet)
const updatedPaperCand = findUpdatedPaper(searchResults, storage);
if (updatedPaperCand !== undefined) {
const updatedPaper = updatedPaperCand[0];
const paperID = updatedPaperCand[1];
// storage update (version only update)
const indexInStorage = storage.findIndex(
record => record.id.article === paperID.article
);
if (indexInStorage === -1) {
throw new Error("this should exist");
}
const record = storage[indexInStorage];
record.id.version = paperID.version;

// commit storage update
const blob = Buffer.from(JSON.stringify(storage, undefined, 2));
await octokit.repos
.createOrUpdateFile({
...github.context.repo,
path: "arXivSearches.json",
message: `Update arXiv search result ${paperID.article}@${paperID.version}`,
content: blob.toString("base64"),
// @ts-ignore
sha: contents.data.sha
})
.catch(err => core.setFailed(err));
console.log("storage updated (version update)");

// do NOT open issue because version up do not change "VC paper or not"

// tweet candidate info
await tweet(
`[paper version up]\n"${updatedPaper.title}"\narXiv: ${updatedPaper.id}`,
core.getInput("twi-cons-key"),
core.getInput("twi-cons-secret"),
core.getInput("twi-token-key"),
core.getInput("twi-token-secret")
)
.then(res => {
console.log(res.status);
return res.text();
})
.catch(err => {
core.setFailed(err);
});
console.log("tweet created.");
return;
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ export type ArXivSearchResults = SearchedPaper[];
// points: this is merely arXiv search result storage (status is for refined search)
export type candidate = "candidate";
export type resolved = "confirmed" | "excluded";
export type Identity = {
repository: "arXiv";
article: string;
version: string;
};
export type ArXivRecord = {
id: string;
id: Identity;
status: candidate | resolved;
};
export type ArXivStorage = ArXivRecord[];
8 changes: 4 additions & 4 deletions src/updateArticles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ import { ArXivStorage } from "./domain";
test("updateArticles", () => {
const input: ArXivStorage = [
{
id: "1",
id: { repository: "arXiv", article: "1", version: "1" },
status: "candidate"
},
{
id: "2",
id: { repository: "arXiv", article: "2", version: "2" },
status: "candidate"
}
];
// replace based on index

const expected: ArXivStorage = [
{
id: "1",
id: { repository: "arXiv", article: "1", version: "1" },
status: "candidate"
},
{
id: "2",
id: { repository: "arXiv", article: "2", version: "2" },
status: "confirmed"
}
];
Expand Down
19 changes: 16 additions & 3 deletions src/updateArticles.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import { ArXivStorage, resolved } from "./domain";
import { ArXivStorage, resolved, Identity } from "./domain";
import { produce } from "immer";

export function arXivID2identity(arXivID: string): Identity {
const result = /http:\/\/arxiv.org\/abs\/(\d+\.\d+)v(\d+)/.exec(arXivID);
if (result && result.length >= 3) {
return {
repository: "arXiv",
article: result[1],
version: result[2]
};
} else {
throw new Error("arXivID parse error");
}
}

export function updateArticleStatus(
storage: ArXivStorage,
id: string,
articleID: string,
status: resolved
): ArXivStorage {
return produce(storage, draft => {
// find index
const index = draft.findIndex(paper => paper.id === id);
const index = draft.findIndex(paper => paper.id.article === articleID);
draft[index].status = status;
});
}

0 comments on commit 0be80b9

Please sign in to comment.