Skip to content

Commit

Permalink
Allow workflow status filter to be used with branch, pr, and commit (#40
Browse files Browse the repository at this point in the history
)

* Allow workflow status filter to be used with branch, pr, and commit

Closes #39

Signed-off-by: Jesse Szwedko <[email protected]>

* Let workflow_conclusion be reassignable

As we default it now

Signed-off-by: Jesse Szwedko <[email protected]>

* Fix workflow status variable name

Signed-off-by: Jesse Szwedko <[email protected]>
  • Loading branch information
jszwedko authored Dec 15, 2020
1 parent f2f4c0c commit 303fcc9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar

> If `commit` or `pr` or `branch` or `run_id` or `workflow_conclusion` is not specified then the artifact from the most recent completed workflow run will be downloaded.
**Do not specify `pr`, `commit`, `branch`, `run_id` or `workflow_conclusion` together. Pick just one or none.**
**Do not specify `pr`, `commit`, `branch`, `run_id` together or `workflow_conclusion` and `run_id` together. Pick just one of each or none.**

```yaml
- name: Download artifact
Expand All @@ -18,10 +18,12 @@ Let's suppose you have a workflow with a job in it that at the end uploads an ar
github_token: ${{secrets.GITHUB_TOKEN}}
# Required, workflow file name or ID
workflow: workflow_name.yml
# Optional, the conclusion of a completed workflow to search for
# Can be one of:
# Optional, the status or conclusion of a completed workflow to search for
# Can be one of a workflow conculsion::
# "failure", "success", "neutral", "cancelled", "skipped", "timed_out", "action_required"
# Ignores conclusion by default (thus using the most recent completed run when no other option is specified, regardless of conclusion)
# Or a workflow status:
# "completed", "in_progress", "queued"
# Default: "completed"
workflow_conclusion: success
# Optional, will get head commit SHA
pr: ${{github.event.pull_request.number}}
Expand Down
33 changes: 19 additions & 14 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const filesize = require('filesize')
const pathname = require('path')
const fs = require("fs")

const allowed_workflow_conclusions = ["failure", "success", "neutral", "cancelled", "skipped", "timed_out", "action_required"];
// https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#list-workflow-runs
// allows for both status or conclusion to be used as status filter
const allowed_workflow_conclusions = ["failure", "success", "neutral", "cancelled", "skipped", "timed_out", "action_required", "queued", "in_progress", "completed"];

async function main() {
try {
Expand All @@ -14,19 +16,27 @@ async function main() {
const [owner, repo] = core.getInput("repo", { required: true }).split("/")
const path = core.getInput("path", { required: true })
const name = core.getInput("name")
const workflow_conclusion = core.getInput("workflow_conclusion")
let workflow_conclusion = core.getInput("workflow_conclusion")
let pr = core.getInput("pr")
let commit = core.getInput("commit")
let branch = core.getInput("branch")
let runID = core.getInput("run_id")

const client = github.getOctokit(token)

if ([runID, branch, pr, commit, workflow_conclusion].filter(elem => elem).length > 1) {
throw new Error("don't specify `run_id`, `branch`, `pr`, `commit` and `workflow_conclusion` together")
if ([runID, branch, pr, commit].filter(elem => elem).length > 1) {
throw new Error("don't specify `run_id`, `branch`, `pr`, `commit` together")
}

if(workflow_conclusion && !allowed_workflow_conclusions.includes(workflow_conclusion)) {
if ([runID, workflow_conclusion].filter(elem => elem).length > 1) {
throw new Error("don't specify `run_id`, `workflow_conclusion` together")
}

if (!workflow_conclusion) {
workflow_conclusion = "completed"
}

if(!allowed_workflow_conclusions.includes(workflow_conclusion)) {
throw new Error(`Unknown workflow conclusion '${workflow_conclusion}'`)
}

Expand All @@ -48,25 +58,20 @@ async function main() {
}

if (!runID) {
const endpoint = "GET /repos/:owner/:repo/actions/workflows/:id/runs"
const endpoint = "GET /repos/:owner/:repo/actions/workflows/:id/runs?status=:status"
const params = {
owner: owner,
repo: repo,
id: workflow,
branch: branch
branch: branch,
status: workflow_conclusion,
}
for await (const runs of client.paginate.iterator(endpoint, params)) {
const run = runs.data.find(r => {
if (commit) {
return r.head_sha == commit
} else if(workflow_conclusion) {
return r.conclusion == workflow_conclusion
} else {
// No PR, commit or conclusion was specified; just return the first one.
// The results appear to be sorted from API, so the most recent is first.
// Just check if workflow run completed.
return r.status == "completed"
}
return true
})

if (run) {
Expand Down

0 comments on commit 303fcc9

Please sign in to comment.