Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Write octodns-sync plan to pull request comment #36

Merged
merged 7 commits into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,21 @@ Default `"public.yaml"`.

Default `""` (empty string).

### `add_pr_comment`

(**Required**) Add plan as a comment, when triggered by a pull request?

Default `"No"`.

If you would like to add the plan `octodns-sync` generates as a pull request comment, be sure to also configure `plan_outputs` in your configuration file. For example in `public.yaml`:

```yaml
manager:
plan_outputs:
html:
class: octodns.provider.plan.PlanHtml
```

## Outputs

`octodns-sync` will compare your configuration file to the configurations your providers have, and report any planned changes. For example:
Expand Down
13 changes: 12 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
name: 'octodns-sync'
description: 'Run github/octodns to deploy your DNS config to any cloud.'
inputs:
add_pr_comment:
description: 'Add plan as a comment, when triggered by a pull request?'
required: true
default: 'No'
config_path:
description: 'Path, relative to your repository root, of the config file
you would like octodns to use.'
Expand All @@ -13,10 +17,17 @@ inputs:
not do it.'
required: false
default: ''
pr_comment_token:
description: 'Provide a token to use, if you set add_pr_comment to Yes.'
required: true
default: 'Not set'

runs:
using: 'docker'
image: 'docker://ghcr.io/solvaholic/octodns-sync:latest'
image: 'docker://ghcr.io/solvaholic/octodns-sync:issue35'
env:
ADD_PR_COMMENT: ${{ inputs.add_pr_comment }}
PR_COMMENT_TOKEN: ${{ inputs.pr_comment_token }}
args:
- ${{ inputs.config_path }}
- ${{ inputs.doit }}
Expand Down
45 changes: 40 additions & 5 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,49 @@ if [ ! -r "${_config_path}" ]; then
fi

# Run octodns-sync.
_logfile="${GITHUB_WORKSPACE}/octodns-sync.log"
_planfile="${GITHUB_WORKSPACE}/octodns-sync.plan"

echo "INFO: _config_path: ${_config_path}"
if [ "${_doit}" = "--doit" ]; then
script "${GITHUB_WORKSPACE}/octodns-sync.log" -e -c \
"octodns-sync --config-file=\"${_config_path}\" \
--log-stream-stdout --doit"
script "${_logfile}" -e -c \
"octodns-sync --config-file=\"${_config_path}\" --doit \
>>\"${_planfile}\""
else
script "${GITHUB_WORKSPACE}/octodns-sync.log" -e -c \
script "${_logfile}" -e -c \
"octodns-sync --config-file=\"${_config_path}\" \
--log-stream-stdout"
>>\"${_planfile}\""
fi

# If ADD_PR_COMMENT is Yes, add comment to PR.
if [ "${ADD_PR_COMMENT}" = "Yes" ]; then
echo "INFO: \$ADD_PR_COMMENT is 'Yes'."
if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then
if [ -z "${PR_COMMENT_TOKEN}" ]; then
echo "FAIL: \$PR_COMMENT_TOKEN is not set."
exit 1
fi
else
echo "SKIP: \$GITHUB_EVENT_NAME is not 'pull_request'."
exit 0
fi
# Construct the comment body
_sha="$(echo "${GITHUB_SHA}" | cut -c 1-7)"
_header="## octoDNS Plan for ${_sha}"
_footer="Automatically generated by octodns-sync"
_body="${_header}

$(cat "${_planfile}")

${_footer}"
# Post the comment
_user="fakename" \
_token="${PR_COMMENT_TOKEN}" \
_body="${_body}" \
GITHUB_EVENT_PATH="${GITHUB_EVENT_PATH}" \
python3 -c "import requests, os, json
comments_url = json.load(open(os.environ['GITHUB_EVENT_PATH'], 'r'))['pull_request']['comments_url']
response = requests.post(comments_url, auth=(os.environ['_user'], os.environ['_token']), json={'body':os.environ['_body']})
print(response)"
Comment on lines +67 to +75
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For posterity: This block looks the way it does, here, because it was the first way I found to meet these constraints:

  • Use the add an issue comment API with a provided personal access token
  • Get $_body into the JSON payload ~safely
  • Do not install gh or curl
  • Do not introduce dependencies, for example an external Python script or package


fi