Skip to content

Commit

Permalink
feat: Add blogs to readme (#4)
Browse files Browse the repository at this point in the history
* feat: Add blogs to readme

* feat: Add codeowners

* fix: Fix lint issue
  • Loading branch information
imskr authored Jul 8, 2022
1 parent d8c270e commit 34c8930
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 33 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @imskr
12 changes: 0 additions & 12 deletions .github/workflows/integration.yml

This file was deleted.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# ReadMe-Blogs
Fetch latest blog posts and add it to GitHub profile's readME dynamically

<!-- BLOG-LIST-START -->
<!-- BLOG-LIST-END -->
20 changes: 18 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@ name: "Readme update Actions"
description: "Fetch latest blog posts and add it to GitHub profile's readme dynamically"
author: "Shubham Kumar"
inputs:
readme_path:
max_post:
rss_list:
description: "RSS feed url"
default: ""
required: true
readme_path:
description: "Path of readme file"
default: "./README.md"
required: false
max_post:
description: "Max number of posts"
default: "3"
required: false
commit_user:
description: "Git username"
default: "imskr"
required: false
commit_email:
description: "Git user email"
default: "[email protected]"
required: false
commit_message:
description: "Git commit message"
default: "Update readme with latest blogs"
required: false
outputs:
result_post:
description: "Stringified posts in readme format"

runs:
using: "docker"
Expand Down
54 changes: 35 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,35 @@ package main

import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"

medium "github.com/readme-update-actions/pkg/structs"
helpers "github.com/readme-update-actions/pkg/utils"
)

// create new error for empty env
var errEnvEmpty = errors.New("getenv: Environment variable empty")

// Get env string from user through actions env
// if no env variables provided throw error
// requires key string
// returns string, error
func getEnvString(key string) (string, error) {
env_string := os.Getenv(key)
if env_string == "" {
return env_string, errEnvEmpty
}
return env_string, nil
}

func main() {
// get the rss list from the actions env
rss_medium, _ := getEnvString("INPUT_RSS_LIST")
rss_medium, _ := helpers.GetEnvString("INPUT_RSS_LIST")

// get the number of posts or stories to commit
max_post, _ := helpers.GetEnvInteger("INPUT_MAX_POST")

// if max_post not in env var set default to 3
if max_post == 0 {
max_post = 3
}

// get readme path from the actions env
readme_path, _ := helpers.GetEnvString("INPUT_README_PATH")

// if path not provided default to root readme
if readme_path == "" {
readme_path = "./README.md"
}

// get medium.com rss feed
mediumResponse, err := http.Get(rss_medium)
Expand All @@ -51,5 +52,20 @@ func main() {
log.Println("Error xml parse", errXMLParse)
}

fmt.Println(rss.Channel.Title)
// store the posts
var items []string

// get the posts
// format it according to readme links format
for i := 0; i < max_post; i++ {
item := fmt.Sprintf("- [%s](%s)\n", rss.Channel.Item[i].Title, rss.Channel.Item[i].Link)
items = append(items, item)
}
result_post := fmt.Sprintf("<!-- BLOG-LIST-START -->"+"\n%s", strings.Join(items, "\n"))

// find readme and replace with our result
err = helpers.ReplaceFile(readme_path, strings.TrimSuffix(result_post, "\n"))
if err != nil {
log.Println("Error updating readme")
}
}
68 changes: 68 additions & 0 deletions pkg/utils/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package helpers

import (
"errors"
"io/ioutil"
"log"
"os"
"strconv"
"strings"
)

// create new error for empty env
var ErrEnvEmpty = errors.New("getenv: Environment variable empty")

// Get env string from user through actions env
// if no env variables provided throw error
// requires key string
// returns string, error
func GetEnvString(key string) (string, error) {
env_string := os.Getenv(key)
if env_string == "" {
return env_string, ErrEnvEmpty
}
return env_string, nil
}

// Get env string from user through actions env and convert to integer
// if no env variables provided throw error
// requires key string
// returns int, error
func GetEnvInteger(key string) (int, error) {
strEnv, err := GetEnvString(key)
if err != nil {
return 0, err
}

intEnv, err := strconv.Atoi(strEnv)
if err != nil {
return 0, err
}
return intEnv, nil
}

// Read a file and replace strings
// requires path of file, resultString
// returns error
func ReplaceFile(path string, resultString string) error {
input, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalln(err)
}

lines := strings.Split(string(input), "\n")

for i, line := range lines {
if strings.Contains(line, "<!-- BLOG-LIST-START -->") {
lines[i] = resultString
}
}

output := strings.Join(lines, "\n")
err = ioutil.WriteFile(path, []byte(output), 0644)
if err != nil {
log.Fatalln(err)
}

return nil
}

0 comments on commit 34c8930

Please sign in to comment.