Skip to content

Commit

Permalink
adding deploy arg (#4)
Browse files Browse the repository at this point in the history
* adding deploy arg
Signed-off-by: vsoch <[email protected]>
  • Loading branch information
vsoch authored Jan 19, 2022
1 parent 2db6169 commit 3ad9409
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 8 deletions.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ and put it in a safe place. We will want to keep this URL as a secret in our eve
Add an GitHub workflow file in `.github/workflows` to specify the following. Note that
the workflow below will do the check and update on any push to main (e.g., a merged pull request).

### Deploy to Slack

```yaml
on:
push:
Expand Down Expand Up @@ -77,3 +79,64 @@ jobs:
name: Show New Jobs
shell: bash
```
By default, given that you have the slack webhook in the environment, deployment will
happen because deploy is true. If you just want to test, then do:
```yaml
...
- id: updater
name: Job Updater
uses: rseng/jobs-updater@main
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
with:
filename: "_data/jobs.yml"
key: "url"
deploy: false
```
If you want to run a test run (meaning a random number of jobs will be selected that
aren't necessarily new) then add test:
```yaml
...
- id: updater
name: Job Updater
uses: rseng/jobs-updater@main
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
with:
filename: "_data/jobs.yml"
key: "url"
test: true
```
If test is true, deploy will always be set to false.
### Deploy to Twitter
To deploy to Twitter (in addiction to slack) you are required to set `deploy_twitter`
to true, and also define all the needed environment variables in your repository
secrets.

```yaml
- id: updater
name: Job Updater
uses: rseng/jobs-updater@add/deploy-arg
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
with:
filename: "_data/jobs.yml"
key: "url"
deploy: true
test: false
# Also deploy to Twitter (all secrets required in repository secrets)
twitter_deploy: true
twitter_api_secret: ${{ secrets.TWITTER_ACCESS_SECRET }}
twitter_api_key: ${{ secrets.TWITTER_ACCESS_TOKEN }}
twitter_consumer_secret: ${{ secrets.TWITTER_CONSUMER_API_SECRET }}
twitter_consumer_key: ${{ secrets.TWITTER_CONSUMER_API_KEY }}
```
46 changes: 45 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,53 @@ inputs:
description: The key of the list to post (defaults to url)
required: false
default: url
test:
description: Test the updater (ensure there are jobs)
required: true
default: false
deploy:
description: Deploy to slack?
required: true
default: true
# If you want to post to Twitter, all of these credentials are required for a specific user

# API keys are typically generated on behalf of a user (at the bottom of the interface)
twitter_deploy:
description: Boolean to deploy to Twitter
required: false
twitter_api_key:
description: API key generated for the user account to tweet
required: false
twitter_api_secret:
description: API secret generated for the user account to tweet
required: false

# Consumer keys are at the top of the developer interface, overall for the app (not a particular user)
twitter_consumer_key:
description: consumer key generated for the entire app
required: false
twitter_consumer_secret:
description: consumer secret generated for the entire app
required: false

outputs:
fields:
description: New fields parsed
value: ${{ steps.jobs-updater.outputs.fields }}
matrix:
description: Matrix (list of lists) with value (index 1), icon (index 2) and full message (index 3)
value: ${{ steps.jobs-updater.outputs.matrix }}
empty_matrix:
description: true if empty, false otherwise
value: ${{ steps.jobs-updated.outputs.empty_matrix }}

runs:
using: "composite"
steps:
- name: Install Python Dependencies
run: pip install pyyaml requests
run: |
pip install pyyaml requests
pip install git+https://github.com/tweepy/tweepy.git
shell: bash

- name: Run action entrypoint
Expand All @@ -28,5 +65,12 @@ runs:
CURRENT_SHA: ${{ github.sha }}
ACTION_DIR: ${{ github.action_path }}
INPUT_REPO: ${{ github.repository }}
INPUT_TEST: ${{ inputs.test }}
INPUT_DEPLOY: ${{ inputs.deploy }}
TWITTER_DEPLOY: ${{ inputs.twitter_deploy }}
TWITTER_API_KEY: ${{ inputs.twitter_api_key }}
TWITTER_API_SECRET: ${{ inputs.twitter_api_secret }}
TWITTER_CONSUMER_KEY: ${{ inputs.twitter_consumer_key }}
TWITTER_CONSUMER_SECRET: ${{ inputs.twitter_consumer_secret }}
run: ${{ github.action_path }}/entrypoint.sh
shell: bash
35 changes: 32 additions & 3 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,40 @@ fi

# Required to have slack webhook in environment
if [ -z ${SLACK_WEBHOOK+x} ]; then
printf "Please export SLACK_WEBHOOK to use this integration\n"
exit 1
printf "Warning, SLACK_WEBHOOK not found, will not deploy to slack.\n"
fi

# Are we deploying?
DEPLOY=true
if [[ "${INPUT_DEPLOY}" == "false" ]]; then
DEPLOY=false
fi

# Do not deploy ever on test
if [[ "${INPUT_TEST}" == "true" ]]; then
printf "🚧️ This is a test run! Deploy set to false 🚧️\n"
DEPLOY=false
fi

# If everything not unset and deploy twitter is true, we deploy!
DEPLOY_TWITTER=false
if [ ! -z ${TWITTER_API_KEY+x} ] && [ ! -z ${TWITTER_API_SECRET+x} ] && [ ! -z ${TWITTER_CONSUMER_KEY+x} ] && [ ! -z ${TWITTER_CONSUMER_SECRET+x} ] && [[ "${TWITTER_DEPLOY}" == "true" ]]; then
DEPLOY_TWITTER=true
fi

if [[ "${DEPLOY}" == "true" ]]; then
COMMAND="python ${ACTION_DIR}/find-updates.py update --key ${INPUT_KEY} --original ${JOBFILE} --updated ${INPUT_FILENAME} --deploy"
else
COMMAND="python ${ACTION_DIR}/find-updates.py update --key ${INPUT_KEY} --original ${JOBFILE} --updated ${INPUT_FILENAME}"
fi

COMMAND="python ${ACTION_DIR}/find-updates.py update --key ${INPUT_KEY} --original ${JOBFILE} --updated ${INPUT_FILENAME}"
if [[ "${INPUT_TEST}" == "true" ]]; then
COMMAND="$COMMAND --test"
fi

if [[ "${DEPLOY_TWITTER}" == "true" ]]; then
COMMAND="$COMMAND --deploy-twitter"
fi

echo "${COMMAND}"

Expand Down
105 changes: 101 additions & 4 deletions find-updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import os
import sys
import yaml
import tweepy


def read_yaml(filename):
Expand Down Expand Up @@ -45,6 +46,32 @@ def get_parser():
help="the original file",
)

update.add_argument(
"--deploy",
"-d",
dest="deploy",
action="store_true",
default=False,
help="deploy to Slack",
)

update.add_argument(
"--deploy-twitter",
dest="deploy_twitter",
action="store_true",
default=False,
help="deploy to Twitter (required api token/secret, and consumer token/secret",
)

update.add_argument(
"--test",
"-t",
dest="test",
action="store_true",
default=False,
help="do a test run instead",
)

update.add_argument(
"--updated",
"-u",
Expand All @@ -61,6 +88,27 @@ def get_parser():
return parser


def get_twitter_client():
envars = {}
for envar in [
"TWITTER_API_KEY",
"TWITTER_API_SECRET",
"TWITTER_CONSUMER_KEY",
"TWITTER_CONSUMER_SECRET",
]:
value = os.environ.get(envar)
if not value:
sys.exit("%s is not set, and required when twitter deploy is true!" % envar)
envars[envar] = value

return tweepy.Client(
consumer_key=envars["TWITTER_CONSUMER_KEY"],
consumer_secret=envars["TWITTER_CONSUMER_SECRET"],
access_token=envars["TWITTER_API_KEY"],
access_token_secret=envars["TWITTER_API_SECRET"],
)


def main():
parser = get_parser()

Expand All @@ -77,12 +125,18 @@ def help(return_code=0):
if not os.path.exists(filename):
sys.exit(f"{filename} does not exist.")

# Cut out early if we are deploying to twitter but missing envars
client = None
if args.deploy_twitter:
client = get_twitter_client()

original = read_yaml(args.original)
updated = read_yaml(args.updated)

# Find new posts in updated
previous = set()
new = set()
entries = set()
for item in original:
if args.key in item:
previous.add(item[args.key])
Expand All @@ -91,25 +145,64 @@ def help(return_code=0):
if args.key in item and item[args.key] not in previous:
new.add(item[args.key])

if not new:
# Also keep list of all for test
elif args.key in item and item[args.key]:
entries.add(item[args.key])

# Test uses all entries
if args.test:
new = entries
elif not new:
print("No new jobs found.")
print("::set-output name=fields::[]")
print("::set-output name=empty_matrix::true")
print("::set-output name=matrix::[]")
sys.exit(0)

# Prepare the data
webhook = os.environ.get("SLACK_WEBHOOK")
if not webhook:
if not webhook and args.deploy:
sys.exit("Cannot find SLACK_WEBHOOK in environment.")

headers = {"Content-type": "application/json"}
matrix = []

# Format into slack messages
icons = ["⭐️", "😍️", "❤️", "👀️", "✨️", "🤖️", "💼️"]
icons = [
"⭐️",
"😍️",
"❤️",
"👀️",
"✨️",
"🤖️",
"💼️",
"🤩️",
"😸️",
"😻️",
"👉️",
"🕶️",
"🔥️",
"💻️",
]
for name in new:
choice = random.choice(icons)
message = 'New Job! %s: %s' % (choice, name)
message = "New Job! %s: %s" % (choice, name)

# Add the job name to the matrix
# IMPORTANT: emojis in output mess up the action
matrix.append([name])
data = {"text": message, "unfurl_links": True}
print(data)

# If we are instructed to deploy to twitter and have a client
if args.deploy_twitter and client:
message = "New #RSEng Job! %s: %s" % (choice, name)
client.create_tweet(text=message)

# Don't continue if testing
if not args.deploy or args.test:
continue

response = requests.post(webhook, headers=headers, data=json.dumps(data))
if response.status_code not in [200, 201]:
print(response)
Expand All @@ -119,6 +212,10 @@ def help(return_code=0):
)

print("::set-output name=fields::%s" % list(new))
print("::set-output name=matrix::%s" % json.dumps(matrix))
print("::set-output name=empty_matrix::false")
print("matrix: %s" % json.dumps(matrix))
print("group: %s" % list(new))


if __name__ == "__main__":
Expand Down

0 comments on commit 3ad9409

Please sign in to comment.