博客
关于我
6.5 GitHub - 脚本 GitHub
阅读量:92 次
发布时间:2019-02-26

本文共 1966 字,大约阅读时间需要 6 分钟。

GitHub脚本与API

1. 钩子与服务

GitHub 的钩子系统是其最灵活的功能之一,允许开发者在仓库事件发生时触发自定义操作。服务则为钩子提供了更具体的处理方式,例如配置持续集成工具(如Jenkins)或问题追踪系统。

在“Webhooks and Services”标签下,可以找到服务与钩子的配置区域。选择“Add Service”后,可以看到类似电子邮件配置的界面。例如,设置邮件服务时,每次仓库推送都会向指定邮箱发送通知。

2. 钩子的深入使用

对于更复杂的需求,可以使用钩子系统。通过指定一个URL,GitHub会在特定事件(如push事件)发生时发送HTTP请求。例如,可以设置一个简单的web服务监听钩子请求,并根据需要执行操作。

3. GitHub API

GitHub API为开发者提供了强大的操作能力。通过API,可以实现许多自动化任务,如创建或修改问题、Pull Request、搜索仓库内容等。

API基础用途

  • 获取用户信息:可以通过curl命令获取用户详细信息,例如:

    curl https://api.github.com/users/schacon

    返回结果包含用户的登录名、ID、头像链接等信息。

  • 获取Markdown渲染:使用API渲染Markdown文件,例如获取Java.gitignore模板:

    curl https://api.github.com/gitignore/templates/Java

    返回的内容包括忽略项列表。

API授权

要执行需要授权的操作(如在问题上评论),需要使用访问令牌。访问令牌可以在“Applications”标签下生成,确保令牌的作用域明确。例如,发送评论可以通过以下命令实现:

curl -H "Content-Type: application/json" \     -H "Authorization: token TOKEN" \     --data '{"body":"A new comment, :+1:"}' \     https://api.github.com/repos/schacon/blink/issues/6/comments

修改Pull Request状态

通过API可以动态更新Pull Request的状态。例如,检查提交是否签名:

require 'httparty'require 'sinatra'require 'json'post '/payload' do  push = JSON.parse(request.body.read)  repo_name = push['repository']['full_name']  push["commits"].each do |commit|    if /Signed-off-by/.match(commit['message'])      state = 'success'      description = 'Successfully signed off!'    else      state = 'failure'      description = 'No signoff found.'    end    status_url = "https://api.github.com/repos/#{repo_name}/statuses/#{commit['id']}"    status = {      "state" => state,      "description" => description,      "target_url" => "http://example.com/signoff",      "context" => "validate/signoff"    }    HTTParty.post(status_url,       :body => status.to_json,      :headers => {        'Content-Type' => 'application/json',        'User-Agent' => 'tonychacon/signoff',        'Authorization' => "token #{ENV['TOKEN']}"      }    )  endend

4. Octokit库

对于需要更方便地使用GitHub API的开发者,Octokit是一个开源库,提供了对GitHub API的封装调用,支持多种语言,包括Ruby、Go等。通过Octokit,可以更加简洁地实现各种GitHub操作。

转载地址:http://dsg.baihongyu.com/

你可能感兴趣的文章
Orderer节点启动报错解决方案:Not bootstrapping because of 3 existing channels
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>
Orleans框架------基于Actor模型生成分布式Id
查看>>
SQL-36 创建一个actor_name表,将actor表中的所有first_name以及last_name导入改表。
查看>>
ORM sqlachemy学习
查看>>
Ormlite数据库
查看>>