github-script icon indicating copy to clipboard operation
github-script copied to clipboard

octokit.rest.actions not available

Open tcarac opened this issue 2 years ago • 16 comments
trafficstars

Describe the bug

TypeError: github.rest.actions.getEnvironmentVariable is not a function
    at eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15099:16), <anonymous>:3:43)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15100:12)
    at main (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15[19](https://github.com/g2crowd/actions_test/actions/runs/4372190858/jobs/7648818737#step:2:20)8:26)
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15175:1
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15230:3
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15233:12)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:8[22](https://github.com/g2crowd/actions_test/actions/runs/4372190858/jobs/7648818737#step:2:23):12)
Error: Unhandled error: TypeError: github.rest.actions.getEnvironmentVariable is not a function

To Reproduce

jobs:
  check-env:
    name: Validate Environment Configuration
    environment: staging
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        with:
          script: |
            const sidecar = await github.rest.actions.getEnvironmentVariable({
              repository_id: context.repo.id,
              environment_name: context.payload.client_payload.environment,
              name: 'SIDECAR'
            });
            console.log(JSON.stringify(sidecar));
            return;

Expected behavior github.rest.actions.getEnvironmentVariable should be defined as documented in octokit/rest

tcarac avatar Mar 09 '23 07:03 tcarac

I think it is because some endpoints are not available in the @octokit/plugin-rest-endpoint-methods version used v6.3.0. It is added in v6.8.0.

mknet3 avatar Mar 10 '23 00:03 mknet3

Same issue (different function, though):

TypeError: github.rest.actions.listRepoVariables is not a function
    at eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15099:16), <anonymous>:16:42)
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15100:12)
Error: Unhandled error: TypeError: github.rest.actions.listRepoVariables is not a function
    at main (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15198:26)
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15175:1
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15230:3
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15233:12)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)

Can't use createRepoVariable, updateRepoVariable. I'd assume they're all part of the newer octokit.

INRIX-Iurii-Okhmat avatar Mar 17 '23 23:03 INRIX-Iurii-Okhmat

i'm experience the same issue, workflow:

name: Publish

on:
  push:
    branches:
      - features/x

permissions: write-all #todo

jobs:
  upsert_repo_vars:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v6
        with:
          github-token: ${{secrets.GITHUB_TOKEN}}
          script: |
            github.rest.actions.createRepoVariable({
              owner: context.repo.owner,
              repo: context.repo.repo,
              name: "VAR",
              value: "TEST"
            });

and getting the following error in the output:

TypeError: github.rest.actions.createRepoVariable is not a function
    at eval (eval at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15143:16), <anonymous>:3:21)
Error: Unhandled error: TypeError: github.rest.actions.createRepoVariable is not a function
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15144:12)
    at main (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15236:26)
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15217:1
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15268:3
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15271:12)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)

alex529 avatar Apr 26 '23 09:04 alex529

Can't use createRepoVariable, updateRepoVariable. I'd assume they're all part of the newer octokit.

This is what I'm running into. Are there any plans to update this to take advantage of the new api methods?

joshft91 avatar May 11 '23 14:05 joshft91

This requires updating the version of used @octokit/plugin-rest-endpoint-methods by @actions/github, which this action depends on. https://github.com/actions/toolkit/blob/457303960f03375db6f033e214b9f90d79c3fe5c/packages/github/package.json#LL44C6-L44C43 Once that's updated, we can update this action.

As a workaround, you can use the request function directly.

await github.request('GET /repositories/{repository_id}/environments/{environment_name}/variables/{name}', {
  repository_id: 'REPOSITORY_ID',
  environment_name: 'ENVIRONMENT_NAME',
  name: 'NAME',
  headers: {
    'X-GitHub-Api-Version': '2022-11-28'
  }
})

Our API documentation has examples you can use: https://docs.github.com/en/rest/actions/variables?apiVersion=2022-11-28#get-an-environment-variable Just make sure to replace octokit with github, since this action exposes an authenticated Octokit instance as that github variable.

joshmgross avatar May 11 '23 14:05 joshmgross

Hey @joshmgross - following up here now that I have a better understanding of using the github-script action and how to use it.

Is there a good way to know which github.rest.actions.xxx methods are currently available in the latest github-script version since others require the update like you mentioned above?

joshft91 avatar May 18 '23 15:05 joshft91

Is there a good way to know which github.rest.actions.xxx methods are currently available in the latest github-script version since others require the update like you mentioned above?

Based on https://stackoverflow.com/questions/152483/is-there-a-way-to-print-all-methods-of-an-object, I printed out all of the available endpoints for github.rest.actions:

      - uses: actions/github-script@v6
        with:
          script: |
            for (const id in github.rest.actions) {
              try {
                if (typeof(github.rest.actions[id]) === "function") {
                  console.log(id);
                }
              } catch (err) {
                console.error(err);
              }
            }

https://github.com/joshmgross/actions-testing/actions/runs/5025713514/jobs/9013042586

Currently available endpoints
addCustomLabelsToSelfHostedRunnerForOrg
addCustomLabelsToSelfHostedRunnerForRepo
addSelectedRepoToOrgSecret
approveWorkflowRun
cancelWorkflowRun
createOrUpdateEnvironmentSecret
createOrUpdateOrgSecret
createOrUpdateRepoSecret
createRegistrationTokenForOrg
createRegistrationTokenForRepo
createRemoveTokenForOrg
createRemoveTokenForRepo
createWorkflowDispatch
deleteActionsCacheById
deleteActionsCacheByKey
deleteArtifact
deleteEnvironmentSecret
deleteOrgSecret
deleteRepoSecret
deleteSelfHostedRunnerFromOrg
deleteSelfHostedRunnerFromRepo
deleteWorkflowRun
deleteWorkflowRunLogs
disableSelectedRepositoryGithubActionsOrganization
disableWorkflow
downloadArtifact
downloadJobLogsForWorkflowRun
downloadWorkflowRunAttemptLogs
downloadWorkflowRunLogs
enableSelectedRepositoryGithubActionsOrganization
enableWorkflow
getActionsCacheList
getActionsCacheUsage
getActionsCacheUsageByRepoForOrg
getActionsCacheUsageForEnterprise
getActionsCacheUsageForOrg
getAllowedActionsOrganization
getAllowedActionsRepository
getArtifact
getEnvironmentPublicKey
getEnvironmentSecret
getGithubActionsDefaultWorkflowPermissionsEnterprise
getGithubActionsDefaultWorkflowPermissionsOrganization
getGithubActionsDefaultWorkflowPermissionsRepository
getGithubActionsPermissionsOrganization
getGithubActionsPermissionsRepository
getJobForWorkflowRun
getOrgPublicKey
getOrgSecret
getPendingDeploymentsForRun
getRepoPermissions
getRepoPublicKey
getRepoSecret
getReviewsForRun
getSelfHostedRunnerForOrg
getSelfHostedRunnerForRepo
getWorkflow
getWorkflowAccessToRepository
getWorkflowRun
getWorkflowRunAttempt
getWorkflowRunUsage
getWorkflowUsage
listArtifactsForRepo
listEnvironmentSecrets
listJobsForWorkflowRun
listJobsForWorkflowRunAttempt
listLabelsForSelfHostedRunnerForOrg
listLabelsForSelfHostedRunnerForRepo
listOrgSecrets
listRepoSecrets
listRepoWorkflows
listRunnerApplicationsForOrg
listRunnerApplicationsForRepo
listSelectedReposForOrgSecret
listSelectedRepositoriesEnabledGithubActionsOrganization
listSelfHostedRunnersForOrg
listSelfHostedRunnersForRepo
listWorkflowRunArtifacts
listWorkflowRuns
listWorkflowRunsForRepo
reRunJobForWorkflowRun
reRunWorkflow
reRunWorkflowFailedJobs
removeAllCustomLabelsFromSelfHostedRunnerForOrg
removeAllCustomLabelsFromSelfHostedRunnerForRepo
removeCustomLabelFromSelfHostedRunnerForOrg
removeCustomLabelFromSelfHostedRunnerForRepo
removeSelectedRepoFromOrgSecret
reviewPendingDeploymentsForRun
setAllowedActionsOrganization
setAllowedActionsRepository
setCustomLabelsForSelfHostedRunnerForOrg
setCustomLabelsForSelfHostedRunnerForRepo
setGithubActionsDefaultWorkflowPermissionsEnterprise
setGithubActionsDefaultWorkflowPermissionsOrganization
setGithubActionsDefaultWorkflowPermissionsRepository
setGithubActionsPermissionsOrganization
setGithubActionsPermissionsRepository
setSelectedReposForOrgSecret
setSelectedRepositoriesEnabledGithubActionsOrganization
setWorkflowAccessToRepository

joshmgross avatar May 19 '23 15:05 joshmgross

@joshmgross just wondering if there's any timeline on when the version would be updated?

subrasub avatar May 30 '23 18:05 subrasub

No timeline, I'm not sure if anything is tracking the update required to @actions/github in https://github.com/actions/toolkit

https://github.com/actions/github-script/issues/345#issuecomment-1544126809

joshmgross avatar Jun 01 '23 21:06 joshmgross

@joshmgross, I'm hitting some permissions errors hitting that specific endpoint. Any idea what permissions my job/workflow token needs to be able to have the environments:read scope?

I even tried with permissions: write-all set, but that didn't work. I'm assuming I'm gonna have to use a PAT if I want to call this?

eabrouwer3 avatar Jun 09 '23 15:06 eabrouwer3

Any idea what permissions my job/workflow token needs to be able to have the environments:read scope?

The environments permission is not available on the Actions token, so you'll need to use a PAT or separate GitHub App token.

https://docs.github.com/en/actions/security-guides/automatic-token-authentication#permissions-for-the-github_token

joshmgross avatar Jun 09 '23 21:06 joshmgross

@joshmgross, is there any special token handling needed to use github.request endpoint with github-script action?

When I used github.rest.actions.addSelectedRepoToOrgVariable in the github-script action, it encountered this error as expected, Error: Unhandled error: TypeError: github.rest.actions.addSelectedRepoToOrgVariable is not a function.

However, I tried using the github.request suggestion (with a token with org variable read/write permission), it encountered this error, Error: Unhandled error: SyntaxError: Invalid or unexpected token.

- name: add org variable 
  uses: actions/github-script@v6
  with:
    github-token: ${{ steps.generate_token.outputs.token }}
  script: |
    await github.request('PUT /orgs/{org}/actions/variables/{name}/repositories/{repository_id}', {
      org: 'my-org',
      name: 'my-var',
      repository_id: 'my-repoid',
      headers: {
        'X-GitHub-Api-Version': '2022-11-28'
      }
    });

To confirm the token isn't the actual issue, I tested successfully using similar token (with org secret read/write permission) with github.rest.actions.addSelectedRepoToOrgSecret endpoint without issue.

However, it encountered similar invalid token error when switched to using github.request('PUT /orgs/{org}/actions/secrets/{secret_name}/repositories/{repository_id}'.

travisgan avatar Oct 04 '23 00:10 travisgan

Disregard my previous comment. The invalid token error isn't related to the authentication token. Maybe during the code snippet copy for github.request function, some formatting / encoding got in the way. Rewriting it from scratch works fine.

travisgan avatar Oct 05 '23 20:10 travisgan