Как подключить github api java
Перейти к содержимому

Как подключить github api java

  • автор:

GitHub API for Java

This library defines an object oriented representation of the GitHub API. By "object oriented" we mean there are classes that correspond to the domain model of GitHub (such as GHUser and GHRepository ), operations that act on them as defined as methods (such as GHUser.follow() ), and those object references are used in favor of using string handle (such as GHUser.isMemberOf(GHOrganization) instead of GHUser.isMemberOf(String) )

The library supports both github.com and GitHub Enterprise.

Most of the GitHub APIs are covered, although there are some corners that are still not yet implemented.

Sample Usage

Authentication

The library allows connecting to GitHub via several different authentication mechanisms.

Programmatically

To connect via Username and Password (not recommended):

To connect via Personal access token:

To connect via JWT token as a GitHub App:

To connect via GitHub App installation token on behalf of a user or organization:

Property file

This library defines a common convention so that applications using this library will look at a consistent location. In this convention, the library looks at

/.github property file. The content of the files depends on the way you want this library to authenticate as shown below:

To connect via Username and Password (not recommended):

To connect via Personal access token:

To connect via Personal access token as a user or organization:

To connect via JWT token as a GitHub App:

/.github property file is properly configured, you can obtain a GitHub instance using:

Environmental variables

This library also allows developers to authenticate GitHub with environmental variables.

To connect via Username and Password (not recommended):

To connect via Personal access token:

To connect via Personal access token as a user or organization:

To connect via JWT token as a GitHub App:

Once exported, you can obtain a GitHub instance using:

Pluggable HTTP client

This library comes with a pluggable connector to use different HTTP client implementations through HttpConnector . In particular, this means you can use OkHttp, so we can make use of it’s HTTP response cache. Making a conditional request against the GitHub API and receiving a 304 response does not count against the rate limit.

The following code shows an example of how to set up persistent cache on the disk:

Как подключить github api java

This library defines an object oriented representation of the GitHub API. By «object oriented» we mean there are classes that correspond to the domain model of GitHub (such as GHUser and GHRepository ), operations that act on them as defined as methods (such as GHUser.follow() ), and those object references are used in favor of using string handle (such as GHUser.isMemberOf(GHOrganization) instead of GHUser.isMemberOf(String) )

The library supports both github.com and GitHub Enterprise.

Most of the GitHub APIs are covered, although there are some corners that are still not yet implemented.

Sample Usage

Authentication

The library allows connecting to GitHub via several different authentication mechanisms.

Programmatically

To connect via Username and Password (not recommended):

To connect via Personal access token:

To connect via JWT token as a GitHub App:

To connect via GitHub App installation token on behalf of a user or organization:

Property file

This library defines a common convention so that applications using this library will look at a consistent location. In this convention, the library looks at

/.github property file. The content of the files depends on the way you want this library to authenticate as shown below:

To connect via Username and Password (not recommended):

To connect via Personal access token:

To connect via Personal access token as a user or organization:

To connect via JWT token as a GitHub App:

/.github property file is properly configured, you can obtain a GitHub instance using:

Environmental variables

This library also allows developers to authenticate GitHub with environmental variables.

To connect via Username and Password (not recommended):

To connect via Personal access token:

To connect via Personal access token as a user or organization:

To connect via JWT token as a GitHub App:

Once exported, you can obtain a GitHub instance using:

Pluggable HTTP client

This library comes with a pluggable connector to use different HTTP client implementations through HttpConnector . In particular, this means you can use OkHttp, so we can make use of it’s HTTP response cache. Making a conditional request against the GitHub API and receiving a 304 response does not count against the rate limit.

The following code shows an example of how to set up persistent cache on the disk:

How to Improve Your Java Workflow with GitHub API

header - How to Improve Your Java Workflow with GitHub API

By now, you have probably heard about the GitHub API. Or you know what GitHub is and you know what an API is. Or you just Googled it, that’s okay too.

From automating GitHub processes to just being a command line fanatic, the GitHub API can be used in many different ways. By taking a brief look at the documentation, you can see the GitHub API can do just about everything and more.

Stay tuned to find out many cool features you probably didn’t know the GitHub API has and make your automation process a walk in the park.

Read to the end for a quick and easy tutorial!

The Convenience of GitHub API

As many enterprises use GitHub in their everyday work, it may not come as a surprise to learn the base URL can be different from those which are public repositories.

The most difficult part in the entire process is actually this — find out what the base URL of your enterprise is. Seems easy, right? The base URL of your enterprise can look something like https://<your.enterprise>.githubname.com/api/v3 .

The v3 in this case stands for version 3, the current version of the GitHub API,while the public GitHub base URL is https://api.github.com .

Once you have found the base URL, keep it somewhere safe — perhaps the part of your brain where it will never be forgotten, you’ll most likely need to create a Personal Access Token (PAT). This can be done by following the official GitHub docs tutorial. If you already have a token, you can just use that one.

You’ll need this token for any kind of request that requires authorization, especially if you want to do more complex tasks than just calling a few GET requests against a public GitHub repository. For these requests, you’ll need to add a Header to the request as such:

Now you’re ready to dive into the fun world of making requests.

SHA-1 values

To make everything easier to find and understand, commits in git have their own Secure Hash Algorithm 1 (SHA-1) value. Other objects in GitHub have sha tokens that point back to the commit, so we can identify the objects using these values.

The easiest way to obtain an SHA-1 value for an object is to store the response when creating said object.

The other way is to fish out the value through a GET request against the object. This way is slightly more difficult, as you would need to know specific information such as an exact pull request number, commit number, or branch name.

Creating and deleting references

Any self-respecting software engineer will tell you to never simply push your code to the master branch. Instead you need to manually create a new branch every time run the code you’d like to automate. That seems quite upsetting.

Just as you’re wondering if there’s even hope to ever sit back and sip your coffee while your code just does its thing, the GitHub API comes to the rescue.

You can create branches with one request:

That has required 2 body parameters:

  • ref (string) — the name of the branch you’d like to create (i.e refs/heads/[your_branch_name])
  • sha (string) — sha token value of the branch you’d like to create a branch on

After hitting send, your new branch will appear like clockwork.

Getting tired of your new snazzy branch? Has it served its purpose? Perhaps you named it incorrectly? Fear not, you can also delete the reference without leaving a trace through the GitHub API. All you’ll need is the name of the branch you just created.

Poof, gone. Nobody will ever need to know.

Creating and updating content

Say you’re using a dependency in a project and you always want to have the latest version on hand. Why waste your valuable time on doing things manually, when you can just click one button and have a computer update the dependencies for you? I mean, that’s where we’re headed with automation, right?

Yet again, the GitHub API can create or update content with a simple request.

The request will create a commit. This time, you’ll have 2 required body parameters (3 if you’re looking to update an existing file):

  • message (string) — the commit message
  • content (string) — the content of the entire file in Base64 encoding. You can use the built-in Java Base64 functionality.
  • sha (string) — the blob SHA of the file being replaced (necessary only if updating a file)

There are a few optional parameters, but if you feel like using your flashy new branch, you can add an extra parameter:

  • branch (string) — the name of your branch

A commit will have been created and your file successfully created/updated. You can thank the GitHub API later.

Manage pull requests (PRs)

As you may have guessed by now, the GitHub API can handle PRs without you having to leave the comfort of your command line or your favourite IDE.

The GitHub API can open PRs, merge them, add reviews to them and so on, but out of the many capabilities, a favorite task has to be- checking their statuses.

Depending on where you work, the PRs may have several checks that have to pass before they can be merged to the master branch. Imagine having a process automated, but not being able to merge your branch, because it’s just not ready yet.

Our superhero of the day can and will assist you. You can use this request to check the status of your PR.

In this case, the ref would be the SHA of one of the commits in your PR.

The status of a PR can either be pending, success or failure. Adding a simple while loop and checking in every now and then will allow your code to keep running after a small wait for your PR to be in the success status.

Let’s Get Started

After all of that reading, it’s time for you to warm up your fingers with a quick exercise. We’ll create a Java project that calls the GitHub API to create a branch, create a file, create a pull request, merge the pull request and delete the branch.

Setup

If at any point you are having any trouble, you can refer to the working code on my personal GitHub.

In this example, I will be creating my project as a Maven project, so I could use third party libraries without having to find them, download them and add them to my classpath.

Using JetBrains IntelliJ, select New Project.

From the dropdown menu on the left, select Maven.

new project on maven

Choose your cloned repository and name the project as you wish. For the purpose of this article, I’m naming the project as “github-api-test”, as seen in the screenshot below:

defining the title and project details for github-api-test

Your project structure should look like this. You can take a look at the pom.xml file and get acquainted. Also set the maven compiler version here as we will be using Java 11 features:

screenshot of the pom.xml file for the github-api-test

Start by creating a new Java class like so:

screenshot of the github-api-test project directory structure

Before diving into everything, we can make our lives easier by preparing 4 different requests: GET, PUT, POST and DELETE — these requests will return us the response body. To get a better understanding of this, you can check out 5 ways to make HTTP requests in Java. We will be reusing these methods to avoid writing duplicate code. To keep things simple and maintainable, we’ll have a few constants — the base URL of your GitHub and your PAT. We will also be adding an object mapper, more about this in Three ways to use Jackson for JSON in Java. Add these to the very beginning of your class.

GitHub Integration using Java GitHub

tutorials → integrations → GitHub Integration using Java

  • Activity Streams
  • Android Chat App
  • Coffee Order System
  • Collaborative Post-It Board
  • IoT Light Sensor
  • Music Collection
  • Pizza Delivery Tracker
  • Realtime Cart
  • Realtime Chat in Browser
  • Comment Feed using Vue
  • Realtime Flight Tracker
  • Realtime Todo List
  • Android
  • Angular 2
  • AngularJS
  • C++
  • HTTP
  • Ionic 2
  • Java
  • JavaScript
  • Node
  • Preact
  • React
  • React Native
  • Swift
  • VueJS
  • Authentication
  • Email Auth
  • HTTP Webhook Auth
  • Open Auth
  • Features
  • Events
  • Remote Procedure Calls
  • General
  • Handling Data Conflicts
  • Security Overview
  • Permissioning
  • User-specific data
  • Valve advanced
  • Valve Realtime
  • Valve simple
  • Realtime Database
  • Anonymous Records
  • Lists
  • Records
  • AWS Lambda
  • AWS SNS
  • GitHub
  • Mailchimp
  • Nexmo
  • Pusher
  • Sendgrid
  • Slack
  • Twilio
  • WebRTC
  • WebRTC 03: Audio & Video
  • WebRTC 01: Data Channels
  • WebRTC 06: File Transfer
  • WebRTC 02: Full Mesh
  • WebRTC: in production
  • WebRTC: Fundamentals & Concepts
  • WebRTC 06: Screen Sharing
  • WebRTC 05: Video Manipulation
  • Realtime Database
  • Modelling relational data
  • Realtime Events
  • Live Progress Bar

In this post, we’ll be building a simple GitHub issue tracker similar to waffle.io. It will show active issues on a project with specific workflow labels, and it will update in real time as issues are created and updated on GitHub.

This post will focus on building a connector using deepstream’s Java API and Kohsuke Kawaguchi’s GitHub API for Java. I will assume a basic understanding of Java. If you haven’t already, take a look at Getting Started with Java. For brevity, we won’t cover how to build the React.js frontend, but feel free to take a look at the one provided. If you’d like to read more about integrating deepstream with React.js, read getting started with React.

Setting Up

If you’d like to follow along, you can clone the completed project on GitHub.

Create a free account and get your API key

Create an application through the deepstreamHub dashboard, and make a note of the corresponding application url.

Within the client directory, run npm install to install the client dependencies, then run python -m SimpleHTTPServer to start a test server. You should then be able to view the client at localhost:8000.

Create a GitHub personal access token with repository access. Then create a file

It’s also possible to put your GitHub credentials in this file to avoid this step — see here.

I recommend setting up a test GitHub repository with some issues, and labeling some of them with ‘roadmap’, ‘ready’, ‘in progress’, ‘awaiting review’, or ‘in review’. It’s also a good idea to give those labels some nice colors — we’ll be using those later on.

Install ngrok which will allow us to listen for external connections from the webhook we’ll use later on. Start it using ngrok http 8080 , and make note of the forwarding address.

We’re using Gradle to manage dependencies.

Project Setup

In our main method, we set the details of our local deepstream server, the webhook URI given when you start ngrok, and the GitHub repository that we’re getting our issues from.

You’ll need to update the WEBHOOK_URI , DEEPSTREAM_URI and GITHUB_REPO environment variables in your build system to get the connector to run locally.

Setting up API Objects

We first setup a connection to our deepstream client.

. then setup a connection to GitHub.

Initial State

Our connector will need to fetch any open issues from GitHub’s API and create corresponding records in deepstream for rendering in the client.

Interesting Labels

We only care about certain labels, so we’ll create a list of these and add them to a deepstream list.

Issue Lists

Now we can start inserting issues into lists based on which labels they have.

In our initializeIssueLists() method, we make deepstream lists for each of the labels and ensure that they’re empty.

We get a list of open issues from GitHub.

Then we add each issue to all the lists for each label it has.

However, we’re only interested in some of the labels. We also want to create a record with our issue id to store the issue data.

We call this method from the constructor.

Coloring Labels

GitHub allows each label to have a color, so we get that from the API and pass it to the client for rendering.

Inside our setupIssueColors method, we simply create a deepstream record that maps label names to hex colors.

Realtime Updates

We use the GitHub webhook API to receive updates when issues are modified.

Event Listener

First, we start a server to listen for webhook events.

Our startServer() method instantiates the RequestHandler, which we’ll describe shortly.

Subscribe to Issue Events

So that we are notified when the issues are modified, we subscribe through the GitHub webhook API, specifying GHEvent.ISSUES , as this is the only event we’re interested in.

Handling Events

Events are handled in the RequestHandler.handle() method. GitHub IssuesEvents carry a JSON payload that gives details of the issue that triggered the event.

The ‘action’ field shows what caused the event. The ones we’re interested in are label modifications and edits.

When an issue is edited, we update the corresponding record.

When an issue is labeled, we add the issue to the corresponding list.

Likewise, we remove issues from lists when they are unlabeled.

Feel free to look through the rest of the code on GitHub. If you want a challenge, perhaps try to allow moving cards on the frontend and modify the corresponding issues on GitHub!

deepstreamHub is funded with resources from the Pro FIT program. The Pro FIT project is co-financed with funds from the European Fund for Regional Development (EFRE) with the goal to research, develop and market enterprise-ready deepstreamHub features.

Учебник. Как реализовать Java OAuth 2.0 для входа в GitHub и Google

Герцог-OAuth-ява-ключи

Одна из последних функций, которую мы добавили в Takipi, – это сторонняя регистрация. Если вы ленивы, как и я, то вы, вероятно, предпочитаете пропустить заполнение форм и создание новых паролей . Когда доступно, многие люди предпочитают сторонний вход, если запрашиваемые разрешения не являются навязчивыми – никто не хочет, чтобы случайные записи отображались на их стене Facebook, поэтому такой доступ включает только пользователя Основная информация, как имя и адреса электронной почты. В этом посте вы получите краткий обзор того, как мы реализовали это в Takipi, как вы можете использовать стороннюю регистрацию в своем приложении, а также несколько полезных советов, которые помогут вам сэкономить драгоценное время, если вы решите интегрировать это с вашим собственным приложением.

Новая статья: Как реализовать Java OAuth 2.0 для входа в систему с помощью GitHub и Google http://t.co/20Hn59dCtf pic.twitter.com/3tNNwPKjET

– Такипи (@takipid) 14 мая 2015 г.

Оказалось, что в нашей маленькой вселенной я не одинок в этом, и многие из наших пользователей чувствуют то же самое. Поскольку мы являемся стартапом инструментов разработчика, использование входа в GitHub и Google от сторонних разработчиков имело смысл. Помимо удобства для ваших пользователей, использование сторонних сервисов может повысить их безопасность. Поскольку Google и GitHub используют двухфакторную аутентификацию (2FA), ваше приложение получает этот уровень безопасности «бесплатно». В нашем случае мы уже включили 2FA, поэтому я думаю, что это делает нас… 3FA & # 55357; & # 56859;

Предварительный просмотр макета для нашей новой домашней страницы с помощью сторонних кнопок входа

Предварительный просмотр макета для нашей новой домашней страницы с помощью сторонних кнопок входа

Чтобы увидеть, как это работает на практике, вы можете проверить эту страницу, которую мы использовали для внутреннего тестирования. Это полностью работает, но обновление пока не доступно на сайте. Здесь у вас есть редкая возможность одним из первых использовать его ��

Напомни мне, как снова работает OAuth 2.0?

Процесс входа в OAuth выглядит следующим образом: пользователь заходит на ваш сайт, нажимает кнопку «Войти с помощью чего угодно» и перенаправляется на страницу разрешений. Страница разрешений поступает от Whither ™, и когда они одобряют запрашиваемые вами разрешения, Whither ™ отправляет им токен, который его браузер затем отправляет на серверную часть вашего приложения. Как только у вас есть токен, вы отправляете его обратно в Wh Wh ™ для проверки и, если он проверен – вы получаете доступ к данным, для которых вам были предоставлены разрешения.

Библиотека Google OAuth2.0

Для реализации стороннего входа на нашем сайте мы использовали клиентскую библиотеку API Google для Java. В нем также есть Jackson2, ProtoBuf и все другие полезные утилиты, которые появляются в топ-100 библиотек, которые используются в лучших Java-проектах GitHub . Это был довольно простой выбор для нас, так как мы уже использовали эту библиотеку для других целей, и, честно говоря, библиотека Google появилась как первый результат на … Google. Подготовьте свои шляпы из фольги .

Реализация входа с помощью Google

Это была легкая часть путешествия; документация на конце Google была ясна и по существу. Так как это их собственная библиотека, они также абстрагируют части процедуры и обрабатывают ее за кулисами, что делает ее еще более простой в реализации. Первым шагом будет создание проекта на консоли разработчика Google, где вы сможете зарегистрировать свое приложение и настроить страницу разрешений.

Страница разрешений Google - настроенная для Takipi

Страница разрешений Google – настроенная для Takipi

Теперь вернемся к Java. В основе этого, процедура сводится к нескольким простым шагам без сюрпризов. Сначала мы создаем и выполняем запрос для получения GoogleTokenResponse, чтобы мы могли проверить токен, полученный от пользователя. Затем мы используем этот ответ для создания GoogleCredential, который позволяет нам вызывать getAccessToken () и возвращать информацию о пользователе в формате JSON:

Использование API GitHub в приложении

Узнайте, как настроить приложение для прослушивания событий и использовать библиотеку Octokit для выполнения операций REST API.

Это руководство поможет вам создать приложение GitHub и запустить его на сервере. Приложение, которое вы создаете, добавит метку ко всем новым проблемам, открытым в репозитории, где оно установлено.

Этот проект позволит вам выполнить следующие действия:

  • программирование приложения для ожидания передачи данных для событий;
  • использование библиотеки Octokit.rb для выполнения операций REST API.

Примечание. В этом руководстве демонстрируется процесс разработки приложений с использованием языка программирования Ruby. Однако существуют и другие способы применения Octokit. Если вы предпочитаете JavaScript, то для разработки приложений GitHub можно использовать Probot и Node.js.

После выполнения этих шагов вы будете готовы к разработке других видов интеграции с помощью полного набора API-интерфейсов GitHub. Вы можете получить для изменения рабочие примеры приложений в GitHub Marketplace и Работает с приложениями GitHub.

Могут быть полезны базовые знания в следующих областях:

Но вы можете работать, имея любой уровень опыта. Мы будем ссылаться на необходимую информацию на этом пути!

Перед началом работы сделайте следующее:

В каталоге вы найдете файл template_server.rb с кодом шаблона, который будет использоваться в этом кратком руководстве, и файл server.rb с готовым кодом проекта.

Выполните действия, описанные в кратком руководстве Настройка среды разработки, чтобы настроить и запустить сервер приложений template_server.rb . Если вы ранее выполнили инструкции из краткого руководства по приложению GitHub, отличного от руководства Настройка среды разработки, зарегистрируйте новое приложение GitHub и запустите новый канал Smee, который будет использоваться с этим кратким руководством.

В этом кратком руководстве содержится тот же код template_server.rb , что и в кратком руководстве Настройка среды разработки. Примечание. Следуя инструкциям из краткого руководства Настройка среды разработки, обязательно используйте файлы проекта, включенные в репозиторий для применения API GitHub в приложении.

Если у вас возникли проблемы с настройкой шаблона приложения GitHub, ознакомьтесь с разделом Устранение неполадок.

Теперь, когда вы знакомы с кодом template_server.rb , вы можете создать код, который автоматически добавляет метку needs-response ко всем проблемам, открытым в репозитории, где установлено приложение.

Файл template_server.rb содержит код шаблона приложения, который еще не был настроен. В этом файле вы увидите код заполнителя для обработки событий веб-перехватчика и другой код для инициализации клиента Octokit.rb.

Примечание. template_server.rb содержит множество комментариев к коду, которые дополняют это руководство и объясняют дополнительные технические сведения. Чтобы получить общие сведения о работе кода, вы можете ознакомиться с комментариями в этом файле, прежде чем продолжить работу с данным разделом.

Окончательный настроенный код, который вы создадите в конце этого руководства, приведен в server.rb . Однако попробуйте дойти до конца, чтобы ознакомиться с ним!

Ниже приведены шаги, которые необходимо выполнить для создания первого приложения GitHub.

Шаг 1. Обновление разрешений приложения

При первой регистрации приложения вы приняли разрешения по умолчанию, а это означает, что у приложения нет доступа к большинству ресурсов. В этом примере приложению потребуется разрешение на чтение проблем и запись меток.

Чтобы обновить разрешения приложения:

  1. Выберите приложение на странице параметров приложения и щелкните Разрешения и веб-перехватчики на боковой панели.
  2. В разделе «Разрешения» откройте пункт «Проблемы» и выберите Чтение и запись в раскрывающемся списке «Доступ» рядом с ним. В описании говорится, что этот параметр предоставляет разрешения для доступа как к проблемам, так и к меткам, а именно это вам и нужно.
  3. Чтобы подписаться на событие, выберите Проблемы в разделе «Подписка на события».
  4. Нажмите Сохранить изменения в нижней части страницы.
  5. Если вы установили приложение в своей учетной записи, проверьте свой почтовый ящик и перейдите по ссылке, чтобы принять новые разрешения. Каждый раз, когда вы изменяете разрешения или веб-перехватчики приложения, пользователи, которые установили приложение (включая вас самих), должны принять новые разрешения, прежде чем изменения вступят в силу. Чтобы принять новые разрешения, можно также перейти на страницу установки и щелкнуть «Настроить» рядом с приложением. В верхней части страницы появится баннер, сообщающий, что приложение запрашивает различные разрешения. Щелкните «Сведения» и нажмите «Принять новые разрешения».

Отлично! Ваше приложение имеет разрешение на выполнение необходимых задач. Теперь вы можете добавить код для его работы.

Шаг 2. Добавление средств обработки событий

Первое, что нужно сделать вашему приложению, — ожидать передачи данных для новых открытых проблем. Теперь, когда вы подписались на событие Issues (Проблемы), вы начнете получать веб-перехватчик issues , который активируется при возникновении определенных действий, связанных с проблемами. Этот тип события можно отфильтровать для конкретного действия в коде.

GitHub отправляет полезные данные веб-перехватчика в виде запросов POST . Так как вы перенаправили полезные данные веб-перехватчика Smee в http://localhost/event_handler:3000 , сервер получит полезные данные запроса POST для маршрута post ‘/event_handler’ .

Пустой маршрут post ‘/event_handler’ уже включен в файл template_server.rb , который вы скачали в разделе предварительных требований. Пустой маршрут выглядит следующим образом:

Используйте этот маршрут для обработки события issues , добавив следующий код:

Каждое событие, которое отправляет GitHub, включает в себя заголовок запроса с именем HTTP_X_GITHUB_EVENT , который указывает тип события в запросе POST . Сейчас вас интересуют только типы событий issues . Каждое событие имеет дополнительное поле action , указывающее тип действия, которое активировало события. Для issues поле action может быть assigned , unassigned , labeled , unlabeled , opened , edited , milestoned , demilestoned , closed или reopened .

Чтобы протестировать обработчик событий, попробуйте добавить временный вспомогательный метод. Обновление выполните позже при работе с разделом Добавление средств обработки меток. Теперь в раздел кода helpers do добавьте следующий код. Новый метод можно поместить выше или ниже любого другого вспомогательного метода. Порядок не имеет значения.

Этот метод получает полезные данные события в формате JSON в качестве аргумента. Это означает, что вы можете анализировать полезные данные в методе и детализировать все необходимые данные. Рекомендуем проверить все полезные данные: попробуйте изменить logger.debug ‘An issue was opened! на logger.debug payload . Отображаемая структура полезных данных должна соответствовать тому, что показано в issues документации по событиям веб-перехватчика.

Отлично! Пришло время проверить изменения.

Примечание. Прежде чем тестировать изменения, необходимо перезапустить сервер Sinatra. Введите Ctrl-C , чтобы остановить сервер, а затем снова выполните ruby template_server.rb . Если вы не хотите делать это при каждом изменении в коде приложения, можно использовать перезагрузку.

В браузере перейдите в репозиторий, в котором установлено приложение. Откройте новую проблему в этом репозитории. В проблеме можете указать все, что хотите. Это просто для тестирования.

Когда вы вернетесь к терминалу, то увидите сообщение в выходных данных: An issue was opened! Congrats! (Поздравляем!) Вы добавили обработчик событий в приложение. ��

Шаг 3. Создание новой метки

Хорошо, ваше приложение может определить открытие проблем. Теперь вы хотите добавить метку needs-response к новой открытой проблеме в репозитории, где установлено приложение.

Перед добавлением метки в любое место необходимо создать пользовательскую метку в репозитории. Это нужно сделать только один раз. Для целей этого руководства создайте метку вручную на сайте GitHub. В репозитории щелкните Проблемы, затем Метки, а после этого выберите Новая метка. Назовите новую метку needs-response .

Совет. Было бы замечательно, если бы ваше приложение могло программно создать метку, не правда ли? Это возможно! Попробуйте добавить код самостоятельно после завершения действий, описанных в этом руководстве.

Теперь, когда есть метка, вы можете запрограммировать приложение, чтобы использовать REST API для добавления метки к любой только что открытой проблеме.

Шаг 4. Добавление средств обработки меток

Поздравляем! Вы добрались до последнего шага: добавление средства обработки меток в приложение. Для этой задачи необходимо использовать библиотеку Octokit.rb Ruby.

В документации по Octokit.rb найдите список методов для метки. Необходимый метод: add_labels_to_an_issue .

Вернитесь к template_server.rb и найдите метод, определенный ранее:

В документации по add_labels_to_an_issue указано, что вам потребуется передать три аргумента в этот метод:

  • репозиторий (строка в формате «owner/name» );
  • номер проблемы (целое число);
  • метки (массив).

Вы можете анализировать полезные данные, чтобы получить как репозиторий, так и номер проблемы. Так как имя метки всегда будет одинаковым ( needs-response ), его можно передать в виде жестко заданной строки в массиве меток. После объединения этих составляющих обновленный метод может выглядеть следующим образом:

Попробуйте открыть новую проблему в тестовом репозитории и посмотрите, что произойдет! Если ничего не произойдет сразу, попробуйте обновить.

В терминале вы не увидите много, но заметите, что пользователь-бот добавил метку к проблеме.

Примечание. Когда приложения GitHub выполняют действия через API, например добавление меток, GitHub показывает эти действия как выполняемые учетными записями ботов. Дополнительные сведения см. в разделе Различия между приложениями GitHub и приложениями OAuth.

Если удалось, поздравляем! Вы успешно создали рабочее приложение! ��

Окончательный код можно увидеть в server.rb в репозитории шаблонов приложений.

В разделе Дальнейшие действия приведены идеи о том, что можно еще сделать.

Ниже указаны некоторые распространенные проблемы и предлагаемые решения. Если у вас возникнут другие проблемы, вы можете обратиться за помощью или советом в Обсуждения API и интеграции в сообществе GitHub.

Вопрос. Что делать, если мой сервер не ожидает передачи данных событий? Клиент Smee работает в окне терминала, и я отправляю события на сайте GitHub.com, открыв новые проблемы, но не вижу выходных данных в окне терминала, где работает сервер.

Ответ. Возможно, у вас неправильно указан домен Smee в параметрах приложения. Перейдите на страницу параметров приложения и дважды проверьте поля, отображаемые в разделе «Настройка среды разработки для создания приложения GitHub». Убедитесь, что домен в этих полях соответствует домену, который вы использовали в smee -u <unique_channel> команде autoTITLE.

Вопрос. Что делать, если мое приложение не работает? Я открыл новую проблему, но даже после обновления она без метки.

Ответ. Убедитесь, что все из приведенных ниже условий выполняются:

  • Вы установили приложение в репозитории, в котором открываете проблему. в окне терминала. без ошибок в другом окне терминала.
  • Приложение имеет разрешения на чтение и запись для проблем и подписку на события проблем.
  • Вы проверили сообщение электронной почты после обновления разрешений и приняли новые разрешения.

После прохождения этого руководства вы узнали об основных стандартных блоках для разработки приложений GitHub! Для этого вы:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *