Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

octokit.rest.actions not available #345

Open
tcarac opened this issue Mar 9, 2023 · 13 comments
Open

octokit.rest.actions not available #345

tcarac opened this issue Mar 9, 2023 · 13 comments
Labels
dependencies Pull requests that update a dependency file good first issue Good for newcomers

Comments

@tcarac
Copy link

tcarac commented Mar 9, 2023

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

@mknet3
Copy link

mknet3 commented Mar 10, 2023

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.

@INRIX-Iurii-Okhmat
Copy link

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.

@joshmgross joshmgross added dependencies Pull requests that update a dependency file good first issue Good for newcomers labels Apr 14, 2023
@alex529
Copy link

alex529 commented Apr 26, 2023

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)

@joshft91
Copy link

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?

@joshmgross
Copy link
Member

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.

@joshft91
Copy link

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?

@joshmgross
Copy link
Member

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

@subrasub
Copy link

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

@joshmgross
Copy link
Member

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

#345 (comment)

@eabrouwer3
Copy link

@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?

@joshmgross
Copy link
Member

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

@travisgan
Copy link

@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
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

9 participants