@@ -8,6 +8,11 @@ const https = require("https");
88const accessToken = core . getInput ( "ln_access_token" ) ;
99const feedList = core . getInput ( "feed_list" ) ;
1010const 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
1318function 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
4494function 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 ) ) ;
0 commit comments