我们创建了一个 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 应用程序安装具有范围权限。具体来说,虽然应用程序可能有权代表用户创建分叉、分支和提交,但如果没有被授予这些权限,它可能没有打开拉取请求或在存储库上执行某些操作的正确权限。权限。
确保您的 GitHub 应用程序具有在上游存储库上打开拉取请求的“所需权限”。您需要确保在 GitHub 应用程序的清单中授予以下权限:
pull requests
:
read and write
许可。这允许您的应用程序创建拉取请求。contents
:
read and write
许可。这允许您的应用程序将提交推送到分支。
导航到
Pull requests
Contents
已启用 Read & write
权限。
中安装 (evolvedbinary/prosemirror-lwdita
)。如果应用程序无权访问该特定存储库,它将无法针对它创建拉取请求。
确保 GitHub 应用程序安装在上游存储库(您尝试打开 PR 的存储库)上。
POST /app/installations/{installation_id}/access_tokens
) 的安装请求中,确保
安装 ID对应于有权访问上游存储库的安装。 如果您的应用程序安装在多个存储库中,则
installation_id
evolvedbinary/prosemirror-lwdita
) 的正确安装。
POST /app/installations/{installation_id}/access_tokens
生成的访问令牌的
范围为安装有权访问的存储库。确保您尝试打开 PR 的存储库在此令牌的范围内。 5. 分叉和用户协会
存储库(marmoure:patch-1
分支)创建一个 PR 到上游存储库。如果您的应用程序代表用户创建分叉,则该分叉还必须具有必要的权限。
marmoure
的身份执行操作,而不是分叉存储库的用户。您可以通过确保您的应用程序代表 分叉存储库 上的 安装 进行操作,但与拉取请求的 上游存储库 进行交互来解决此问题:
为有权访问marmoure/prosemirror-lwdita
)的安装生成访问令牌。打开对evolvedbinary/prosemirror-lwdita
) 的拉取请求。
// 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
});