Skip to content

Commit accd469

Browse files
authored
restore test setup and add more metadata fields to post (#5)
* restore test setup and add more metadata fields to post Signed-off-by: vsoch <vsoch@users.noreply.github.com>
1 parent 3ad9409 commit accd469

File tree

6 files changed

+133
-39
lines changed

6 files changed

+133
-39
lines changed

.github/workflows/post-jobs-slack.yaml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
on:
2-
push:
3-
paths:
4-
- 'example/jobs.yaml'
5-
branches:
6-
- main
2+
pull_request: []
73

84
jobs:
95
slack-poster:
@@ -21,8 +17,32 @@ jobs:
2117
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
2218
with:
2319
filename: "example/jobs.yaml"
24-
key: "url"
20+
previous_filename: "example/jobs-previous.yaml"
21+
keys: "url"
22+
unique: "url"
23+
24+
deploy: false
25+
test: true
26+
27+
28+
- id: multifield_updater
29+
name: Job Updater
30+
uses: ./
31+
env:
32+
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
33+
with:
34+
filename: "example/jobs.yaml"
35+
previous_filename: "example/jobs-previous.yaml"
36+
keys: "name,location,url"
37+
unique: "url"
38+
39+
deploy: false
40+
test: true
2541

2642
- run: echo ${{ steps.updater.outputs.fields }}
27-
name: Show New Jobs
43+
name: Show Keys Used
44+
shell: bash
45+
46+
- run: echo ${{ steps.updater.outputs.matrix }}
47+
name: Show Matrix
2848
shell: bash

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ and put it in a safe place. We will want to keep this URL as a secret in our eve
4444

4545
## 2. Usage
4646

47-
Add an GitHub workflow file in `.github/workflows` to specify the following. Note that
47+
Add a GitHub workflow file in `.github/workflows` to specify the following. Note that
4848
the workflow below will do the check and update on any push to main (e.g., a merged pull request).
4949

5050
### Deploy to Slack
@@ -73,13 +73,15 @@ jobs:
7373
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
7474
with:
7575
filename: "_data/jobs.yml"
76-
key: "url"
76+
keys: "url,name"
77+
unique: "url"
7778

7879
- run: echo ${{ steps.updater.outputs.fields }}
7980
name: Show New Jobs
8081
shell: bash
8182
```
8283
84+
In the above, we will include the url and name fields, and use the url field to determine uniqueness (default).
8385
By default, given that you have the slack webhook in the environment, deployment will
8486
happen because deploy is true. If you just want to test, then do:
8587
@@ -92,7 +94,7 @@ happen because deploy is true. If you just want to test, then do:
9294
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
9395
with:
9496
filename: "_data/jobs.yml"
95-
key: "url"
97+
keys: "url"
9698
deploy: false
9799
```
98100
@@ -108,7 +110,7 @@ aren't necessarily new) then add test:
108110
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
109111
with:
110112
filename: "_data/jobs.yml"
111-
key: "url"
113+
keys: "url"
112114
test: true
113115
```
114116
@@ -128,7 +130,7 @@ secrets.
128130
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
129131
with:
130132
filename: "_data/jobs.yml"
131-
key: "url"
133+
keys: "url,name"
132134
133135
deploy: true
134136
test: false

action.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,16 @@ inputs:
44
filename:
55
description: the filename for the jobs
66
required: true
7-
key:
8-
description: The key of the list to post (defaults to url)
7+
previous_filename:
8+
description: the previous filename (usually only for manual tesing)
99
required: false
10+
keys:
11+
description: Comma separated list of keys to post (defaults to url)
12+
required: false
13+
default: url
14+
unique:
15+
description: Field to use to determine uniqueness
16+
required: true
1017
default: url
1118
test:
1219
description: Test the updater (ensure there are jobs)
@@ -39,7 +46,7 @@ inputs:
3946

4047
outputs:
4148
fields:
42-
description: New fields parsed
49+
description: Fields (keys) parsed
4350
value: ${{ steps.jobs-updater.outputs.fields }}
4451
matrix:
4552
description: Matrix (list of lists) with value (index 1), icon (index 2) and full message (index 3)
@@ -60,8 +67,10 @@ runs:
6067
- name: Run action entrypoint
6168
id: jobs-updater
6269
env:
70+
INPUT_PREVIOUS_FILENAME: ${{ inputs.previous_filename }}
6371
INPUT_FILENAME: ${{ inputs.filename }}
64-
INPUT_KEY: ${{ inputs.key }}
72+
INPUT_KEYS: ${{ inputs.keys }}
73+
INPUT_UNIQUE: ${{ inputs.unique }}
6574
CURRENT_SHA: ${{ github.sha }}
6675
ACTION_DIR: ${{ github.action_path }}
6776
INPUT_REPO: ${{ github.repository }}

entrypoint.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ if [[ ! -f "${JOBFILE}" ]]; then
2929
exit 1
3030
fi
3131

32+
# If we are verbatim given a previous filename, use it
33+
if [ ! -z ${INPUT_PREVIOUS_FILENAME} ]; then
34+
JOBFILE="${INPUT_PREVIOUS_FILENAME}"
35+
fi
36+
3237
# Required to have slack webhook in environment
3338
if [ -z ${SLACK_WEBHOOK+x} ]; then
3439
printf "Warning, SLACK_WEBHOOK not found, will not deploy to slack.\n"
@@ -53,9 +58,9 @@ if [ ! -z ${TWITTER_API_KEY+x} ] && [ ! -z ${TWITTER_API_SECRET+x} ] && [ ! -z $
5358
fi
5459

5560
if [[ "${DEPLOY}" == "true" ]]; then
56-
COMMAND="python ${ACTION_DIR}/find-updates.py update --key ${INPUT_KEY} --original ${JOBFILE} --updated ${INPUT_FILENAME} --deploy"
61+
COMMAND="python ${ACTION_DIR}/find-updates.py update --keys ${INPUT_KEYS} --unique ${INPUT_UNIQUE} --original ${JOBFILE} --updated ${INPUT_FILENAME} --deploy"
5762
else
58-
COMMAND="python ${ACTION_DIR}/find-updates.py update --key ${INPUT_KEY} --original ${JOBFILE} --updated ${INPUT_FILENAME}"
63+
COMMAND="python ${ACTION_DIR}/find-updates.py update --keys ${INPUT_KEYS} --unique ${INPUT_UNIQUE} --original ${JOBFILE} --updated ${INPUT_FILENAME}"
5964
fi
6065

6166
if [[ "${INPUT_TEST}" == "true" ]]; then

example/jobs-previous.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
- expires: 2021-09-30
2+
location: Scoot Science - remote in the US or Canada
3+
name: Data Engineer & Full Stack Software Engineer
4+
url: https://www.scootscience.com/careers/
5+
- expires: 2020-12-31
6+
location: National Center for Supercomputing Applications / University of Illinois,
7+
Urbana, IL
8+
name: Assistant Research Programmer/Research Programmer/Senior Research Programmer
9+
url: https://jobs.illinois.edu/academic-job-board/job-details?jobID=130370&job=research-programmer-national-center-for-supercomputing-applications-130370
10+
- expires: 2020-12-31
11+
location: National Center for Supercomputing Applications / University of Illinois, Urbana, IL
12+
name: Assistant Research Programmer/Research Programmer/Senior Research Programmer
13+
url: https://jobs.hr.wisc.edu/en-us/job/510571/researcher
14+
- expires: 2020-12-31
15+
location: National Center for Supercomputing Applications / University of Illinois, Urbana, IL
16+
name: Assistant Research Programmer/Research Programmer/Senior Research Programmer
17+
url: https://jobs.ornl.gov/job/Oak-Ridge-Full-Stack-Software-Engineer-TN-37830/793411000/

find-updates.py

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
# This script does the following.
44
# 1. Reads in a current and changed yaml file
55
# 2. Finds changes between the two
6-
# 3. Post them to slack
6+
# 3. Post them to slack and/or Twitter
77

88
import argparse
9-
from datetime import datetime
109
import requests
1110
import random
1211
import json
@@ -80,9 +79,16 @@ def get_parser():
8079
)
8180

8281
update.add_argument(
83-
"--key",
84-
dest="key",
85-
help="The key to post to slack",
82+
"--keys",
83+
dest="keys",
84+
help="The keys (comma separated list) to post to slack or Twitter",
85+
)
86+
87+
update.add_argument(
88+
"--unique",
89+
dest="unique",
90+
help="The key to use to determine uniqueness (defaults to url)",
91+
default="url",
8692
)
8793

8894
return parser
@@ -109,6 +115,20 @@ def get_twitter_client():
109115
)
110116

111117

118+
def prepare_post(entry, keys):
119+
"""Prepare the slack or tweet. There should be a descriptor for
120+
all fields except for url.
121+
"""
122+
post = ""
123+
for key in keys:
124+
if key in entry:
125+
if key == "url":
126+
post = post + entry[key] + "\n"
127+
else:
128+
post = post + key.capitalize() + ": " + entry[key] + "\n"
129+
return post
130+
131+
112132
def main():
113133
parser = get_parser()
114134

@@ -133,21 +153,25 @@ def help(return_code=0):
133153
original = read_yaml(args.original)
134154
updated = read_yaml(args.updated)
135155

156+
# Parse keys into list
157+
keys = [x for x in args.keys.split(",") if x]
158+
136159
# Find new posts in updated
137160
previous = set()
138-
new = set()
139-
entries = set()
140161
for item in original:
141-
if args.key in item:
142-
previous.add(item[args.key])
162+
if args.unique in item:
163+
previous.add(item[args.unique])
143164

165+
# Create a lookup by the unique id
166+
new = []
167+
entries = []
144168
for item in updated:
145-
if args.key in item and item[args.key] not in previous:
146-
new.add(item[args.key])
169+
if args.unique in item and item[args.unique] not in previous:
170+
new.append(item)
147171

148172
# Also keep list of all for test
149-
elif args.key in item and item[args.key]:
150-
entries.add(item[args.key])
173+
elif args.unique in item and item[args.unique]:
174+
entries.append(item)
151175

152176
# Test uses all entries
153177
if args.test:
@@ -184,20 +208,37 @@ def help(return_code=0):
184208
"🔥️",
185209
"💻️",
186210
]
187-
for name in new:
211+
212+
for entry in new:
213+
214+
# Prepare the post
215+
post = prepare_post(entry, keys)
216+
188217
choice = random.choice(icons)
189-
message = "New Job! %s: %s" % (choice, name)
218+
message = "New Job! %s\n%s" % (choice, post)
219+
print(message)
220+
221+
# Convert dates, etc. back to string
222+
filtered = {}
223+
for k, v in entry.items():
224+
try:
225+
filtered[k] = json.dumps(v)
226+
except:
227+
continue
190228

191229
# Add the job name to the matrix
192230
# IMPORTANT: emojis in output mess up the action
193-
matrix.append([name])
231+
matrix.append(filtered)
194232
data = {"text": message, "unfurl_links": True}
195-
print(data)
196233

197234
# If we are instructed to deploy to twitter and have a client
198235
if args.deploy_twitter and client:
199-
message = "New #RSEng Job! %s: %s" % (choice, name)
200-
client.create_tweet(text=message)
236+
message = "New #RSEng Job! %s\n%s" % (choice, post)
237+
print(message)
238+
try:
239+
client.create_tweet(text=message)
240+
except Exception as e:
241+
print("Issue posting tweet: %s, and length is %s" % (e, len(message)))
201242

202243
# Don't continue if testing
203244
if not args.deploy or args.test:
@@ -211,11 +252,11 @@ def help(return_code=0):
211252
% (response.reason, response.status_code)
212253
)
213254

214-
print("::set-output name=fields::%s" % list(new))
255+
print("::set-output name=fields::%s" % json.dumps(keys))
215256
print("::set-output name=matrix::%s" % json.dumps(matrix))
216257
print("::set-output name=empty_matrix::false")
217258
print("matrix: %s" % json.dumps(matrix))
218-
print("group: %s" % list(new))
259+
print("group: %s" % new)
219260

220261

221262
if __name__ == "__main__":

0 commit comments

Comments
 (0)