Skip to content

Commit

Permalink
Merge pull request #7 from Arisamiga/lastpost-file
Browse files Browse the repository at this point in the history
Addition of Lastpost file
  • Loading branch information
Arisamiga authored Dec 16, 2023
2 parents a06e097 + b1b071a commit ed97800
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 8 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@ jobs:
embed_image: # Url of embed image
```
Add on the above `feed_list` your own RSS feed URL.

Add on the above `ln_access_token` your LinkedIn Access Token.

(Optional) Add a url of a image on `embed_image` that you would like to use as a image in your embed
| Parameter | Required | Description | Default |
| ----------------- | -------- | ------------------------------------------------------------ | ---------------------------------- |
| `feed_list` | ✓ | Your own RSS feed URL | (No Default URL) |
| `ln_access_token` | ✓ | Your LinkedIn Access Token | (No Default Access Token) |
| `embed_image` | X | The URL of the image you want to use in the embed. | (No Default URL) |
| `last_post_path` | X | The path to the file you want to use to store the last post. | `.github/.lastPost.txt` |
| `commit_user` | X | The username of the commiter. | `Linkedin-Post-Action` |
| `commit_email` | X | The email of the commiter. | `linkedin-post-action@example.com` |
| `commit_message` | X | The commit message. | `Update Last Post File` |

## How to get your LinkedIn Access Token

Expand Down
18 changes: 17 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@ inputs:
feed_list: # Url of RSS
description: "RSS Link"
required: true
ln_access_token: # Url of LinkedIn Access Token
ln_access_token: # LinkedIn Access Token
description: "LinkedIn Access Token | Can be Generated at (https://www.linkedin.com/developers/tools/oauth/)"
required: true
embed_image: # Url of embed image
description: "Url Of image to be used as a embed image on the post"
required: false
last_post_path: # Path to the file that stores the last post (Default: .github/.lastPost.txt)
description: "Path to the file that stores the last post"
default: ".github/.lastPost.txt"
required: false
commit_user: # Username of the commiter
description: "Username of commit user"
default: "Linkedin-Post-Action"
required: false
commit_email: # Email of the commiter
description: "Email of commit user"
default: "linkedin-post-action@example.com"
required: false
commit_message: # Commit message
description: "Commit message"
default: "Update Last Post File"
required: false
runs:
using: "node16"
main: "dist/index.js"
Expand Down
70 changes: 70 additions & 0 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.

62 changes: 62 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const https = require("https");
const accessToken = core.getInput("ln_access_token");
const feedList = core.getInput("feed_list");
const embedImage = core.getInput("embed_image");
const lastPostPath = core.getInput("last_post_path");

const commitUser = core.getInput("commit_user");
const commitEmail = core.getInput("commit_email");
const commitMessage = core.getInput("commit_message");

// Get LinkedIn ID, i.e. ownerId
function getLinkedinId(accessToken) {
Expand Down Expand Up @@ -40,6 +45,51 @@ function getLinkedinId(accessToken) {
});
}

// Check if post has already been published
function wasPostPublished(feed) {
// Read .lastPost file in .github/workflows/ to check if the post has been posted
const fs = require("fs");
const path = require("path");
const lastPost = path.join(process.env.GITHUB_WORKSPACE, lastPostPath);

let lastPostContent = "";
try {
lastPostContent = fs.readFileSync(lastPost, "utf8");
} catch (e) {
console.log("No .lastPost.txt file found");

// Create directories if they dont exist
fs.mkdirSync(path.dirname(lastPost), { recursive: true });

// Create file if it doesn't exist
fs.writeFileSync(lastPost, "");
}
// If the post has been posted, skip
if (lastPostContent === feed.items[0].link) {
console.log("Post already posted");
return true;
}
// If the post has not been posted, post
fs.writeFileSync(lastPost, feed.items[0].link);

return false;
}

function pushPastFile() {
// push the file changes to repository
const { exec } = require("child_process");

exec("git config --global user.email " + commitEmail);

exec("git config --global user.name " + commitUser);

exec("git add .");

exec("git commit -m '" + commitMessage + "'");

exec("git push");
}

// Publish content on LinkedIn
function postShare(
accessToken,
Expand Down Expand Up @@ -132,6 +182,13 @@ try {
console.log(feed.title);
getLinkedinId(accessToken)
.then((ownerId) => {
const pastPostCheck = wasPostPublished(feed);
if (pastPostCheck) {
core.warning("Post was already published");
core.warning("Ending job because post was already published");
return;
}

postShare(
accessToken,
ownerId,
Expand All @@ -146,8 +203,13 @@ try {
core.setFailed(
"Failed to post on LinkedIn, please check your access token is valid"
);
return;
} else if (r.status !== 201) {
core.setFailed("Failed to post on LinkedIn");
return;
}
if (!pastPostCheck) {
pushPastFile();
}
})
.catch((e) => console.log(e));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "linkedin-rss",
"version": "1.0.0",
"version": "1.3.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit ed97800

Please sign in to comment.