使用GitHub Webhook在拉取请求中获取我所在工作的分支名称

3
我正在尝试在Git Hub上标记Pull Request时从WebHook触发器获取所有细节。尽管我使用payload获取了很多信息,但我真正需要的是工作的分支名称,该分支具有我打开的Pull Request。
在GitHub页面上,我看到: Pavel wants to merge 2 commits into master from <-- 我想以某种方式获取该分支名称。 谢谢!

请问你能截图一下你所说的内容以便更清晰地理解吗? - antzshrek
3个回答

4

虽然可能有更好的解决方法,但我是这样解决的:

“content”是通过WebHook接收到的有效载荷JSON。

“pull_request_url”是我正在处理的PR的URL。

“pr_content”是在调用PR URL时收到的JSON。

从那里,我只需从“pr_content” JSON中提取分支名称。

def get_branch_name(content):
    pull_request_url = content['pull_request']['_links']['self']['href']
    pr_content = get(pull_request_url)
    if pr_content.status_code == 200:
        branch_name = (pr_content.json()['head']['label']).split(":")[1]
    else:
        branch_name = 'master'
    return branch_name

2

0
以下方法适用于我。PS:在CloudBees Jenkins Enterprise 2.263.2.3-rolling上测试通过。
描述性流水线中,我在stages > stage块中使用以下代码。
    stage ('stage_name') {
        steps {
            echo 'hello from common section'
            echo "BRANCH_NAME: ${env.GIT_BRANCH.split("/")[1]}"
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接