我想从google表单触发github工作流。
我首先使用curl测试github side,它工作得很好。现在我想用谷歌脚本做同样的事情。这是我的代码
const data = {"ref": "main"};
var options = {
method: "post",
header: {authorization: 'token <MY_TESTED_GITHUB_TOKEN>'},
payload: JSON.stringify(data)
};
var response = UrlFetchApp.fetch("https://api.github.com/repos/<ID>/<MY_REPO>/actions/workflows/new_request.yml/dispatches", options);
返回以下错误消息:
异常:https://api.github.com请求失败,返回代码404。截断的服务器响应:{"message":"Not Found","documentation_url":"https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"}(使用muteHttpExceptions选项检查完整的响应)
从你的错误信息中,我猜你想把下面的curl命令转换为Google Apps Script。Ref
curl -L
-X POST
-H "Accept: application/vnd.github+json"
-H "Authorization: Bearer <YOUR-TOKEN>"
-H "X-GitHub-Api-Version: 2022-11-28"
https://api.github.com/repos/OWNER/REPO/actions/workflows/WORKFLOW_ID/dispatches
-d '{"ref":"topic-branch","inputs":{"name":"Mona the Octocat","home":"San Francisco, CA"}}'
当我看到您显示的脚本时,似乎您的脚本的请求可能与上面的curl命令不同。在这种情况下,下面的示例脚本如何?
示例脚本:
在使用此脚本之前,请根据您的实际情况修改data
,url
和您的访问令牌。
function myFunction() {
const url = "https://api.github.com/repos/OWNER/REPO/actions/workflows/WORKFLOW_ID/dispatches";
const data = {"ref":"topic-branch","inputs":{"name":"Mona the Octocat","home":"San Francisco, CA"}};
const res = UrlFetchApp.fetch(url, {
method: "post",
headers: {
"Authorization": "Bearer <YOUR-TOKEN>",
"X-GitHub-Api-Version": "2022-11-28",
"Accept": "application/vnd.github+json",
},
payload: JSON.stringify(data)
});
console.log(res.getContentText())
}
- 我认为这个Google Apps Script的请求和上面的curl命令是一样的。但是,如果这不是你期望的结果,你能提供
I first tested github side using curl and it worked fine.
的curl命令吗?通过这个,我想修改脚本。
引用:
- 创建工作流调度事件 参数
- 获取(url)