Github App访问令牌无法打开PR

问题描述 投票:0回答:1

我们创建了一个 GitHub 应用程序,它的功能之一是(代表 GitHub 用户)代表他们分叉一个(上游)存储库,创建一个分支,向该分支进行一些提交,然后从他们的分支发送 PR分叉回上游存储库。

对于我们的应用程序,我们遵循“作为应用程序安装进行身份验证”方法(请参阅:https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/ about-authentication-with-a-github-app),因为我们想要“将应用程序活动归因于应用程序”。

不幸的是,当我们的应用程序尝试针对上游存储库打开 PR 时,我们从 GitHub API 收到以下错误响应:“资源无法通过集成访问”。

其他应用程序,例如 Depenadabot(请参阅:https://github.com/apps/dependabot)似乎能够执行此操作,例如它打开此 PR 的位置:evolvedbinary/exist#27。

下面是一个代码片段,我们使用 JavaScript 和 GitHub 提供的 Octokit 库(参见:https://github.com/octokit)来尝试实现这一目标。

我们做错了什么?

    // get the access token for the installation
    const installationId = 0; // Your installation ID

    // Authenticate as the GitHub App using the JWT
    const jwttoken = ``

    const octokitApp = new Octokit({
      auth: jwttoken,
    });

    const response = await octokitApp.request('POST /app/installations/{installation_id}/access_tokens', {
      installation_id: installationId, // Your installation ID
    });

    const accessToken = response.data.token;

    const octokit = new Octokit({
      auth: accessToken
    });

    const response = await octokit.pulls.create({
      owner: 'evolvedbinary',
      repo: 'prosemirror-lwdita',
      title: 'Update README.md',
      body: `This pull request was created by **Petal-demo bot**`,
      head: 'marmoure:patch-1',
      base: 'main',
    });
github-api github-app
1个回答
0
投票

GitHub 应用程序安装具有范围权限。具体来说,虽然应用程序可能有权代表用户创建分叉、分支和提交,但如果没有被授予这些权限,它可能没有打开拉取请求或在存储库上执行某些操作的正确权限。权限。

要检查的关键事项:

1. 应用程序权限

确保您的 GitHub 应用程序具有在上游存储库上打开拉取请求的“所需权限”。您需要确保在 GitHub 应用程序的清单中授予以下权限:

  • pull requests
    read and write
    许可。这允许您的应用程序创建拉取请求。
  • contents
    read and write
    许可。这允许您的应用程序将提交推送到分支。
    
    
  • 要检查或修改这些权限,请转到 GitHub 应用程序的设置:

导航到
    GitHub 应用程序设置
  • > 权限和事件 确保
  • Pull requests
  • Contents
    已启用
    Read & write
    权限。
    
    
  • 2.
应用程序的存储库访问

GitHub 应用程序还需要在

上游存储库

安装 (evolvedbinary/prosemirror-lwdita)。如果应用程序无权访问该特定存储库,它将无法针对它创建拉取请求。

确认:

确保 GitHub 应用程序安装在上游存储库(您尝试打开 PR 的存储库)上。
  • 拥有上游存储库的用户或组织必须安装该应用程序并授予其访问权限。
  • 3.
安装范围

在生成访问令牌 (

POST /app/installations/{installation_id}/access_tokens

) 的安装请求中,确保

安装 ID
对应于有权访问上游存储库的安装。

如果您的应用程序安装在多个存储库中,则
    installation_id
  • 必须引用包含目标存储库 (
    evolvedbinary/prosemirror-lwdita
    ) 的正确安装。
    
    
  • 4.
代币范围

通过

POST /app/installations/{installation_id}/access_tokens

生成的访问令牌的

范围
为安装有权访问的存储库。确保您尝试打开 PR 的存储库在此令牌的范围内。 5.

分叉和用户协会

您似乎正在尝试从

forked

存储库(marmoure:patch-1 分支)创建一个 PR 到上游存储库。如果您的应用程序代表用户创建分叉,则该分叉还必须具有必要的权限。


确保拥有分叉的用户 (
    marmoure
  • ) 已授予您的 GitHub 应用程序代表他们行事的权限。如果应用程序对分叉存储库以及上游存储库具有适当的权限,则它只能推送分支并创建 PR。
    
    
  • 潜在的解决方案

如果所有权限均正确,但您仍然收到错误,则可能是您尝试以

GitHub 应用程序安装

的身份执行操作,而不是分叉存储库的用户。您可以通过确保您的应用程序代表 分叉存储库 上的 安装 进行操作,但与拉取请求的 上游存储库 进行交互来解决此问题:

为有权访问
    分叉存储库
  1. marmoure/prosemirror-lwdita)的安装生成访问令牌。
    打开对
  2. 上游存储库
  3. (evolvedbinary/prosemirror-lwdita) 的拉取请求。
    
    
  4. 以下是修改流程以从分叉打开 PR 的方法:

// Step 1: Authenticate as the GitHub App using the JWT const jwttoken = ``; // JWT token generated for the GitHub App const octokitApp = new Octokit({ auth: jwttoken, }); // Step 2: Generate the access token for the installation const installationId = 0; // The installation ID for the user who forked the repo const tokenResponse = await octokitApp.request('POST /app/installations/{installation_id}/access_tokens', { installation_id: installationId, // Installation ID for the user's fork }); const accessToken = tokenResponse.data.token; // Step 3: Use the access token to act as the installation const octokit = new Octokit({ auth: accessToken }); // Step 4: Create the pull request from the fork to the upstream repo const prResponse = await octokit.pulls.create({ owner: 'evolvedbinary', // The upstream repository owner repo: 'prosemirror-lwdita', // The upstream repository name title: 'Update README.md', body: `This pull request was created by **Petal-demo bot**`, head: 'marmoure:patch-1', // The branch from the forked repository base: 'main', // The base branch in the upstream repository });

	
© www.soinside.com 2019 - 2024. All rights reserved.