项目作者: elwayman02

项目描述 :
Ember Data library for the GitHub API
高级语言: JavaScript
项目地址: git://github.com/elwayman02/ember-data-github.git
创建时间: 2015-04-26T05:01:42Z
项目社区:https://github.com/elwayman02/ember-data-github

开源协议:MIT License

下载


Ember Data Github

Build Status
Ember Observer Score
Code Climate

Ember Data abstraction for the GitHub REST API v3.

Installation

  1. ember install ember-data-github

Usage

You need to choose how you wish to authenticate your GitHub requests using OAuth. ember-data-github provides a simple
and direct mechanism that is specific to itself. Alternatively, you can use a more general authentication framework like
ember-simple-auth.

Authenticating Directly

If you already have a token to use the OAuth endpoints, such as a Personal access token, you must set the property
named githubAccessToken on github-session service with the currently logged in user’s GitHub access token.

Authenticating with ember-simple-auth

If you are using ember-simple-auth (ESA) to authenticate, perhaps with
torii and ESA’s torii-provider, you can authenticate by creating a github
authorizer and extending ember-data-github‘s adapter for each model you use. See the respective addon docs and
GitHub’s OAuth docs to set it up.

Once you have a token, the authorizer will look like

  1. // app/authorizers/github.js
  2. import { inject as service } from '@ember/service';
  3. import { isEmpty } from '@ember/utils';
  4. import Base from 'ember-simple-auth/authorizers/base';
  5. export default Base.extend({
  6. session: service(),
  7. authorize(sessionData, block) {
  8. if (this.get('session.isAuthenticated') && !isEmpty(sessionData.access_token)) {
  9. block('Authorization', `token ${sessionData.access_token}`);
  10. }
  11. }
  12. });

assuming access_token is the name of the property containing the token. This automatically injects the Authorization
header into the API requests using ESA mechanisms.

An extended adapter for github-user would look like

  1. // app/adapters/github-user.js
  2. import GitHubUserAdapter from 'ember-data-github/adapters/github-user';
  3. import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
  4. export default GitHubUserAdapter.extend(DataAdapterMixin, {
  5. authorizer: 'authorizer:github'
  6. });

Retrieving GitHub Data

Users

Get the current authenticated user
  1. this.get('store').findRecord('github-user', '#');
Get a single user
  1. this.get('store').findRecord('github-user', 'jimmay5469'); // get a user by user login
  2. this.get('store').findRecord('github-user', 917672); // get a user by user id

Repositories

Get
  1. this.get('store').findRecord('github-repository', 'jimmay5469/old-hash'); // get a repository by repository name
  2. this.get('store').findRecord('github-repository', 34598603); // get a repository by repository id
Get By User
  1. this.get('store').query('github-repository', { user: 'elwayman02' }); // get repositories owned by user
  2. this.get('store').query('github-repository', { user: 'elwayman02', type: 'all' }); // get all repositories for user
  3. this.get('store').query('github-repository', { user: 'elwayman02', sort: 'updated', direction: 'asc' }); // get repositories owned by user sorted by last updated, ascending
Repository Contents
Get

Note: At this time we only support getting file contents.

  1. this.get('store').queryRecord('github-repository-contents', { repo: 'jmar910/test-repo-yay', file: 'app.json' }); // get file contents from repo
Branches
List branches
  1. this.get('store').query('github-branch', { repo: 'jimmay5469/old-hash' });
Get
  1. this.get('store').findRecord('github-branch', 'jimmay5469/old-hash/branches/master'); // get a branch
  2. this.get('store').queryRecord('github-branch', { repo: 'jimmay5469/old-hash', branch: 'master' }); // get a specific branch
Releases
List releases for a repository
  1. this.get('store').query('github-release', { repo: 'jimmay5469/old-hash' });
Get a single release
  1. this.get('store').queryRecord('github-release', { repo: 'jimmay5469/old-hash', releaseId: 1 });
Commits
Compate two commits
  1. this.get('store').queryRecord('github-compare', { repo: 'jimmay5469/old-hash', base: '1234', head: '5678' });

Pull Requests

List pull requests
  1. this.get('store').query('github-pull', { repo: 'jimmay5469/old-hash' });
Get a single pull request
  1. this.get('store').queryRecord('github-pull', { repo: 'jimmay5469/old-hash', pullId: 1 });

GitHub Organizations

Get an organizaton
  1. this.get('store').findRecord('github-organization', { org: 'my-org' });
Get organization members
  1. this.get('store').query('github-members', { org: 'my-org' })

Git Blobs

Get a blob
  1. this.get('store').queryRecord('github-blob', { repo: 'jimmay5469/old-hash', sha: '47c5438403ca875f170db2aa07d1bfa3689406e3' });

Git Trees

Get a Tree
  1. this.get('store').queryRecord('github-tree', { repo: 'jimmay5469/old-hash', sha: '47c5438403ca875f170db2aa07d1bfa3689406e3' });

Testing with Mirage

This addon uses ember-cli-mirage in its tests. It is often beneficial for consuming apps to be able to re-use the factories and models defined in mirage, so if you would like to use these in your tests you can add the mirage-support object to your ember-cli-build.js file:

  1. module.exports = function(defaults) {
  2. let app = new EmberApp(defaults, {
  3. ...
  4. 'mirage-support': {
  5. includeAll: true
  6. }
  7. ...
  8. });
  9. return app.toTree();
  10. };

As long as ember-cli-mirage is not disabled, the files in this addon’s mirage-support directory will be merged with the consuming app’s namespace, and be made available to that mirage context.
The 'mirage-support' key has 3 options:

Key Type Description
includeAll Boolean If true, includes the full mirage-support tree, i.e. no-brainer just use it all.
exclude {Array of GlobStrings,RegExps,Functions} This value gets passed directly to broccoli-funnel, only if includeAll is specified. Allows for excluding certain files from import.
include {Array of GlobStrings,RegExps,Functions} Passed dirctly to broccoli-funnel. Allows to pick only certain files to be imported into app namespace.

Contributing

Installation

  • git clone git@github.com:elwayman02/ember-data-github.git
  • cd ember-data-github
  • yarn

Running

Running Tests

  • ember test – Runs the test suite on the current Ember version
  • ember test --server – Runs the test suite in “watch mode”
  • ember try:each – Runs the test suite against multiple Ember versions

Building

  • ember build

For more information on using ember-cli, visit https://ember-cli.com/.