diff --git a/.github/entrypoint.sh b/.github/entrypoint.sh deleted file mode 100755 index 78d1ed7..0000000 --- a/.github/entrypoint.sh +++ /dev/null @@ -1,107 +0,0 @@ -#! /bin/sh -# -# Entry point for Github Action container. -set -e - -IMPORT_PATH="${INPUT_IMPORT_PATH:-github.com/${GITHUB_REPOSITORY}}" -# Branch in push mode, or PR# in pull_request mode. -BRANCH=$(echo "${GITHUB_REF}" | cut -d/ -f3) -EMAIL="${INPUT_COMMIT-EMAIL:-posener@gmail.com}" - -REVIEWS_URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${BRANCH}/reviews" -COMMENT_HEADER="[Goreadme](https://github.com/posener/goreadme) diff for \`${README_FILE}\` file for this PR:" - -echo "Processing: ${IMPORT_PATH}@${BRANCH} -Event: ${GITHUB_EVENT_NAME} -" - -get_existing_comment_id() { - CURRENT_REVIEWS="$(curl -sS "${REVIEWS_URL}")" - LINE="$(echo "${CURRENT_REVIEWS}" | jq ".[] | .body" | grep -Fn "${COMMENT_HEADER}" | cut -d: -f1)" - if [[ -n "${LINE}" ]] - then - echo "${CURRENT_REVIEWS}" | jq ".["$(( LINE - 1 ))"] | .id" - fi -} - -# Run Goreadme on the current HEAD. -goreadme -import-path="${IMPORT_PATH}" "$@" > "${README_FILE}" - -# Check if README was modified or was added, and don't push changes if nothing changed. -git add "${README_FILE}" -if git diff --staged --exit-code --no-color "${README_FILE}" > readme_diff.txt -then - echo "No changes were made to ${README_FILE}, aborting" - exit 0 -fi - -# Diff content. -DIFF=$(cat readme_diff.txt | tail +5 | sed "s/\`/'/g") -echo "Diff: - -${DIFF} -" - -if [[ "${GITHUB_EVENT_NAME}" == "push" ]] -then - echo "Push mode" - - # Configure git before commit. - git config user.name Goreadme - git config user.email "${EMAIL}" - - # Commit and push chnages to upstream branch. - git commit -m "Update readme according to Go doc" - git push origin HEAD:"${BRANCH}" -else - echo "Pull request mode" - # Add comment on PR if Github token was given. - if [[ -z "${GITHUB_TOKEN}" ]] - then - echo "In order to add request comment, set the github_token input." - exit 0 - fi - - # Prepare comment text. - - COMMENT_TEXT="${COMMENT_HEADER} - -\`\`\`diff -${DIFF} -\`\`\` - -This change will be automatically pushed when this PR is merged. -" - - # Prepare the comment text for json: - COMMENT_TEXT=$(echo "${COMMENT_TEXT}" | sed 's/\"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g') - ACTION="POST" - - # Check if there is already a review that contains goreadme contnet - COMMENT_ID="$(get_existing_comment_id)" - if [[ -z "${COMMENT_ID}" ]] - then - echo "Creating new comment" - CURL_BODY="{\"body\": \"${COMMENT_TEXT}\", \"event\": \"COMMENT\"}" - else - echo "Updating comment with ID: ${COMMENT_ID}" - ACTION="PUT" - CURL_BODY="{\"body\": \"${COMMENT_TEXT}\"}" - fi - - # Perform comment post/update request. - if ! curl -X "${ACTION}" "${REVIEWS_URL}/${COMMENT_ID}" \ - --fail -sS \ - -H "Content-Type: application/json" \ - -H "Authorization: token ${GITHUB_TOKEN}" \ - -d "${CURL_BODY}" - then - echo "Failed updating comment. - -URL: ${REVIEWS_URL}/${COMMENT_ID} -Action: ${ACTION} -Body: ${CURL_BODY} -" - exit 1 - fi -fi \ No newline at end of file diff --git a/.github/workflows/goreadme.yml b/.github/workflows/goreadme.yml index 5e807f7..392e9c0 100644 --- a/.github/workflows/goreadme.yml +++ b/.github/workflows/goreadme.yml @@ -12,8 +12,9 @@ jobs: - name: Update readme according to Go doc uses: ./ with: - recursive: 'true' - badge-travisci: 'true' - badge-codecov: 'true' - badge-godoc: 'true' + recursive: true + badge-travisci: true + badge-codecov: true + badge-godoc: true github-token: '${{ secrets.GITHUB_TOKEN }}' + debug: true diff --git a/cmd/action/main.go b/cmd/action/main.go index 66b291e..6890abc 100644 --- a/cmd/action/main.go +++ b/cmd/action/main.go @@ -9,7 +9,9 @@ import ( "log" "net/http" "os" + "path/filepath" + "github.com/golang/gddo/gosrc" "github.com/posener/goaction" "github.com/posener/goaction/actionutil" "github.com/posener/goreadme" @@ -27,6 +29,8 @@ var ( ) func init() { + log.SetFlags(log.Lshortfile) + flag.StringVar(&cfg.ImportPath, "import-path", "", "Override package import path.") flag.BoolVar(&cfg.RecursiveSubPackages, "recursive", false, "Load docs recursively.") flag.BoolVar(&cfg.Functions, "functions", false, "Write functions section.") @@ -45,14 +49,26 @@ func init() { if *debug { os.Setenv("GOREADME_DEBUG", "1") } + + // Set import path if was not overridden. + if cfg.ImportPath == "" { + cfg.ImportPath = "github.com/" + goaction.Repository + } } func main() { ctx := context.Background() + + localPath, err := filepath.Abs("./") + if err != nil { + log.Fatal(err) + } + gosrc.SetLocalDevMode(localPath) + gr := func(w io.Writer) error { return goreadme.New(http.DefaultClient).WithConfig(cfg).Create(ctx, ".", w) } - err := script.Writer("goreadme", gr).ToFile(*path) + err = script.Writer("goreadme", gr).ToFile(*path) if err != nil { log.Fatalf("Failed: %s", err) } @@ -71,13 +87,14 @@ func main() { diff := gitDiff() - if diff == "" { - log.Println("No changes were made. Aborting") - os.Exit(0) - } + log.Printf("Diff:\n\n%s\n", diff) switch { case goaction.IsPush(): + if diff == "" { + log.Println("No changes were made. Skipping push.") + break + } push() case goaction.IsPR(): pr(diff) @@ -113,10 +130,13 @@ func pr(diff string) { return } - body := fmt.Sprintf( - "[goreadme](https://github.com/posener/goreadme) diff for %s file for this PR:\n\n%s", - *path, - diff) + body := "[goreadme](https://github.com/posener/goreadme) will not make any changes in this PR" + if diff != "" { + body = fmt.Sprintf( + "[goreadme](https://github.com/posener/goreadme) diff for %s file for this PR:\n\n%s", + *path, + diff) + } ctx := context.Background() err := actionutil.PRComment(ctx, githubToken, "goreadme", body) diff --git a/internal/template/template.go b/internal/template/template.go index 5e3cb8d..9b28ef0 100644 --- a/internal/template/template.go +++ b/internal/template/template.go @@ -15,13 +15,6 @@ func Execute(w io.Writer, data interface{}) error { return main.Execute(&multiNewLineEliminator{w: w}, data) } -func importPath(p *doc.Package) string { - if len(p.References) > 0 { - return p.References[0] - } - return p.ImportPath -} - var base = template.New("base").Funcs( template.FuncMap{ "gocode": func(s string) string { @@ -37,10 +30,10 @@ var base = template.New("base").Funcs( return "`" + s + "`" }, "importPath": func(p *doc.Package) string { - return importPath(p) + return p.ImportPath }, "fullName": func(p *doc.Package) string { - return strings.TrimPrefix(importPath(p), "github.com/") + return strings.TrimPrefix(p.ImportPath, "github.com/") }, "urlOrName": func(f *doc.File) string { if f.URL != "" {