Skip to content

Commit

Permalink
Update linter to handle actions in monorepos (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrassia authored Feb 5, 2024
1 parent c122cc9 commit ad7cf4f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/test-lint-workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: Test Lint Workflow Action

on:
pull_request:
paths:
- "lint-workflow"
push:
branches:
- "main"
workflow_dispatch:

jobs:
test-lint-workflow:
runs-on: ubuntu-latest
steps:
- name: Checkout Branch
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: Get workflow list
id: workflow-list
run: |
WORKFLOW_LIST=$(ls .github/workflows | xargs -I {} echo -n ".github/workflows/{} ")
echo "workflow-list=$WORKFLOW_LIST" >> $GITHUB_OUTPUT
- uses: ./lint-workflow
with:
workflows: ${{ steps.workflow-list.outputs.workflow-list }}
36 changes: 21 additions & 15 deletions lint-workflow/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ def action_repo_exists(action_id):

path, *hash = action_id.split("@")

if "bitwarden/gh-actions" in path:
path_list = path.split("/", 2)
forward_slash_count = path.count("/")
if forward_slash_count > 1:
path_list = path.split("/", forward_slash_count)
url = f"https://api.github.com/repos/{path_list[0]}/{path_list[1]}"
response = get_github_api_response(url, action_id)

Expand Down Expand Up @@ -152,33 +153,38 @@ def get_action_update(action_id):
if path in memoized_action_update_urls:
return memoized_action_update_urls[path]
else:
if "bitwarden/gh-actions" in path:
path_list = path.split("/", 2)
url = f"https://api.github.com/repos/{path_list[0]}/{path_list[1]}/commits?path={path_list[2]}"
forward_slash_count = path.count("/")
path_list = path.split("/", forward_slash_count)
repo = f"{path_list[0]}/{path_list[1]}"
# Get latest release information.
response = get_github_api_response(
f"https://api.github.com/repos/{repo}/releases/latest", action_id
)
# No releases exist in repository.
if response.status == 404:
commit_path = '/'.join(path_list[2:])
url = f"https://api.github.com/repos/{repo}/commits?path={commit_path}"
response = get_github_api_response(url, action_id)
if not response:
return None

sha = json.loads(response.data)[0]["sha"]
if sha not in hash:
update_url = (
f"https://github.com/{path_list[0]}/{path_list[1]}/commit/{sha}"
f"https://github.com/{repo}/commit/{sha}"
)
memoized_action_update_urls[path] = update_url
return update_url
# Response errored out.
elif not response:
return None
# Get tag name from latest release.
else:
# Get tag from latest release
response = get_github_api_response(
f"https://api.github.com/repos/{path}/releases/latest", action_id
)
if not response:
return None

tag_name = json.loads(response.data)["tag_name"]

# Get the URL to the commit for the tag
response = get_github_api_response(
f"https://api.github.com/repos/{path}/git/ref/tags/{tag_name}",
f"https://api.github.com/repos/{repo}/git/ref/tags/{tag_name}",
action_id,
)
if not response:
Expand All @@ -196,7 +202,7 @@ def get_action_update(action_id):
sha = json.loads(response.data)["object"]["sha"]

if sha not in hash:
update_url = f"https://github.com/{path}/commit/{sha}"
update_url = f"https://github.com/{repo}/commit/{sha}"
memoized_action_update_urls[path] = update_url
return update_url

Expand Down

0 comments on commit ad7cf4f

Please sign in to comment.