Skip to content

Commit

Permalink
Merge pull request #22 from nobe4/pull-sources-via-api
Browse files Browse the repository at this point in the history
Fetch sources via github's api
  • Loading branch information
nobe4 authored Jun 11, 2022
2 parents 6061772 + db5d741 commit 1585137
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 94 deletions.
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16.15.1
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@
## Usage

```yaml
uses: nobe4/girssa@latest
name: build rss feed

on:
workflow_dispatch:
schedule:
# Every day at midnight UTC
- cron: "0 0 * * *"

jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: nobe4/girssa@<TODO current version>
```
See [`action.yml`](./action.yml) for options.
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.1
v0.0.2
4 changes: 2 additions & 2 deletions dist/index.js

Large diffs are not rendered by default.

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

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ const core = require("@actions/core");

const sources = require("./src/sources.js");
const issues = require("./src/issues.js");
const github = require("./src/github.js");

async function run() {
try {
const noop = core.getInput("noop", { required: true });
const noop = core.getInput("noop", { required: true }) === "true";
const sources_path = core.getInput("sources", { required: true });
const token = core.getInput("token", { required: true });
const full_repository = core.getInput("repository", { required: true });

core.notice(
`Running with noop: ${noop}, sources: ${sources_path}, repo: ${full_repository}`
);

const [owner, repo] = full_repository.split("/");

issues.setup(token, owner, repo, noop);
github.setup(token, owner, repo, noop);

sources
.read(sources_path)
Expand Down
23 changes: 23 additions & 0 deletions scripts/clear_issues.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

set -e

#
# Remove *all* the issues from the specified repository.
#
# Usage : ./clear_issues.sh REPO
#
# REPO needs to be OWNER/NAME
#
# Requirements:
# - `gh`: The github cli configured.
#

REPO="$1"
[[ -z "${REPO}" ]] && echo "missing REPO argument" && exit 1

echo "Removing all the issues from ${REPO}"

gh --repo "$REPO" issue list --json number --state all --limit 100 \
| jq -r '.[] | .number' \
| xargs -n1 gh --repo "$REPO" issue delete
1 change: 0 additions & 1 deletion scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
set -e
[[ -n "${TRACE}" ]] && set -x


# Run everything from the repository's root.
REPO_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )"
cd "${REPO_DIR}"
Expand Down
19 changes: 19 additions & 0 deletions src/github.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Handle interactions with Github API

const github = require("@actions/github");

const self = {
client: undefined,
owner: undefined,
repo: undefined,
noop: false,

setup(token, owner, repo, noop) {
self.client = github.getOctokit(token);
self.owner = owner;
self.repo = repo;
self.noop = noop;
},
};

module.exports = self;
42 changes: 18 additions & 24 deletions src/issues.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
// Handle interactions with github issues

const core = require("@actions/core");
const github = require("@actions/github");
const github = require("./github.js");

const self = {
client: undefined,
owner: undefined,
repo: undefined,
noop: false,

setup(token, owner, repo, noop) {
self.client = github.getOctokit(token);
self.owner = owner;
self.repo = repo;
self.noop = noop;
},

// list lists all the issues in the repository.
//
// @return {Promise} - Resolve with the list of fetched issues.
// Reject with any error that occured.
list() {
return new Promise((resolve, reject) => {
// Bypass if noop is set
if (self.noop) {
core.notice(`[NOOP] List all the issues in ${self.owner}/${self.repo}`);
if (github.noop) {
core.notice(
`[NOOP] List all the issues in ${github.owner}/${github.repo}`
);
resolve([]);
return;
}

core.debug(`List all the issues in ${self.owner}/${self.repo}`);
core.debug(`List all the issues in ${github.owner}/${github.repo}`);

self.client.rest.issues
github.client.rest.issues
.listForRepo({
owner: self.owner,
repo: self.repo,
owner: github.owner,
repo: github.repo,
state: "all",
})

.then(({ data }) => resolve(data))
.catch(reject);
.catch((e) => {
core.warning(`issues.list error: ${e}`);
core.warning(e.stack);
reject(e);
});
});
},

Expand Down Expand Up @@ -87,18 +81,18 @@ const self = {
const full_title = `${item.title} - ${item.id}`;

// Bypass if noop is set
if (self.noop) {
if (github.noop) {
const message = `[NOOP] Created issue for: '${full_title}'`;
core.notice(message);
resolve(message);
return;
}

self.client.rest.issues
github.client.rest.issues

.create({
owner: self.owner,
repo: self.repo,
owner: github.owner,
repo: github.repo,
title: full_title,
body: `${item.link}\n\n${item.content}\n\n${item.published}`,
})
Expand Down
6 changes: 5 additions & 1 deletion src/rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const self = {
});
})

.on("error", reject)
.on("error", (e) => {
core.warning(`rss.fetch error: ${e}`);
core.warning(e.stack);
reject(e);
})

// This is what actually sends the request.
.end();
Expand Down
53 changes: 40 additions & 13 deletions src/sources.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Handle interactions with the sources file.

const core = require("@actions/core");
const fs = require("fs");
const rss = require("./rss.js");
const github = require("./github.js");

const self = {
// read reads the source file from its path.
Expand All @@ -12,20 +12,42 @@ const self = {
// @return {object} - Parsed content of the json file.
read(path) {
return new Promise(function (resolve, reject) {
core.debug(`Reading source file ${path}`);
// Bypass if noop is set
if (github.noop) {
core.notice(
`[NOOP] Reading source file ${github.owner}/${github.repo}/${path}`
);
resolve([]);
return;
}

fs.readFile(path, "utf8", (err, content) => {
if (err) {
reject(err);
return;
}
core.debug(`Reading source file ${github.owner}/${github.repo}/${path}`);

try {
resolve(JSON.parse(content));
} catch (e) {
github.client.rest.repos
.getContent({
owner: github.owner,
repo: github.repo,
path: path,
})

// Extract content and parse JSON
.then(({ data }) => {
core.debug(`Received from API: ${JSON.stringify(data)}`);
return data.content;
})

// Using atob fails in nodejs 16.X with [InvalidCharacterError]: Invalid character
.then((encoded) => Buffer.from(encoded, "base64").toString("utf-8"))

.then(JSON.parse)

.then(resolve)

.catch((e) => {
core.warning(`sources.read error: ${e}`);
core.warning(e.stack);
reject(e);
}
});
});
});
},

Expand Down Expand Up @@ -77,7 +99,12 @@ const self = {
.then(self.filter_results)

.then(resolve)
.catch(reject);

.catch((e) => {
core.warning(`sources.process error: ${e}`);
core.warning(e.stack);
reject(e);
});
});
},
};
Expand Down
15 changes: 15 additions & 0 deletions tests/github.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const action_github = require("@actions/github");
const github = require("../src/github.js");

describe("setup", () => {
it("setup the client, owner and repo correctly", () => {
jest.spyOn(action_github, "getOctokit").mockReturnValueOnce("client");

github.setup("token", "owner", "repo", true);

expect(github.client).toBe("client");
expect(github.owner).toBe("owner");
expect(github.repo).toBe("repo");
expect(github.noop).toBeTruthy();
});
});
40 changes: 13 additions & 27 deletions tests/issues.test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
const github = require("@actions/github");

const github = require("../src/github.js");
const issues = require("../src/issues.js");

beforeEach(() => {
issues.noop = false;
issues.owner = "owner";
issues.repo = "repo";
github.noop = false;
github.owner = "owner";
github.repo = "repo";
});

afterEach(() => jest.clearAllMocks());

describe("setup", () => {
it("setup the client, owner and repo correctly", () => {
jest.spyOn(github, "getOctokit").mockReturnValueOnce("client");

issues.setup("token", "owner", "repo", true);

expect(issues.client).toBe("client");
expect(issues.owner).toBe("owner");
expect(issues.repo).toBe("repo");
expect(issues.noop).toBeTruthy();
});
});

describe("list", () => {
it("doesn't list if nooped", () => {
issues.noop = true;
github.noop = true;
expect(issues.list()).resolves.toHaveLength(0);
});

it("lists all the repo", () => {
const list_spy = jest.fn();
issues.client = { rest: { issues: { listForRepo: list_spy } } };
github.client = { rest: { issues: { listForRepo: list_spy } } };

list_spy.mockResolvedValueOnce({ data: "OK" });
expect(issues.list()).resolves.toBe("OK");
Expand Down Expand Up @@ -65,7 +51,7 @@ describe("create_one", () => {
};

it("doesn't create if nooped", () => {
issues.noop = true;
github.noop = true;

expect(issues.create_one(item)).resolves.toStrictEqual(
"[NOOP] Created issue for: 'title - id'"
Expand All @@ -74,7 +60,7 @@ describe("create_one", () => {

it("create an issue from the item correctly", () => {
const create_spy = jest.fn();
issues.client = { rest: { issues: { create: create_spy } } };
github.client = { rest: { issues: { create: create_spy } } };

create_spy.mockResolvedValueOnce({
data: {
Expand All @@ -87,16 +73,16 @@ describe("create_one", () => {
);

expect(create_spy).toHaveBeenCalledWith({
owner: issues.owner,
repo: issues.repo,
owner: github.owner,
repo: github.repo,
title: "title - id",
body: "link\n\ncontent\n\npublished",
});
});

it("fails to create an issue", () => {
const create_spy = jest.fn();
issues.client = { rest: { issues: { create: create_spy } } };
github.client = { rest: { issues: { create: create_spy } } };

create_spy.mockRejectedValueOnce({
response: {
Expand All @@ -112,8 +98,8 @@ describe("create_one", () => {
);

expect(create_spy).toHaveBeenCalledWith({
owner: issues.owner,
repo: issues.repo,
owner: github.owner,
repo: github.repo,
title: "title - id",
body: "link\n\ncontent\n\npublished",
});
Expand Down
Loading

0 comments on commit 1585137

Please sign in to comment.