Skip to content

Commit ed97800

Browse files
authored
Merge pull request #7 from Arisamiga/lastpost-file
Addition of Lastpost file
2 parents a06e097 + b1b071a commit ed97800

6 files changed

Lines changed: 160 additions & 8 deletions

File tree

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ jobs:
3636
embed_image: # Url of embed image
3737
```
3838
39-
Add on the above `feed_list` your own RSS feed URL.
40-
41-
Add on the above `ln_access_token` your LinkedIn Access Token.
42-
43-
(Optional) Add a url of a image on `embed_image` that you would like to use as a image in your embed
39+
| Parameter | Required | Description | Default |
40+
| ----------------- | -------- | ------------------------------------------------------------ | ---------------------------------- |
41+
| `feed_list` | ✓ | Your own RSS feed URL | (No Default URL) |
42+
| `ln_access_token` | ✓ | Your LinkedIn Access Token | (No Default Access Token) |
43+
| `embed_image` | X | The URL of the image you want to use in the embed. | (No Default URL) |
44+
| `last_post_path` | X | The path to the file you want to use to store the last post. | `.github/.lastPost.txt` |
45+
| `commit_user` | X | The username of the commiter. | `Linkedin-Post-Action` |
46+
| `commit_email` | X | The email of the commiter. | `linkedin-post-action@example.com` |
47+
| `commit_message` | X | The commit message. | `Update Last Post File` |
4448

4549
## How to get your LinkedIn Access Token
4650

action.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,28 @@ inputs:
55
feed_list: # Url of RSS
66
description: "RSS Link"
77
required: true
8-
ln_access_token: # Url of LinkedIn Access Token
8+
ln_access_token: # LinkedIn Access Token
99
description: "LinkedIn Access Token | Can be Generated at (https://www.linkedin.com/developers/tools/oauth/)"
1010
required: true
1111
embed_image: # Url of embed image
1212
description: "Url Of image to be used as a embed image on the post"
1313
required: false
14+
last_post_path: # Path to the file that stores the last post (Default: .github/.lastPost.txt)
15+
description: "Path to the file that stores the last post"
16+
default: ".github/.lastPost.txt"
17+
required: false
18+
commit_user: # Username of the commiter
19+
description: "Username of commit user"
20+
default: "Linkedin-Post-Action"
21+
required: false
22+
commit_email: # Email of the commiter
23+
description: "Email of commit user"
24+
default: "linkedin-post-action@example.com"
25+
required: false
26+
commit_message: # Commit message
27+
description: "Commit message"
28+
default: "Update Last Post File"
29+
required: false
1430
runs:
1531
using: "node16"
1632
main: "dist/index.js"

dist/index.js

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const https = require("https");
88
const accessToken = core.getInput("ln_access_token");
99
const feedList = core.getInput("feed_list");
1010
const embedImage = core.getInput("embed_image");
11+
const lastPostPath = core.getInput("last_post_path");
12+
13+
const commitUser = core.getInput("commit_user");
14+
const commitEmail = core.getInput("commit_email");
15+
const commitMessage = core.getInput("commit_message");
1116

1217
// Get LinkedIn ID, i.e. ownerId
1318
function getLinkedinId(accessToken) {
@@ -40,6 +45,51 @@ function getLinkedinId(accessToken) {
4045
});
4146
}
4247

48+
// Check if post has already been published
49+
function wasPostPublished(feed) {
50+
// Read .lastPost file in .github/workflows/ to check if the post has been posted
51+
const fs = require("fs");
52+
const path = require("path");
53+
const lastPost = path.join(process.env.GITHUB_WORKSPACE, lastPostPath);
54+
55+
let lastPostContent = "";
56+
try {
57+
lastPostContent = fs.readFileSync(lastPost, "utf8");
58+
} catch (e) {
59+
console.log("No .lastPost.txt file found");
60+
61+
// Create directories if they dont exist
62+
fs.mkdirSync(path.dirname(lastPost), { recursive: true });
63+
64+
// Create file if it doesn't exist
65+
fs.writeFileSync(lastPost, "");
66+
}
67+
// If the post has been posted, skip
68+
if (lastPostContent === feed.items[0].link) {
69+
console.log("Post already posted");
70+
return true;
71+
}
72+
// If the post has not been posted, post
73+
fs.writeFileSync(lastPost, feed.items[0].link);
74+
75+
return false;
76+
}
77+
78+
function pushPastFile() {
79+
// push the file changes to repository
80+
const { exec } = require("child_process");
81+
82+
exec("git config --global user.email " + commitEmail);
83+
84+
exec("git config --global user.name " + commitUser);
85+
86+
exec("git add .");
87+
88+
exec("git commit -m '" + commitMessage + "'");
89+
90+
exec("git push");
91+
}
92+
4393
// Publish content on LinkedIn
4494
function postShare(
4595
accessToken,
@@ -132,6 +182,13 @@ try {
132182
console.log(feed.title);
133183
getLinkedinId(accessToken)
134184
.then((ownerId) => {
185+
const pastPostCheck = wasPostPublished(feed);
186+
if (pastPostCheck) {
187+
core.warning("Post was already published");
188+
core.warning("Ending job because post was already published");
189+
return;
190+
}
191+
135192
postShare(
136193
accessToken,
137194
ownerId,
@@ -146,8 +203,13 @@ try {
146203
core.setFailed(
147204
"Failed to post on LinkedIn, please check your access token is valid"
148205
);
206+
return;
149207
} else if (r.status !== 201) {
150208
core.setFailed("Failed to post on LinkedIn");
209+
return;
210+
}
211+
if (!pastPostCheck) {
212+
pushPastFile();
151213
}
152214
})
153215
.catch((e) => console.log(e));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "linkedin-rss",
3-
"version": "1.0.0",
3+
"version": "1.3.0",
44
"description": "",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)