From 006ff7fd620050c738b6ee6eeb41f03ffab92a6a Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 15 Jul 2020 20:34:01 +0800 Subject: [PATCH 1/2] ADD devops builds's api --- app/controllers/dev_ops/builds_controller.rb | 8 ++-- .../dev_ops/cloud_accounts_controller.rb | 9 ++-- app/libs/dev_ops/drone/request.rb | 42 +++++++++---------- app/models/dev_ops/cloud_account.rb | 2 +- app/models/repository.rb | 2 +- config/routes.rb | 3 +- 6 files changed, 30 insertions(+), 36 deletions(-) diff --git a/app/controllers/dev_ops/builds_controller.rb b/app/controllers/dev_ops/builds_controller.rb index 0cf160a1..2474b113 100644 --- a/app/controllers/dev_ops/builds_controller.rb +++ b/app/controllers/dev_ops/builds_controller.rb @@ -1,6 +1,6 @@ -class DevOps::BuildsController < ApplicationController +class ::DevOps::BuildsController < ApplicationController before_action :require_login - before_action :find_project + before_action :find_repo def index cloud_account = @repo.dev_ops_cloud_account @@ -37,7 +37,7 @@ class DevOps::BuildsController < ApplicationController end private - def find_project - @repo = Repository.find params[:id] + def find_repo + @repo = ::Repository.find params[:id] end end diff --git a/app/controllers/dev_ops/cloud_accounts_controller.rb b/app/controllers/dev_ops/cloud_accounts_controller.rb index 5acc6934..ca506fa4 100644 --- a/app/controllers/dev_ops/cloud_accounts_controller.rb +++ b/app/controllers/dev_ops/cloud_accounts_controller.rb @@ -14,12 +14,12 @@ class DevOps::CloudAccountsController < ApplicationController logger.info "######### create_params: #{create_params}" - if cloud_account = @project.dev_ops_cloud_account + if cloud_account = @repo.dev_ops_cloud_account cloud_account else cloud_account = DevOps::CloudAccount.new(create_params) cloud_account.user = current_user - cloud_account.project_id = @project.id + cloud_account.repo_id = @repo.id cloud_account.save! end @@ -64,11 +64,10 @@ class DevOps::CloudAccountsController < ApplicationController private def devops_params - params.permit(:account, :secret, :ip_num, :project_id) + params.permit(:account, :secret, :ip_num, :repo_id) end def find_project - @project = Project.find_by_id params[:project_id] - render_not_found("未找到project_id为:#{params[:project_id]}相关的项目") if @project.blank? + @repo = Repository.find params[:repo_id] end end diff --git a/app/libs/dev_ops/drone/request.rb b/app/libs/dev_ops/drone/request.rb index 615e53b6..6ce738a7 100644 --- a/app/libs/dev_ops/drone/request.rb +++ b/app/libs/dev_ops/drone/request.rb @@ -24,22 +24,22 @@ end def get(endpoint, path, options={}) - set_request_defaults(endpoint) + validate_request_params!(endpoint) request(:get, endpoint, path, options) end def post(endpoint, path, options={}) - set_request_defaults(endpoint) + validate_request_params!(endpoint) request(:post, endpoint, path, options) end def put(endpoint, path, options={}) - set_request_defaults(endpoint) + validate_request_params!(endpoint) request(:put, endpoint, path, options) end def delete(endpoint, path, options={}) - set_request_defaults(endpoint) + validate_request_params!(endpoint) request(:delete, endpoint, path, options) end @@ -47,27 +47,10 @@ def request(method, endpoint, path, **params) Rails.logger.info("[drone] request: #{method} #{path} #{params.except(:secret).inspect}") - client = Faraday.new(path: domain) + client = Faraday.new(url: endpoint) response = client.public_send(method, path, params) - result = JSON.parse(response.body) - Rails.logger.info("[drone] response:#{response.status} #{result.inspect}") - - if response.status != 200 - raise DevOps::Drone::Error.parse(result) - end - - if result['errcode'].present? && result['errcode'].to_i.nonzero? - raise DevOps::Drone::Error.parse(result) - end - - result - end - - # Sets a base_uri and default_params for requests. - # @raise [Error::MissingCredentials] if endpoint not set. - def set_request_defaults(endpoint, private_token, sudo=nil) - raise "Please set an endpoint to API" unless endpoint + json_response(response) end # Checks the response code for common errors. @@ -89,8 +72,21 @@ response.parsed_response end + # Checks a base_uri and params for requests. + def validate_request_params!(endpoint) + raise "Please set an endpoint to API" unless endpoint + end + def error_message(response) "Server responded with code #{response.code}, message: #{response.parsed_response.message}. " \ "Request URI: #{response.request.base_uri}#{response.request.path}" end + + def json_response(response) + result = JSON.parse(response.body) + status = response.status + Rails.logger.info("[drone] response:#{status} #{result.inspect}") + + response.status != 200 ? result.merge!(status: response.status) : result + end end diff --git a/app/models/dev_ops/cloud_account.rb b/app/models/dev_ops/cloud_account.rb index 778335fb..8b07a749 100644 --- a/app/models/dev_ops/cloud_account.rb +++ b/app/models/dev_ops/cloud_account.rb @@ -1,7 +1,7 @@ class DevOps::CloudAccount < ApplicationRecord belongs_to :project belongs_to :user - belongs_to :repository + belongs_to :repository, foreign_key: :repo_id def drone_host [drone_ip, ":80"].join diff --git a/app/models/repository.rb b/app/models/repository.rb index ec623292..373604c8 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -3,7 +3,7 @@ class Repository < ApplicationRecord belongs_to :project, :touch => true belongs_to :user has_one :mirror, foreign_key: :repo_id - has_one :dev_ops_cloud_account, foreign_key: :repo_id + has_one :dev_ops_cloud_account, class_name: 'DevOps::CloudAccount', foreign_key: :repo_id has_many :version_releases, dependent: :destroy validates :identifier, presence: true diff --git a/config/routes.rb b/config/routes.rb index 709ff907..5589b55b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,13 +22,12 @@ Rails.application.routes.draw do get :common end end - resources :builds, only: :index do collection do get ':number', to: 'builds#detail', as: 'detail' - get ':number/logs/:stage/:step', to: 'builds#detail', as: 'logs' post ':number', to: 'builds#restart', as: 'restart' delete ':number', to: 'builds#delete', as: 'delete' + get ':number/logs/:stage/:step', to: 'builds#detail', as: 'logs' end end end From 8d5a373c6b02f81f63b9088a84124cc7ac2c720f Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Wed, 15 Jul 2020 20:34:25 +0800 Subject: [PATCH 2/2] Update readme.md ducument --- README.md | 341 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 339 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 66bc4977..c30dbd75 100644 --- a/README.md +++ b/README.md @@ -2340,7 +2340,7 @@ http://localhost:3000/api//api/repositories/3868/delete_file | jq "author": { "name": "18816895620", "email": "2456233122@qq.com", - "date": "2020-01-08T07:57:34Z" + "date": "2020-01-08T07:57:34Z"`` }, "committer": { "name": "18816895620", @@ -2364,9 +2364,23 @@ POST /api/dev_ops/cloud_accounts *示例* ``` -curl -X POST http://localhost:3000/api/dev_ops/cloud_accounts | jq +curl -X POST \ +-d "account=xx" \ +-d "secret=xxx" \ +-d "ip_num=xx.xx.xx.xx" \ +-d "repo_id=5988" \ +https://localhost:3000/api/dev_ops/cloud_accounts.json | jq ``` +*请求参数说明:* + +|参数名|必选|类型|说明| +|-|-|-|-| +|account |是|string |云服务器ssh连接登录用户名 | +|secret |是|string |云服务器ssh连接登录秘密 | +|ip_num |否|string |云服务器公网IP | +|repo_id |否|string |repository id| + *返回参数说明:* |参数名|类型|说明| @@ -2459,6 +2473,12 @@ GET /api/dev_ops/languages/:id curl -X GET http://localhost:3000/api/dev_ops/languages/114.json | jq ``` +*请求参数说明:* + +|参数名|必选|类型|说明| +|-|-|-|-| +|id |是|int |language's id | + *返回参数说明:* |参数名|类型|说明| @@ -2480,3 +2500,320 @@ curl -X GET http://localhost:3000/api/dev_ops/languages/114.json | jq ] ``` --- + +#### 获取构建列表 +``` +GET /api/dev_ops/builds +``` + +*示例* +``` +curl -X GET http://localhost:3000/api/dev_ops/builds | jq +``` + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|id |int|build's id| +|number |string|build's number| +|status |string|build's result| +|event |string|build's event| + +返回值 +``` +[ + { + "id": 100207, + "repo_id": 296163, + "number": 42, + "status": "success", + "event": "pull_request", + "action": "sync", + "link": "https://github.com/octoat/hello-world/compare/e3320539a4c0...9fc1ad6ebf12", + "message": "updated README", + "before": "e3320539a4c03ccfda992641646deb67d8bf98f3", + "after": "9fc1ad6ebf12462f3f9773003e26b4c6f54a772e", + "ref": "refs/heads/master", + "source_repo": "spaceghost/hello-world", + "source": "develop", + "target": "master", + "author_login": "octocat", + "author_name": "The Octocat", + "author_email": "octocat@github.com", + "author_avatar": "http://www.gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c", + "sender": "bradrydzewski", + "started": 1564085874, + "finished": 1564086343, + "created": 1564085874, + "updated": 1564085874, + "version": 3 + } +] +``` +--- + +#### 获取某条构建详情信息 +``` +GET /api/dev_ops/builds/:number +``` + +*示例* +``` +curl -X GET http://localhost:3000/api/dev_ops/builds/42 | jq +``` + +*请求参数说明:* + +|参数名|必选|类型|说明| +|-|-|-|-| +|number |是|int |build's number | + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|id |int|build's id| +|status |string|build's status| +|event |string|build's event| + +返回值 +``` +{ + "id": 100207, + "repo_id": 296163, + "number": 42, + "status": "pending", + "event": "pull_request", + "action": "sync", + "link": "https://github.com/octoat/hello-world/compare/e3320539a4c0...9fc1ad6ebf12", + "message": "updated README", + "before": "e3320539a4c03ccfda992641646deb67d8bf98f3", + "after": "9fc1ad6ebf12462f3f9773003e26b4c6f54a772e", + "ref": "refs/heads/master", + "source_repo": "spaceghost/hello-world", + "source": "develop", + "target": "master", + "author_login": "octocat", + "author_name": "The Octocat", + "author_email": "octocat@github.com", + "author_avatar": "http://www.gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c", + "sender": "bradrydzewski", + "started": 0, + "finished": 0, + "created": 1564085874, + "updated": 1564085874, + "version": 1, + "stages": [ + { + "id": 199937, + "repo_id": 296163, + "build_id": 100207, + "number": 1, + "name": "default", + "kind": "pipeline", + "type": "docker", + "status": "pending", + "errignore": false, + "exit_code": 0, + "machine": "15e89c0f84f1", + "os": "linux", + "arch": "amd64", + "started": 0, + "stopped": 0, + "created": 1564085874, + "updated": 1564086343, + "version": 1, + "on_success": true, + "on_failure": false + } + ] +} +``` +--- + +#### 重启构建/重新构建 +``` +POST /api/dev_ops/builds/:number +``` + +*示例* +``` +curl -X POST http://localhost:3000/api/dev_ops/builds/42 | jq +``` + +*请求参数说明:* + +|参数名|必选|类型|说明| +|-|-|-|-| +|number |是|int |build's number | + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|id |int|build's id| +|status |string|build's status| +|event |string|build's event| + +返回值 +``` +{ + "id": 100207, + "repo_id": 296163, + "number": 42, + "status": "pending", + "event": "pull_request", + "action": "sync", + "link": "https://github.com/octoat/hello-world/compare/e3320539a4c0...9fc1ad6ebf12", + "message": "updated README", + "before": "e3320539a4c03ccfda992641646deb67d8bf98f3", + "after": "9fc1ad6ebf12462f3f9773003e26b4c6f54a772e", + "ref": "refs/heads/master", + "source_repo": "spaceghost/hello-world", + "source": "develop", + "target": "master", + "author_login": "octocat", + "author_name": "The Octocat", + "author_email": "octocat@github.com", + "author_avatar": "http://www.gravatar.com/avatar/7194e8d48fa1d2b689f99443b767316c", + "sender": "bradrydzewski", + "started": 0, + "finished": 0, + "created": 1564085874, + "updated": 1564085874, + "version": 1, + "stages": [ + { + "id": 199937, + "repo_id": 296163, + "build_id": 100207, + "number": 1, + "name": "default", + "kind": "pipeline", + "type": "docker", + "status": "pending", + "errignore": false, + "exit_code": 0, + "machine": "15e89c0f84f1", + "os": "linux", + "arch": "amd64", + "started": 0, + "stopped": 0, + "created": 1564085874, + "updated": 1564086343, + "version": 1, + "on_success": true, + "on_failure": false + } + ] +} +``` +--- + +#### 关闭构建 +``` +DELETE /api/dev_ops/builds/:number +``` + +*示例* +``` +curl -X DELETE http://localhost:3000/api/dev_ops/builds/42 | jq +``` + +*请求参数说明:* + +|参数名|必选|类型|说明| +|-|-|-|-| +|number |是|int |build's number | + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|id |int|build's id| +|status |string|build's status| +|event |string|build's event| + +返回值 +``` +``` +--- + +#### 获取某条构建的log信息 +``` +GET /api/dev_ops/builds/:number/logs/:stage/:step +``` + +*示例* +``` +curl -X GET http://localhost:3000/api/dev_ops/builds/42/logs/ | jq +``` + +*请求参数说明:* + +|参数名|必选|类型|说明| +|-|-|-|-| +|number |是|int |build's number | +|stage |是|int |build's stage id | +|step |是|int |build's step id | + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|id |int|build's id| +|status |string|build's status| +|event |string|build's event| + +返回值 +``` +[ + { + "proc": "clone", + "pos": 0, + "out": "+ git init\n" + }, + { + "proc": "clone", + "pos": 1, + "out": "Initialized empty Git repository in /drone/src/github.com/octocat/hello-world/.git/\n" + }, + { + "proc": "clone", + "pos": 2, + "out": "+ git remote add origin https://github.com/octocat/hello-world.git\n" + }, + { + "proc": "clone", + "pos": 3, + "out": "+ git fetch --no-tags origin +refs/heads/master:\n" + }, + { + "proc": "clone", + "pos": 4, + "out": "From https://github.com/octocat/hello-world\n" + }, + { + "proc": "clone", + "pos": 5, + "out": " * branch master -> FETCH_HEAD\n" + }, + { + "proc": "clone", + "pos": 6, + "out": " * [new branch] master -> origin/master\n" + }, + { + "proc": "clone", + "pos": 7, + "out": "+ git reset --hard -q 62126a02ffea3dabd7789e5c5407553490973665\n" + }, + { + "proc": "clone", + "pos": 8, + "out": "+ git submodule update --init --recursive\n" + } +] +``` +---