Skip to content

Commit

Permalink
ci: Import from actions-sync, check carefully
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Aug 16, 2024
1 parent ca5d7df commit 2b54065
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 80 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/R-CMD-check-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ jobs:

runs-on: ubuntu-22.04

name: ${{ matrix.package }}
name: 'rcc-dev: ${{ matrix.package }}'

# Begin custom: services
# End custom: services
Expand Down Expand Up @@ -147,4 +147,4 @@ jobs:

- uses: ./.github/workflows/check
with:
results: ${{ matrix.package }}
results: ${{ matrix.package }}
22 changes: 19 additions & 3 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@ on:
branches:
- main
- master
workflow_dispatch:
inputs:
ref:
description: "Branch or tag to check out"
required: false
default: "main"
merge_group:
types:
- checks_requested
schedule:
- cron: "10 0 * * *"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref || github.sha }}-${{ github.base_ref || '' }}
group: ${{ github.workflow }}-${{ github.ref }}-${{ inputs.ref || github.head_ref || github.sha }}-${{ github.base_ref || '' }}
cancel-in-progress: true

name: rcc
Expand All @@ -35,6 +44,8 @@ jobs:

steps:
- uses: actions/checkout@v4
with:
ref: ${{ inputs.ref }}

- uses: ./.github/workflows/rate-limit
with:
Expand All @@ -59,6 +70,9 @@ jobs:
- uses: ./.github/workflows/versions-matrix
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository

- uses: ./.github/workflows/dep-suggests-matrix
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository

- uses: ./.github/workflows/update-snapshots
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository

Expand All @@ -71,6 +85,8 @@ jobs:
- id: commit
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
uses: ./.github/workflows/commit
with:
token: ${{ secrets.GITHUB_TOKEN }}

- uses: ./.github/workflows/check
with:
Expand Down Expand Up @@ -151,7 +167,7 @@ jobs:

runs-on: ${{ matrix.os }}

name: ${{ matrix.os }} (${{ matrix.r }}) ${{ matrix.desc }}
name: 'rcc: ${{ matrix.os }} (${{ matrix.r }}) ${{ matrix.desc }}'

# Begin custom: services
# End custom: services
Expand Down Expand Up @@ -212,7 +228,7 @@ jobs:
use-public-rspm: true

- id: set-matrix
uses: ./.github/workflows/dep-matrix-suggests
uses: ./.github/workflows/dep-suggests-matrix-read

check-suggests-matrix:
runs-on: ubuntu-22.04
Expand Down
50 changes: 39 additions & 11 deletions .github/workflows/commit/action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
name: "Action to commit changes to the repository"
inputs:
token:
description: "GitHub token"
required: true
outputs:
sha:
description: "SHA of generated commit"
Expand All @@ -7,23 +11,47 @@ outputs:
runs:
using: "composite"
steps:
- name: Commit if changed
- name: Commit if changed, create a PR if protected
id: commit
env:
GITHUB_TOKEN: ${{ inputs.token }}
run: |
set -x
if [ -n "$(git status --porcelain)" ]; then
echo "Changed"
git fetch
if [ -n "${GITHUB_HEAD_REF}" ]; then
protected=${{ github.ref_protected }}
if [ "${protected}" = "true" ]; then
current_branch=$(git branch --show-current)
new_branch=gha-commit
git checkout -b ${new_branch}
git add .
git stash save
git switch ${GITHUB_HEAD_REF}
git merge origin/${GITHUB_BASE_REF} --no-edit
git stash pop
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
# Force-push, used in only one place
# Alternative: separate branch names for each usage
git push -u origin HEAD -f
existing_pr=$(gh pr list --state open --base main --head ${new_branch} --json number --jq '.[] | .number')
if [ -n "${existing_pr}" ]; then
echo "Existing PR: ${existing_pr}"
else
gh pr create --base main --head ${new_branch} --title "chore: Auto-update from GitHub Actions" --body "Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
fi
gh workflow run rcc -f ref=$(git rev-parse HEAD)
else
git fetch
if [ -n "${GITHUB_HEAD_REF}" ]; then
git add .
git stash save
git switch ${GITHUB_HEAD_REF}
git merge origin/${GITHUB_BASE_REF} --no-edit
git stash pop
fi
git add .
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
git push -u origin HEAD
fi
git add .
git commit -m "chore: Auto-update from GitHub Actions"$'\n'$'\n'"Run: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
git push -u origin HEAD
echo sha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
fi
# Unconditionally set the output because it could come from a manually triggered run
echo sha=$(git rev-parse HEAD) >> $GITHUB_OUTPUT
shell: bash
56 changes: 0 additions & 56 deletions .github/workflows/dep-matrix-suggests/action.yml

This file was deleted.

24 changes: 24 additions & 0 deletions .github/workflows/dep-suggests-matrix-read/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: "Actions to read a matrix with all suggested packages, computed with the dep-suggests-matrix action"
outputs:
matrix:
description: "Generated matrix"
value: ${{ steps.set-matrix.outputs.matrix }}

runs:
using: "composite"
steps:
- name: Install json2yaml
run: |
sudo npm install -g json2yaml
shell: bash

- id: set-matrix
run: |
# Empty contents if no suggested packages
matrix=$(cat .github/dep-suggests-matrix.json || true)
if [ -n "$matrix" ]; then
echo $matrix | jq .
echo $matrix | json2yaml
fi
echo "matrix=$matrix" | tee -a $GITHUB_OUTPUT
shell: bash
47 changes: 47 additions & 0 deletions .github/workflows/dep-suggests-matrix/action.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# FIXME: Dynamic lookup by parsing https://svn.r-project.org/R/tags/
get_deps <- function() {
# Determine package dependencies
if (!requireNamespace("desc", quietly = TRUE)) {
install.packages("desc")
}

deps_df <- desc::desc_get_deps()
deps_df_optional <- deps_df$package[deps_df$type %in% c("Suggests", "Enhances")]
deps_df_hard <- deps_df$package[deps_df$type %in% c("Depends", "Imports", "LinkingTo")]
deps_df_base <- unlist(tools::standard_package_names(), use.names = FALSE)

packages <- sort(deps_df_optional)
packages <- intersect(packages, rownames(available.packages()))

# Too big to fail, or can't be avoided:
off_limits <- c("testthat", "rmarkdown", "rcmdcheck", deps_df_hard, deps_df_base)
off_limits_dep <- unlist(tools::package_dependencies(off_limits, recursive = TRUE, which = "strong"))
setdiff(packages, c(off_limits, off_limits_dep))
}

if (Sys.getenv("GITHUB_BASE_REF") != "") {
print(Sys.getenv("GITHUB_BASE_REF"))
has_diff <- (system("git diff ${{ github.event.pull_request.base.sha }}... | egrep '^[+][^+]' | grep -q ::") == 0)
if (has_diff) {
system("git diff ${{ github.event.pull_request.base.sha }}... | egrep '^[+][^+]' | grep -q ::")
packages <- get_deps()
} else {
writeLines("No changes using :: found, not checking without suggested packages")
packages <- character()
}
} else {
packages <- get_deps()
}

if (length(packages) > 0) {
json <- paste0(
'{"package":[',
paste0('"', packages, '"', collapse = ","),
']}'
)
} else {
json <- character()
}

writeLines(json, ".github/dep-suggests-matrix.json")
writeLines(json)
13 changes: 13 additions & 0 deletions .github/workflows/dep-suggests-matrix/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: "Actions to compute a matrix with all suggested packages"
outputs:
matrix:
description: "Generated matrix"
value: ${{ steps.set-matrix.outputs.matrix }}

runs:
using: "composite"
steps:
- id: set-matrix
run: |
Rscript ./.github/workflows/dep-suggests-matrix/action.R
shell: bash
2 changes: 1 addition & 1 deletion .github/workflows/revdep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ jobs:

runs-on: ubuntu-22.04

name: ${{ matrix.package }}
name: 'revdep: ${{ matrix.package }}'

# Begin custom: services
# End custom: services
Expand Down
19 changes: 12 additions & 7 deletions .github/workflows/update-status/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ inputs:
sha:
description: 'SHA to update'
required: true
state:
description: 'state to update'
required: true

runs:
using: "composite"
Expand All @@ -24,11 +21,19 @@ runs:
-H "X-GitHub-Api-Version: 2022-11-28" \
repos/${{ github.repository }}/actions/runs/${{ github.run_id }} | jq -r .html_url)
gh api \
--method POST \
# Check if a failing status has been set already
status=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
repos/${{ github.repository }}/statuses/${{ inputs.sha }} \
-f "state=${{ job.status }}" -f "target_url=$html_url" -f "description=${{ github.workflow }} / ${{ github.job }}" -f "context=actions-sync"
repos/${{ github.repository }}/commits/${{ inputs.sha }}/status | jq -r .state)
if [ "${status}" != "error" ] && [ "${status}" != "failure" ]; then
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
repos/${{ github.repository }}/statuses/${{ inputs.sha }} \
-f "state=${{ job.status }}" -f "target_url=$html_url" -f "description=${{ github.workflow }} / ${{ github.job }}" -f "context=actions-sync"
fi
fi
shell: bash

0 comments on commit 2b54065

Please sign in to comment.