Skip to content

Commit

Permalink
Notify on slack when queries are exceeding limits
Browse files Browse the repository at this point in the history
  • Loading branch information
asdil12 committed Jan 24, 2024
1 parent 56a273d commit 8775dc4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
18 changes: 18 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ inputs:
redmine_api_key:
description: The Redmine API key
required: true
slack_webhook_url:
description: URL to a slack webhook to post a notification
required: false
default: ''

runs:
using: composite
Expand All @@ -39,12 +43,22 @@ runs:
shell: bash
- run: sudo apt-get install -y kramdown weasyprint imagemagick ghostscript
shell: bash
- name: Get previous published state
uses: actions/checkout@v4
with:
ref: ${{ inputs.folder }}
path: ${{ inputs.folder }}
fetch-depth: '1'
- run: rm -rf ${{ inputs.folder }}/.git
shell: bash
- name: Render Markdown from configured backlog queries
run: |
python backlogger/backlogger.py ${{ inputs.config }} ${{ inputs.args }}
echo "BACKLOG_STATUS=$?" >> "$GITHUB_ENV"
env:
REDMINE_API_KEY: ${{ inputs.REDMINE_API_KEY }}
STATE_FOLDER: ${{ inputs.folder }}
SLACK_WEBHOOK_URL: ${{ inputs.slack_webhook_url }}
shell: bash
continue-on-error: true
- name: Define variables
Expand Down Expand Up @@ -84,3 +98,7 @@ runs:
sudo sed -i '/rights="none" pattern="PDF"/d' /etc/ImageMagick-6/policy.xml
weasyprint index.html - | convert - -trim ${{ env.PREVIEW_NAME }}
shell: bash
- name: Publish state json
run: |
cp state.json ${{ inputs.folder }}/
shell: bash
31 changes: 29 additions & 2 deletions backlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def check_backlog(conf):
def render_table(data):
all_good = True
rows = []
bad_queries = {}
for conf in data["queries"]:
good, issue_count = check_backlog(conf)
url = data["web"] + "?" + conf["query"]
Expand All @@ -135,7 +136,8 @@ def render_table(data):
)
if not good:
all_good = False
return (all_good, rows)
bad_queries[conf['title']] = {"url": url, "issue_count": issue_count, "limit": limit}
return (all_good, rows, bad_queries)

def remove_project_part_from_url(url):
return(re.sub("projects\/.*\/", "", url))
Expand Down Expand Up @@ -236,10 +238,35 @@ def render_influxdb(data):
print("\n".join(line for line in render_influxdb(data)))
else:
initialize_md(data)
all_good, rows, bad_queries = render_table(data)
with open("index.md", "a") as md:
all_good, rows = render_table(data)
for row in rows:
md.write("|".join(row) + "\n")
if os.environ.get('STATE_FOLDER'):
old_state_file = os.environ['STATE_FOLDER'], "state.json"
if os.path.exists(old_state_file):
# open state.json from last run, see if anything changed and send slack notification if needed
with open(old_state_file), "r") as sj:
state = json.load(sj)
old_bad_queries = set(state["bad_queries"].keys())
new_bad_queries = set(bad_queries.keys())
fixed_queries = old_bad_queries - new_bad_queries
broken_queries = new_bad_queries - old_bad_queries
msg = None
if broken_queries:
# something new broke
msg = f":red_circle: Some queries are exceeding limits: {', '.join(new_bad_queries)}"
elif fixed_queries and not new_bad_queries:
# this is the first green run so let's let everyone know
msg = f":green_heart: All queries within limits again!"
if msg and os.environ.get('SLACK_WEBHOOK_URL'):
requests.post(os.environ['SLACK_WEBHOOK_URL'], json={"msg": msg})
with open("state.json", "w") as sj:
state = {
"bad_queries": bad_querie,
"updated": datetime.now().isoformat()
}
json.dump(state, sj)
except FileNotFoundError:
sys.exit("Configuration file {} not found".format(switches.config))
if switches.exit_code and not all_good:
Expand Down

0 comments on commit 8775dc4

Please sign in to comment.